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