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

zrouter-src at zrouter.org zrouter-src at zrouter.org
Wed Feb 22 11:15:49 UTC 2012


details:   /rev/dcdd441b8a07
changeset: 60:dcdd441b8a07
user:      "Nicolai Petri <nicolai at petri.dk>"
date:      Wed Feb 22 12:15:30 2012 +0100
description:
Minor style changes (p_ for parameters, uppercase ENUM names)

diffstat:

 zconf++/http_json_handler.cc |    6 +-
 zconf++/http_json_handler.h  |    5 +-
 zconf++/model.cc             |  142 +++++++++++++++++++++---------------------
 zconf++/model.h              |   48 +++++++++----
 4 files changed, 108 insertions(+), 93 deletions(-)

diffs (525 lines):

diff -r 728193b62c30 -r dcdd441b8a07 zconf++/http_json_handler.cc
--- a/zconf++/http_json_handler.cc	Fri Feb 03 11:04:19 2012 +0100
+++ b/zconf++/http_json_handler.cc	Wed Feb 22 12:15:30 2012 +0100
@@ -39,7 +39,7 @@
 	using namespace http::server;
 
 	http_json_handler::http_json_handler(Model::Model &p_model)
-	:model(p_model) {
+	:m_model(p_model) {
 
 	}
 
@@ -64,7 +64,7 @@
 		boost::split(uri_parts,  url, boost::is_any_of("/"));
 		std::vector<std::string>::iterator it = uri_parts.begin();
 		it+=2;					 // Skip first junk (/model)
-		Leaf *leaf = &model;
+		Leaf *leaf = &m_model;
 		for (; it != uri_parts.end(); it++) {
 			if (*it == "")
 				continue;
@@ -82,7 +82,7 @@
 		std::stringstream ss;
 		ss.str().reserve(16384); // Reserve 16K to avoid realloc hell on small request.
 		// TEST: Do above actually make a change ?
-		if (leaf->type() == action_leaf) {
+		if (leaf->type() == ACTION_LEAF) {
 			Json::Reader r;
 			Leaf params;
 			std::cout << "GOT BODY : " << req.body << std::endl;
diff -r 728193b62c30 -r dcdd441b8a07 zconf++/http_json_handler.h
--- a/zconf++/http_json_handler.h	Fri Feb 03 11:04:19 2012 +0100
+++ b/zconf++/http_json_handler.h	Wed Feb 22 12:15:30 2012 +0100
@@ -40,10 +40,9 @@
 			virtual const std::string ns_prefix() ;
 			virtual const http::handlers::e_request_handler_type type() ;
 			virtual const bool can_handle(const std::string &url, const http::server::request& req) ;
-			virtual void handle(const std::string &url, const http::server::request& req, http::server::reply& rep) ;
-			//	virtual void
+			virtual void handle(const std::string &url, const http::server::request& p_req, http::server::reply& p_rep) ;
 		protected:
-			Model::Model& model;
+			Model::Model& m_model;
 	};
 
 }
diff -r 728193b62c30 -r dcdd441b8a07 zconf++/model.cc
--- a/zconf++/model.cc	Fri Feb 03 11:04:19 2012 +0100
+++ b/zconf++/model.cc	Wed Feb 22 12:15:30 2012 +0100
@@ -36,10 +36,10 @@
 	void Leaf::check_type_const(const LeafType t) const
 	{
 		if (
-				m_type == empty_leaf
+				m_type == EMPTY_LEAF
 				|| m_type == t
-				|| (m_type == enum_leaf && t == numeric_leaf)
-				|| (m_type == enum_leaf && t == string_leaf)
+				|| (m_type == ENUM_LEAF && t == NUMERIC_LEAF)
+				|| (m_type == ENUM_LEAF && t == STRING_LEAF)
 			 ) {
 			// Unused leaf, let's just change type
 			//m_type = t;
@@ -47,60 +47,60 @@
 			throw std::string("Type mismatch2");
 	};
 	void Leaf::check_type(LeafType t) {
-		if (m_type == empty_leaf) {
+		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)) {
+		} 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);
+				check_type(ARRAY_LEAF);
 				return m_array;
 			}
 			LeafMap& Leaf::map() {
-				check_type(object_leaf);
+				check_type(OBJECT_LEAF);
 				return m_map;
 			}
 			Leaf Leaf::call(LeafRef p_request) {
-				check_type_const(action_leaf);
+				check_type_const(ACTION_LEAF);
 				return m_action_handler->handler("dummy", *this, p_request);
 			}
 			LeafRef Leaf::describe() {
-				check_type_const(action_leaf);
+				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) {
+				if (m_type == EMPTY_LEAF) {
 				}
-				else if (m_type == string_leaf) {
+				else if (m_type == STRING_LEAF) {
 					m_value_string = source.m_value_string;
 				}
-				else if (m_type == numeric_leaf || m_type == boolean_leaf) {
+				else if (m_type == NUMERIC_LEAF || m_type == BOOLEAN_LEAF) {
 					m_value_int = source.m_value_int;
 				}
-				else if (m_type == real_leaf) {
+				else if (m_type == REAL_LEAF) {
 					m_value_real = source.m_value_real;
 				}
-				else if (m_type == object_leaf) {
+				else if (m_type == OBJECT_LEAF) {
 					m_map = source.m_map;
 				}
-				else if (m_type == array_leaf) {
+				else if (m_type == ARRAY_LEAF) {
 					m_array = source.m_array;
 				}
-				else if (m_type == action_leaf) {
+				else if (m_type == ACTION_LEAF) {
 					m_action_handler = source.m_action_handler;
 				}
-				else if (m_type == enum_leaf) {
+				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() : m_type(EMPTY_LEAF), m_parent(NULL) { };
 			Leaf::Leaf(const LeafType p_type)
 				: m_type(p_type),
 					m_parent(NULL)
@@ -108,56 +108,56 @@
 			Leaf::Leaf(const Leaf& source)
 				: m_type(source.m_type)
 			, m_parent(NULL) {
-				if (m_type == empty_leaf) {
+				if (m_type == EMPTY_LEAF) {
 					return;
 				}
-				else if (m_type == string_leaf) {
+				else if (m_type == STRING_LEAF) {
 					m_value_string = source.m_value_string;
 				}
-				else if (m_type == numeric_leaf || m_type == boolean_leaf) {
+				else if (m_type == NUMERIC_LEAF || m_type == BOOLEAN_LEAF) {
 					m_value_int = source.m_value_int;
 				}
-				else if (m_type == real_leaf) {
+				else if (m_type == REAL_LEAF) {
 					m_value_real = source.m_value_real;
 				}
-				else if (m_type == object_leaf) {
+				else if (m_type == OBJECT_LEAF) {
 					m_map = source.m_map;
 				}
-				else if (m_type == array_leaf) {
+				else if (m_type == ARRAY_LEAF) {
 					m_array = source.m_array;
 				}
-				else if (m_type == action_leaf) {
+				else if (m_type == ACTION_LEAF) {
 					m_action_handler = source.m_action_handler;
 				}
-				else if (m_type == enum_leaf) {
+				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 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(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( 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) {
+			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);
+				check_type(STRING_LEAF);
 				return source == m_value_string;
 			}
 			const bool Leaf::operator==(const char *p_cstr) {
-				check_type(string_leaf);
+				check_type(STRING_LEAF);
 				return m_value_string == std::string(p_cstr);
 			}
 	
 			Leaf& Leaf::operator[](const std::string& p_key) {
-				check_type(object_leaf);
+				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;
@@ -169,17 +169,17 @@
 				return m_type;
 			}
 			bool Leaf::contains(const std::string p_key) {
-				check_type_const(object_leaf);
+				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);
+				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
+				check_type_const(ARRAY_LEAF); // FIXME: We should support objects too
 				BOOST_FOREACH( LeafRef L, m_array ) {
-					if ((L.m_type == numeric_leaf || L.m_type == enum_leaf) && L.get_int() == p_value) 
+					if ((L.m_type == NUMERIC_LEAF || L.m_type == ENUM_LEAF) && L.get_int() == p_value) 
 						return true;
 				}
 				return false;
@@ -189,7 +189,7 @@
 				return *m_parent;
 			}
 			Leaf& Leaf::operator[](const int p_idx) {
-				check_type(array_leaf);
+				check_type(ARRAY_LEAF);
 				if (p_idx >= m_array.size()) {
 					m_array.resize(p_idx+1, Leaf());
 					m_array[p_idx].m_parent = this;
@@ -198,49 +198,49 @@
 			}
 			const std::string Leaf::get_string() const
 			{
-				check_type_const(string_leaf);
+				check_type_const(STRING_LEAF);
 				return m_value_string;
 			}
 			const std::string Leaf::get_string() {
-				check_type(string_leaf);
+				check_type(STRING_LEAF);
 				return m_value_string;
 			}
 			void Leaf::push_back(const Leaf& p_impl) {
-				check_type(array_leaf);
+				check_type(ARRAY_LEAF);
 				m_array.push_back(p_impl);
 			};
 			const int Leaf::numChildren() const
 			{
-				if (m_type == object_leaf)
+				if (m_type == OBJECT_LEAF)
 					return m_map.size();
-				else if (m_type == array_leaf)
+				else if (m_type == ARRAY_LEAF)
 					return m_array.size();
 				else
 					return 0;
 			}
 			Leaf::operator const std::string () {
-				check_type_const(string_leaf);
+				check_type_const(STRING_LEAF);
 				return m_value_string;
 			}
 			const bool Leaf::get_bool() {
-				check_type_const(boolean_leaf);
+				check_type_const(BOOLEAN_LEAF);
 				return m_value_int != 0;
 			}
 			const int Leaf::get_int() {
-				check_type_const(numeric_leaf);
+				check_type_const(NUMERIC_LEAF);
 				return m_value_int;
 			}
 			const double Leaf::get_real() {
-				check_type_const(real_leaf);
+				check_type_const(REAL_LEAF);
 				return m_value_real;
 			}
 			Leaf::operator const char* () {
-				check_type_const(string_leaf);
+				check_type_const(STRING_LEAF);
 				return m_value_string.c_str();
 			}
 			const bool Leaf::is_null() const
 			{
-				return m_type == empty_leaf;
+				return m_type == EMPTY_LEAF;
 			}
 		
 
@@ -286,19 +286,19 @@
 	std::string ModelWriter::write(Leaf& l, const std::string &indent) {
 		std::string rv;
 		//	for (int c=0; c < l.numChildren(); c++) {
-		if (l.type() == string_leaf) {
+		if (l.type() == STRING_LEAF) {
 			//		cout << "  dumping " << " => " << l.get_string() << endl;
 			//cout << indent << l.get_string() << endl;
 			rv += format_json_string(l);
 		}
-		else if (l.type() == object_leaf) {
+		else if (l.type() == OBJECT_LEAF) {
 			rv.reserve(4096);	 // Reserve just some space to avoid lots of reallocs
 			rv += "{\n";
 			LeafMap &m = l.map();
 			LeafMap::iterator it = m.begin();
 			bool first = true;
 			while (it != m.end()) {
-				if (it->second.type() != action_leaf) {
+				if (it->second.type() != ACTION_LEAF) {
 					rv += indent;
 					if (first == false) {
 						rv += ",";
@@ -312,7 +312,7 @@
 			}
 			rv += indent + "}";
 		}
-		else if (l.type() == array_leaf) {
+		else if (l.type() == ARRAY_LEAF) {
 			rv.reserve(4096);	 // Reserve just some space to avoid lots of reallocs
 			rv += "[\n";
 			LeafArray &a = l.array();
@@ -321,23 +321,23 @@
 			}
 			rv += indent + "]";
 		}
-		else if (l.type() == real_leaf) {
+		else if (l.type() == REAL_LEAF) {
 			rv += format_json_real(l.get_real());
 		}
-		else if (l.type() == numeric_leaf) {
+		else if (l.type() == NUMERIC_LEAF) {
 			rv += format_json_int(l.get_int());
 			/* Actions should only be dumped on request */
 		}
-		else if (l.type() == action_leaf) {
+		else if (l.type() == ACTION_LEAF) {
 			rv += "\"@ACTION@\"";
 		}
-		else if (l.type() == empty_leaf) {
+		else if (l.type() == EMPTY_LEAF) {
 			rv += "null";
 		}
-		else if (l.type() == boolean_leaf) {
+		else if (l.type() == BOOLEAN_LEAF) {
 			rv += l.get_bool() ? "true" : "false";
 		}
-		else if (l.type() == enum_leaf) {
+		else if (l.type() == ENUM_LEAF) {
 			rv += format_json_string(format_json_int(l.get_int()) + "::" + l.get_string());
 		}
 		else {
@@ -350,16 +350,16 @@
 	void ModelWriter::write(std::stringstream &buf, Leaf& l, const std::string &indent) {
 		//	for (int c=0; c < l.numChildren(); c++) {
 		buf << indent;
-		if (l.type() == string_leaf) {
+		if (l.type() == STRING_LEAF) {
 			buf << format_json_string(l);
 		}
-		else if (l.type() == object_leaf) {
+		else if (l.type() == OBJECT_LEAF) {
 			buf << "{\n";
 			LeafMap &m = l.map();
 			LeafMap::iterator it = m.begin();
 			bool first = true;
 			while (it != m.end()) {
-				if (it->second.type() != action_leaf) {
+				if (it->second.type() != ACTION_LEAF) {
 					buf << indent;
 					if (first == false) {
 						buf << ",";
@@ -373,26 +373,26 @@
 			}
 			buf << indent << "}";
 		}
-		else if (l.type() == array_leaf) {
+		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)
+				if (a[c].type() != ACTION_LEAF)
 					buf << indent /*+ Int2String(c) + " => "*/ << (c>0 ? "," : "")  << write(a[c], indent+"    ") << "\n";
 			}
 			buf << indent << "]";
 		}
-		else if (l.type() == real_leaf) {
+		else if (l.type() == REAL_LEAF) {
 			buf << format_json_real(l.get_real());
 		}
-		else if (l.type() == numeric_leaf) {
+		else if (l.type() == NUMERIC_LEAF) {
 			buf << format_json_int(l.get_int());
 		}
-		else if (l.type() == action_leaf) {
+		else if (l.type() == ACTION_LEAF) {
 			buf << "\"@ACTION@\"";
 		}
-		else if (l.type() == empty_leaf) {
+		else if (l.type() == EMPTY_LEAF) {
 			buf << "null";
 		}
 		else {
diff -r 728193b62c30 -r dcdd441b8a07 zconf++/model.h
--- a/zconf++/model.h	Fri Feb 03 11:04:19 2012 +0100
+++ b/zconf++/model.h	Wed Feb 22 12:15:30 2012 +0100
@@ -31,9 +31,25 @@
 #  include <vector>
 #  include <iostream>
 
+
+/* Compat hacks 
+#define EMPTY_LEAF LeafType::EMPTY
+#define OBJECT_LEAF LeafType::OBJECT
+#define ARRAY_LEAF LeafType::ARRAY
+#define NUMERIC_LEAF LeafType::NUMERIC
+#define BOOLEAN_LEAF LeafType::BOOLEAN
+#define ENUM_LEAF LeafType::ENUM
+#define ACTION_LEAF LeafType::ACTION
+#define STRING_LEAF LeafType::STRING
+#define REAL_LEAF LeafType::REAL
+*/
 namespace ZRouter
 {
-	enum LeafType { empty_leaf, object_leaf, array_leaf, numeric_leaf, boolean_leaf, string_leaf, real_leaf, action_leaf, event_leaf, enum_leaf, set_leaf };
+#ifdef NOTYET_CXX11
+	enum class LeafType { EMPTY, OBJECT, ARRAY, NUMERIC, BOOLEAN, STRING, REAL, ACTION, EVENT, ENUM, SET};
+#else
+	enum LeafType { EMPTY_LEAF, OBJECT_LEAF, ARRAY_LEAF, NUMERIC_LEAF, BOOLEAN_LEAF, STRING_LEAF, REAL_LEAF, ACTION_LEAF, EVENT_LEAF, ENUM_LEAF, SET_LEAF};
+#endif
 	class ModelWriter;
 	class Leaf;
 	typedef Leaf& LeafRef;
@@ -46,7 +62,7 @@
 			// classes will use a pointer to an object and a pointer to a member function
 			// to make the function call
 								 // call using operator
-			virtual Leaf handler(const std::string &uri, LeafRef pSelf, LeafRef pRequest )=0;
+			virtual Leaf handler(const std::string &p_uri, LeafRef p_self, LeafRef p_request )=0;
 								 // call using function
 			virtual LeafRef describe()=0;
 	};
@@ -65,17 +81,17 @@
 			int m_value_int;
 			double m_value_real;
 
-			void check_type_const(const LeafType t) const;
-			void check_type(LeafType t);
+			void check_type_const(const LeafType p_t) const;
+			void check_type(LeafType p_t);
 		public:
 			LeafArray& array();
 			LeafMap& map();
 			Leaf call(LeafRef p_request);
 			LeafRef describe();
-			const Leaf& operator=(const Leaf& source);
+			const Leaf& operator=(const Leaf& p_source);
 			Leaf();
 			Leaf(const LeafType p_type);
-			Leaf(const Leaf& source);
+			Leaf(const Leaf& p_source);
 			Leaf(const std::string& p_str );
 			explicit Leaf(const int p_key, const std::string& p_str );
 			
@@ -103,7 +119,7 @@
 			const double get_real();
 			operator const char* ();
 			const bool is_null() const;
-			const bool operator==(const std::string source);
+			const bool operator==(const std::string p_source);
 			const bool operator==(const char *p_cstr);
 	};
 	class Model : public Leaf
@@ -118,24 +134,24 @@
 	class ModelWriter
 	{
 		public:
-			static std::string write(Leaf& start_leaf, const std::string& indent);
-			static void write(std::stringstream &dest, Leaf& start_leaf, const std::string& indent);
+			static std::string write(Leaf& p_start_leaf, const std::string& p_indent);
+			static void write(std::stringstream &p_dest, Leaf& p_start_leaf, const std::string& p_indent);
 	};
 	template <class TClass>
 		class TModelActionHandler : public IModelActionHandler
 	{
 		protected:
 			TClass *m_obj;
-			Leaf (TClass::*m_handler)(const std::string& pUri, LeafRef pSelf, LeafRef pRequest);
+			Leaf (TClass::*m_handler)(const std::string& p_uri, LeafRef p_self, LeafRef p_request);
 			LeafRef (TClass::*m_describer)();
 		public:
-			TModelActionHandler(TClass *pObject, Leaf(TClass::*pHndlr)(const std::string& pUri, LeafRef pSelf, LeafRef pReq), LeafRef(TClass::*pDscrp)())
-				: m_obj(pObject)
-				, m_handler(pHndlr)
-				, m_describer(pDscrp)
+			TModelActionHandler(TClass *p_object, Leaf(TClass::*p_hndlr)(const std::string& p_uri, LeafRef p_self, LeafRef p_req), LeafRef(TClass::*p_dscrp)())
+				: m_obj(p_object)
+				, m_handler(p_hndlr)
+				, m_describer(p_dscrp)
 				{ ; }
-			virtual Leaf handler(const std::string &uri, LeafRef pSelf, LeafRef pRequest ) {
-				return (*m_obj.*m_handler)(uri, pSelf, pRequest);
+			virtual Leaf handler(const std::string &p_uri, LeafRef p_self, LeafRef p_request ) {
+				return (*m_obj.*m_handler)(p_uri, p_self, p_request);
 			}
 			virtual LeafRef describe() {
 				return (*m_obj.*m_describer)();


More information about the Zrouter-src mailing list