1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://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 ******************************************************************************/
20 #include "base/application.h"
21 #include "base/array.h"
22 #include "base/logger_fwd.h"
23 #include "base/utility.h"
25 #include <boost/lexical_cast.hpp>
27 using namespace icinga;
29 Value::operator double(void) const
31 const double *value = boost::get<double>(&m_Value);
39 return boost::lexical_cast<double>(m_Value);
42 Value::operator String(void) const
45 double integral, fractional;
51 fractional = modf(boost::get<double>(m_Value), &integral);
54 return boost::lexical_cast<String>(m_Value);
56 return boost::lexical_cast<String>((long)integral);
58 return boost::get<String>(m_Value);
60 object = boost::get<Object::Ptr>(m_Value).get();
61 return "Object of type '" + Utility::GetTypeName(typeid(*object)) + "'";
63 BOOST_THROW_EXCEPTION(std::runtime_error("Unknown value type."));
67 std::ostream& icinga::operator<<(std::ostream& stream, const Value& value)
69 stream << static_cast<String>(value);
73 std::istream& icinga::operator>>(std::istream& stream, Value& value)
81 bool Value::operator==(bool rhs) const
83 return *this == Value(rhs);
86 bool Value::operator!=(bool rhs) const
88 return !(*this == rhs);
91 bool Value::operator==(int rhs) const
93 return *this == Value(rhs);
96 bool Value::operator!=(int rhs) const
98 return !(*this == rhs);
101 bool Value::operator==(double rhs) const
103 return *this == Value(rhs);
106 bool Value::operator!=(double rhs) const
108 return !(*this == rhs);
111 bool Value::operator==(const char *rhs) const
113 return static_cast<String>(*this) == rhs;
116 bool Value::operator!=(const char *rhs) const
118 return !(*this == rhs);
121 bool Value::operator==(const String& rhs) const
123 return static_cast<String>(*this) == rhs;
126 bool Value::operator!=(const String& rhs) const
128 return !(*this == rhs);
131 bool Value::operator==(const Value& rhs) const
133 if (IsEmpty() != rhs.IsEmpty())
139 if (IsObject() != rhs.IsObject())
143 if (IsObjectType<Array>() && rhs.IsObjectType<Array>()) {
144 Array::Ptr arr1 = *this;
145 Array::Ptr arr2 = rhs;
150 if (arr1->GetLength() != arr2->GetLength())
153 for (int i = 0; i < arr1->GetLength(); i++) {
154 if (arr1->Get(i) != arr2->Get(i))
161 return static_cast<Object::Ptr>(*this) == static_cast<Object::Ptr>(rhs);
164 if ((IsNumber() || IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(IsEmpty() && rhs.IsEmpty()))
165 return static_cast<double>(*this) == static_cast<double>(rhs);
167 if ((IsString() || IsEmpty()) && (rhs.IsString() || rhs.IsEmpty()) && !(IsEmpty() && rhs.IsEmpty()))
168 return static_cast<String>(*this) == static_cast<String>(rhs);
173 bool Value::operator!=(const Value& rhs) const
175 return !(*this == rhs);
178 Value icinga::operator+(const Value& lhs, const char *rhs)
180 return lhs + Value(rhs);
183 Value icinga::operator+(const char *lhs, const Value& rhs)
185 return Value(lhs) + rhs;
188 Value icinga::operator+(const Value& lhs, const String& rhs)
190 return lhs + Value(rhs);
193 Value icinga::operator+(const String& lhs, const Value& rhs)
195 return Value(lhs) + rhs;
198 Value icinga::operator+(const Value& lhs, const Value& rhs)
200 if ((lhs.IsString() || lhs.IsEmpty()) && (rhs.IsString() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
201 return static_cast<String>(lhs) + static_cast<String>(rhs);
202 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
203 return static_cast<double>(lhs) + static_cast<double>(rhs);
204 else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
205 Array::Ptr result = make_shared<Array>();
207 static_cast<Array::Ptr>(lhs)->CopyTo(result);
209 static_cast<Array::Ptr>(rhs)->CopyTo(result);
211 } else if ((lhs.IsObjectType<Dictionary>() || lhs.IsEmpty()) && (rhs.IsObjectType<Dictionary>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
212 Dictionary::Ptr result = make_shared<Dictionary>();
214 static_cast<Dictionary::Ptr>(lhs)->CopyTo(result);
216 static_cast<Dictionary::Ptr>(rhs)->CopyTo(result);
219 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator + cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
223 Value icinga::operator+(const Value& lhs, double rhs)
225 return lhs + Value(rhs);
228 Value icinga::operator+(double lhs, const Value& rhs)
230 return Value(lhs) + rhs;
233 Value icinga::operator+(const Value& lhs, int rhs)
235 return lhs + Value(rhs);
238 Value icinga::operator+(int lhs, const Value& rhs)
240 return Value(lhs) + rhs;
243 Value icinga::operator-(const Value& lhs, const Value& rhs)
245 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
246 return static_cast<double>(lhs) - static_cast<double>(rhs);
248 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator - cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
251 Value icinga::operator-(const Value& lhs, double rhs)
253 return lhs - Value(rhs);
256 Value icinga::operator-(double lhs, const Value& rhs)
258 return Value(lhs) - rhs;
261 Value icinga::operator-(const Value& lhs, int rhs)
263 return lhs - Value(rhs);
266 Value icinga::operator-(int lhs, const Value& rhs)
268 return Value(lhs) - rhs;
271 Value icinga::operator*(const Value& lhs, const Value& rhs)
273 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
274 return static_cast<double>(lhs) * static_cast<double>(rhs);
276 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator * cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
279 Value icinga::operator*(const Value& lhs, double rhs)
281 return lhs * Value(rhs);
284 Value icinga::operator*(double lhs, const Value& rhs)
286 return Value(lhs) * rhs;
289 Value icinga::operator*(const Value& lhs, int rhs)
291 return lhs * Value(rhs);
294 Value icinga::operator*(int lhs, const Value& rhs)
296 return Value(lhs) * rhs;
299 Value icinga::operator/(const Value& lhs, const Value& rhs)
303 else if (rhs.IsEmpty())
304 BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is Empty."));
305 else if (lhs.IsNumber() && rhs.IsNumber()) {
306 if (static_cast<double>(rhs) == 0)
307 BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is 0."));
309 return static_cast<double>(lhs) / static_cast<double>(rhs);
311 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator / cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
314 Value icinga::operator/(const Value& lhs, double rhs)
316 return lhs / Value(rhs);
319 Value icinga::operator/(double lhs, const Value& rhs)
321 return Value(lhs) / rhs;
324 Value icinga::operator/(const Value& lhs, int rhs)
326 return lhs / Value(rhs);
329 Value icinga::operator/(int lhs, const Value& rhs)
331 return Value(lhs) / rhs;
334 Value icinga::operator&(const Value& lhs, const Value& rhs)
336 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
337 return static_cast<int>(lhs) & static_cast<int>(rhs);
339 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator & cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
342 Value icinga::operator&(const Value& lhs, double rhs)
344 return lhs & Value(rhs);
347 Value icinga::operator&(double lhs, const Value& rhs)
349 return Value(lhs) & rhs;
352 Value icinga::operator&(const Value& lhs, int rhs)
354 return lhs & Value(rhs);
357 Value icinga::operator&(int lhs, const Value& rhs)
359 return Value(lhs) & rhs;
362 Value icinga::operator|(const Value& lhs, const Value& rhs)
364 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
365 return static_cast<int>(lhs) | static_cast<int>(rhs);
367 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator | cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
370 Value icinga::operator|(const Value& lhs, double rhs)
372 return lhs | Value(rhs);
375 Value icinga::operator|(double lhs, const Value& rhs)
377 return Value(lhs) | rhs;
380 Value icinga::operator|(const Value& lhs, int rhs)
382 return lhs | Value(rhs);
385 Value icinga::operator|(int lhs, const Value& rhs)
387 return Value(lhs) | rhs;
390 Value icinga::operator<<(const Value& lhs, const Value& rhs)
392 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
393 return static_cast<int>(lhs) << static_cast<int>(rhs);
395 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator << cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
398 Value icinga::operator<<(const Value& lhs, double rhs)
400 return lhs << Value(rhs);
403 Value icinga::operator<<(double lhs, const Value& rhs)
405 return Value(lhs) << rhs;
408 Value icinga::operator<<(const Value& lhs, int rhs)
410 return lhs << Value(rhs);
413 Value icinga::operator<<(int lhs, const Value& rhs)
415 return Value(lhs) << rhs;
418 Value icinga::operator>>(const Value& lhs, const Value& rhs)
420 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
421 return static_cast<int>(lhs) >> static_cast<int>(rhs);
423 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator >> cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
426 Value icinga::operator>>(const Value& lhs, double rhs)
428 return lhs >> Value(rhs);
431 Value icinga::operator>>(double lhs, const Value& rhs)
433 return Value(lhs) >> rhs;
436 Value icinga::operator>>(const Value& lhs, int rhs)
438 return lhs >> Value(rhs);
441 Value icinga::operator>>(int lhs, const Value& rhs)
443 return Value(lhs) >> rhs;
446 Value icinga::operator<(const Value& lhs, const Value& rhs)
448 if (lhs.IsString() && rhs.IsString())
449 return static_cast<String>(lhs) < static_cast<String>(rhs);
450 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
451 return static_cast<int>(lhs) < static_cast<int>(rhs);
452 else if (lhs.GetTypeName() != rhs.GetTypeName())
453 return lhs.GetTypeName() < rhs.GetTypeName();
455 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator < cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
458 Value icinga::operator<(const Value& lhs, double rhs)
460 return lhs < Value(rhs);
463 Value icinga::operator<(double lhs, const Value& rhs)
465 return Value(lhs) < rhs;
468 Value icinga::operator<(const Value& lhs, int rhs)
470 return lhs < Value(rhs);
473 Value icinga::operator<(int lhs, const Value& rhs)
475 return Value(lhs) < rhs;
478 Value icinga::operator>(const Value& lhs, const Value& rhs)
480 if (lhs.IsString() && rhs.IsString())
481 return static_cast<String>(lhs) > static_cast<String>(rhs);
482 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
483 return static_cast<int>(lhs) > static_cast<int>(rhs);
484 else if (lhs.GetTypeName() != rhs.GetTypeName())
485 return lhs.GetTypeName() > rhs.GetTypeName();
487 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator > cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
490 Value icinga::operator>(const Value& lhs, double rhs)
492 return lhs > Value(rhs);
495 Value icinga::operator>(double lhs, const Value& rhs)
497 return Value(lhs) > rhs;
500 Value icinga::operator>(const Value& lhs, int rhs)
502 return lhs > Value(rhs);
505 Value icinga::operator>(int lhs, const Value& rhs)
507 return Value(lhs) > rhs;
510 Value icinga::operator<=(const Value& lhs, const Value& rhs)
512 if (lhs.IsString() && rhs.IsString())
513 return static_cast<String>(lhs) <= static_cast<String>(rhs);
514 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
515 return static_cast<int>(lhs) <= static_cast<int>(rhs);
516 else if (lhs.GetTypeName() != rhs.GetTypeName())
517 return lhs.GetTypeName() <= rhs.GetTypeName();
519 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator <= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
522 Value icinga::operator<=(const Value& lhs, double rhs)
524 return lhs <= Value(rhs);
527 Value icinga::operator<=(double lhs, const Value& rhs)
529 return Value(lhs) <= rhs;
532 Value icinga::operator<=(const Value& lhs, int rhs)
534 return lhs <= Value(rhs);
537 Value icinga::operator<=(int lhs, const Value& rhs)
539 return Value(lhs) <= rhs;
542 Value icinga::operator>=(const Value& lhs, const Value& rhs)
544 if (lhs.IsString() && rhs.IsString())
545 return static_cast<String>(lhs) >= static_cast<String>(rhs);
546 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
547 return static_cast<int>(lhs) >= static_cast<int>(rhs);
548 else if (lhs.GetTypeName() != rhs.GetTypeName())
549 return lhs.GetTypeName() >= rhs.GetTypeName();
551 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator >= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
554 Value icinga::operator>=(const Value& lhs, double rhs)
556 return lhs >= Value(rhs);
559 Value icinga::operator>=(double lhs, const Value& rhs)
561 return Value(lhs) >= rhs;
564 Value icinga::operator>=(const Value& lhs, int rhs)
566 return lhs >= Value(rhs);
569 Value icinga::operator>=(int lhs, const Value& rhs)
571 return Value(lhs) >= rhs;