]> granicus.if.org Git - icinga2/blob - lib/base/value-operators.cpp
lib->compat->statusdatawriter: fix notifications_enabled
[icinga2] / lib / base / value-operators.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "base/value.hpp"
21 #include "base/array.hpp"
22 #include "base/dictionary.hpp"
23 #include "base/datetime.hpp"
24 #include "base/convert.hpp"
25 #include "base/utility.hpp"
26 #include "base/objectlock.hpp"
27 #include <boost/lexical_cast.hpp>
28
29 using namespace icinga;
30
31 Value::operator double() const
32 {
33         const double *value = boost::get<double>(&m_Value);
34
35         if (value)
36                 return *value;
37
38         const bool *fvalue = boost::get<bool>(&m_Value);
39
40         if (fvalue)
41                 return *fvalue;
42
43         if (IsEmpty())
44                 return 0;
45
46         try {
47                 return boost::lexical_cast<double>(m_Value);
48         } catch (const std::exception&) {
49                 std::ostringstream msgbuf;
50                 msgbuf << "Can't convert '" << *this << "' to a floating point number.";
51                 BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
52         }
53 }
54
55 Value::operator String() const
56 {
57         Object *object;
58
59         switch (GetType()) {
60                 case ValueEmpty:
61                         return String();
62                 case ValueNumber:
63                         return Convert::ToString(boost::get<double>(m_Value));
64                 case ValueBoolean:
65                         if (boost::get<bool>(m_Value))
66                                 return "true";
67                         else
68                                 return "false";
69                 case ValueString:
70                         return boost::get<String>(m_Value);
71                 case ValueObject:
72                         object = boost::get<Object::Ptr>(m_Value).get();
73                         return object->ToString();
74                 default:
75                         BOOST_THROW_EXCEPTION(std::runtime_error("Unknown value type."));
76         }
77 }
78
79 std::ostream& icinga::operator<<(std::ostream& stream, const Value& value)
80 {
81         if (value.IsBoolean())
82                 stream << static_cast<int>(value);
83         else
84                 stream << static_cast<String>(value);
85
86         return stream;
87 }
88
89 std::istream& icinga::operator>>(std::istream& stream, Value& value)
90 {
91         String tstr;
92         stream >> tstr;
93         value = tstr;
94         return stream;
95 }
96
97 bool Value::operator==(bool rhs) const
98 {
99         return *this == Value(rhs);
100 }
101
102 bool Value::operator!=(bool rhs) const
103 {
104         return !(*this == rhs);
105 }
106
107 bool Value::operator==(int rhs) const
108 {
109         return *this == Value(rhs);
110 }
111
112 bool Value::operator!=(int rhs) const
113 {
114         return !(*this == rhs);
115 }
116
117 bool Value::operator==(double rhs) const
118 {
119         return *this == Value(rhs);
120 }
121
122 bool Value::operator!=(double rhs) const
123 {
124         return !(*this == rhs);
125 }
126
127 bool Value::operator==(const char *rhs) const
128 {
129         return static_cast<String>(*this) == rhs;
130 }
131
132 bool Value::operator!=(const char *rhs) const
133 {
134         return !(*this == rhs);
135 }
136
137 bool Value::operator==(const String& rhs) const
138 {
139         return static_cast<String>(*this) == rhs;
140 }
141
142 bool Value::operator!=(const String& rhs) const
143 {
144         return !(*this == rhs);
145 }
146
147 bool Value::operator==(const Value& rhs) const
148 {
149         if (IsNumber() && rhs.IsNumber())
150                 return Get<double>() == rhs.Get<double>();
151         else if ((IsBoolean() || IsNumber()) && (rhs.IsBoolean() || rhs.IsNumber()) && !(IsEmpty() && rhs.IsEmpty()))
152                 return static_cast<double>(*this) == static_cast<double>(rhs);
153
154         if (IsString() && rhs.IsString())
155                 return Get<String>() == rhs.Get<String>();
156         else if ((IsString() || IsEmpty()) && (rhs.IsString() || rhs.IsEmpty()) && !(IsEmpty() && rhs.IsEmpty()))
157                 return static_cast<String>(*this) == static_cast<String>(rhs);
158
159         if (IsEmpty() != rhs.IsEmpty())
160                 return false;
161
162         if (IsEmpty())
163                 return true;
164
165         if (IsObject() != rhs.IsObject())
166                 return false;
167
168         if (IsObject()) {
169                 if (IsObjectType<DateTime>() && rhs.IsObjectType<DateTime>()) {
170                         DateTime::Ptr dt1 = *this;
171                         DateTime::Ptr dt2 = rhs;
172
173                         return dt1->GetValue() == dt2->GetValue();
174                 }
175
176                 if (IsObjectType<Array>() && rhs.IsObjectType<Array>()) {
177                         Array::Ptr arr1 = *this;
178                         Array::Ptr arr2 = rhs;
179
180                         if (arr1 == arr2)
181                                 return true;
182
183                         if (arr1->GetLength() != arr2->GetLength())
184                                 return false;
185
186                         for (Array::SizeType i = 0; i < arr1->GetLength(); i++) {
187                                 if (arr1->Get(i) != arr2->Get(i))
188                                         return false;
189                         }
190
191                         return true;
192                 }
193
194                 return Get<Object::Ptr>() == rhs.Get<Object::Ptr>();
195         }
196
197         return false;
198 }
199
200 bool Value::operator!=(const Value& rhs) const
201 {
202         return !(*this == rhs);
203 }
204
205 Value icinga::operator+(const Value& lhs, const char *rhs)
206 {
207         return lhs + Value(rhs);
208 }
209
210 Value icinga::operator+(const char *lhs, const Value& rhs)
211 {
212         return Value(lhs) + rhs;
213 }
214
215 Value icinga::operator+(const Value& lhs, const String& rhs)
216 {
217         return lhs + Value(rhs);
218 }
219
220 Value icinga::operator+(const String& lhs, const Value& rhs)
221 {
222         return Value(lhs) + rhs;
223 }
224
225 Value icinga::operator+(const Value& lhs, const Value& rhs)
226 {
227         if ((lhs.IsEmpty() || lhs.IsNumber()) && !lhs.IsString() && (rhs.IsEmpty() || rhs.IsNumber()) && !rhs.IsString() && !(lhs.IsEmpty() && rhs.IsEmpty()))
228                 return static_cast<double>(lhs) + static_cast<double>(rhs);
229         if ((lhs.IsString() || lhs.IsEmpty() || lhs.IsNumber()) && (rhs.IsString() || rhs.IsEmpty() || rhs.IsNumber()) && (!(lhs.IsEmpty() && rhs.IsEmpty()) || lhs.IsString() || rhs.IsString()))
230                 return static_cast<String>(lhs) + static_cast<String>(rhs);
231         else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
232                 return static_cast<double>(lhs) + static_cast<double>(rhs);
233         else if (lhs.IsObjectType<DateTime>() && rhs.IsNumber())
234                 return new DateTime(Convert::ToDateTimeValue(lhs) + rhs);
235         else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
236                 Array::Ptr result = new Array();
237                 if (!lhs.IsEmpty())
238                         static_cast<Array::Ptr>(lhs)->CopyTo(result);
239                 if (!rhs.IsEmpty())
240                         static_cast<Array::Ptr>(rhs)->CopyTo(result);
241                 return result;
242         } else if ((lhs.IsObjectType<Dictionary>() || lhs.IsEmpty()) && (rhs.IsObjectType<Dictionary>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
243                 Dictionary::Ptr result = new Dictionary();
244                 if (!lhs.IsEmpty())
245                         static_cast<Dictionary::Ptr>(lhs)->CopyTo(result);
246                 if (!rhs.IsEmpty())
247                         static_cast<Dictionary::Ptr>(rhs)->CopyTo(result);
248                 return result;
249         } else {
250                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator + cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
251         }
252 }
253
254 Value icinga::operator+(const Value& lhs, double rhs)
255 {
256         return lhs + Value(rhs);
257 }
258
259 Value icinga::operator+(double lhs, const Value& rhs)
260 {
261         return Value(lhs) + rhs;
262 }
263
264 Value icinga::operator+(const Value& lhs, int rhs)
265 {
266         return lhs + Value(rhs);
267 }
268
269 Value icinga::operator+(int lhs, const Value& rhs)
270 {
271         return Value(lhs) + rhs;
272 }
273
274 Value icinga::operator-(const Value& lhs, const Value& rhs)
275 {
276         if ((lhs.IsNumber() || lhs.IsEmpty()) && !lhs.IsString() && (rhs.IsNumber() || rhs.IsEmpty()) && !rhs.IsString() && !(lhs.IsEmpty() && rhs.IsEmpty()))
277                 return static_cast<double>(lhs) - static_cast<double>(rhs);
278         else if (lhs.IsObjectType<DateTime>() && rhs.IsNumber())
279                 return new DateTime(Convert::ToDateTimeValue(lhs) - rhs);
280         else if (lhs.IsObjectType<DateTime>() && rhs.IsObjectType<DateTime>())
281                 return Convert::ToDateTimeValue(lhs) - Convert::ToDateTimeValue(rhs);
282         else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
283                 return new DateTime(Convert::ToDateTimeValue(lhs) - Convert::ToDateTimeValue(rhs));
284         else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
285                 if (lhs.IsEmpty())
286                         return new Array();
287
288                 ArrayData result;
289                 Array::Ptr left = lhs;
290                 Array::Ptr right = rhs;
291
292                 ObjectLock olock(left);
293                 for (const Value& lv : left) {
294                         bool found = false;
295                         ObjectLock xlock(right);
296                         for (const Value& rv : right) {
297                                 if (lv == rv) {
298                                         found = true;
299                                         break;
300                                 }
301                         }
302
303                         if (found)
304                                 continue;
305
306                         result.push_back(lv);
307                 }
308
309                 return new Array(std::move(result));
310         } else
311                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator - cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
312 }
313
314 Value icinga::operator-(const Value& lhs, double rhs)
315 {
316         return lhs - Value(rhs);
317 }
318
319 Value icinga::operator-(double lhs, const Value& rhs)
320 {
321         return Value(lhs) - rhs;
322 }
323
324 Value icinga::operator-(const Value& lhs, int rhs)
325 {
326         return lhs - Value(rhs);
327 }
328
329 Value icinga::operator-(int lhs, const Value& rhs)
330 {
331         return Value(lhs) - rhs;
332 }
333
334 Value icinga::operator*(const Value& lhs, const Value& rhs)
335 {
336         if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
337                 return static_cast<double>(lhs) * static_cast<double>(rhs);
338         else
339                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator * cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
340 }
341
342 Value icinga::operator*(const Value& lhs, double rhs)
343 {
344         return lhs * Value(rhs);
345 }
346
347 Value icinga::operator*(double lhs, const Value& rhs)
348 {
349         return Value(lhs) * rhs;
350 }
351
352 Value icinga::operator*(const Value& lhs, int rhs)
353 {
354         return lhs * Value(rhs);
355 }
356
357 Value icinga::operator*(int lhs, const Value& rhs)
358 {
359         return Value(lhs) * rhs;
360 }
361
362 Value icinga::operator/(const Value& lhs, const Value& rhs)
363 {
364         if (rhs.IsEmpty())
365                 BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is Empty."));
366         else if ((lhs.IsEmpty() || lhs.IsNumber()) && rhs.IsNumber()) {
367                 if (static_cast<double>(rhs) == 0)
368                         BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is 0."));
369
370                 return static_cast<double>(lhs) / static_cast<double>(rhs);
371         } else
372                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator / cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
373 }
374
375 Value icinga::operator/(const Value& lhs, double rhs)
376 {
377         return lhs / Value(rhs);
378 }
379
380 Value icinga::operator/(double lhs, const Value& rhs)
381 {
382         return Value(lhs) / rhs;
383 }
384
385 Value icinga::operator/(const Value& lhs, int rhs)
386 {
387         return lhs / Value(rhs);
388 }
389
390 Value icinga::operator/(int lhs, const Value& rhs)
391 {
392         return Value(lhs) / rhs;
393 }
394
395 Value icinga::operator%(const Value& lhs, const Value& rhs)
396 {
397         if (rhs.IsEmpty())
398                 BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator % is Empty."));
399         else if ((rhs.IsNumber() || lhs.IsNumber()) && rhs.IsNumber()) {
400                 if (static_cast<double>(rhs) == 0)
401                         BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator % is 0."));
402
403                 return static_cast<int>(lhs) % static_cast<int>(rhs);
404         } else
405                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator % cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
406 }
407
408 Value icinga::operator%(const Value& lhs, double rhs)
409 {
410         return lhs % Value(rhs);
411 }
412
413 Value icinga::operator%(double lhs, const Value& rhs)
414 {
415         return Value(lhs) % rhs;
416 }
417
418 Value icinga::operator%(const Value& lhs, int rhs)
419 {
420         return lhs % Value(rhs);
421 }
422
423 Value icinga::operator%(int lhs, const Value& rhs)
424 {
425         return Value(lhs) % rhs;
426 }
427
428 Value icinga::operator^(const Value& lhs, const Value& rhs)
429 {
430         if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
431                 return static_cast<int>(lhs) ^ static_cast<int>(rhs);
432         else
433                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator & cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
434 }
435
436 Value icinga::operator^(const Value& lhs, double rhs)
437 {
438         return lhs ^ Value(rhs);
439 }
440
441 Value icinga::operator^(double lhs, const Value& rhs)
442 {
443         return Value(lhs) ^ rhs;
444 }
445
446 Value icinga::operator^(const Value& lhs, int rhs)
447 {
448         return lhs ^ Value(rhs);
449 }
450
451 Value icinga::operator^(int lhs, const Value& rhs)
452 {
453         return Value(lhs) ^ rhs;
454 }
455
456 Value icinga::operator&(const Value& lhs, const Value& rhs)
457 {
458         if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
459                 return static_cast<int>(lhs) & static_cast<int>(rhs);
460         else
461                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator & cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
462 }
463
464 Value icinga::operator&(const Value& lhs, double rhs)
465 {
466         return lhs & Value(rhs);
467 }
468
469 Value icinga::operator&(double lhs, const Value& rhs)
470 {
471         return Value(lhs) & rhs;
472 }
473
474 Value icinga::operator&(const Value& lhs, int rhs)
475 {
476         return lhs & Value(rhs);
477 }
478
479 Value icinga::operator&(int lhs, const Value& rhs)
480 {
481         return Value(lhs) & rhs;
482 }
483
484 Value icinga::operator|(const Value& lhs, const Value& rhs)
485 {
486         if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
487                 return static_cast<int>(lhs) | static_cast<int>(rhs);
488         else
489                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator | cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
490 }
491
492 Value icinga::operator|(const Value& lhs, double rhs)
493 {
494         return lhs | Value(rhs);
495 }
496
497 Value icinga::operator|(double lhs, const Value& rhs)
498 {
499         return Value(lhs) | rhs;
500 }
501
502 Value icinga::operator|(const Value& lhs, int rhs)
503 {
504         return lhs | Value(rhs);
505 }
506
507 Value icinga::operator|(int lhs, const Value& rhs)
508 {
509         return Value(lhs) | rhs;
510 }
511
512 Value icinga::operator<<(const Value& lhs, const Value& rhs)
513 {
514         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
517                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator << cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
518 }
519
520 Value icinga::operator<<(const Value& lhs, double rhs)
521 {
522         return lhs << Value(rhs);
523 }
524
525 Value icinga::operator<<(double lhs, const Value& rhs)
526 {
527         return Value(lhs) << rhs;
528 }
529
530 Value icinga::operator<<(const Value& lhs, int rhs)
531 {
532         return lhs << Value(rhs);
533 }
534
535 Value icinga::operator<<(int lhs, const Value& rhs)
536 {
537         return Value(lhs) << rhs;
538 }
539
540 Value icinga::operator>>(const Value& lhs, const Value& rhs)
541 {
542         if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
543                 return static_cast<int>(lhs) >> static_cast<int>(rhs);
544         else
545                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator >> cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
546 }
547
548 Value icinga::operator>>(const Value& lhs, double rhs)
549 {
550         return lhs >> Value(rhs);
551 }
552
553 Value icinga::operator>>(double lhs, const Value& rhs)
554 {
555         return Value(lhs) >> rhs;
556 }
557
558 Value icinga::operator>>(const Value& lhs, int rhs)
559 {
560         return lhs >> Value(rhs);
561 }
562
563 Value icinga::operator>>(int lhs, const Value& rhs)
564 {
565         return Value(lhs) >> rhs;
566 }
567
568 bool icinga::operator<(const Value& lhs, const Value& rhs)
569 {
570         if (lhs.IsString() && rhs.IsString())
571                 return static_cast<String>(lhs) < static_cast<String>(rhs);
572         else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
573                 return static_cast<double>(lhs) < static_cast<double>(rhs);
574         else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
575                 return Convert::ToDateTimeValue(lhs) < Convert::ToDateTimeValue(rhs);
576         else if (lhs.IsObjectType<Array>() && rhs.IsObjectType<Array>()) {
577                 Array::Ptr larr = lhs;
578                 Array::Ptr rarr = rhs;
579
580                 ObjectLock llock(larr);
581                 ObjectLock rlock(rarr);
582
583                 Array::SizeType llen = larr->GetLength();
584                 Array::SizeType rlen = rarr->GetLength();
585
586                 for (Array::SizeType i = 0; i < std::max(llen, rlen); i++) {
587                         Value lval = (i >= llen) ? Empty : larr->Get(i);
588                         Value rval = (i >= rlen) ? Empty : rarr->Get(i);
589
590                         if (lval < rval)
591                                 return true;
592                         else if (lval > rval)
593                                 return false;
594                 }
595
596                 return false;
597         } else
598                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator < cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
599 }
600
601 bool icinga::operator<(const Value& lhs, double rhs)
602 {
603         return lhs < Value(rhs);
604 }
605
606 bool icinga::operator<(double lhs, const Value& rhs)
607 {
608         return Value(lhs) < rhs;
609 }
610
611 bool icinga::operator<(const Value& lhs, int rhs)
612 {
613         return lhs < Value(rhs);
614 }
615
616 bool icinga::operator<(int lhs, const Value& rhs)
617 {
618         return Value(lhs) < rhs;
619 }
620
621 bool icinga::operator>(const Value& lhs, const Value& rhs)
622 {
623         if (lhs.IsString() && rhs.IsString())
624                 return static_cast<String>(lhs) > static_cast<String>(rhs);
625         else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
626                 return static_cast<double>(lhs) > static_cast<double>(rhs);
627         else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
628                 return Convert::ToDateTimeValue(lhs) > Convert::ToDateTimeValue(rhs);
629         else if (lhs.IsObjectType<Array>() && rhs.IsObjectType<Array>()) {
630                 Array::Ptr larr = lhs;
631                 Array::Ptr rarr = rhs;
632
633                 ObjectLock llock(larr);
634                 ObjectLock rlock(rarr);
635
636                 Array::SizeType llen = larr->GetLength();
637                 Array::SizeType rlen = rarr->GetLength();
638
639                 for (Array::SizeType i = 0; i < std::max(llen, rlen); i++) {
640                         Value lval = (i >= llen) ? Empty : larr->Get(i);
641                         Value rval = (i >= rlen) ? Empty : rarr->Get(i);
642
643                         if (lval > rval)
644                                 return true;
645                         else if (lval < rval)
646                                 return false;
647                 }
648
649                 return false;
650         } else
651                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator > cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
652 }
653
654 bool icinga::operator>(const Value& lhs, double rhs)
655 {
656         return lhs > Value(rhs);
657 }
658
659 bool icinga::operator>(double lhs, const Value& rhs)
660 {
661         return Value(lhs) > rhs;
662 }
663
664 bool icinga::operator>(const Value& lhs, int rhs)
665 {
666         return lhs > Value(rhs);
667 }
668
669 bool icinga::operator>(int lhs, const Value& rhs)
670 {
671         return Value(lhs) > rhs;
672 }
673
674 bool icinga::operator<=(const Value& lhs, const Value& rhs)
675 {
676         if (lhs.IsString() && rhs.IsString())
677                 return static_cast<String>(lhs) <= static_cast<String>(rhs);
678         else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
679                 return static_cast<double>(lhs) <= static_cast<double>(rhs);
680         else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
681                 return Convert::ToDateTimeValue(lhs) <= Convert::ToDateTimeValue(rhs);
682         else
683                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator <= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
684 }
685
686 bool icinga::operator<=(const Value& lhs, double rhs)
687 {
688         return lhs <= Value(rhs);
689 }
690
691 bool icinga::operator<=(double lhs, const Value& rhs)
692 {
693         return Value(lhs) <= rhs;
694 }
695
696 bool icinga::operator<=(const Value& lhs, int rhs)
697 {
698         return lhs <= Value(rhs);
699 }
700
701 bool icinga::operator<=(int lhs, const Value& rhs)
702 {
703         return Value(lhs) <= rhs;
704 }
705
706 bool icinga::operator>=(const Value& lhs, const Value& rhs)
707 {
708         if (lhs.IsString() && rhs.IsString())
709                 return static_cast<String>(lhs) >= static_cast<String>(rhs);
710         else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
711                 return static_cast<double>(lhs) >= static_cast<double>(rhs);
712         else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
713                 return Convert::ToDateTimeValue(lhs) >= Convert::ToDateTimeValue(rhs);
714         else
715                 BOOST_THROW_EXCEPTION(std::invalid_argument("Operator >= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
716 }
717
718 bool icinga::operator>=(const Value& lhs, double rhs)
719 {
720         return lhs >= Value(rhs);
721 }
722
723 bool icinga::operator>=(double lhs, const Value& rhs)
724 {
725         return Value(lhs) >= rhs;
726 }
727
728 bool icinga::operator>=(const Value& lhs, int rhs)
729 {
730         return lhs >= Value(rhs);
731 }
732
733 bool icinga::operator>=(int lhs, const Value& rhs)
734 {
735         return Value(lhs) >= rhs;
736 }