]> granicus.if.org Git - icinga2/blob - lib/base/value.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / value.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef VALUE_H
4 #define VALUE_H
5
6 #include "base/object.hpp"
7 #include "base/string.hpp"
8 #include <boost/variant/variant.hpp>
9 #include <boost/variant/get.hpp>
10 #include <boost/throw_exception.hpp>
11
12 namespace icinga
13 {
14
15 typedef double Timestamp;
16
17 /**
18  * The type of a Value.
19  *
20  * @ingroup base
21  */
22 enum ValueType
23 {
24         ValueEmpty = 0,
25         ValueNumber = 1,
26         ValueBoolean = 2,
27         ValueString = 3,
28         ValueObject = 4
29 };
30
31 /**
32  * A type that can hold an arbitrary value.
33  *
34  * @ingroup base
35  */
36 class Value
37 {
38 public:
39         Value() = default;
40         Value(std::nullptr_t);
41         Value(int value);
42         Value(unsigned int value);
43         Value(long value);
44         Value(unsigned long value);
45         Value(long long value);
46         Value(unsigned long long value);
47         Value(double value);
48         Value(bool value);
49         Value(const String& value);
50         Value(String&& value);
51         Value(const char *value);
52         Value(const Value& other);
53         Value(Value&& other);
54         Value(Object *value);
55         Value(const intrusive_ptr<Object>& value);
56
57         template<typename T>
58         Value(const intrusive_ptr<T>& value)
59                 : Value(static_pointer_cast<Object>(value))
60         {
61                 static_assert(!std::is_same<T, Object>::value, "T must not be Object");
62         }
63
64         bool ToBool() const;
65
66         operator double() const;
67         operator String() const;
68
69         Value& operator=(const Value& other);
70         Value& operator=(Value&& other);
71
72         bool operator==(bool rhs) const;
73         bool operator!=(bool rhs) const;
74
75         bool operator==(int rhs) const;
76         bool operator!=(int rhs) const;
77
78         bool operator==(double rhs) const;
79         bool operator!=(double rhs) const;
80
81         bool operator==(const char *rhs) const;
82         bool operator!=(const char *rhs) const;
83
84         bool operator==(const String& rhs) const;
85         bool operator!=(const String& rhs) const;
86
87         bool operator==(const Value& rhs) const;
88         bool operator!=(const Value& rhs) const;
89
90         template<typename T>
91         operator intrusive_ptr<T>() const
92         {
93                 if (IsEmpty() && !IsString())
94                         return intrusive_ptr<T>();
95
96                 if (!IsObject())
97                         BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert value of type '" + GetTypeName() + "' to an object."));
98
99                 const auto& object = Get<Object::Ptr>();
100
101                 ASSERT(object);
102
103                 intrusive_ptr<T> tobject = dynamic_pointer_cast<T>(object);
104
105                 if (!tobject)
106                         BOOST_THROW_EXCEPTION(std::bad_cast());
107
108                 return tobject;
109         }
110
111         bool IsEmpty() const;
112         bool IsScalar() const;
113         bool IsNumber() const;
114         bool IsBoolean() const;
115         bool IsString() const;
116         bool IsObject() const;
117
118         template<typename T>
119         bool IsObjectType() const
120         {
121                 if (!IsObject())
122                         return false;
123
124                 return dynamic_cast<T *>(Get<Object::Ptr>().get());
125         }
126
127         ValueType GetType() const;
128
129         void Swap(Value& other);
130
131         String GetTypeName() const;
132
133         Type::Ptr GetReflectionType() const;
134
135         Value Clone() const;
136
137         template<typename T>
138         const T& Get() const
139         {
140                 return boost::get<T>(m_Value);
141         }
142
143 private:
144         boost::variant<boost::blank, double, bool, String, Object::Ptr> m_Value;
145 };
146
147 extern template const double& Value::Get<double>() const;
148 extern template const bool& Value::Get<bool>() const;
149 extern template const String& Value::Get<String>() const;
150 extern template const Object::Ptr& Value::Get<Object::Ptr>() const;
151
152 extern Value Empty;
153
154 Value operator+(const Value& lhs, const char *rhs);
155 Value operator+(const char *lhs, const Value& rhs);
156
157 Value operator+(const Value& lhs, const String& rhs);
158 Value operator+(const String& lhs, const Value& rhs);
159
160 Value operator+(const Value& lhs, const Value& rhs);
161 Value operator+(const Value& lhs, double rhs);
162 Value operator+(double lhs, const Value& rhs);
163 Value operator+(const Value& lhs, int rhs);
164 Value operator+(int lhs, const Value& rhs);
165
166 Value operator-(const Value& lhs, const Value& rhs);
167 Value operator-(const Value& lhs, double rhs);
168 Value operator-(double lhs, const Value& rhs);
169 Value operator-(const Value& lhs, int rhs);
170 Value operator-(int lhs, const Value& rhs);
171
172 Value operator*(const Value& lhs, const Value& rhs);
173 Value operator*(const Value& lhs, double rhs);
174 Value operator*(double lhs, const Value& rhs);
175 Value operator*(const Value& lhs, int rhs);
176 Value operator*(int lhs, const Value& rhs);
177
178 Value operator/(const Value& lhs, const Value& rhs);
179 Value operator/(const Value& lhs, double rhs);
180 Value operator/(double lhs, const Value& rhs);
181 Value operator/(const Value& lhs, int rhs);
182 Value operator/(int lhs, const Value& rhs);
183
184 Value operator%(const Value& lhs, const Value& rhs);
185 Value operator%(const Value& lhs, double rhs);
186 Value operator%(double lhs, const Value& rhs);
187 Value operator%(const Value& lhs, int rhs);
188 Value operator%(int lhs, const Value& rhs);
189
190 Value operator^(const Value& lhs, const Value& rhs);
191 Value operator^(const Value& lhs, double rhs);
192 Value operator^(double lhs, const Value& rhs);
193 Value operator^(const Value& lhs, int rhs);
194 Value operator^(int lhs, const Value& rhs);
195
196 Value operator&(const Value& lhs, const Value& rhs);
197 Value operator&(const Value& lhs, double rhs);
198 Value operator&(double lhs, const Value& rhs);
199 Value operator&(const Value& lhs, int rhs);
200 Value operator&(int lhs, const Value& rhs);
201
202 Value operator|(const Value& lhs, const Value& rhs);
203 Value operator|(const Value& lhs, double rhs);
204 Value operator|(double lhs, const Value& rhs);
205 Value operator|(const Value& lhs, int rhs);
206 Value operator|(int lhs, const Value& rhs);
207
208 Value operator<<(const Value& lhs, const Value& rhs);
209 Value operator<<(const Value& lhs, double rhs);
210 Value operator<<(double lhs, const Value& rhs);
211 Value operator<<(const Value& lhs, int rhs);
212 Value operator<<(int lhs, const Value& rhs);
213
214 Value operator>>(const Value& lhs, const Value& rhs);
215 Value operator>>(const Value& lhs, double rhs);
216 Value operator>>(double lhs, const Value& rhs);
217 Value operator>>(const Value& lhs, int rhs);
218 Value operator>>(int lhs, const Value& rhs);
219
220 bool operator<(const Value& lhs, const Value& rhs);
221 bool operator<(const Value& lhs, double rhs);
222 bool operator<(double lhs, const Value& rhs);
223 bool operator<(const Value& lhs, int rhs);
224 bool operator<(int lhs, const Value& rhs);
225
226 bool operator>(const Value& lhs, const Value& rhs);
227 bool operator>(const Value& lhs, double rhs);
228 bool operator>(double lhs, const Value& rhs);
229 bool operator>(const Value& lhs, int rhs);
230 bool operator>(int lhs, const Value& rhs);
231
232 bool operator<=(const Value& lhs, const Value& rhs);
233 bool operator<=(const Value& lhs, double rhs);
234 bool operator<=(double lhs, const Value& rhs);
235 bool operator<=(const Value& lhs, int rhs);
236 bool operator<=(int lhs, const Value& rhs);
237
238 bool operator>=(const Value& lhs, const Value& rhs);
239 bool operator>=(const Value& lhs, double rhs);
240 bool operator>=(double lhs, const Value& rhs);
241 bool operator>=(const Value& lhs, int rhs);
242 bool operator>=(int lhs, const Value& rhs);
243
244 std::ostream& operator<<(std::ostream& stream, const Value& value);
245 std::istream& operator>>(std::istream& stream, Value& value);
246
247 }
248
249 extern template class boost::variant<boost::blank, double, bool, icinga::String, icinga::Object::Ptr>;
250
251 #endif /* VALUE_H */