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/value.hpp"
21 #include "base/array.hpp"
22 #include "base/dictionary.hpp"
23 #include "base/utility.hpp"
24 #include "base/objectlock.hpp"
25 #include <boost/foreach.hpp>
26 #include <boost/lexical_cast.hpp>
28 using namespace icinga;
30 Value::operator double(void) const
32 const double *value = boost::get<double>(&m_Value);
40 return boost::lexical_cast<double>(m_Value);
43 Value::operator String(void) const
46 double integral, fractional;
52 fractional = std::modf(boost::get<double>(m_Value), &integral);
55 return boost::lexical_cast<std::string>(m_Value);
57 return boost::lexical_cast<std::string>((long)integral);
59 if (boost::get<bool>(m_Value))
64 return boost::get<String>(m_Value);
66 object = boost::get<Object::Ptr>(m_Value).get();
67 return object->ToString();
69 BOOST_THROW_EXCEPTION(std::runtime_error("Unknown value type."));
73 std::ostream& icinga::operator<<(std::ostream& stream, const Value& value)
75 stream << static_cast<String>(value);
79 std::istream& icinga::operator>>(std::istream& stream, Value& value)
87 bool Value::operator==(bool rhs) const
89 return *this == Value(rhs);
92 bool Value::operator!=(bool rhs) const
94 return !(*this == rhs);
97 bool Value::operator==(int rhs) const
99 return *this == Value(rhs);
102 bool Value::operator!=(int rhs) const
104 return !(*this == rhs);
107 bool Value::operator==(double rhs) const
109 return *this == Value(rhs);
112 bool Value::operator!=(double rhs) const
114 return !(*this == rhs);
117 bool Value::operator==(const char *rhs) const
119 return static_cast<String>(*this) == rhs;
122 bool Value::operator!=(const char *rhs) const
124 return !(*this == rhs);
127 bool Value::operator==(const String& rhs) const
129 return static_cast<String>(*this) == rhs;
132 bool Value::operator!=(const String& rhs) const
134 return !(*this == rhs);
137 bool Value::operator==(const Value& rhs) const
139 if (IsNumber() && rhs.IsNumber())
140 return Get<double>() == rhs.Get<double>();
141 else if ((IsBoolean() || IsNumber() || IsEmpty()) && (rhs.IsBoolean() || rhs.IsNumber() || rhs.IsEmpty()) && !(IsEmpty() && rhs.IsEmpty()))
142 return static_cast<double>(*this) == static_cast<double>(rhs);
144 if (IsString() && rhs.IsString())
145 return Get<String>() == rhs.Get<String>();
146 else if ((IsString() || IsEmpty()) && (rhs.IsString() || rhs.IsEmpty()) && !(IsEmpty() && rhs.IsEmpty()))
147 return static_cast<String>(*this) == static_cast<String>(rhs);
149 if (IsEmpty() != rhs.IsEmpty())
155 if (IsObject() != rhs.IsObject())
159 if (IsObjectType<Array>() && rhs.IsObjectType<Array>()) {
160 Array::Ptr arr1 = *this;
161 Array::Ptr arr2 = rhs;
166 if (arr1->GetLength() != arr2->GetLength())
169 for (Array::SizeType i = 0; i < arr1->GetLength(); i++) {
170 if (arr1->Get(i) != arr2->Get(i))
177 return Get<Object::Ptr>() == rhs.Get<Object::Ptr>();
183 bool Value::operator!=(const Value& rhs) const
185 return !(*this == rhs);
188 Value icinga::operator+(const Value& lhs, const char *rhs)
190 return lhs + Value(rhs);
193 Value icinga::operator+(const char *lhs, const Value& rhs)
195 return Value(lhs) + rhs;
198 Value icinga::operator+(const Value& lhs, const String& rhs)
200 return lhs + Value(rhs);
203 Value icinga::operator+(const String& lhs, const Value& rhs)
205 return Value(lhs) + rhs;
208 Value icinga::operator+(const Value& lhs, const Value& rhs)
210 if ((lhs.IsEmpty() || lhs.IsNumber()) && (rhs.IsEmpty() || rhs.IsNumber()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
211 return static_cast<double>(lhs) + static_cast<double>(rhs);
212 if ((lhs.IsString() || lhs.IsEmpty() || lhs.IsNumber()) && (rhs.IsString() || rhs.IsEmpty() || rhs.IsNumber()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
213 return static_cast<String>(lhs) + static_cast<String>(rhs);
214 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
215 return static_cast<double>(lhs) + static_cast<double>(rhs);
216 else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
217 Array::Ptr result = new Array();
219 static_cast<Array::Ptr>(lhs)->CopyTo(result);
221 static_cast<Array::Ptr>(rhs)->CopyTo(result);
223 } else if ((lhs.IsObjectType<Dictionary>() || lhs.IsEmpty()) && (rhs.IsObjectType<Dictionary>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
224 Dictionary::Ptr result = new Dictionary();
226 static_cast<Dictionary::Ptr>(lhs)->CopyTo(result);
228 static_cast<Dictionary::Ptr>(rhs)->CopyTo(result);
231 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator + cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
235 Value icinga::operator+(const Value& lhs, double rhs)
237 return lhs + Value(rhs);
240 Value icinga::operator+(double lhs, const Value& rhs)
242 return Value(lhs) + rhs;
245 Value icinga::operator+(const Value& lhs, int rhs)
247 return lhs + Value(rhs);
250 Value icinga::operator+(int lhs, const Value& rhs)
252 return Value(lhs) + rhs;
255 Value icinga::operator-(const Value& lhs, const Value& rhs)
257 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
258 return static_cast<double>(lhs) - static_cast<double>(rhs);
259 else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
263 Array::Ptr result = new Array();
264 Array::Ptr left = lhs;
265 Array::Ptr right = rhs;
267 ObjectLock olock(left);
268 BOOST_FOREACH(const Value& lv, left) {
270 ObjectLock xlock(right);
271 BOOST_FOREACH(const Value& rv, right) {
286 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator - cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
289 Value icinga::operator-(const Value& lhs, double rhs)
291 return lhs - Value(rhs);
294 Value icinga::operator-(double lhs, const Value& rhs)
296 return Value(lhs) - rhs;
299 Value icinga::operator-(const Value& lhs, int rhs)
301 return lhs - Value(rhs);
304 Value icinga::operator-(int lhs, const Value& rhs)
306 return Value(lhs) - rhs;
309 Value icinga::operator*(const Value& lhs, const Value& rhs)
311 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
312 return static_cast<double>(lhs) * static_cast<double>(rhs);
314 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator * cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
317 Value icinga::operator*(const Value& lhs, double rhs)
319 return lhs * Value(rhs);
322 Value icinga::operator*(double lhs, const Value& rhs)
324 return Value(lhs) * rhs;
327 Value icinga::operator*(const Value& lhs, int rhs)
329 return lhs * Value(rhs);
332 Value icinga::operator*(int lhs, const Value& rhs)
334 return Value(lhs) * rhs;
337 Value icinga::operator/(const Value& lhs, const Value& rhs)
341 else if (rhs.IsEmpty())
342 BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is Empty."));
343 else if (lhs.IsNumber() && rhs.IsNumber()) {
344 if (static_cast<double>(rhs) == 0)
345 BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is 0."));
347 return static_cast<double>(lhs) / static_cast<double>(rhs);
349 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator / cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
352 Value icinga::operator/(const Value& lhs, double rhs)
354 return lhs / Value(rhs);
357 Value icinga::operator/(double lhs, const Value& rhs)
359 return Value(lhs) / rhs;
362 Value icinga::operator/(const Value& lhs, int rhs)
364 return lhs / Value(rhs);
367 Value icinga::operator/(int lhs, const Value& rhs)
369 return Value(lhs) / rhs;
372 Value icinga::operator&(const Value& lhs, const Value& rhs)
374 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
375 return static_cast<int>(lhs) & static_cast<int>(rhs);
377 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator & cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
380 Value icinga::operator&(const Value& lhs, double rhs)
382 return lhs & Value(rhs);
385 Value icinga::operator&(double lhs, const Value& rhs)
387 return Value(lhs) & rhs;
390 Value icinga::operator&(const Value& lhs, int rhs)
392 return lhs & Value(rhs);
395 Value icinga::operator&(int lhs, const Value& rhs)
397 return Value(lhs) & rhs;
400 Value icinga::operator|(const Value& lhs, const Value& rhs)
402 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
403 return static_cast<int>(lhs) | static_cast<int>(rhs);
405 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator | cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
408 Value icinga::operator|(const Value& lhs, double rhs)
410 return lhs | Value(rhs);
413 Value icinga::operator|(double lhs, const Value& rhs)
415 return Value(lhs) | rhs;
418 Value icinga::operator|(const Value& lhs, int rhs)
420 return lhs | Value(rhs);
423 Value icinga::operator|(int lhs, const Value& rhs)
425 return Value(lhs) | rhs;
428 Value icinga::operator<<(const Value& lhs, const Value& rhs)
430 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
431 return static_cast<int>(lhs) << static_cast<int>(rhs);
433 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator << cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
436 Value icinga::operator<<(const Value& lhs, double rhs)
438 return lhs << Value(rhs);
441 Value icinga::operator<<(double lhs, const Value& rhs)
443 return Value(lhs) << rhs;
446 Value icinga::operator<<(const Value& lhs, int rhs)
448 return lhs << Value(rhs);
451 Value icinga::operator<<(int lhs, const Value& rhs)
453 return Value(lhs) << rhs;
456 Value icinga::operator>>(const Value& lhs, const Value& rhs)
458 if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
459 return static_cast<int>(lhs) >> static_cast<int>(rhs);
461 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator >> cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
464 Value icinga::operator>>(const Value& lhs, double rhs)
466 return lhs >> Value(rhs);
469 Value icinga::operator>>(double lhs, const Value& rhs)
471 return Value(lhs) >> rhs;
474 Value icinga::operator>>(const Value& lhs, int rhs)
476 return lhs >> Value(rhs);
479 Value icinga::operator>>(int lhs, const Value& rhs)
481 return Value(lhs) >> rhs;
484 bool icinga::operator<(const Value& lhs, const Value& rhs)
486 if (lhs.IsString() && rhs.IsString())
487 return static_cast<String>(lhs) < static_cast<String>(rhs);
488 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
489 return static_cast<int>(lhs) < static_cast<int>(rhs);
490 else if (lhs.GetTypeName() != rhs.GetTypeName())
491 return lhs.GetTypeName() < rhs.GetTypeName();
493 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator < cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
496 bool icinga::operator<(const Value& lhs, double rhs)
498 return lhs < Value(rhs);
501 bool icinga::operator<(double lhs, const Value& rhs)
503 return Value(lhs) < rhs;
506 bool icinga::operator<(const Value& lhs, int rhs)
508 return lhs < Value(rhs);
511 bool icinga::operator<(int lhs, const Value& rhs)
513 return Value(lhs) < rhs;
516 bool icinga::operator>(const Value& lhs, const Value& rhs)
518 if (lhs.IsString() && rhs.IsString())
519 return static_cast<String>(lhs) > static_cast<String>(rhs);
520 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
521 return static_cast<int>(lhs) > static_cast<int>(rhs);
522 else if (lhs.GetTypeName() != rhs.GetTypeName())
523 return lhs.GetTypeName() > rhs.GetTypeName();
525 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator > cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
528 bool icinga::operator>(const Value& lhs, double rhs)
530 return lhs > Value(rhs);
533 bool icinga::operator>(double lhs, const Value& rhs)
535 return Value(lhs) > rhs;
538 bool icinga::operator>(const Value& lhs, int rhs)
540 return lhs > Value(rhs);
543 bool icinga::operator>(int lhs, const Value& rhs)
545 return Value(lhs) > rhs;
548 bool icinga::operator<=(const Value& lhs, const Value& rhs)
550 if (lhs.IsString() && rhs.IsString())
551 return static_cast<String>(lhs) <= static_cast<String>(rhs);
552 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
553 return static_cast<int>(lhs) <= static_cast<int>(rhs);
554 else if (lhs.GetTypeName() != rhs.GetTypeName())
555 return lhs.GetTypeName() <= rhs.GetTypeName();
557 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator <= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
560 bool icinga::operator<=(const Value& lhs, double rhs)
562 return lhs <= Value(rhs);
565 bool icinga::operator<=(double lhs, const Value& rhs)
567 return Value(lhs) <= rhs;
570 bool icinga::operator<=(const Value& lhs, int rhs)
572 return lhs <= Value(rhs);
575 bool icinga::operator<=(int lhs, const Value& rhs)
577 return Value(lhs) <= rhs;
580 bool icinga::operator>=(const Value& lhs, const Value& rhs)
582 if (lhs.IsString() && rhs.IsString())
583 return static_cast<String>(lhs) >= static_cast<String>(rhs);
584 else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
585 return static_cast<int>(lhs) >= static_cast<int>(rhs);
586 else if (lhs.GetTypeName() != rhs.GetTypeName())
587 return lhs.GetTypeName() >= rhs.GetTypeName();
589 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator >= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
592 bool icinga::operator>=(const Value& lhs, double rhs)
594 return lhs >= Value(rhs);
597 bool icinga::operator>=(double lhs, const Value& rhs)
599 return Value(lhs) >= rhs;
602 bool icinga::operator>=(const Value& lhs, int rhs)
604 return lhs >= Value(rhs);
607 bool icinga::operator>=(int lhs, const Value& rhs)
609 return Value(lhs) >= rhs;