Browse Source

[5014_phase2] ListElement can now be modified

 It now contains vector of ElementPtr, rather than ConstElementPtr
Tomek Mrugalski 8 years ago
parent
commit
04fa0d3f0b
3 changed files with 33 additions and 25 deletions
  1. 12 7
      src/lib/cc/data.cc
  2. 14 11
      src/lib/cc/data.h
  3. 7 7
      src/lib/cc/tests/data_unittests.cc

+ 12 - 7
src/lib/cc/data.cc

@@ -86,7 +86,7 @@ Element::getValue(std::string&) const {
 }
 
 bool
-Element::getValue(std::vector<ConstElementPtr>&) const {
+Element::getValue(std::vector<ElementPtr>&) const {
     return (false);
 }
 
@@ -116,7 +116,7 @@ Element::setValue(const std::string&) {
 }
 
 bool
-Element::setValue(const std::vector<ConstElementPtr>&) {
+Element::setValue(const std::vector<ElementPtr>&) {
     return (false);
 }
 
@@ -130,13 +130,18 @@ Element::get(const int) const {
     throwTypeError("get(int) called on a non-list Element");
 }
 
+ElementPtr
+Element::getNonConst(const int) {
+    throwTypeError("get(int) called on a non-list Element");
+}
+
 void
-Element::set(const size_t, ConstElementPtr) {
+Element::set(const size_t, ElementPtr) {
     throwTypeError("set(int, element) called on a non-list Element");
 }
 
 void
-Element::add(ConstElementPtr) {
+Element::add(ElementPtr) {
     throwTypeError("add() called on a non-list Element");
 }
 
@@ -507,7 +512,7 @@ fromStringstreamList(std::istream& in, const std::string& file, int& line,
 {
     int c = 0;
     ElementPtr list = Element::createList(Element::Position(file, line, pos));
-    ConstElementPtr cur_list_element;
+    ElementPtr cur_list_element;
 
     skipChars(in, WHITESPACE, line, pos);
     while (c != EOF && c != ']') {
@@ -805,8 +810,8 @@ void
 ListElement::toJSON(std::ostream& ss) const {
     ss << "[ ";
 
-    const std::vector<ConstElementPtr>& v = listValue();
-    for (std::vector<ConstElementPtr>::const_iterator it = v.begin();
+    const std::vector<ElementPtr>& v = listValue();
+    for (std::vector<ElementPtr>::const_iterator it = v.begin();
          it != v.end(); ++it) {
         if (it != v.begin()) {
             ss << ", ";

+ 14 - 11
src/lib/cc/data.h

@@ -216,7 +216,7 @@ public:
     { throwTypeError("boolValue() called on non-Bool Element"); };
     virtual std::string stringValue() const
     { throwTypeError("stringValue() called on non-string Element"); };
-    virtual const std::vector<ConstElementPtr>& listValue() const {
+    virtual const std::vector<ElementPtr>& listValue() const {
         // replace with real exception or empty vector?
         throwTypeError("listValue() called on non-list Element");
     };
@@ -239,7 +239,7 @@ public:
     virtual bool getValue(double& t) const;
     virtual bool getValue(bool& t) const;
     virtual bool getValue(std::string& t) const;
-    virtual bool getValue(std::vector<ConstElementPtr>& t) const;
+    virtual bool getValue(std::vector<ElementPtr>& t) const;
     virtual bool getValue(std::map<std::string, ConstElementPtr>& t) const;
     //@}
 
@@ -259,7 +259,7 @@ public:
     virtual bool setValue(const double v);
     virtual bool setValue(const bool t);
     virtual bool setValue(const std::string& v);
-    virtual bool setValue(const std::vector<ConstElementPtr>& v);
+    virtual bool setValue(const std::vector<ElementPtr>& v);
     virtual bool setValue(const std::map<std::string, ConstElementPtr>& v);
     //@}
 
@@ -277,15 +277,17 @@ public:
     /// \param i The position of the ElementPtr to return
     virtual ConstElementPtr get(const int i) const;
 
+    virtual ElementPtr getNonConst(const int i);
+
     /// Sets the ElementPtr at the given index. If the index is out
     /// of bounds, this function throws an std::out_of_range exception.
     /// \param i The position of the ElementPtr to set
     /// \param element The ElementPtr to set at the position
-    virtual void set(const size_t i, ConstElementPtr element);
+    virtual void set(const size_t i, ElementPtr element);
 
     /// Adds an ElementPtr to the list
     /// \param element The ElementPtr to add
-    virtual void add(ConstElementPtr element);
+    virtual void add(ElementPtr element);
 
     /// Removes the element at the given position. If the index is out
     /// of nothing happens.
@@ -603,29 +605,30 @@ public:
 };
 
 class ListElement : public Element {
-    std::vector<ConstElementPtr> l;
+    std::vector<ElementPtr> l;
 
 public:
     ListElement(const Position& pos = ZERO_POSITION())
         : Element(list, pos) {}
-    const std::vector<ConstElementPtr>& listValue() const { return (l); }
+    const std::vector<ElementPtr>& listValue() const { return (l); }
     using Element::getValue;
-    bool getValue(std::vector<ConstElementPtr>& t) const {
+    bool getValue(std::vector<ElementPtr>& t) const {
         t = l;
         return (true);
     }
     using Element::setValue;
-    bool setValue(const std::vector<ConstElementPtr>& v) {
+    bool setValue(const std::vector<ElementPtr>& v) {
         l = v;
         return (true);
     }
     using Element::get;
     ConstElementPtr get(int i) const { return (l.at(i)); }
+    ElementPtr getNonConst(int i)  { return (l.at(i)); }
     using Element::set;
-    void set(size_t i, ConstElementPtr e) {
+    void set(size_t i, ElementPtr e) {
         l.at(i) = e;
     }
-    void add(ConstElementPtr e) { l.push_back(e); };
+    void add(ElementPtr e) { l.push_back(e); };
     using Element::remove;
     void remove(int i) { l.erase(l.begin() + i); };
     void toJSON(std::ostream& ss) const;

+ 7 - 7
src/lib/cc/tests/data_unittests.cc

@@ -210,7 +210,7 @@ testGetValueInt() {
     double d;
     bool b;
     std::string s;
-    std::vector<ConstElementPtr> v;
+    std::vector<ElementPtr> v;
     std::map<std::string, ConstElementPtr> m;
 
     el = Element::create(1);
@@ -270,7 +270,7 @@ testGetValueDouble() {
     double d;
     bool b;
     std::string s;
-    std::vector<ConstElementPtr> v;
+    std::vector<ElementPtr> v;
     std::map<std::string, ConstElementPtr> m;
 
     el = Element::create(1.1);
@@ -297,7 +297,7 @@ testGetValueBool() {
     double d;
     bool b;
     std::string s;
-    std::vector<ConstElementPtr> v;
+    std::vector<ElementPtr> v;
     std::map<std::string, ConstElementPtr> m;
 
     el = Element::create(true);
@@ -324,7 +324,7 @@ testGetValueString() {
     double d;
     bool b;
     std::string s;
-    std::vector<ConstElementPtr> v;
+    std::vector<ElementPtr> v;
     std::map<std::string, ConstElementPtr> m;
 
     el = Element::create("foo");
@@ -351,7 +351,7 @@ testGetValueList() {
     double d;
     bool b;
     std::string s;
-    std::vector<ConstElementPtr> v;
+    std::vector<ElementPtr> v;
     std::map<std::string, ConstElementPtr> m;
 
     el = Element::createList();
@@ -378,7 +378,7 @@ testGetValueMap() {
     double d;
     bool b;
     std::string s;
-    std::vector<ConstElementPtr> v;
+    std::vector<ElementPtr> v;
     std::map<std::string, ConstElementPtr> m;
 
     el = Element::createMap();
@@ -406,7 +406,7 @@ TEST(Element, create_and_value_throws) {
     double d = 0.0;
     bool b = false;
     std::string s("asdf");
-    std::vector<ConstElementPtr> v;
+    std::vector<ElementPtr> v;
     std::map<std::string, ConstElementPtr> m;
     ConstElementPtr tmp;