[icinga-checkins] icinga.org: icinga2/master: Implemented Value::GetType().

git at icinga.org git at icinga.org
Fri Feb 15 16:34:45 CET 2013


Module: icinga2
Branch: master
Commit: a022be9de2364b6f97db1e107849e9ee77f199d3
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=a022be9de2364b6f97db1e107849e9ee77f199d3

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Fri Feb 15 14:39:26 2013 +0100

Implemented Value::GetType().

---

 lib/base/value.cpp |   47 ++++++++++++++++++++++++++++++-----------------
 lib/base/value.h   |   15 +++++++++++++++
 2 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/lib/base/value.cpp b/lib/base/value.cpp
index 53578fe..60a8529 100644
--- a/lib/base/value.cpp
+++ b/lib/base/value.cpp
@@ -109,24 +109,27 @@ String Value::Serialize(void) const
  */
 cJSON *Value::ToJson(void) const
 {
-	if (m_Value.type() == typeid(long)) {
-		return cJSON_CreateNumber(boost::get<long>(m_Value));
-	} else if (m_Value.type() == typeid(double)) {
-		return cJSON_CreateNumber(boost::get<double>(m_Value));
-	} else if (m_Value.type() == typeid(String)) {
-		return cJSON_CreateString(boost::get<String>(m_Value).CStr());
-	} else if (m_Value.type() == typeid(Object::Ptr)) {
-		if (IsObjectType<Dictionary>()) {
-			Dictionary::Ptr dictionary = *this;
-			return dictionary->ToJson();
-		} else {
-			Logger::Write(LogDebug, "base", "Ignoring unknown object while converting variant to JSON.");
+	switch (GetType()) {
+		case ValueNumber:
+			return cJSON_CreateNumber(boost::get<double>(m_Value));
+
+		case ValueString:
+			return cJSON_CreateString(boost::get<String>(m_Value).CStr());
+
+		case ValueObject:
+			if (IsObjectType<Dictionary>()) {
+				Dictionary::Ptr dictionary = *this;
+				return dictionary->ToJson();
+			} else {
+				Logger::Write(LogDebug, "base", "Ignoring unknown object while converting variant to JSON.");
+				return cJSON_CreateNull();
+			}
+
+		case ValueEmpty:
 			return cJSON_CreateNull();
-		}
-	} else if (m_Value.type() == typeid(boost::blank)) {
-		return cJSON_CreateNull();
-	} else {
-		BOOST_THROW_EXCEPTION(runtime_error("Invalid variant type."));
+
+		default:
+			BOOST_THROW_EXCEPTION(runtime_error("Invalid variant type."));
 	}
 }
 
@@ -148,3 +151,13 @@ Value Value::Deserialize(const String& jsonString)
 
 	return value;
 }
+
+/**
+ * Returns the type of the value.
+ *
+ * @returns The type.
+ */
+ValueType Value::GetType(void) const
+{
+	return static_cast<ValueType>(m_Value.which());
+}
diff --git a/lib/base/value.h b/lib/base/value.h
index b6623d8..45ece75 100644
--- a/lib/base/value.h
+++ b/lib/base/value.h
@@ -26,6 +26,19 @@ namespace icinga
 {
 
 /**
+ * The type of a Value.
+ *
+ * @ingroup base
+ */
+enum ValueType
+{
+	ValueEmpty = 0,
+	ValueNumber = 1,
+	ValueString = 2,
+	ValueObject = 3
+};
+
+/**
  * A type that can hold an arbitrary value.
  *
  * @ingroup base
@@ -127,6 +140,8 @@ public:
 	String Serialize(void) const;
 	static Value Deserialize(const String& jsonString);
 
+	ValueType GetType(void) const;
+
 private:
 	mutable boost::variant<boost::blank, double, String, Object::Ptr> m_Value;
 };





More information about the icinga-checkins mailing list