From: Tomek Mrugalski Date: Thu, 24 Nov 2016 13:44:00 +0000 (+0100) Subject: [5014_phase2] ListElement can now be modified X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=98e76cb3a3efc663bffc703c38796d9dd6ab63ad;p=thirdparty%2Fkea.git [5014_phase2] ListElement can now be modified It now contains vector of ElementPtr, rather than ConstElementPtr --- diff --git a/src/lib/cc/data.cc b/src/lib/cc/data.cc index 65d9f6808f..89042b9e2e 100644 --- a/src/lib/cc/data.cc +++ b/src/lib/cc/data.cc @@ -86,7 +86,7 @@ Element::getValue(std::string&) const { } bool -Element::getValue(std::vector&) const { +Element::getValue(std::vector&) const { return (false); } @@ -116,7 +116,7 @@ Element::setValue(const std::string&) { } bool -Element::setValue(const std::vector&) { +Element::setValue(const std::vector&) { 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& v = listValue(); - for (std::vector::const_iterator it = v.begin(); + const std::vector& v = listValue(); + for (std::vector::const_iterator it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) { ss << ", "; diff --git a/src/lib/cc/data.h b/src/lib/cc/data.h index 791a555344..f1a08fca40 100644 --- a/src/lib/cc/data.h +++ b/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& listValue() const { + virtual const std::vector& 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& t) const; + virtual bool getValue(std::vector& t) const; virtual bool getValue(std::map& 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& v); + virtual bool setValue(const std::vector& v); virtual bool setValue(const std::map& 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 l; + std::vector l; public: ListElement(const Position& pos = ZERO_POSITION()) : Element(list, pos) {} - const std::vector& listValue() const { return (l); } + const std::vector& listValue() const { return (l); } using Element::getValue; - bool getValue(std::vector& t) const { + bool getValue(std::vector& t) const { t = l; return (true); } using Element::setValue; - bool setValue(const std::vector& v) { + bool setValue(const std::vector& 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; diff --git a/src/lib/cc/tests/data_unittests.cc b/src/lib/cc/tests/data_unittests.cc index 6b1a657e5c..1f6333fc97 100644 --- a/src/lib/cc/tests/data_unittests.cc +++ b/src/lib/cc/tests/data_unittests.cc @@ -210,7 +210,7 @@ testGetValueInt() { double d; bool b; std::string s; - std::vector v; + std::vector v; std::map m; el = Element::create(1); @@ -270,7 +270,7 @@ testGetValueDouble() { double d; bool b; std::string s; - std::vector v; + std::vector v; std::map m; el = Element::create(1.1); @@ -297,7 +297,7 @@ testGetValueBool() { double d; bool b; std::string s; - std::vector v; + std::vector v; std::map m; el = Element::create(true); @@ -324,7 +324,7 @@ testGetValueString() { double d; bool b; std::string s; - std::vector v; + std::vector v; std::map m; el = Element::create("foo"); @@ -351,7 +351,7 @@ testGetValueList() { double d; bool b; std::string s; - std::vector v; + std::vector v; std::map m; el = Element::createList(); @@ -378,7 +378,7 @@ testGetValueMap() { double d; bool b; std::string s; - std::vector v; + std::vector v; std::map 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 v; + std::vector v; std::map m; ConstElementPtr tmp;