icinga2/lib/base/value.hpp

252 lines
6.7 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#ifndef VALUE_H
#define VALUE_H
2012-04-18 15:22:25 +02:00
2014-05-25 16:23:35 +02:00
#include "base/object.hpp"
#include "base/string.hpp"
2013-03-18 19:02:42 +01:00
#include <boost/variant/variant.hpp>
#include <boost/variant/get.hpp>
2018-01-04 18:24:45 +01:00
#include <boost/throw_exception.hpp>
2013-03-15 18:21:29 +01:00
2012-04-18 15:22:25 +02:00
namespace icinga
{
typedef double Timestamp;
2013-02-15 14:39:26 +01:00
/**
* The type of a Value.
*
* @ingroup base
*/
enum ValueType
{
ValueEmpty = 0,
ValueNumber = 1,
ValueBoolean = 2,
ValueString = 3,
ValueObject = 4
2013-02-15 14:39:26 +01:00
};
/**
* A type that can hold an arbitrary value.
2012-05-18 22:21:28 +02:00
*
* @ingroup base
*/
2017-12-31 07:22:16 +01:00
class Value
2012-04-18 15:22:25 +02:00
{
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<Object>& value);
2012-07-11 20:55:46 +02:00
template<typename T>
Value(const intrusive_ptr<T>& value)
: Value(static_pointer_cast<Object>(value))
2012-07-11 20:55:46 +02:00
{
static_assert(!std::is_same<T, Object>::value, "T must not be Object");
2012-07-11 20:55:46 +02:00
}
bool ToBool() const;
operator double() const;
operator String() const;
2012-04-20 13:49:04 +02:00
Value& operator=(const Value& other);
Value& operator=(Value&& other);
2016-08-27 20:03:12 +02:00
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;
2012-04-20 13:49:04 +02:00
template<typename T>
operator intrusive_ptr<T>() const
2012-07-11 20:55:46 +02:00
{
if (IsEmpty() && !IsString())
return intrusive_ptr<T>();
2012-07-24 13:13:02 +02:00
2014-11-06 19:35:47 +01:00
if (!IsObject())
BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert value of type '" + GetTypeName() + "' to an object."));
2014-11-06 19:35:47 +01:00
const auto& object = Get<Object::Ptr>();
2012-04-18 15:22:25 +02:00
2014-11-06 09:05:12 +01:00
ASSERT(object);
intrusive_ptr<T> tobject = dynamic_pointer_cast<T>(object);
if (!tobject)
2013-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::bad_cast());
2012-04-18 15:22:25 +02:00
return tobject;
2012-07-11 20:55:46 +02:00
}
2012-04-18 15:22:25 +02:00
bool IsEmpty() const;
bool IsScalar() const;
bool IsNumber() const;
bool IsBoolean() const;
bool IsString() const;
bool IsObject() const;
2012-04-20 13:49:04 +02:00
2012-07-11 20:55:46 +02:00
template<typename T>
bool IsObjectType() const
2012-07-11 20:55:46 +02:00
{
if (!IsObject())
return false;
return dynamic_cast<T *>(Get<Object::Ptr>().get());
2012-07-11 20:55:46 +02:00
}
ValueType GetType() const;
void Swap(Value& other);
String GetTypeName() const;
2013-02-15 14:39:26 +01:00
Type::Ptr GetReflectionType() const;
2017-12-13 12:54:14 +01:00
Value Clone() const;
template<typename T>
const T& Get() const
{
return boost::get<T>(m_Value);
}
private:
boost::variant<boost::blank, double, bool, String, Object::Ptr> m_Value;
2012-04-18 15:22:25 +02:00
};
extern template const double& Value::Get<double>() const;
extern template const bool& Value::Get<bool>() const;
extern template const String& Value::Get<String>() const;
extern template const Object::Ptr& Value::Get<Object::Ptr>() const;
2017-12-31 07:22:16 +01:00
extern 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);
2012-04-18 15:22:25 +02:00
}
extern template class boost::variant<boost::blank, double, bool, icinga::String, icinga::Object::Ptr>;
#endif /* VALUE_H */