2014-03-20 13:45:10 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2014-03-20 13:45:10 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/value.hpp"
|
|
|
|
#include "base/array.hpp"
|
|
|
|
#include "base/dictionary.hpp"
|
2016-03-29 12:45:22 +02:00
|
|
|
#include "base/datetime.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2014-04-02 17:39:36 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2014-03-20 13:45:10 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
Value::operator double(void) const
|
|
|
|
{
|
|
|
|
const double *value = boost::get<double>(&m_Value);
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
return *value;
|
|
|
|
|
2014-12-17 08:54:28 +01:00
|
|
|
const bool *fvalue = boost::get<bool>(&m_Value);
|
|
|
|
|
|
|
|
if (fvalue)
|
|
|
|
return *fvalue;
|
|
|
|
|
2014-03-20 13:45:10 +01:00
|
|
|
if (IsEmpty())
|
|
|
|
return 0;
|
|
|
|
|
2015-01-29 16:51:47 +01:00
|
|
|
try {
|
|
|
|
return boost::lexical_cast<double>(m_Value);
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Can't convert '" << *this << "' to a floating point number.";
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
|
|
|
|
}
|
2014-03-20 13:45:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Value::operator String(void) const
|
|
|
|
{
|
|
|
|
Object *object;
|
|
|
|
|
|
|
|
switch (GetType()) {
|
|
|
|
case ValueEmpty:
|
|
|
|
return String();
|
|
|
|
case ValueNumber:
|
2016-06-16 15:32:29 +02:00
|
|
|
return Convert::ToString(boost::get<double>(m_Value));
|
2014-12-10 09:04:49 +01:00
|
|
|
case ValueBoolean:
|
|
|
|
if (boost::get<bool>(m_Value))
|
|
|
|
return "true";
|
|
|
|
else
|
|
|
|
return "false";
|
2014-03-20 13:45:10 +01:00
|
|
|
case ValueString:
|
|
|
|
return boost::get<String>(m_Value);
|
|
|
|
case ValueObject:
|
|
|
|
object = boost::get<Object::Ptr>(m_Value).get();
|
2014-12-08 09:12:40 +01:00
|
|
|
return object->ToString();
|
2014-03-20 13:45:10 +01:00
|
|
|
default:
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Unknown value type."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& icinga::operator<<(std::ostream& stream, const Value& value)
|
|
|
|
{
|
2014-12-17 08:54:28 +01:00
|
|
|
if (value.IsBoolean())
|
|
|
|
stream << static_cast<int>(value);
|
|
|
|
else
|
|
|
|
stream << static_cast<String>(value);
|
|
|
|
|
2014-03-20 13:45:10 +01:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::istream& icinga::operator>>(std::istream& stream, Value& value)
|
|
|
|
{
|
|
|
|
String tstr;
|
|
|
|
stream >> tstr;
|
|
|
|
value = tstr;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2015-03-02 12:55:48 +01:00
|
|
|
Value& Value::operator=(const Value& other)
|
|
|
|
{
|
|
|
|
m_Value = other.m_Value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-03-20 13:45:10 +01:00
|
|
|
bool Value::operator==(bool rhs) const
|
|
|
|
{
|
|
|
|
return *this == Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator!=(bool rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator==(int rhs) const
|
|
|
|
{
|
|
|
|
return *this == Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator!=(int rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator==(double rhs) const
|
|
|
|
{
|
|
|
|
return *this == Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator!=(double rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator==(const char *rhs) const
|
|
|
|
{
|
|
|
|
return static_cast<String>(*this) == rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator!=(const char *rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator==(const String& rhs) const
|
|
|
|
{
|
|
|
|
return static_cast<String>(*this) == rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator!=(const String& rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator==(const Value& rhs) const
|
|
|
|
{
|
2014-11-11 23:28:53 +01:00
|
|
|
if (IsNumber() && rhs.IsNumber())
|
|
|
|
return Get<double>() == rhs.Get<double>();
|
2015-07-30 19:58:22 +02:00
|
|
|
else if ((IsBoolean() || IsNumber()) && (rhs.IsBoolean() || rhs.IsNumber()) && !(IsEmpty() && rhs.IsEmpty()))
|
2014-10-30 13:04:00 +01:00
|
|
|
return static_cast<double>(*this) == static_cast<double>(rhs);
|
|
|
|
|
2014-11-11 23:28:53 +01:00
|
|
|
if (IsString() && rhs.IsString())
|
|
|
|
return Get<String>() == rhs.Get<String>();
|
|
|
|
else if ((IsString() || IsEmpty()) && (rhs.IsString() || rhs.IsEmpty()) && !(IsEmpty() && rhs.IsEmpty()))
|
2014-10-30 13:04:00 +01:00
|
|
|
return static_cast<String>(*this) == static_cast<String>(rhs);
|
|
|
|
|
2014-03-20 13:45:10 +01:00
|
|
|
if (IsEmpty() != rhs.IsEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (IsEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (IsObject() != rhs.IsObject())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (IsObject()) {
|
2016-03-29 12:45:22 +02:00
|
|
|
if (IsObjectType<DateTime>() && rhs.IsObjectType<DateTime>()) {
|
|
|
|
DateTime::Ptr dt1 = *this;
|
|
|
|
DateTime::Ptr dt2 = rhs;
|
|
|
|
|
|
|
|
return dt1->GetValue() == dt2->GetValue();
|
|
|
|
}
|
|
|
|
|
2014-03-20 13:45:10 +01:00
|
|
|
if (IsObjectType<Array>() && rhs.IsObjectType<Array>()) {
|
|
|
|
Array::Ptr arr1 = *this;
|
|
|
|
Array::Ptr arr2 = rhs;
|
|
|
|
|
|
|
|
if (arr1 == arr2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (arr1->GetLength() != arr2->GetLength())
|
|
|
|
return false;
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
for (Array::SizeType i = 0; i < arr1->GetLength(); i++) {
|
2014-03-20 13:45:10 +01:00
|
|
|
if (arr1->Get(i) != arr2->Get(i))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:28:53 +01:00
|
|
|
return Get<Object::Ptr>() == rhs.Get<Object::Ptr>();
|
2014-03-20 13:45:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::operator!=(const Value& rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator+(const Value& lhs, const char *rhs)
|
|
|
|
{
|
|
|
|
return lhs + Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator+(const char *lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator+(const Value& lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return lhs + Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator+(const String& lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator+(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
2015-08-01 04:35:50 +02:00
|
|
|
if ((lhs.IsEmpty() || lhs.IsNumber()) && !lhs.IsString() && (rhs.IsEmpty() || rhs.IsNumber()) && !rhs.IsString() && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
2014-11-25 12:26:49 +01:00
|
|
|
return static_cast<double>(lhs) + static_cast<double>(rhs);
|
2015-09-22 13:02:35 +02:00
|
|
|
if ((lhs.IsString() || lhs.IsEmpty() || lhs.IsNumber()) && (rhs.IsString() || rhs.IsEmpty() || rhs.IsNumber()) && (!(lhs.IsEmpty() && rhs.IsEmpty()) || lhs.IsString() || rhs.IsString()))
|
2014-03-20 13:45:10 +01:00
|
|
|
return static_cast<String>(lhs) + static_cast<String>(rhs);
|
2015-09-22 13:02:35 +02:00
|
|
|
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
2014-03-20 13:45:10 +01:00
|
|
|
return static_cast<double>(lhs) + static_cast<double>(rhs);
|
2016-03-29 12:45:22 +02:00
|
|
|
else if (lhs.IsObjectType<DateTime>() && rhs.IsNumber())
|
|
|
|
return new DateTime(Convert::ToDateTimeValue(lhs) + rhs);
|
2015-09-22 13:02:35 +02:00
|
|
|
else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr result = new Array();
|
2014-03-23 11:27:40 +01:00
|
|
|
if (!lhs.IsEmpty())
|
|
|
|
static_cast<Array::Ptr>(lhs)->CopyTo(result);
|
|
|
|
if (!rhs.IsEmpty())
|
|
|
|
static_cast<Array::Ptr>(rhs)->CopyTo(result);
|
|
|
|
return result;
|
2015-09-22 13:02:35 +02:00
|
|
|
} else if ((lhs.IsObjectType<Dictionary>() || lhs.IsEmpty()) && (rhs.IsObjectType<Dictionary>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary();
|
2014-03-23 11:27:40 +01:00
|
|
|
if (!lhs.IsEmpty())
|
|
|
|
static_cast<Dictionary::Ptr>(lhs)->CopyTo(result);
|
|
|
|
if (!rhs.IsEmpty())
|
|
|
|
static_cast<Dictionary::Ptr>(rhs)->CopyTo(result);
|
2014-03-20 13:45:10 +01:00
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator + cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator+(const Value& lhs, double rhs)
|
|
|
|
{
|
|
|
|
return lhs + Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator+(double lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator+(const Value& lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs + Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator+(int lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator-(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
2015-07-30 20:01:08 +02:00
|
|
|
if ((lhs.IsNumber() || lhs.IsEmpty()) && !lhs.IsString() && (rhs.IsNumber() || rhs.IsEmpty()) && !rhs.IsString() && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
2014-03-20 13:45:10 +01:00
|
|
|
return static_cast<double>(lhs) - static_cast<double>(rhs);
|
2016-03-29 12:45:22 +02:00
|
|
|
else if (lhs.IsObjectType<DateTime>() && rhs.IsNumber())
|
|
|
|
return new DateTime(Convert::ToDateTimeValue(lhs) - rhs);
|
|
|
|
else if (lhs.IsObjectType<DateTime>() && rhs.IsObjectType<DateTime>())
|
|
|
|
return Convert::ToDateTimeValue(lhs) - Convert::ToDateTimeValue(rhs);
|
|
|
|
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return new DateTime(Convert::ToDateTimeValue(lhs) - Convert::ToDateTimeValue(rhs));
|
2014-04-02 17:39:36 +02:00
|
|
|
else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
|
|
|
|
if (lhs.IsEmpty())
|
2014-11-08 21:17:16 +01:00
|
|
|
return new Array();
|
2014-04-02 17:39:36 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr result = new Array();
|
2014-04-02 17:39:36 +02:00
|
|
|
Array::Ptr left = lhs;
|
|
|
|
Array::Ptr right = rhs;
|
|
|
|
|
|
|
|
ObjectLock olock(left);
|
|
|
|
BOOST_FOREACH(const Value& lv, left) {
|
|
|
|
bool found = false;
|
|
|
|
ObjectLock xlock(right);
|
|
|
|
BOOST_FOREACH(const Value& rv, right) {
|
|
|
|
if (lv == rv) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result->Add(lv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} else
|
2014-03-20 13:45:10 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator - cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator-(const Value& lhs, double rhs)
|
|
|
|
{
|
|
|
|
return lhs - Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator-(double lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) - rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator-(const Value& lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs - Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator-(int lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) - rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator*(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return static_cast<double>(lhs) * static_cast<double>(rhs);
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator * cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator*(const Value& lhs, double rhs)
|
|
|
|
{
|
|
|
|
return lhs * Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator*(double lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) * rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator*(const Value& lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs * Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator*(int lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) * rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator/(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
2014-12-16 08:52:32 +01:00
|
|
|
if (rhs.IsEmpty())
|
2014-03-20 13:45:10 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is Empty."));
|
2014-12-16 08:52:32 +01:00
|
|
|
else if ((lhs.IsEmpty() || lhs.IsNumber()) && rhs.IsNumber()) {
|
2014-03-20 13:45:10 +01:00
|
|
|
if (static_cast<double>(rhs) == 0)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is 0."));
|
|
|
|
|
|
|
|
return static_cast<double>(lhs) / static_cast<double>(rhs);
|
|
|
|
} else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator / cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator/(const Value& lhs, double rhs)
|
|
|
|
{
|
|
|
|
return lhs / Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator/(double lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) / rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator/(const Value& lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs / Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator/(int lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) / rhs;
|
|
|
|
}
|
|
|
|
|
2014-12-16 08:52:32 +01:00
|
|
|
Value icinga::operator%(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
if (rhs.IsEmpty())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator % is Empty."));
|
|
|
|
else if ((rhs.IsNumber() || lhs.IsNumber()) && rhs.IsNumber()) {
|
|
|
|
if (static_cast<double>(rhs) == 0)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator % is 0."));
|
|
|
|
|
|
|
|
return static_cast<int>(lhs) % static_cast<int>(rhs);
|
|
|
|
} else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator % cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator%(const Value& lhs, double rhs)
|
|
|
|
{
|
|
|
|
return lhs % Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator%(double lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) % rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator%(const Value& lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs % Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator%(int lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) % rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator^(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return static_cast<int>(lhs) ^ static_cast<int>(rhs);
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator & cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator^(const Value& lhs, double rhs)
|
|
|
|
{
|
2015-03-02 12:51:11 +01:00
|
|
|
return lhs ^ Value(rhs);
|
2014-12-16 08:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator^(double lhs, const Value& rhs)
|
|
|
|
{
|
2015-03-02 12:51:11 +01:00
|
|
|
return Value(lhs) ^ rhs;
|
2014-12-16 08:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator^(const Value& lhs, int rhs)
|
|
|
|
{
|
2015-03-02 12:51:11 +01:00
|
|
|
return lhs ^ Value(rhs);
|
2014-12-16 08:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator^(int lhs, const Value& rhs)
|
|
|
|
{
|
2015-03-02 12:51:11 +01:00
|
|
|
return Value(lhs) ^ rhs;
|
2014-12-16 08:52:32 +01:00
|
|
|
}
|
|
|
|
|
2014-03-20 13:45:10 +01:00
|
|
|
Value icinga::operator&(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return static_cast<int>(lhs) & static_cast<int>(rhs);
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator & cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator&(const Value& lhs, double rhs)
|
|
|
|
{
|
|
|
|
return lhs & Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator&(double lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) & rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator&(const Value& lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs & Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator&(int lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) & rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator|(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return static_cast<int>(lhs) | static_cast<int>(rhs);
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator | cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator|(const Value& lhs, double rhs)
|
|
|
|
{
|
|
|
|
return lhs | Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator|(double lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) | rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator|(const Value& lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs | Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator|(int lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) | rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator<<(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return static_cast<int>(lhs) << static_cast<int>(rhs);
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator << cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator<<(const Value& lhs, double rhs)
|
|
|
|
{
|
|
|
|
return lhs << Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator<<(double lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) << rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator<<(const Value& lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs << Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator<<(int lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) << rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator>>(const Value& lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return static_cast<int>(lhs) >> static_cast<int>(rhs);
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator >> cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator>>(const Value& lhs, double rhs)
|
|
|
|
{
|
|
|
|
return lhs >> Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator>>(double lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) >> rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator>>(const Value& lhs, int rhs)
|
|
|
|
{
|
|
|
|
return lhs >> Value(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::operator>>(int lhs, const Value& rhs)
|
|
|
|
{
|
|
|
|
return Value(lhs) >> rhs;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<(const Value& lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
if (lhs.IsString() && rhs.IsString())
|
|
|
|
return static_cast<String>(lhs) < static_cast<String>(rhs);
|
|
|
|
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
2015-01-16 10:35:20 +01:00
|
|
|
return static_cast<double>(lhs) < static_cast<double>(rhs);
|
2016-03-29 12:45:22 +02:00
|
|
|
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return Convert::ToDateTimeValue(lhs) < Convert::ToDateTimeValue(rhs);
|
2014-03-20 13:45:10 +01:00
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator < cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<(const Value& lhs, double rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return lhs < Value(rhs);
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<(double lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return Value(lhs) < rhs;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<(const Value& lhs, int rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return lhs < Value(rhs);
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<(int lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return Value(lhs) < rhs;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>(const Value& lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
if (lhs.IsString() && rhs.IsString())
|
|
|
|
return static_cast<String>(lhs) > static_cast<String>(rhs);
|
|
|
|
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
2015-01-16 10:35:20 +01:00
|
|
|
return static_cast<double>(lhs) > static_cast<double>(rhs);
|
2016-03-29 12:45:22 +02:00
|
|
|
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return Convert::ToDateTimeValue(lhs) > Convert::ToDateTimeValue(rhs);
|
2014-03-20 13:45:10 +01:00
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator > cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>(const Value& lhs, double rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return lhs > Value(rhs);
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>(double lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return Value(lhs) > rhs;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>(const Value& lhs, int rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return lhs > Value(rhs);
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>(int lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return Value(lhs) > rhs;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<=(const Value& lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
if (lhs.IsString() && rhs.IsString())
|
|
|
|
return static_cast<String>(lhs) <= static_cast<String>(rhs);
|
|
|
|
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
2015-01-16 10:35:20 +01:00
|
|
|
return static_cast<double>(lhs) <= static_cast<double>(rhs);
|
2016-03-29 12:45:22 +02:00
|
|
|
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return Convert::ToDateTimeValue(lhs) <= Convert::ToDateTimeValue(rhs);
|
2014-03-20 13:45:10 +01:00
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator <= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<=(const Value& lhs, double rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return lhs <= Value(rhs);
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<=(double lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return Value(lhs) <= rhs;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<=(const Value& lhs, int rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return lhs <= Value(rhs);
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator<=(int lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return Value(lhs) <= rhs;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>=(const Value& lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
if (lhs.IsString() && rhs.IsString())
|
|
|
|
return static_cast<String>(lhs) >= static_cast<String>(rhs);
|
|
|
|
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
2015-01-16 10:35:20 +01:00
|
|
|
return static_cast<double>(lhs) >= static_cast<double>(rhs);
|
2016-03-29 12:45:22 +02:00
|
|
|
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
|
|
|
return Convert::ToDateTimeValue(lhs) >= Convert::ToDateTimeValue(rhs);
|
2014-03-20 13:45:10 +01:00
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator >= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>=(const Value& lhs, double rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return lhs >= Value(rhs);
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>=(double lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return Value(lhs) >= rhs;
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>=(const Value& lhs, int rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return lhs >= Value(rhs);
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
bool icinga::operator>=(int lhs, const Value& rhs)
|
2014-03-20 13:45:10 +01:00
|
|
|
{
|
|
|
|
return Value(lhs) >= rhs;
|
|
|
|
}
|