/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ #ifndef VALUE_H #define VALUE_H #include "base/object.hpp" #include "base/string.hpp" #include #include #include namespace icinga { typedef double Timestamp; /** * The type of a Value. * * @ingroup base */ enum ValueType { ValueEmpty = 0, ValueNumber = 1, ValueBoolean = 2, ValueString = 3, ValueObject = 4 }; /** * A type that can hold an arbitrary value. * * @ingroup base */ class Value { public: Value() = default; Value(std::nullptr_t); Value(int value); Value(unsigned int value); Value(long value); Value(unsigned long value); Value(long long value); Value(unsigned long long value); Value(double value); Value(bool value); Value(const String& value); Value(String&& value); Value(const char *value); Value(const Value& other); Value(Value&& other); Value(Object *value); Value(const intrusive_ptr& value); template Value(const intrusive_ptr& value) : Value(static_pointer_cast(value)) { static_assert(!std::is_same::value, "T must not be Object"); } bool ToBool() const; operator double() const; operator String() const; Value& operator=(const Value& other); Value& operator=(Value&& other); bool operator==(bool rhs) const; bool operator!=(bool rhs) const; bool operator==(int rhs) const; bool operator!=(int rhs) const; bool operator==(double rhs) const; bool operator!=(double rhs) const; bool operator==(const char *rhs) const; bool operator!=(const char *rhs) const; bool operator==(const String& rhs) const; bool operator!=(const String& rhs) const; bool operator==(const Value& rhs) const; bool operator!=(const Value& rhs) const; template operator intrusive_ptr() const { if (IsEmpty() && !IsString()) return intrusive_ptr(); if (!IsObject()) BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert value of type '" + GetTypeName() + "' to an object.")); const auto& object = Get(); ASSERT(object); intrusive_ptr tobject = dynamic_pointer_cast(object); if (!tobject) BOOST_THROW_EXCEPTION(std::bad_cast()); return tobject; } bool IsEmpty() const; bool IsScalar() const; bool IsNumber() const; bool IsBoolean() const; bool IsString() const; bool IsObject() const; template bool IsObjectType() const { if (!IsObject()) return false; return dynamic_cast(Get().get()); } ValueType GetType() const; void Swap(Value& other); String GetTypeName() const; Type::Ptr GetReflectionType() const; Value Clone() const; template const T& Get() const { return boost::get(m_Value); } private: boost::variant m_Value; }; extern template const double& Value::Get() const; extern template const bool& Value::Get() const; extern template const String& Value::Get() const; extern template const Object::Ptr& Value::Get() const; extern const Value Empty; Value operator+(const Value& lhs, const char *rhs); Value operator+(const char *lhs, const Value& rhs); Value operator+(const Value& lhs, const String& rhs); Value operator+(const String& lhs, const Value& rhs); Value operator+(const Value& lhs, const Value& rhs); Value operator+(const Value& lhs, double rhs); Value operator+(double lhs, const Value& rhs); Value operator+(const Value& lhs, int rhs); Value operator+(int lhs, const Value& rhs); Value operator-(const Value& lhs, const Value& rhs); Value operator-(const Value& lhs, double rhs); Value operator-(double lhs, const Value& rhs); Value operator-(const Value& lhs, int rhs); Value operator-(int lhs, const Value& rhs); Value operator*(const Value& lhs, const Value& rhs); Value operator*(const Value& lhs, double rhs); Value operator*(double lhs, const Value& rhs); Value operator*(const Value& lhs, int rhs); Value operator*(int lhs, const Value& rhs); Value operator/(const Value& lhs, const Value& rhs); Value operator/(const Value& lhs, double rhs); Value operator/(double lhs, const Value& rhs); Value operator/(const Value& lhs, int rhs); Value operator/(int lhs, const Value& rhs); Value operator%(const Value& lhs, const Value& rhs); Value operator%(const Value& lhs, double rhs); Value operator%(double lhs, const Value& rhs); Value operator%(const Value& lhs, int rhs); Value operator%(int lhs, const Value& rhs); Value operator^(const Value& lhs, const Value& rhs); Value operator^(const Value& lhs, double rhs); Value operator^(double lhs, const Value& rhs); Value operator^(const Value& lhs, int rhs); Value operator^(int lhs, const Value& rhs); Value operator&(const Value& lhs, const Value& rhs); Value operator&(const Value& lhs, double rhs); Value operator&(double lhs, const Value& rhs); Value operator&(const Value& lhs, int rhs); Value operator&(int lhs, const Value& rhs); Value operator|(const Value& lhs, const Value& rhs); Value operator|(const Value& lhs, double rhs); Value operator|(double lhs, const Value& rhs); Value operator|(const Value& lhs, int rhs); Value operator|(int lhs, const Value& rhs); Value operator<<(const Value& lhs, const Value& rhs); Value operator<<(const Value& lhs, double rhs); Value operator<<(double lhs, const Value& rhs); Value operator<<(const Value& lhs, int rhs); Value operator<<(int lhs, const Value& rhs); Value operator>>(const Value& lhs, const Value& rhs); Value operator>>(const Value& lhs, double rhs); Value operator>>(double lhs, const Value& rhs); Value operator>>(const Value& lhs, int rhs); Value operator>>(int lhs, const Value& rhs); bool operator<(const Value& lhs, const Value& rhs); bool operator<(const Value& lhs, double rhs); bool operator<(double lhs, const Value& rhs); bool operator<(const Value& lhs, int rhs); bool operator<(int lhs, const Value& rhs); bool operator>(const Value& lhs, const Value& rhs); bool operator>(const Value& lhs, double rhs); bool operator>(double lhs, const Value& rhs); bool operator>(const Value& lhs, int rhs); bool operator>(int lhs, const Value& rhs); bool operator<=(const Value& lhs, const Value& rhs); bool operator<=(const Value& lhs, double rhs); bool operator<=(double lhs, const Value& rhs); bool operator<=(const Value& lhs, int rhs); bool operator<=(int lhs, const Value& rhs); bool operator>=(const Value& lhs, const Value& rhs); bool operator>=(const Value& lhs, double rhs); bool operator>=(double lhs, const Value& rhs); bool operator>=(const Value& lhs, int rhs); bool operator>=(int lhs, const Value& rhs); std::ostream& operator<<(std::ostream& stream, const Value& value); std::istream& operator>>(std::istream& stream, Value& value); } extern template class boost::variant; #endif /* VALUE_H */