]> granicus.if.org Git - icinga2/blob - lib/base/value.hpp
Update copyright headers for 2016
[icinga2] / lib / base / value.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef VALUE_H
21 #define VALUE_H
22
23 #include "base/object.hpp"
24 #include "base/string.hpp"
25 #include <boost/variant/variant.hpp>
26 #include <boost/variant/get.hpp>
27
28 namespace icinga
29 {
30
31 /**
32  * The type of a Value.
33  *
34  * @ingroup base
35  */
36 enum ValueType
37 {
38         ValueEmpty = 0,
39         ValueNumber = 1,
40         ValueBoolean = 2,
41         ValueString = 3,
42         ValueObject = 4
43 };
44
45 /**
46  * A type that can hold an arbitrary value.
47  *
48  * @ingroup base
49  */
50 class I2_BASE_API Value
51 {
52 public:
53         inline Value(void)
54         { }
55
56         inline Value(int value)
57                 : m_Value(double(value))
58         { }
59
60         inline Value(unsigned int value)
61                 : m_Value(double(value))
62         { }
63
64         inline Value(long value)
65                 : m_Value(double(value))
66         { }
67
68         inline Value(unsigned long value)
69                 : m_Value(double(value))
70         { }
71
72         inline Value(double value)
73                 : m_Value(value)
74         { }
75
76         inline Value(bool value)
77                 : m_Value(value)
78         { }
79
80         inline Value(const String& value)
81                 : m_Value(value)
82         { }
83
84         inline Value(const char *value)
85                 : m_Value(String(value))
86         { }
87
88         inline Value(const Value& other)
89                 : m_Value(other.m_Value)
90         { }
91
92         inline Value(Object *value)
93         {
94                 if (!value)
95                         return;
96
97                 m_Value = Object::Ptr(value);
98         }
99
100         template<typename T>
101         inline Value(const intrusive_ptr<T>& value)
102         {
103                 if (!value)
104                         return;
105
106                 m_Value = static_pointer_cast<Object>(value);
107         }
108
109         bool ToBool(void) const;
110
111         operator double(void) const;
112         operator String(void) const;
113
114         Value& operator=(const Value& other);
115
116         bool operator==(bool rhs) const;
117         bool operator!=(bool rhs) const;
118
119         bool operator==(int rhs) const;
120         bool operator!=(int rhs) const;
121
122         bool operator==(double rhs) const;
123         bool operator!=(double rhs) const;
124
125         bool operator==(const char *rhs) const;
126         bool operator!=(const char *rhs) const;
127
128         bool operator==(const String& rhs) const;
129         bool operator!=(const String& rhs) const;
130
131         bool operator==(const Value& rhs) const;
132         bool operator!=(const Value& rhs) const;
133
134         template<typename T>
135         operator intrusive_ptr<T>(void) const
136         {
137                 if (IsEmpty())
138                         return intrusive_ptr<T>();
139
140                 if (!IsObject())
141                         BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert value of type '" + GetTypeName() + "' to an object."));
142
143                 Object::Ptr object = boost::get<Object::Ptr>(m_Value);
144
145                 ASSERT(object);
146
147                 intrusive_ptr<T> tobject = dynamic_pointer_cast<T>(object);
148
149                 if (!tobject)
150                         BOOST_THROW_EXCEPTION(std::bad_cast());
151
152                 return tobject;
153         }
154
155         /**
156         * Checks whether the variant is empty.
157         *
158         * @returns true if the variant is empty, false otherwise.
159         */
160         inline bool IsEmpty(void) const
161         {
162                 return (GetType() == ValueEmpty || (IsString() && boost::get<String>(m_Value).IsEmpty()));
163         }
164
165         /**
166         * Checks whether the variant is scalar (i.e. not an object and not empty).
167         *
168         * @returns true if the variant is scalar, false otherwise.
169         */
170         inline bool IsScalar(void) const
171         {
172                 return !IsEmpty() && !IsObject();
173         }
174
175         /**
176         * Checks whether the variant is a number.
177         *
178         * @returns true if the variant is a number.
179         */
180         inline bool IsNumber(void) const
181         {
182                 return (GetType() == ValueNumber);
183         }
184
185         /**
186          * Checks whether the variant is a boolean.
187          *
188          * @returns true if the variant is a boolean.
189          */
190         inline bool IsBoolean(void) const
191         {
192                 return (GetType() == ValueBoolean);
193         }
194
195         /**
196         * Checks whether the variant is a string.
197         *
198         * @returns true if the variant is a string.
199         */
200         inline bool IsString(void) const
201         {
202                 return (GetType() == ValueString);
203         }
204
205         /**
206         * Checks whether the variant is a non-null object.
207         *
208         * @returns true if the variant is a non-null object, false otherwise.
209         */
210         inline bool IsObject(void) const
211         {
212                 return  (GetType() == ValueObject);
213         }
214
215         template<typename T>
216         bool IsObjectType(void) const
217         {
218                 if (!IsObject())
219                         return false;
220
221                 return (dynamic_pointer_cast<T>(boost::get<Object::Ptr>(m_Value)) != NULL);
222         }
223
224         /**
225         * Returns the type of the value.
226         *
227         * @returns The type.
228         */
229         ValueType GetType(void) const
230         {
231                 return static_cast<ValueType>(m_Value.which());
232         }
233
234         String GetTypeName(void) const;
235
236         Type::Ptr GetReflectionType(void) const;
237         
238         Value Clone(void) const;
239
240 private:
241         boost::variant<boost::blank, double, bool, String, Object::Ptr> m_Value;
242
243         template<typename T>
244         const T& Get(void) const
245         {
246                 return boost::get<T>(m_Value);
247         }
248 };
249
250 extern I2_BASE_API Value Empty;
251
252 I2_BASE_API Value operator+(const Value& lhs, const char *rhs);
253 I2_BASE_API Value operator+(const char *lhs, const Value& rhs);
254
255 I2_BASE_API Value operator+(const Value& lhs, const String& rhs);
256 I2_BASE_API Value operator+(const String& lhs, const Value& rhs);
257
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);
263
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);
269
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);
275
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);
281
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);
287
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);
293
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);
299
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);
305
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);
311
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);
317
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);
323
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);
329
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);
335
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);
341
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);
344
345 }
346
347 #endif /* VALUE_H */