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