[svn] commit: r122 - in /experiments/jelte-configuration: data.cc data.h

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Oct 27 13:32:33 UTC 2009


Author: jelte
Date: Tue Oct 27 13:32:33 2009
New Revision: 122

Log:
added a simple find method for nested MapElements
should we also come up with a syntax to find in lists (in a better way than by index)?

Modified:
    experiments/jelte-configuration/data.cc
    experiments/jelte-configuration/data.h

Modified: experiments/jelte-configuration/data.cc
==============================================================================
--- experiments/jelte-configuration/data.cc (original)
+++ experiments/jelte-configuration/data.cc Tue Oct 27 13:32:33 2009
@@ -405,6 +405,29 @@
     return ss.str();
 }
 
+// currently throws when one of the types in the path (except the one
+// we're looking for) is not a MapElement
+// returns 0 if it could simply not be found
+// should that also be an exception?
+ElementPtr
+MapElement::find(const std::string& id)
+{
+    if (get_type() != map) {
+        throw 0;
+    }
+    size_t sep = id.find('/');
+    if (sep == std::string::npos) {
+        return get(id);
+    } else {
+        ElementPtr ce = get(id.substr(0, sep));
+        if (ce) {
+            return ce->find(id.substr(sep+1));
+        } else {
+            return ElementPtr();
+        }
+    }
+}
+
 int main(int argc, char **argv)
 {
     
@@ -427,7 +450,7 @@
     cout << "Vector element direct: " << ve->string_value() << endl;
 
     //std::string s = "[ 1, 2, 3, 4]";
-    std::string s = "{ \"test\": [ 47806, 42, 12.23, 1, \"1asdf\"], \"foo\": \"bar\" }";
+    std::string s = "{ \"test\": [ 47806, 42, 12.23, 1, \"1asdf\"], \"foo\": \"bar\", \"aaa\": { \"bbb\": { \"ccc\": 1234, \"ddd\": \"blup\" } } }";
     //std::string s = "{ \"test\": 1 }";
     //std::string s = "[ 1, 2 ,3\" ]";
     std::stringstream ss;
@@ -440,6 +463,7 @@
         cout << "could not read element" << endl;
         exit(0);
     }
+    cout << "find aaa/bbb/ccc: " << e->find("aaa/bbb/ccc") << endl;
     //cout << "part: " << e->get("test")->str() << endl;
 
 /*

Modified: experiments/jelte-configuration/data.h
==============================================================================
--- experiments/jelte-configuration/data.h (original)
+++ experiments/jelte-configuration/data.h Tue Oct 27 13:32:33 2009
@@ -63,6 +63,7 @@
         virtual ElementPtr get(const std::string& name) { throw 0;} ;
         virtual void set(const std::string& name, ElementPtr element) { throw 0; };
         virtual void remove(const std::string& name) { throw 0; };
+        virtual ElementPtr find(const std::string& identifier) { throw 0; };
 
         //
         // the _value() functions return false if the given reference
@@ -168,6 +169,7 @@
         void set(const std::string& s, ElementPtr p) { m[s] = p; };
         std::string str();
         std::string str_xml(size_t prefix = 0);
+        ElementPtr find(const std::string& id);
     };
 
 } }




More information about the bind10-changes mailing list