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

zrouter-src at zrouter.org zrouter-src at zrouter.org
Mon Jan 23 17:24:01 UTC 2012


details:   /rev/ad6a1fb5049f
changeset: 50:ad6a1fb5049f
user:      "Nicolai Petri <nicolai at petri.dk>"
date:      Mon Jan 23 18:23:27 2012 +0100
description:
Add initial support for enum_leaf.
enum_leaf is probably wrong name since it's basically a key=>value datatype.
Add initial version of array.containsValue(int)
Move entire implementation into model.cc

diffstat:

 zconf++/model.cc |  240 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 zconf++/model.h  |  214 ++++++++----------------------------------------
 2 files changed, 254 insertions(+), 200 deletions(-)

diffs (534 lines):

diff -r e94d6f37ea4a -r ad6a1fb5049f zconf++/model.cc
--- a/zconf++/model.cc	Mon Jan 23 18:21:58 2012 +0100
+++ b/zconf++/model.cc	Mon Jan 23 18:23:27 2012 +0100
@@ -24,6 +24,7 @@
  */
 
 #include <boost/lexical_cast.hpp>
+#include <boost/foreach.hpp>
 #include "model.h"
 
 using namespace std;
@@ -33,8 +34,218 @@
 {
 
 	Model::Model() : Leaf() {
+	}
 
-	}
+	void Leaf::check_type_const(const LeafType t) const
+	{
+		if (
+				m_type == empty_leaf
+				|| m_type == t
+				|| (m_type == enum_leaf && t == numeric_leaf)
+				|| (m_type == enum_leaf && t == string_leaf)
+			 ) {
+			// Unused leaf, let's just change type
+			//m_type = t;
+		} else if (m_type != t)
+			throw std::string("Type mismatch2");
+	};
+	void Leaf::check_type(LeafType t) {
+		if (m_type == empty_leaf) {
+			// Unused leaf, let's just change type
+			m_type = t;
+		} 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");
+	};
+			LeafArray& Leaf::array() {
+				check_type(array_leaf);
+				return m_array;
+			}
+			LeafMap& Leaf::map() {
+				check_type(object_leaf);
+				return m_map;
+			}
+			Leaf Leaf::call(LeafRef p_request) {
+				check_type_const(action_leaf);
+				return m_action_handler->handler("dummy", *this, p_request);
+			}
+			LeafRef Leaf::describe() {
+				check_type_const(action_leaf);
+				return m_action_handler->describe();
+			}
+			const Leaf& Leaf::operator=(const Leaf& source) {
+				m_type = source.m_type;
+				if (m_type == empty_leaf) {
+				}
+				else if (m_type == string_leaf) {
+					m_value_string = source.m_value_string;
+				}
+				else if (m_type == numeric_leaf || m_type == boolean_leaf) {
+					m_value_int = source.m_value_int;
+				}
+				else if (m_type == real_leaf) {
+					m_value_real = source.m_value_real;
+				}
+				else if (m_type == object_leaf) {
+					m_map = source.m_map;
+				}
+				else if (m_type == array_leaf) {
+					m_array = source.m_array;
+				}
+				else if (m_type == action_leaf) {
+					m_action_handler = source.m_action_handler;
+				}
+				else if (m_type == enum_leaf) {
+					m_value_int = source.m_value_int;
+					m_value_string = source.m_value_string;
+				}
+				return *this;
+			};
+
+			Leaf::Leaf() : m_type(empty_leaf), m_parent(NULL) { };
+			Leaf::Leaf(const LeafType p_type)
+				: m_type(p_type),
+					m_parent(NULL)
+					{ };
+			Leaf::Leaf(const Leaf& source)
+				: m_type(source.m_type)
+			, m_parent(NULL) {
+				if (m_type == empty_leaf) {
+					return;
+				}
+				else if (m_type == string_leaf) {
+					m_value_string = source.m_value_string;
+				}
+				else if (m_type == numeric_leaf || m_type == boolean_leaf) {
+					m_value_int = source.m_value_int;
+				}
+				else if (m_type == real_leaf) {
+					m_value_real = source.m_value_real;
+				}
+				else if (m_type == object_leaf) {
+					m_map = source.m_map;
+				}
+				else if (m_type == array_leaf) {
+					m_array = source.m_array;
+				}
+				else if (m_type == action_leaf) {
+					m_action_handler = source.m_action_handler;
+				}
+				else if (m_type == enum_leaf) {
+					m_value_int = source.m_value_int;
+					m_value_string = source.m_value_string;
+				}
+			};
+			Leaf::Leaf(const std::string& p_str ) : m_value_string(p_str), m_type(string_leaf) {
+			};
+			Leaf::Leaf(const int p_key, const std::string& p_str ) : m_value_string(p_str), m_value_int(p_key), m_type(enum_leaf) {
+			};
+			
+			Leaf::Leaf( IModelActionHandler* p_action_hndl) : m_action_handler(p_action_hndl), m_type(action_leaf) { ; }
+			Leaf::Leaf(const int p_int ) : m_value_int(p_int), m_type(numeric_leaf) { };
+			Leaf::Leaf(const bool p_bool ) : m_value_int(p_bool), m_type(boolean_leaf) { };
+
+			Leaf::Leaf(const char* p_str) : m_value_string(p_str), m_type(string_leaf) { };
+			Leaf::Leaf(const double& p_val) :m_value_real(p_val), m_type(real_leaf) {
+			}
+
+			const bool Leaf::operator==(const std::string source) {
+				check_type(string_leaf);
+				return source == m_value_string;
+			}
+			const bool Leaf::operator==(const char *p_cstr) {
+				check_type(string_leaf);
+				return m_value_string == std::string(p_cstr);
+			}
+	
+			Leaf& Leaf::operator[](const std::string& p_key) {
+				check_type(object_leaf);
+				if (m_map.find(p_key) == m_map.end()) {
+					m_map[p_key] = Leaf();
+					m_map[p_key].m_parent = this;
+				}
+				return m_map[p_key];
+			}
+			const LeafType Leaf::type() const
+			{
+				return m_type;
+			}
+			bool Leaf::contains(const std::string p_key) {
+				check_type_const(object_leaf);
+				return (m_map.find(p_key) != m_map.end());
+			}
+			bool Leaf::contains(const int p_idx) {
+				check_type_const(array_leaf);
+				return (p_idx < m_array.size());
+			}
+			bool Leaf::containsValue(const int p_value) {
+				check_type_const(array_leaf); // FIXME: We should support objects too
+				BOOST_FOREACH( LeafRef L, m_array ) {
+					if (L.m_type == numeric_leaf && L.get_int() == p_value) 
+						return true;
+				}
+				return false;
+			}
+			LeafRef Leaf::parent() const
+			{
+				return *m_parent;
+			}
+			Leaf& Leaf::operator[](const int p_idx) {
+				check_type(array_leaf);
+				if (p_idx >= m_array.size()) {
+					m_array.resize(p_idx+1, Leaf());
+					m_array[p_idx].m_parent = this;
+				}
+				return m_array[p_idx];
+			}
+			const std::string Leaf::get_string() const
+			{
+				check_type_const(string_leaf);
+				return m_value_string;
+			}
+			const std::string Leaf::get_string() {
+				check_type(string_leaf);
+				return m_value_string;
+			}
+			void Leaf::push_back(const Leaf& p_impl) {
+				check_type(array_leaf);
+				m_array.push_back(p_impl);
+			};
+			const int Leaf::numChildren() const
+			{
+				if (m_type == object_leaf)
+					return m_map.size();
+				else if (m_type == array_leaf)
+					return m_array.size();
+				else
+					return 0;
+			}
+			Leaf::operator const std::string () {
+				check_type_const(string_leaf);
+				return m_value_string;
+			}
+			const bool Leaf::get_bool() {
+				check_type_const(boolean_leaf);
+				return m_value_int != 0;
+			}
+			const int Leaf::get_int() {
+				check_type_const(numeric_leaf);
+				return m_value_int;
+			}
+			const double Leaf::get_real() {
+				check_type_const(real_leaf);
+				return m_value_real;
+			}
+			Leaf::operator const char* () {
+				check_type_const(string_leaf);
+				return m_value_string.c_str();
+			}
+			const bool Leaf::is_null() const
+			{
+				return m_type == empty_leaf;
+			}
+		
 
 	Leaf Model::populate_system() {
 		Leaf s;
@@ -84,7 +295,6 @@
 			rv += format_json_string(l);
 		}
 		else if (l.type() == object_leaf) {
-			//cout << "  dumping " << " => object" << endl;
 			rv.reserve(4096);	 // Reserve just some space to avoid lots of reallocs
 			rv += "{\n";
 			LeafMap &m = l.map();
@@ -104,14 +314,8 @@
 				it++;
 			}
 			rv += indent + "}";
-			/*std::list<std::string> keys = l.keys();
-			for (std::list<std::string>::const_iterator it = keys.begin(); it != keys.end(); it++) {
-				rv += indent + (*it) + " => " + write(l[*it], indent + "@@@@") + " \n";
-				//cout << indent << "  dumping " << (*it) << " => " << write(l[(*it)], indent + "@@@@") << endl;
-			}*/
 		}
 		else if (l.type() == array_leaf) {
-			//cout << "  dumping " << " => array" << endl;
 			rv.reserve(4096);	 // Reserve just some space to avoid lots of reallocs
 			rv += "[\n";
 			LeafArray &a = l.array();
@@ -136,9 +340,11 @@
 		else if (l.type() == boolean_leaf) {
 			rv += l.get_bool() ? "true" : "false";
 		}
+		else if (l.type() == enum_leaf) {
+			rv += format_json_string(format_json_int(l.get_int()) + "::" + l.get_string());
+		}
 		else {
 			throw std::string("FIXME");
-			//cout << "Foo biz baz" << endl;
 		}
 		//	}
 		return rv;
@@ -148,12 +354,9 @@
 		//	for (int c=0; c < l.numChildren(); c++) {
 		buf << indent;
 		if (l.type() == string_leaf) {
-			//		cout << "  dumping " << " => " << l.get_string() << endl;
-			//cout << indent << l.get_string() << endl;
 			buf << format_json_string(l);
 		}
 		else if (l.type() == object_leaf) {
-			//cout << "  dumping " << " => object" << endl;
 			buf << "{\n";
 			LeafMap &m = l.map();
 			LeafMap::iterator it = m.begin();
@@ -172,11 +375,6 @@
 				it++;
 			}
 			buf << indent << "}";
-			/*std::list<std::string> keys = l.keys();
-			for (std::list<std::string>::const_iterator it = keys.begin(); it != keys.end(); it++) {
-				rv += indent + (*it) + " => " + write(l[*it], indent + "@@@@") + " \n";
-				//cout << indent << "  dumping " << (*it) << " => " << write(l[(*it)], indent + "@@@@") << endl;
-			}*/
 		}
 		else if (l.type() == array_leaf) {
 			//cout << "  dumping " << " => array" << endl;
@@ -202,15 +400,7 @@
 		}
 		else {
 			throw std::string("FIXME");
-			//cout << "Foo biz baz" << endl;
 		}
-		//	}
 	}
 }
 
-
-/*
-void dump_json(mObject &root) {
-	std::cout << write_formatted(root) << std::endl;
-}
-*/
diff -r e94d6f37ea4a -r ad6a1fb5049f zconf++/model.h
--- a/zconf++/model.h	Mon Jan 23 18:21:58 2012 +0100
+++ b/zconf++/model.h	Mon Jan 23 18:23:27 2012 +0100
@@ -65,183 +65,47 @@
 			IModelActionHandler *m_action_handler;
 			int m_value_int;
 			double m_value_real;
-			void check_type_const(const LeafType t) const
-			{
-				if (m_type == empty_leaf || m_type == t) {
-					// Unused leaf, let's just change type
-					//m_type = t;
-				} else if (m_type != t)
-				throw std::string("Type mismatch2");
-			};
-			void check_type(LeafType t) {
-				if (m_type == empty_leaf) {
-					// Unused leaf, let's just change type
-					m_type = t;
-				} else if (m_type != t)
-				throw std::string("Type mismatch");
-			};
+
+			void check_type_const(const LeafType t) const;
+			void check_type(LeafType t);
 		public:
-			LeafArray& array() {
-				check_type(array_leaf);
-				return m_array;
-			}
-			LeafMap& map() {
-				check_type(object_leaf);
-				return m_map;
-			}
-			Leaf call(LeafRef p_request) {
-				check_type_const(action_leaf);
-				return m_action_handler->handler("dummy", *this, p_request);
-			}
-			LeafRef describe() {
-				check_type_const(action_leaf);
-				return m_action_handler->describe();
-			}
-			const Leaf& operator=(const Leaf& source) {
-				m_type = source.m_type;
-				if (m_type == empty_leaf) {
-				}
-				else if (m_type == string_leaf) {
-					m_value_string = source.m_value_string;
-				}
-				else if (m_type == numeric_leaf || m_type == boolean_leaf) {
-					m_value_int = source.m_value_int;
-				}
-				else if (m_type == real_leaf) {
-					m_value_real = source.m_value_real;
-				}
-				else if (m_type == object_leaf) {
-					m_map = source.m_map;
-				}
-				else if (m_type == array_leaf) {
-					m_array = source.m_array;
-				}
-				else if (m_type == action_leaf) {
-					m_action_handler = source.m_action_handler;
-				}
-				return *this;
-			};
-			Leaf() : m_type(empty_leaf), m_parent(NULL) { };
-			Leaf(const Leaf& source)
-				: m_type(source.m_type)
-			, m_parent(NULL) {
-				if (m_type == empty_leaf) {
-					return;
-				}
-				else if (m_type == string_leaf) {
-					m_value_string = source.m_value_string;
-				}
-				else if (m_type == numeric_leaf || m_type == boolean_leaf) {
-					m_value_int = source.m_value_int;
-				}
-				else if (m_type == real_leaf) {
-					m_value_real = source.m_value_real;
-				}
-				else if (m_type == object_leaf) {
-					m_map = source.m_map;
-				}
-				else if (m_type == array_leaf) {
-					m_array = source.m_array;
-				}
-				else if (m_type == action_leaf) {
-					m_action_handler = source.m_action_handler;
-				}
-			};
-			Leaf(const std::string& p_str ) : m_value_string(p_str), m_type(string_leaf) {
-			};
-			explicit Leaf( IModelActionHandler* p_action_hndl) : m_action_handler(p_action_hndl), m_type(action_leaf) { ; }
-			Leaf(const int p_int ) : m_value_int(p_int), m_type(numeric_leaf) { };
-			explicit Leaf(const bool p_bool ) : m_value_int(p_bool), m_type(boolean_leaf) { };
+			LeafArray& array();
+			LeafMap& map();
+			Leaf call(LeafRef p_request);
+			LeafRef describe();
+			const Leaf& operator=(const Leaf& source);
+			Leaf();
+			Leaf(const LeafType p_type);
+			Leaf(const Leaf& source);
+			Leaf(const std::string& p_str );
+			explicit Leaf(const int p_key, const std::string& p_str );
+			
+			explicit Leaf( IModelActionHandler* p_action_hndl);
+			Leaf(const int p_int );
+			explicit Leaf(const bool p_bool );
 
-			Leaf(const char* p_str) : m_value_string(p_str), m_type(string_leaf) { };
-			explicit Leaf(const double& p_val) :m_value_real(p_val), m_type(real_leaf) {
-			}
-			Leaf& operator[](const std::string& p_key) {
-				check_type(object_leaf);
-				if (m_map.find(p_key) == m_map.end()) {
-					m_map[p_key] = Leaf();
-					m_map[p_key].m_parent = this;
-				}
-				return m_map[p_key];
-			}
-			const LeafType type() const
-			{
-				return m_type;
-			}
-			bool contains(const std::string p_key) {
-				check_type_const(object_leaf);
-				return (m_map.find(p_key) != m_map.end());
-			}
-			bool contains(const int p_idx) {
-				check_type_const(array_leaf);
-				return (p_idx < m_array.size());
-			}
-			LeafRef parent() const
-			{
-				return *m_parent;
-			}
-			Leaf& operator[](const int p_idx) {
-				check_type(array_leaf);
-				if (p_idx >= m_array.size()) {
-					m_array.resize(p_idx+1, Leaf());
-					m_array[p_idx].m_parent = this;
-				}
-				return m_array[p_idx];
-			}
-			const std::string get_string() const
-			{
-				check_type_const(string_leaf);
-				return m_value_string;
-			}
-			const std::string get_string() {
-				check_type(string_leaf);
-				return m_value_string;
-			}
-			void push_back(const Leaf& p_impl) {
-				check_type(array_leaf);
-				m_array.push_back(p_impl);
-			};
-			const int numChildren() const
-			{
-				if (m_type == object_leaf)
-					return m_map.size();
-				else if (m_type == array_leaf)
-					return m_array.size();
-				else
-					return 0;
-			}
-			operator const std::string () {
-				check_type_const(string_leaf);
-				return m_value_string;
-			}
-			const bool get_bool() {
-				check_type_const(boolean_leaf);
-				return m_value_int != 0;
-			}
-			const int get_int() {
-				check_type_const(numeric_leaf);
-				return m_value_int;
-			}
-			const double get_real() {
-				check_type_const(real_leaf);
-				return m_value_real;
-			}
-			operator const char* () {
-				check_type_const(string_leaf);
-				return m_value_string.c_str();
-			}
-			const bool is_null() const
-			{
-				return m_type == empty_leaf;
-			}
-			const bool operator==(const std::string source) {
-				check_type(string_leaf);
-				return source == m_value_string;
-			}
-			const bool operator==(const char *p_cstr) {
-				check_type(string_leaf);
-				return m_value_string == std::string(p_cstr);
-			}
+			Leaf(const char* p_str);
+			explicit Leaf(const double& p_val);
+			
+			Leaf& operator[](const std::string& p_key);
+			const LeafType type() const;
+			bool contains(const std::string p_key);
+			bool contains(const int p_idx);
+			bool containsValue(const int p_value);
+			LeafRef parent() const;
+			Leaf& operator[](const int p_idx);
+			const std::string get_string() const;
+			const std::string get_string();
+			void push_back(const Leaf& p_impl);
+			const int numChildren() const;
+			operator const std::string ();
+			const bool get_bool();
+			const int get_int();
+			const double get_real();
+			operator const char* ();
+			const bool is_null() const;
+			const bool operator==(const std::string source);
+			const bool operator==(const char *p_cstr);
 	};
 	class Model : public Leaf
 	{


More information about the Zrouter-src mailing list