1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/value.hpp"
4 #include "base/array.hpp"
5 #include "base/dictionary.hpp"
6 #include "base/type.hpp"
8 using namespace icinga;
10 template class boost::variant<boost::blank, double, bool, String, Object::Ptr>;
11 template const double& Value::Get<double>() const;
12 template const bool& Value::Get<bool>() const;
13 template const String& Value::Get<String>() const;
14 template const Object::Ptr& Value::Get<Object::Ptr>() const;
18 Value::Value(std::nullptr_t)
21 Value::Value(int value)
22 : m_Value(double(value))
25 Value::Value(unsigned int value)
26 : m_Value(double(value))
29 Value::Value(long value)
30 : m_Value(double(value))
33 Value::Value(unsigned long value)
34 : m_Value(double(value))
37 Value::Value(long long value)
38 : m_Value(double(value))
41 Value::Value(unsigned long long value)
42 : m_Value(double(value))
45 Value::Value(double value)
49 Value::Value(bool value)
53 Value::Value(const String& value)
57 Value::Value(String&& value)
61 Value::Value(const char *value)
62 : m_Value(String(value))
65 Value::Value(const Value& other)
66 : m_Value(other.m_Value)
69 Value::Value(Value&& other)
71 #if BOOST_VERSION >= 105400
72 m_Value = std::move(other.m_Value);
73 #else /* BOOST_VERSION */
74 m_Value.swap(other.m_Value);
75 #endif /* BOOST_VERSION */
78 Value::Value(Object *value)
79 : Value(Object::Ptr(value))
82 Value::Value(const intrusive_ptr<Object>& value)
88 Value& Value::operator=(const Value& other)
90 m_Value = other.m_Value;
94 Value& Value::operator=(Value&& other)
96 #if BOOST_VERSION >= 105400
97 m_Value = std::move(other.m_Value);
98 #else /* BOOST_VERSION */
99 m_Value.swap(other.m_Value);
100 #endif /* BOOST_VERSION */
106 * Checks whether the variant is empty.
108 * @returns true if the variant is empty, false otherwise.
110 bool Value::IsEmpty() const
112 return (GetType() == ValueEmpty || (IsString() && boost::get<String>(m_Value).IsEmpty()));
116 * Checks whether the variant is scalar (i.e. not an object and not empty).
118 * @returns true if the variant is scalar, false otherwise.
120 bool Value::IsScalar() const
122 return !IsEmpty() && !IsObject();
126 * Checks whether the variant is a number.
128 * @returns true if the variant is a number.
130 bool Value::IsNumber() const
132 return (GetType() == ValueNumber);
136 * Checks whether the variant is a boolean.
138 * @returns true if the variant is a boolean.
140 bool Value::IsBoolean() const
142 return (GetType() == ValueBoolean);
146 * Checks whether the variant is a string.
148 * @returns true if the variant is a string.
150 bool Value::IsString() const
152 return (GetType() == ValueString);
156 * Checks whether the variant is a non-null object.
158 * @returns true if the variant is a non-null object, false otherwise.
160 bool Value::IsObject() const
162 return (GetType() == ValueObject);
166 * Returns the type of the value.
170 ValueType Value::GetType() const
172 return static_cast<ValueType>(m_Value.which());
175 void Value::Swap(Value& other)
177 m_Value.swap(other.m_Value);
180 bool Value::ToBool() const
184 return static_cast<bool>(boost::get<double>(m_Value));
187 return boost::get<bool>(m_Value);
190 return !boost::get<String>(m_Value).IsEmpty();
193 if (IsObjectType<Dictionary>()) {
194 Dictionary::Ptr dictionary = *this;
195 return dictionary->GetLength() > 0;
196 } else if (IsObjectType<Array>()) {
197 Array::Ptr array = *this;
198 return array->GetLength() > 0;
207 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid variant type."));
211 String Value::GetTypeName() const
225 t = boost::get<Object::Ptr>(m_Value)->GetReflectionType();
227 if (IsObjectType<Array>())
229 else if (IsObjectType<Dictionary>())
240 Type::Ptr Value::GetReflectionType() const
244 return Object::TypeInstance;
246 return Type::GetByName("Number");
248 return Type::GetByName("Boolean");
250 return Type::GetByName("String");
252 return boost::get<Object::Ptr>(m_Value)->GetReflectionType();
258 Value Value::Clone() const
261 return static_cast<Object::Ptr>(*this)->Clone();