1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
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>
15 typedef double Timestamp;
18 * The type of a Value.
32 * A type that can hold an arbitrary value.
40 Value(std::nullptr_t);
42 Value(unsigned int value);
44 Value(unsigned long value);
45 Value(long long value);
46 Value(unsigned long long value);
49 Value(const String& value);
50 Value(String&& value);
51 Value(const char *value);
52 Value(const Value& other);
55 Value(const intrusive_ptr<Object>& value);
58 Value(const intrusive_ptr<T>& value)
59 : Value(static_pointer_cast<Object>(value))
61 static_assert(!std::is_same<T, Object>::value, "T must not be Object");
66 operator double() const;
67 operator String() const;
69 Value& operator=(const Value& other);
70 Value& operator=(Value&& other);
72 bool operator==(bool rhs) const;
73 bool operator!=(bool rhs) const;
75 bool operator==(int rhs) const;
76 bool operator!=(int rhs) const;
78 bool operator==(double rhs) const;
79 bool operator!=(double rhs) const;
81 bool operator==(const char *rhs) const;
82 bool operator!=(const char *rhs) const;
84 bool operator==(const String& rhs) const;
85 bool operator!=(const String& rhs) const;
87 bool operator==(const Value& rhs) const;
88 bool operator!=(const Value& rhs) const;
91 operator intrusive_ptr<T>() const
93 if (IsEmpty() && !IsString())
94 return intrusive_ptr<T>();
97 BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert value of type '" + GetTypeName() + "' to an object."));
99 const auto& object = Get<Object::Ptr>();
103 intrusive_ptr<T> tobject = dynamic_pointer_cast<T>(object);
106 BOOST_THROW_EXCEPTION(std::bad_cast());
111 bool IsEmpty() const;
112 bool IsScalar() const;
113 bool IsNumber() const;
114 bool IsBoolean() const;
115 bool IsString() const;
116 bool IsObject() const;
119 bool IsObjectType() const
124 return dynamic_cast<T *>(Get<Object::Ptr>().get());
127 ValueType GetType() const;
129 void Swap(Value& other);
131 String GetTypeName() const;
133 Type::Ptr GetReflectionType() const;
140 return boost::get<T>(m_Value);
144 boost::variant<boost::blank, double, bool, String, Object::Ptr> m_Value;
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;
154 Value operator+(const Value& lhs, const char *rhs);
155 Value operator+(const char *lhs, const Value& rhs);
157 Value operator+(const Value& lhs, const String& rhs);
158 Value operator+(const String& lhs, const Value& rhs);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
244 std::ostream& operator<<(std::ostream& stream, const Value& value);
245 std::istream& operator>>(std::istream& stream, Value& value);
249 extern template class boost::variant<boost::blank, double, bool, icinga::String, icinga::Object::Ptr>;