diff --git a/lib/base/value-operators.cpp b/lib/base/value-operators.cpp index ca3d87902..2ef1541c8 100644 --- a/lib/base/value-operators.cpp +++ b/lib/base/value-operators.cpp @@ -336,11 +336,9 @@ Value icinga::operator*(int lhs, const Value& rhs) Value icinga::operator/(const Value& lhs, const Value& rhs) { - if (lhs.IsEmpty()) - return 0; - else if (rhs.IsEmpty()) + if (rhs.IsEmpty()) BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is Empty.")); - else if (lhs.IsNumber() && rhs.IsNumber()) { + else if ((lhs.IsEmpty() || lhs.IsNumber()) && rhs.IsNumber()) { if (static_cast(rhs) == 0) BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is 0.")); @@ -369,6 +367,67 @@ Value icinga::operator/(int lhs, const Value& rhs) return Value(lhs) / rhs; } +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(rhs) == 0) + BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator % is 0.")); + + return static_cast(lhs) % static_cast(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(lhs) ^ static_cast(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())) diff --git a/lib/base/value.hpp b/lib/base/value.hpp index 58b8b0375..0e3a6123a 100644 --- a/lib/base/value.hpp +++ b/lib/base/value.hpp @@ -278,6 +278,18 @@ I2_BASE_API Value operator/(double lhs, const Value& rhs); I2_BASE_API Value operator/(const Value& lhs, int rhs); I2_BASE_API Value operator/(int lhs, const Value& rhs); +I2_BASE_API Value operator%(const Value& lhs, const Value& rhs); +I2_BASE_API Value operator%(const Value& lhs, double rhs); +I2_BASE_API Value operator%(double lhs, const Value& rhs); +I2_BASE_API Value operator%(const Value& lhs, int rhs); +I2_BASE_API Value operator%(int lhs, const Value& rhs); + +I2_BASE_API Value operator^(const Value& lhs, const Value& rhs); +I2_BASE_API Value operator^(const Value& lhs, double rhs); +I2_BASE_API Value operator^(double lhs, const Value& rhs); +I2_BASE_API Value operator^(const Value& lhs, int rhs); +I2_BASE_API Value operator^(int lhs, const Value& rhs); + I2_BASE_API Value operator&(const Value& lhs, const Value& rhs); I2_BASE_API Value operator&(const Value& lhs, double rhs); I2_BASE_API Value operator&(double lhs, const Value& rhs); diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index e6bdcf4d1..c4ad058ca 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -159,12 +159,12 @@ Value DivideExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const Value ModuloExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { - return (long)m_Operand1->Evaluate(frame) % (long)m_Operand2->Evaluate(frame); + return m_Operand1->Evaluate(frame) % m_Operand2->Evaluate(frame); } Value XorExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { - return (long)m_Operand1->Evaluate(frame) ^ (long)m_Operand2->Evaluate(frame); + return m_Operand1->Evaluate(frame) ^ m_Operand2->Evaluate(frame); } Value BinaryAndExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const