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

zrouter-src at zrouter.org zrouter-src at zrouter.org
Wed Jan 11 06:01:35 UTC 2012


details:   /rev/2ed0901283a0
changeset: 36:2ed0901283a0
user:      "Nicolai Petri <nicolai at petri.dk>"
date:      Wed Jan 11 07:01:01 2012 +0100
description:
Add Leaf::parent() so it's possible to travel "backwards" in the tree.
Change action handlers slightly so a handler get's the action leaf as parameter also.
 - with support for ::parent() it allows one actionhandler to be registered in multiple places in the tree and it will be able to find its current context.

diffstat:

 zconf++/model.cc   |   2 ++
 zconf++/model.h    |  33 ++++++++++++++++++---------------
 zconf++/zconfig.cc |   2 +-
 3 files changed, 21 insertions(+), 16 deletions(-)

diffs (133 lines):

diff -r 4e7e139aedde -r 2ed0901283a0 zconf++/model.cc
--- a/zconf++/model.cc	Wed Jan 11 06:57:43 2012 +0100
+++ b/zconf++/model.cc	Wed Jan 11 07:01:01 2012 +0100
@@ -84,6 +84,7 @@
 			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();
 			LeafMap::iterator it = m.begin();
@@ -108,6 +109,7 @@
 			}*/
 		} 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();
 			for (int c=0; c < a.size(); c++) {
diff -r 4e7e139aedde -r 2ed0901283a0 zconf++/model.h
--- a/zconf++/model.h	Wed Jan 11 06:57:43 2012 +0100
+++ b/zconf++/model.h	Wed Jan 11 07:01:01 2012 +0100
@@ -45,7 +45,7 @@
 			// two possible functions to call member function. virtual cause derived
 			// classes will use a pointer to an object and a pointer to a member function
 			// to make the function call
-			virtual Leaf handler(const std::string &uri, LeafRef pRequest )=0;  // call using operator
+			virtual Leaf handler(const std::string &uri, LeafRef pSelf, LeafRef pRequest )=0;  // call using operator
 			virtual LeafRef describe()=0;        // call using function
 	};
 
@@ -56,6 +56,7 @@
 			LeafType m_type;
 			std::string m_value_string;
 			LeafMap m_map;
+			Leaf* m_parent;
 			LeafArray m_array;
 			IModelActionHandler *m_action_handler;
 			int m_value_int;
@@ -85,7 +86,7 @@
 			}
 			Leaf call(LeafRef p_request) {
 				check_type_const(action_leaf);
-				return m_action_handler->handler("dummy", p_request);	
+				return m_action_handler->handler("dummy", *this, p_request);	
 			}
 			LeafRef describe() {
 				check_type_const(action_leaf);
@@ -129,15 +130,10 @@
 				}
 				return *this;
 			}; 
-			Leaf() : m_type(empty_leaf) { };
+			Leaf() : m_type(empty_leaf), m_parent(NULL) { };
 			Leaf(const Leaf& source)
 				: m_type(source.m_type)
-			/*	, m_value_string(source.m_value_string)
-				, m_value_int(source.m_value_int)
-				, m_value_real(source.m_value_real)
-				, m_map(source.m_map)
-				, m_array(source.m_array)
-				, m_action_handler(source.m_action_handler)*/
+				, m_parent(NULL)
 				{
 				if (m_type == empty_leaf) {
 					return;
@@ -181,8 +177,10 @@
 			} */
 			Leaf& operator[](const std::string& p_key) {
 				check_type(object_leaf);
-				if (m_map.find(p_key) == m_map.end())
+				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 Leaf& operator[](const std::string& p_key) const  {
@@ -195,6 +193,9 @@
 			const LeafType type() const {
 				return m_type;
 			}
+			LeafRef parent() const {
+				return *m_parent;
+			}
 /*			const Leaf& operator[](const int p_idx) const {
 				check_type_const(array_leaf);
 				if (p_idx >= m_array.size())
@@ -210,8 +211,10 @@
 				check_type(array_leaf);
 	//			if (m_array.find(p_key) == m_map.end())
 	//				m_map[p_key] = boost::shared_ptr<Leaf_Impl>(new Leaf_Impl());
-				if (p_idx >= m_array.size())
+				if (p_idx >= m_array.size()) {
 					m_array.resize(p_idx+1, Leaf());
+					m_array[p_idx].m_parent = this;
+				}
 					//m_array[p_idx] = boost::shared_ptr<Leaf_Impl>(new Leaf_Impl());
 				//if (m_array[p_idx].get() == NULL)
 				//	m_array[p_idx] = boost::shared_ptr<Leaf_Impl>(new Leaf_Impl());
@@ -351,16 +354,16 @@
 		class TModelActionHandler : public IModelActionHandler {
 			protected:
 				TClass *m_obj;
-				Leaf (TClass::*m_handler)(const std::string& pUri, LeafRef pRequest);
+				Leaf (TClass::*m_handler)(const std::string& pUri, LeafRef pSelf, LeafRef pRequest);
 				LeafRef (TClass::*m_describer)();
 			public:
-			TModelActionHandler(TClass *pObject, Leaf(TClass::*pHndlr)(const std::string& pUri, LeafRef pReq), LeafRef(TClass::*pDscrp)())
+			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)
 			{ ; }
-			virtual Leaf handler(const std::string &uri, LeafRef pRequest ) {
-				return (*m_obj.*m_handler)(uri, pRequest);
+			virtual Leaf handler(const std::string &uri, LeafRef pSelf, LeafRef pRequest ) {
+				return (*m_obj.*m_handler)(uri, pSelf, pRequest);
 			}
 			virtual LeafRef describe() {
 				return (*m_obj.*m_describer)();
diff -r 4e7e139aedde -r 2ed0901283a0 zconf++/zconfig.cc
--- a/zconf++/zconfig.cc	Wed Jan 11 06:57:43 2012 +0100
+++ b/zconf++/zconfig.cc	Wed Jan 11 07:01:01 2012 +0100
@@ -45,7 +45,7 @@
 		LeafRef configured();
 		Leaf m_dummy;
 
-		virtual Leaf action_add_ip(const std::string &uri, LeafRef pRequest ) {
+		virtual Leaf action_add_ip(const std::string &uri, LeafRef pSelf, LeafRef pRequest ) {
 			LeafRef lIf = (*m_running)["networking"]["interfaces"][pRequest["parent"]];
 			lIf["addresses"][pRequest["address"]] = pRequest;
 			return Leaf("Success");


More information about the Zrouter-src mailing list