[icinga-checkins] icinga.org: icinga2/feature/escalation-notification-5011: Implement additional operators for the Value and String classes.

git at icinga.org git at icinga.org
Thu Nov 7 16:41:37 CET 2013


Module: icinga2
Branch: feature/escalation-notification-5011
Commit: 038be974e4b3ff1b7ad31c43550fc4a5c584dbcc
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=038be974e4b3ff1b7ad31c43550fc4a5c584dbcc

Author: Gunnar Beutner <gunnar at beutner.name>
Date:   Thu Nov  7 13:41:24 2013 +0100

Implement additional operators for the Value and String classes.

Refs #2710

---

 lib/base/qstring.cpp |    7 +++++
 lib/base/qstring.h   |    3 ++
 lib/base/value.cpp   |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/base/value.h     |   18 +++++++++++
 4 files changed, 109 insertions(+)

diff --git a/lib/base/qstring.cpp b/lib/base/qstring.cpp
index c362812..c3ca495 100644
--- a/lib/base/qstring.cpp
+++ b/lib/base/qstring.cpp
@@ -18,6 +18,7 @@
  ******************************************************************************/
 
 #include "base/qstring.h"
+#include "base/value.h"
 #include <boost/algorithm/string/trim.hpp>
 #include <boost/algorithm/string/join.hpp>
 #include <boost/algorithm/string/compare.hpp>
@@ -86,6 +87,12 @@ String& String::operator+=(const char *rhs)
 	return *this;
 }
 
+String& String::operator+=(const Value& rhs)
+{
+	m_Data += static_cast<String>(rhs);
+	return *this;
+}
+
 String& String::operator+=(char rhs)
 {
 	m_Data += rhs;
diff --git a/lib/base/qstring.h b/lib/base/qstring.h
index 9ca0e5e..74f0202 100644
--- a/lib/base/qstring.h
+++ b/lib/base/qstring.h
@@ -28,6 +28,8 @@
 
 namespace icinga {
 
+class Value;
+
 /**
  * String class.
  *
@@ -64,6 +66,7 @@ public:
 
 	String& operator+=(const String& rhs);
 	String& operator+=(const char *rhs);
+	String& operator+=(const Value& rhs);
 	String& operator+=(char rhs);
 
 	bool IsEmpty(void) const;
diff --git a/lib/base/value.cpp b/lib/base/value.cpp
index 2366895..0216bb8 100644
--- a/lib/base/value.cpp
+++ b/lib/base/value.cpp
@@ -241,6 +241,77 @@ ValueType Value::GetType(void) const
 	return static_cast<ValueType>(m_Value.which());
 }
 
+bool Value::operator==(int rhs)
+{
+	if (!IsScalar())
+		return false;
+
+	return static_cast<double>(*this) == rhs;
+}
+
+bool Value::operator!=(int rhs)
+{
+	return !(*this == rhs);
+}
+
+bool Value::operator==(double rhs)
+{
+	if (!IsScalar())
+		return false;
+
+	return static_cast<double>(*this) == rhs;
+}
+
+bool Value::operator!=(double rhs)
+{
+	return !(*this == rhs);
+}
+
+bool Value::operator==(const char *rhs)
+{
+	return static_cast<String>(*this) == rhs;
+}
+
+bool Value::operator!=(const char *rhs)
+{
+	return !(*this == rhs);
+}
+
+bool Value::operator==(const String& rhs)
+{
+	return static_cast<String>(*this) == rhs;
+}
+
+bool Value::operator!=(const String& rhs)
+{
+	return !(*this == rhs);
+}
+
+bool Value::operator==(const Value& rhs)
+{
+	if (IsEmpty() != rhs.IsEmpty())
+		return false;
+
+	if (IsEmpty())
+		return true;
+
+	if (IsObject() != rhs.IsObject())
+		return false;
+
+	if (IsObject())
+		return static_cast<Object::Ptr>(*this) == static_cast<Object::Ptr>(rhs);
+
+	if (GetType() == ValueNumber || rhs.GetType() == ValueNumber)
+		return static_cast<double>(*this) == static_cast<double>(rhs);
+	else
+		return static_cast<String>(*this) == static_cast<String>(rhs);
+}
+
+bool Value::operator!=(const Value& rhs)
+{
+	return !(*this == rhs);
+}
+
 Value icinga::operator+(const Value& lhs, const char *rhs)
 {
 	return static_cast<String>(lhs) + rhs;
@@ -251,6 +322,16 @@ Value icinga::operator+(const char *lhs, const Value& rhs)
 	return lhs + static_cast<String>(rhs);
 }
 
+Value icinga::operator+(const Value& lhs, const String& rhs)
+{
+	return static_cast<String>(lhs) + rhs;
+}
+
+Value icinga::operator+(const String& lhs, const Value& rhs)
+{
+	return lhs + static_cast<String>(rhs);
+}
+
 std::ostream& icinga::operator<<(std::ostream& stream, const Value& value)
 {
 	stream << static_cast<String>(value);
diff --git a/lib/base/value.h b/lib/base/value.h
index 1abf979..29bf59b 100644
--- a/lib/base/value.h
+++ b/lib/base/value.h
@@ -73,6 +73,21 @@ public:
 	operator double(void) const;
 	operator String(void) const;
 
+	bool operator==(int rhs);
+	bool operator!=(int rhs);
+
+	bool operator==(double rhs);
+	bool operator!=(double rhs);
+
+	bool operator==(const char *rhs);
+	bool operator!=(const char *rhs);
+
+	bool operator==(const String& rhs);
+	bool operator!=(const String& rhs);
+
+	bool operator==(const Value& rhs);
+	bool operator!=(const Value& rhs);
+
 	template<typename T>
 	operator shared_ptr<T>(void) const
 	{
@@ -117,6 +132,9 @@ static Value Empty;
 I2_BASE_API Value operator+(const Value& lhs, const char *rhs);
 I2_BASE_API Value operator+(const char *lhs, const Value& rhs);
 
+I2_BASE_API Value operator+(const Value& lhs, const String& rhs);
+I2_BASE_API Value operator+(const String& lhs, const Value& rhs);
+
 I2_BASE_API std::ostream& operator<<(std::ostream& stream, const Value& value);
 I2_BASE_API std::istream& operator>>(std::istream& stream, Value& value);
 





More information about the icinga-checkins mailing list