[Zrouter-src] ZRouter.org: push to zconf++ zconf++/model.cc zconf++/model.h zc...

zrouter-src at zrouter.org zrouter-src at zrouter.org
Thu Feb 23 10:21:54 UTC 2012


details:   /rev/8c59f501af34
changeset: 69:8c59f501af34
user:      "Nicolai Petri <nicolai at petri.dk>"
date:      Thu Feb 23 10:43:36 2012 +0100
description:
Add zexception.h that contains a custom Z-Router exception handler.
Make ModelWriter take an additional option argument and add support for enum and boolean which was removed in previous commit.

diffstat:

 zconf++/model.cc     |  30 +++++++++++++++++++-----------
 zconf++/model.h      |  10 ++++++++--
 zconf++/zexception.h |  12 ++++++++++++
 3 files changed, 39 insertions(+), 13 deletions(-)

diffs (127 lines):

diff -r bb0eeb06468e -r 8c59f501af34 zconf++/model.cc
--- a/zconf++/model.cc	Thu Feb 23 10:41:29 2012 +0100
+++ b/zconf++/model.cc	Thu Feb 23 10:43:36 2012 +0100
@@ -26,6 +26,7 @@
 #include <boost/lexical_cast.hpp>
 #include <boost/foreach.hpp>
 #include "model.h"
+#include "zexception.h"
 
 namespace ZRouter
 {
@@ -44,7 +45,7 @@
 			// Unused leaf, let's just change type
 			//m_type = t;
 		} else if (m_type != t)
-			throw std::string("Type mismatch2");
+			throw Z_Runtime_Error(__FILE__, __func__, "TYPE_MISMATCH");
 	};
 	void Leaf::check_type(LeafType t) {
 		if (m_type == EMPTY_LEAF) {
@@ -53,7 +54,7 @@
 		} else if ((m_type == ENUM_LEAF && t == NUMERIC_LEAF) || (m_type == ENUM_LEAF && t == STRING_LEAF)) {
 			// Enums can be handles as int and strings.
 		} else if (m_type != t)
-			throw std::string("Type mismatch");
+			throw Z_Runtime_Error(__FILE__, __func__, "TYPE_MISMATCH");
 	};
 			LeafArray& Leaf::array() {
 				check_type(ARRAY_LEAF);
@@ -291,16 +292,15 @@
 		return boost::lexical_cast<std::string>(p_real);
 	}
 
-	std::string ModelWriter::write(Leaf& l, const std::string &indent) {
+	std::string ModelWriter::write(Leaf& l, const std::string &indent, u_char p_options) {
 		std::stringstream dest;
 		dest.str().reserve(8192);
-		write(dest, l, indent);
+		write(dest, l, indent, p_options);
 		return dest.str();
 	}
 
-	void ModelWriter::write(std::ostream &buf, Leaf& l, const std::string &indent) {
-		//	for (int c=0; c < l.numChildren(); c++) {
-		buf << indent;
+	void ModelWriter::write(std::ostream &buf, Leaf& l, const std::string &indent, u_char p_options) {
+		//buf << indent;
 		if (l.type() == STRING_LEAF) {
 			buf << format_json_string(l);
 		}
@@ -318,19 +318,18 @@
 					else {
 						first = false;
 					}
-					buf << format_json_string(it->first) << ": " << write(it->second, indent+"    ") << "\n";
+					buf << format_json_string(it->first) << ": " << write(it->second, indent+"    ", p_options) << "\n";
 				}
 				it++;
 			}
 			buf << indent << "}";
 		}
 		else if (l.type() == ARRAY_LEAF) {
-			//cout << "  dumping " << " => array" << endl;
 			buf << "[\n";
 			LeafArray &a = l.array();
 			for (int c=0; c < a.size(); c++) {
 				if (a[c].type() != ACTION_LEAF)
-					buf << indent /*+ Int2String(c) + " => "*/ << (c>0 ? "," : "")  << write(a[c], indent+"    ") << "\n";
+					buf << indent /*+ Int2String(c) + " => "*/ << (c>0 ? "," : "")  << write(a[c], indent+"    ", p_options) << "\n";
 			}
 			buf << indent << "]";
 		}
@@ -346,8 +345,17 @@
 		else if (l.type() == EMPTY_LEAF) {
 			buf << "null";
 		}
+		else if (l.type() == BOOLEAN_LEAF) {
+			buf << l.get_bool() ? "true" : "false";
+    }
+		else if (l.type() == ENUM_LEAF) {
+			if (p_options & optReadableEnums) 
+				buf << format_json_string(format_json_int(l.get_int()) + "::" + l.get_string());
+			else
+				buf << format_json_int(l.get_int());
+		}
 		else {
-			throw std::string("FIXME");
+			throw Z_Runtime_Error(__FILE__, __func__, "UNKNOWN_LEAF_TYPE");
 		}
 	}
 }
diff -r bb0eeb06468e -r 8c59f501af34 zconf++/model.h
--- a/zconf++/model.h	Thu Feb 23 10:41:29 2012 +0100
+++ b/zconf++/model.h	Thu Feb 23 10:43:36 2012 +0100
@@ -136,8 +136,14 @@
 	class ModelWriter
 	{
 		public:
-			static std::string write(Leaf& p_start_leaf, const std::string& p_indent);
-			static void write(std::ostream &p_dest, Leaf& p_start_leaf, const std::string& p_indent);
+			enum Options {
+				optNone          = 0x00,
+				optPretty        = 0x01,
+				optWithActions   = 0x02,
+				optReadableEnums = 0x04
+			};
+			static std::string write(Leaf& p_start_leaf, const std::string& p_indent, u_char p_options = 0x0);
+			static void write(std::ostream &p_dest, Leaf& p_start_leaf, const std::string& p_indent, u_char p_options = 0x0);
 	};
 	template <class TClass>
 		class TModelActionHandler : public IModelActionHandler
diff -r bb0eeb06468e -r 8c59f501af34 zconf++/zexception.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zconf++/zexception.h	Thu Feb 23 10:43:36 2012 +0100
@@ -0,0 +1,12 @@
+#include <stdexcept>
+#include <string>
+
+namespace ZRouter {
+ 
+	class Z_Runtime_Error : public std::runtime_error {
+		public:
+			Z_Runtime_Error(const std::string& area, const std::string& func, const std::string& msg) : std::runtime_error(area + "::" + func + "::" + msg) {};
+};
+
+}
+


More information about the Zrouter-src mailing list