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