2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2012-05-10 12:06:41 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
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>
|
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
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
class I2_BASE_API Value
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-11-11 23:28:53 +01:00
|
|
|
inline Value(void)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
inline Value(int value)
|
|
|
|
: m_Value(double(value))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
inline Value(unsigned int value)
|
|
|
|
: m_Value(double(value))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
inline Value(long value)
|
|
|
|
: m_Value(double(value))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
inline Value(unsigned long value)
|
|
|
|
: m_Value(double(value))
|
|
|
|
{ }
|
|
|
|
|
2016-03-29 08:42:02 +02:00
|
|
|
inline Value(long long value)
|
|
|
|
: m_Value(double(value))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
inline Value(unsigned long long value)
|
|
|
|
: m_Value(double(value))
|
|
|
|
{ }
|
|
|
|
|
2014-11-11 23:28:53 +01:00
|
|
|
inline Value(double value)
|
|
|
|
: m_Value(value)
|
|
|
|
{ }
|
|
|
|
|
2014-12-10 09:04:49 +01:00
|
|
|
inline Value(bool value)
|
|
|
|
: m_Value(value)
|
|
|
|
{ }
|
|
|
|
|
2014-11-11 23:28:53 +01:00
|
|
|
inline Value(const String& value)
|
|
|
|
: m_Value(value)
|
|
|
|
{ }
|
|
|
|
|
2016-08-26 18:11:28 +02:00
|
|
|
inline Value(String&& value)
|
|
|
|
: m_Value(value)
|
|
|
|
{ }
|
|
|
|
|
2014-11-11 23:28:53 +01:00
|
|
|
inline Value(const char *value)
|
|
|
|
: m_Value(String(value))
|
|
|
|
{ }
|
2012-04-20 13:49:04 +02:00
|
|
|
|
2016-08-27 20:03:12 +02:00
|
|
|
Value(const Value& other)
|
|
|
|
: m_Value(other.m_Value)
|
|
|
|
{ }
|
2016-08-27 12:35:05 +02:00
|
|
|
|
|
|
|
Value(Value&& other)
|
|
|
|
{
|
2016-08-27 20:03:12 +02:00
|
|
|
#if BOOST_VERSION >= 105400
|
|
|
|
m_Value = std::move(other.m_Value);
|
|
|
|
#else /* BOOST_VERSION */
|
2016-08-27 12:35:05 +02:00
|
|
|
m_Value.swap(other.m_Value);
|
|
|
|
#endif /* BOOST_VERSION */
|
2016-08-27 20:03:12 +02:00
|
|
|
}
|
2016-08-26 08:58:49 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
inline Value(Object *value)
|
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_Value = Object::Ptr(value);
|
|
|
|
}
|
|
|
|
|
2012-07-11 20:55:46 +02:00
|
|
|
template<typename T>
|
2014-11-08 21:17:16 +01:00
|
|
|
inline Value(const intrusive_ptr<T>& value)
|
2012-07-11 20:55:46 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
|
2013-03-18 19:02:42 +01:00
|
|
|
m_Value = static_pointer_cast<Object>(value);
|
2012-07-11 20:55:46 +02:00
|
|
|
}
|
|
|
|
|
2014-03-20 13:02:02 +01:00
|
|
|
bool ToBool(void) const;
|
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
operator double(void) const;
|
|
|
|
operator String(void) const;
|
2012-04-20 13:49:04 +02:00
|
|
|
|
2016-08-27 20:03:12 +02:00
|
|
|
Value& operator=(const Value& other)
|
|
|
|
{
|
|
|
|
m_Value = other.m_Value;
|
|
|
|
return *this;
|
|
|
|
}
|
2016-08-27 12:35:05 +02:00
|
|
|
|
|
|
|
Value& operator=(Value&& other)
|
|
|
|
{
|
2016-08-27 20:03:12 +02:00
|
|
|
#if BOOST_VERSION >= 105400
|
|
|
|
m_Value = std::move(other.m_Value);
|
|
|
|
#else /* BOOST_VERSION */
|
2016-08-27 12:35:05 +02:00
|
|
|
m_Value.swap(other.m_Value);
|
|
|
|
#endif /* BOOST_VERSION */
|
2015-03-02 12:55:48 +01:00
|
|
|
|
2016-08-27 20:03:12 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
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>
|
2014-11-08 21:17:16 +01:00
|
|
|
operator intrusive_ptr<T>(void) 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
|
|
|
|
2016-04-19 09:37:41 +02:00
|
|
|
const Object::Ptr& object = boost::get<Object::Ptr>(m_Value);
|
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
|
|
|
|
2014-11-11 23:28:53 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the variant is empty.
|
|
|
|
*
|
|
|
|
* @returns true if the variant is empty, false otherwise.
|
|
|
|
*/
|
|
|
|
inline bool IsEmpty(void) const
|
|
|
|
{
|
2014-12-20 09:31:55 +01:00
|
|
|
return (GetType() == ValueEmpty || (IsString() && boost::get<String>(m_Value).IsEmpty()));
|
2014-11-11 23:28:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the variant is scalar (i.e. not an object and not empty).
|
|
|
|
*
|
|
|
|
* @returns true if the variant is scalar, false otherwise.
|
|
|
|
*/
|
|
|
|
inline bool IsScalar(void) const
|
|
|
|
{
|
|
|
|
return !IsEmpty() && !IsObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the variant is a number.
|
|
|
|
*
|
|
|
|
* @returns true if the variant is a number.
|
|
|
|
*/
|
|
|
|
inline bool IsNumber(void) const
|
|
|
|
{
|
|
|
|
return (GetType() == ValueNumber);
|
|
|
|
}
|
|
|
|
|
2014-12-10 09:04:49 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the variant is a boolean.
|
|
|
|
*
|
|
|
|
* @returns true if the variant is a boolean.
|
|
|
|
*/
|
|
|
|
inline bool IsBoolean(void) const
|
|
|
|
{
|
|
|
|
return (GetType() == ValueBoolean);
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:28:53 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the variant is a string.
|
|
|
|
*
|
|
|
|
* @returns true if the variant is a string.
|
|
|
|
*/
|
|
|
|
inline bool IsString(void) const
|
|
|
|
{
|
|
|
|
return (GetType() == ValueString);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the variant is a non-null object.
|
|
|
|
*
|
|
|
|
* @returns true if the variant is a non-null object, false otherwise.
|
|
|
|
*/
|
|
|
|
inline bool IsObject(void) const
|
|
|
|
{
|
2015-09-23 10:48:49 +02:00
|
|
|
return (GetType() == ValueObject);
|
2014-11-11 23:28:53 +01:00
|
|
|
}
|
2012-04-20 13:49:04 +02:00
|
|
|
|
2012-07-11 20:55:46 +02:00
|
|
|
template<typename T>
|
|
|
|
bool IsObjectType(void) const
|
|
|
|
{
|
|
|
|
if (!IsObject())
|
|
|
|
return false;
|
2012-05-21 23:42:54 +02:00
|
|
|
|
2016-08-27 20:37:25 +02:00
|
|
|
return (dynamic_cast<T *>(boost::get<Object::Ptr>(m_Value).get()) != NULL);
|
2012-07-11 20:55:46 +02:00
|
|
|
}
|
2012-05-21 23:42:54 +02:00
|
|
|
|
2014-11-11 23:28:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the type of the value.
|
|
|
|
*
|
|
|
|
* @returns The type.
|
|
|
|
*/
|
2016-08-27 07:35:04 +02:00
|
|
|
inline ValueType GetType(void) const
|
2014-11-11 23:28:53 +01:00
|
|
|
{
|
|
|
|
return static_cast<ValueType>(m_Value.which());
|
|
|
|
}
|
|
|
|
|
2016-08-27 07:35:04 +02:00
|
|
|
inline void Swap(Value& other)
|
|
|
|
{
|
|
|
|
m_Value.swap(other.m_Value);
|
|
|
|
}
|
|
|
|
|
2014-03-20 13:02:02 +01:00
|
|
|
String GetTypeName(void) const;
|
2013-02-15 14:39:26 +01:00
|
|
|
|
2014-12-12 15:19:23 +01:00
|
|
|
Type::Ptr GetReflectionType(void) const;
|
2015-08-17 13:59:49 +02:00
|
|
|
|
|
|
|
Value Clone(void) const;
|
2014-12-12 15:19:23 +01:00
|
|
|
|
2014-11-11 23:28:53 +01:00
|
|
|
template<typename T>
|
|
|
|
const T& Get(void) const
|
|
|
|
{
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
extern I2_BASE_API Value Empty;
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2013-08-19 08:53:59 +02:00
|
|
|
I2_BASE_API Value operator+(const Value& lhs, const char *rhs);
|
|
|
|
I2_BASE_API Value operator+(const char *lhs, const Value& rhs);
|
|
|
|
|
2013-11-07 13:41:24 +01:00
|
|
|
I2_BASE_API Value operator+(const Value& lhs, const String& rhs);
|
|
|
|
I2_BASE_API Value operator+(const String& lhs, const Value& rhs);
|
|
|
|
|
2014-03-20 13:02:02 +01:00
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
2014-12-16 08:52:32 +01:00
|
|
|
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);
|
|
|
|
|
2014-03-20 13:02:02 +01:00
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
2014-05-11 06:00:34 +02:00
|
|
|
I2_BASE_API bool operator<(const Value& lhs, const Value& rhs);
|
|
|
|
I2_BASE_API bool operator<(const Value& lhs, double rhs);
|
|
|
|
I2_BASE_API bool operator<(double lhs, const Value& rhs);
|
|
|
|
I2_BASE_API bool operator<(const Value& lhs, int rhs);
|
|
|
|
I2_BASE_API bool operator<(int lhs, const Value& rhs);
|
|
|
|
|
|
|
|
I2_BASE_API bool operator>(const Value& lhs, const Value& rhs);
|
|
|
|
I2_BASE_API bool operator>(const Value& lhs, double rhs);
|
|
|
|
I2_BASE_API bool operator>(double lhs, const Value& rhs);
|
|
|
|
I2_BASE_API bool operator>(const Value& lhs, int rhs);
|
|
|
|
I2_BASE_API bool operator>(int lhs, const Value& rhs);
|
|
|
|
|
|
|
|
I2_BASE_API bool operator<=(const Value& lhs, const Value& rhs);
|
|
|
|
I2_BASE_API bool operator<=(const Value& lhs, double rhs);
|
|
|
|
I2_BASE_API bool operator<=(double lhs, const Value& rhs);
|
|
|
|
I2_BASE_API bool operator<=(const Value& lhs, int rhs);
|
|
|
|
I2_BASE_API bool operator<=(int lhs, const Value& rhs);
|
|
|
|
|
|
|
|
I2_BASE_API bool operator>=(const Value& lhs, const Value& rhs);
|
|
|
|
I2_BASE_API bool operator>=(const Value& lhs, double rhs);
|
|
|
|
I2_BASE_API bool operator>=(double lhs, const Value& rhs);
|
|
|
|
I2_BASE_API bool operator>=(const Value& lhs, int rhs);
|
|
|
|
I2_BASE_API bool operator>=(int lhs, const Value& rhs);
|
2014-03-20 13:02:02 +01:00
|
|
|
|
2013-07-23 08:57:22 +02:00
|
|
|
I2_BASE_API std::ostream& operator<<(std::ostream& stream, const Value& value);
|
|
|
|
I2_BASE_API std::istream& operator>>(std::istream& stream, Value& value);
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
#endif /* VALUE_H */
|