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

BIND 10 source code commits bind10-changes at lists.isc.org
Wed Oct 28 13:43:38 UTC 2009


Author: jelte
Date: Wed Oct 28 13:43:37 2009
New Revision: 137

Log:
added BoolElement

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 Wed Oct 28 13:43:37 2009
@@ -5,7 +5,7 @@
 #include <iostream>
 #include <sstream>
 #include <boost/foreach.hpp>
-
+#include <boost/algorithm/string.hpp>
 
 using namespace std;
 using namespace ISC::Data;
@@ -38,6 +38,17 @@
 {
     try {
         return ElementPtr(new StringElement(s));
+    } catch (std::bad_alloc) {
+        return ElementPtr();
+    }
+}
+
+ElementPtr
+Element::create(const bool b)
+{
+    try {
+        cout << "creating boolelement" << endl;
+        return ElementPtr(new BoolElement(b));
     } catch (std::bad_alloc) {
         return ElementPtr();
     }
@@ -142,6 +153,17 @@
     return ss.str();
 }
 
+static std::string
+word_from_stringstream(std::stringstream &in)
+{
+    std::stringstream ss;
+    while (isalpha(in.peek())) {
+        ss << (char) in.get();
+    }
+    return ss.str();
+}
+
+
 static ElementPtr
 from_stringstream_int_or_double(std::stringstream &in)
 {
@@ -154,6 +176,19 @@
         return Element::create(d);
     } else {
         return Element::create(i);
+    }
+}
+
+static ElementPtr
+from_stringstream_bool(std::stringstream &in)
+{
+    std::string word = word_from_stringstream(in);
+    if (boost::iequals(word, "true")) {
+        return Element::create(true);
+    } else if (boost::iequals(word, "false")) {
+        return Element::create(false);
+    } else {
+        return ElementPtr();
     }
 }
 
@@ -232,13 +267,21 @@
                 element = from_stringstream_int_or_double(in);
                 el_read = true;
                 break;
-            case '[':
-                element = from_stringstream_list(in);
+            case 't':
+            case 'T':
+            case 'f':
+            case 'F':
+                in.putback(c);
+                element = from_stringstream_bool(in);
                 el_read = true;
                 break;
             case '"':
                 in.putback('"');
                 element = from_stringstream_string(in);
+                el_read = true;
+                break;
+            case '[':
+                element = from_stringstream_list(in);
                 el_read = true;
                 break;
             case '{':
@@ -281,6 +324,16 @@
 }
 
 std::string
+BoolElement::str()
+{
+    if (b) {
+        return "true";
+    } else {
+        return "false";
+    }
+}
+
+std::string
 StringElement::str()
 {
     std::stringstream ss;
@@ -349,6 +402,15 @@
 
 std::string
 DoubleElement::str_xml(size_t prefix)
+{
+    std::stringstream ss;
+    pre(ss, prefix);
+    ss << str();
+    return ss.str();
+}
+
+std::string
+BoolElement::str_xml(size_t prefix)
 {
     std::stringstream ss;
     pre(ss, prefix);
@@ -459,7 +521,8 @@
     cout << "ie value: " << ie->int_value() << endl;
     ElementPtr de = Element::create(12.0);
     cout << "de value: " << de->double_value() << endl;
-    ElementPtr se = Element::create("hello, world");
+    ElementPtr se = Element::create(std::string("hello, world").c_str());
+    cout << "se type " << se->get_type() << endl;
     cout << "se value: " << se->string_value() << endl;
     std::vector<ElementPtr> v;
     v.push_back(Element::create(12));
@@ -474,7 +537,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\", \"aaa\": { \"bbb\": { \"ccc\": 1234, \"ddd\": \"blup\" } } }";
+    std::string s = "{ \"test\": [ 47806, true, 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;
@@ -515,5 +578,8 @@
 
     cout << "test: " << e << endl;
 */
+    ElementPtr be = Element::create(true);
+    cout << "boolelement: " << be << endl;
+
     return 0;
 }

Modified: experiments/jelte-configuration/data.h
==============================================================================
--- experiments/jelte-configuration/data.h (original)
+++ experiments/jelte-configuration/data.h Wed Oct 28 13:43:37 2009
@@ -35,8 +35,7 @@
         Element(int t) { type = t; }
 
     public:
-
-        enum types { integer, real, string, list, map };
+        enum types { integer, real, boolean, string, list, map };
         // base class; make dtor virtual
         virtual ~Element() {};
 
@@ -54,6 +53,7 @@
         // to use get_value()
         virtual int int_value() { throw TypeError(); };
         virtual double double_value() { throw TypeError(); };
+        virtual bool bool_value() { throw TypeError(); };
         virtual std::string string_value() { throw TypeError(); };
         virtual const std::vector<boost::shared_ptr<Element> >& list_value() { throw TypeError(); }; // replace with real exception or empty vector?
         virtual const std::map<std::string, boost::shared_ptr<Element> >& map_value() { throw TypeError(); }; // replace with real exception or empty map?
@@ -85,12 +85,14 @@
         //
         virtual bool get_value(int& t) { return false; };
         virtual bool get_value(double& t) { return false; };
+        virtual bool get_value(bool& t) { return false; };
         virtual bool get_value(std::string& t) { return false; };
         virtual bool get_value(std::vector<ElementPtr>& t) { return false; };
         virtual bool get_value(std::map<std::string, ElementPtr>& t) { return false; };
 
         virtual bool set_value(const int v) { return false; };
         virtual bool set_value(const double v) { return false; };
+        virtual bool set_value(const bool t) { return false; };
         virtual bool set_value(const std::string& v) { return false; };
         virtual bool set_value(const std::vector<ElementPtr>& v) { return false; };
         virtual bool set_value(const std::map<std::string, ElementPtr>& v) { return false; };
@@ -105,7 +107,11 @@
         // allocated
         static ElementPtr create(const int i);
         static ElementPtr create(const double d);
+        static ElementPtr create(const bool b);
         static ElementPtr create(const std::string& s);
+        // need both std:string and char *, since c++ will match
+        // bool before std::string when you pass it a char *
+        static ElementPtr create(const char *s) { return create(std::string(s)); }; 
         static ElementPtr create(const std::vector<ElementPtr>& v);
         static ElementPtr create(const std::map<std::string, ElementPtr>& m);
 
@@ -141,12 +147,24 @@
         std::string str_xml(size_t prefix = 0);
     };
 
+	class BoolElement : public Element {
+		bool b;
+		
+		public:
+		BoolElement(const bool v) : Element(boolean), b(v) {};
+		bool bool_value() { return b; }
+		bool get_value(bool& t) { t = b; return true; };
+		bool set_value(const bool v) { b = v; return true; };
+		std::string str();
+		std::string str_xml(size_t prefix = 0);
+	};
+	
     class StringElement : public Element {
         std::string s;
 
         public:
         StringElement(std::string v) : Element(string), s(v) {};
-        std::string string_value() { return s; }
+        std::string string_value() { return s; };
         bool get_value(std::string& t) { t = s; return true; };
         bool set_value(const std::string& v) { s = v; return true; };
         std::string str();




More information about the bind10-changes mailing list