1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
23 #include "base/object.hpp"
24 #include "base/string.hpp"
25 #include <boost/variant/variant.hpp>
26 #include <boost/variant/get.hpp>
32 * The type of a Value.
46 * A type that can hold an arbitrary value.
50 class I2_BASE_API Value
56 inline Value(int value)
57 : m_Value(double(value))
60 inline Value(unsigned int value)
61 : m_Value(double(value))
64 inline Value(long value)
65 : m_Value(double(value))
68 inline Value(unsigned long value)
69 : m_Value(double(value))
72 inline Value(double value)
76 inline Value(bool value)
80 inline Value(const String& value)
84 inline Value(const char *value)
85 : m_Value(String(value))
88 inline Value(const Value& other)
89 : m_Value(other.m_Value)
92 inline Value(Object *value)
97 m_Value = Object::Ptr(value);
101 inline Value(const intrusive_ptr<T>& value)
106 m_Value = static_pointer_cast<Object>(value);
109 bool ToBool(void) const;
111 operator double(void) const;
112 operator String(void) const;
114 Value& operator=(const Value& other);
116 bool operator==(bool rhs) const;
117 bool operator!=(bool rhs) const;
119 bool operator==(int rhs) const;
120 bool operator!=(int rhs) const;
122 bool operator==(double rhs) const;
123 bool operator!=(double rhs) const;
125 bool operator==(const char *rhs) const;
126 bool operator!=(const char *rhs) const;
128 bool operator==(const String& rhs) const;
129 bool operator!=(const String& rhs) const;
131 bool operator==(const Value& rhs) const;
132 bool operator!=(const Value& rhs) const;
135 operator intrusive_ptr<T>(void) const
138 return intrusive_ptr<T>();
141 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert value of type '" + GetTypeName() + "' to an object."));
143 Object::Ptr object = boost::get<Object::Ptr>(m_Value);
147 intrusive_ptr<T> tobject = dynamic_pointer_cast<T>(object);
150 BOOST_THROW_EXCEPTION(std::bad_cast());
156 * Checks whether the variant is empty.
158 * @returns true if the variant is empty, false otherwise.
160 inline bool IsEmpty(void) const
162 return (GetType() == ValueEmpty || (IsString() && boost::get<String>(m_Value).IsEmpty()));
166 * Checks whether the variant is scalar (i.e. not an object and not empty).
168 * @returns true if the variant is scalar, false otherwise.
170 inline bool IsScalar(void) const
172 return !IsEmpty() && !IsObject();
176 * Checks whether the variant is a number.
178 * @returns true if the variant is a number.
180 inline bool IsNumber(void) const
182 return (GetType() == ValueNumber);
186 * Checks whether the variant is a boolean.
188 * @returns true if the variant is a boolean.
190 inline bool IsBoolean(void) const
192 return (GetType() == ValueBoolean);
196 * Checks whether the variant is a string.
198 * @returns true if the variant is a string.
200 inline bool IsString(void) const
202 return (GetType() == ValueString);
206 * Checks whether the variant is a non-null object.
208 * @returns true if the variant is a non-null object, false otherwise.
210 inline bool IsObject(void) const
212 return (GetType() == ValueObject);
216 bool IsObjectType(void) const
221 return (dynamic_pointer_cast<T>(boost::get<Object::Ptr>(m_Value)) != NULL);
225 * Returns the type of the value.
229 ValueType GetType(void) const
231 return static_cast<ValueType>(m_Value.which());
234 String GetTypeName(void) const;
236 Type::Ptr GetReflectionType(void) const;
238 Value Clone(void) const;
241 boost::variant<boost::blank, double, bool, String, Object::Ptr> m_Value;
244 const T& Get(void) const
246 return boost::get<T>(m_Value);
250 extern I2_BASE_API Value Empty;
252 I2_BASE_API Value operator+(const Value& lhs, const char *rhs);
253 I2_BASE_API Value operator+(const char *lhs, const Value& rhs);
255 I2_BASE_API Value operator+(const Value& lhs, const String& rhs);
256 I2_BASE_API Value operator+(const String& lhs, const Value& rhs);
258 I2_BASE_API Value operator+(const Value& lhs, const Value& rhs);
259 I2_BASE_API Value operator+(const Value& lhs, double rhs);
260 I2_BASE_API Value operator+(double lhs, const Value& rhs);
261 I2_BASE_API Value operator+(const Value& lhs, int rhs);
262 I2_BASE_API Value operator+(int lhs, const Value& rhs);
264 I2_BASE_API Value operator-(const Value& lhs, const Value& rhs);
265 I2_BASE_API Value operator-(const Value& lhs, double rhs);
266 I2_BASE_API Value operator-(double lhs, const Value& rhs);
267 I2_BASE_API Value operator-(const Value& lhs, int rhs);
268 I2_BASE_API Value operator-(int lhs, const Value& rhs);
270 I2_BASE_API Value operator*(const Value& lhs, const Value& rhs);
271 I2_BASE_API Value operator*(const Value& lhs, double rhs);
272 I2_BASE_API Value operator*(double lhs, const Value& rhs);
273 I2_BASE_API Value operator*(const Value& lhs, int rhs);
274 I2_BASE_API Value operator*(int lhs, const Value& rhs);
276 I2_BASE_API Value operator/(const Value& lhs, const Value& rhs);
277 I2_BASE_API Value operator/(const Value& lhs, double rhs);
278 I2_BASE_API Value operator/(double lhs, const Value& rhs);
279 I2_BASE_API Value operator/(const Value& lhs, int rhs);
280 I2_BASE_API Value operator/(int lhs, const Value& rhs);
282 I2_BASE_API Value operator%(const Value& lhs, const Value& rhs);
283 I2_BASE_API Value operator%(const Value& lhs, double rhs);
284 I2_BASE_API Value operator%(double lhs, const Value& rhs);
285 I2_BASE_API Value operator%(const Value& lhs, int rhs);
286 I2_BASE_API Value operator%(int lhs, const Value& rhs);
288 I2_BASE_API Value operator^(const Value& lhs, const Value& rhs);
289 I2_BASE_API Value operator^(const Value& lhs, double rhs);
290 I2_BASE_API Value operator^(double lhs, const Value& rhs);
291 I2_BASE_API Value operator^(const Value& lhs, int rhs);
292 I2_BASE_API Value operator^(int lhs, const Value& rhs);
294 I2_BASE_API Value operator&(const Value& lhs, const Value& rhs);
295 I2_BASE_API Value operator&(const Value& lhs, double rhs);
296 I2_BASE_API Value operator&(double lhs, const Value& rhs);
297 I2_BASE_API Value operator&(const Value& lhs, int rhs);
298 I2_BASE_API Value operator&(int lhs, const Value& rhs);
300 I2_BASE_API Value operator|(const Value& lhs, const Value& rhs);
301 I2_BASE_API Value operator|(const Value& lhs, double rhs);
302 I2_BASE_API Value operator|(double lhs, const Value& rhs);
303 I2_BASE_API Value operator|(const Value& lhs, int rhs);
304 I2_BASE_API Value operator|(int lhs, const Value& rhs);
306 I2_BASE_API Value operator<<(const Value& lhs, const Value& rhs);
307 I2_BASE_API Value operator<<(const Value& lhs, double rhs);
308 I2_BASE_API Value operator<<(double lhs, const Value& rhs);
309 I2_BASE_API Value operator<<(const Value& lhs, int rhs);
310 I2_BASE_API Value operator<<(int lhs, const Value& rhs);
312 I2_BASE_API Value operator>>(const Value& lhs, const Value& rhs);
313 I2_BASE_API Value operator>>(const Value& lhs, double rhs);
314 I2_BASE_API Value operator>>(double lhs, const Value& rhs);
315 I2_BASE_API Value operator>>(const Value& lhs, int rhs);
316 I2_BASE_API Value operator>>(int lhs, const Value& rhs);
318 I2_BASE_API bool operator<(const Value& lhs, const Value& rhs);
319 I2_BASE_API bool operator<(const Value& lhs, double rhs);
320 I2_BASE_API bool operator<(double lhs, const Value& rhs);
321 I2_BASE_API bool operator<(const Value& lhs, int rhs);
322 I2_BASE_API bool operator<(int lhs, const Value& rhs);
324 I2_BASE_API bool operator>(const Value& lhs, const Value& rhs);
325 I2_BASE_API bool operator>(const Value& lhs, double rhs);
326 I2_BASE_API bool operator>(double lhs, const Value& rhs);
327 I2_BASE_API bool operator>(const Value& lhs, int rhs);
328 I2_BASE_API bool operator>(int lhs, const Value& rhs);
330 I2_BASE_API bool operator<=(const Value& lhs, const Value& rhs);
331 I2_BASE_API bool operator<=(const Value& lhs, double rhs);
332 I2_BASE_API bool operator<=(double lhs, const Value& rhs);
333 I2_BASE_API bool operator<=(const Value& lhs, int rhs);
334 I2_BASE_API bool operator<=(int lhs, const Value& rhs);
336 I2_BASE_API bool operator>=(const Value& lhs, const Value& rhs);
337 I2_BASE_API bool operator>=(const Value& lhs, double rhs);
338 I2_BASE_API bool operator>=(double lhs, const Value& rhs);
339 I2_BASE_API bool operator>=(const Value& lhs, int rhs);
340 I2_BASE_API bool operator>=(int lhs, const Value& rhs);
342 I2_BASE_API std::ostream& operator<<(std::ostream& stream, const Value& value);
343 I2_BASE_API std::istream& operator>>(std::istream& stream, Value& value);