Commit 04fa0d3f authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰

[5014_phase2] ListElement can now be modified

 It now contains vector of ElementPtr, rather than ConstElementPtr
parent cbba4ee6
...@@ -86,7 +86,7 @@ Element::getValue(std::string&) const { ...@@ -86,7 +86,7 @@ Element::getValue(std::string&) const {
} }
bool bool
Element::getValue(std::vector<ConstElementPtr>&) const { Element::getValue(std::vector<ElementPtr>&) const {
return (false); return (false);
} }
...@@ -116,7 +116,7 @@ Element::setValue(const std::string&) { ...@@ -116,7 +116,7 @@ Element::setValue(const std::string&) {
} }
bool bool
Element::setValue(const std::vector<ConstElementPtr>&) { Element::setValue(const std::vector<ElementPtr>&) {
return (false); return (false);
} }
...@@ -130,13 +130,18 @@ Element::get(const int) const { ...@@ -130,13 +130,18 @@ Element::get(const int) const {
throwTypeError("get(int) called on a non-list Element"); throwTypeError("get(int) called on a non-list Element");
} }
ElementPtr
Element::getNonConst(const int) {
throwTypeError("get(int) called on a non-list Element");
}
void void
Element::set(const size_t, ConstElementPtr) { Element::set(const size_t, ElementPtr) {
throwTypeError("set(int, element) called on a non-list Element"); throwTypeError("set(int, element) called on a non-list Element");
} }
void void
Element::add(ConstElementPtr) { Element::add(ElementPtr) {
throwTypeError("add() called on a non-list Element"); throwTypeError("add() called on a non-list Element");
} }
...@@ -507,7 +512,7 @@ fromStringstreamList(std::istream& in, const std::string& file, int& line, ...@@ -507,7 +512,7 @@ fromStringstreamList(std::istream& in, const std::string& file, int& line,
{ {
int c = 0; int c = 0;
ElementPtr list = Element::createList(Element::Position(file, line, pos)); ElementPtr list = Element::createList(Element::Position(file, line, pos));
ConstElementPtr cur_list_element; ElementPtr cur_list_element;
skipChars(in, WHITESPACE, line, pos); skipChars(in, WHITESPACE, line, pos);
while (c != EOF && c != ']') { while (c != EOF && c != ']') {
...@@ -805,8 +810,8 @@ void ...@@ -805,8 +810,8 @@ void
ListElement::toJSON(std::ostream& ss) const { ListElement::toJSON(std::ostream& ss) const {
ss << "[ "; ss << "[ ";
const std::vector<ConstElementPtr>& v = listValue(); const std::vector<ElementPtr>& v = listValue();
for (std::vector<ConstElementPtr>::const_iterator it = v.begin(); for (std::vector<ElementPtr>::const_iterator it = v.begin();
it != v.end(); ++it) { it != v.end(); ++it) {
if (it != v.begin()) { if (it != v.begin()) {
ss << ", "; ss << ", ";
......
...@@ -216,7 +216,7 @@ public: ...@@ -216,7 +216,7 @@ public:
{ throwTypeError("boolValue() called on non-Bool Element"); }; { throwTypeError("boolValue() called on non-Bool Element"); };
virtual std::string stringValue() const virtual std::string stringValue() const
{ throwTypeError("stringValue() called on non-string Element"); }; { 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? // replace with real exception or empty vector?
throwTypeError("listValue() called on non-list Element"); throwTypeError("listValue() called on non-list Element");
}; };
...@@ -239,7 +239,7 @@ public: ...@@ -239,7 +239,7 @@ public:
virtual bool getValue(double& t) const; virtual bool getValue(double& t) const;
virtual bool getValue(bool& t) const; virtual bool getValue(bool& t) const;
virtual bool getValue(std::string& 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; virtual bool getValue(std::map<std::string, ConstElementPtr>& t) const;
//@} //@}
...@@ -259,7 +259,7 @@ public: ...@@ -259,7 +259,7 @@ public:
virtual bool setValue(const double v); virtual bool setValue(const double v);
virtual bool setValue(const bool t); virtual bool setValue(const bool t);
virtual bool setValue(const std::string& v); 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); virtual bool setValue(const std::map<std::string, ConstElementPtr>& v);
//@} //@}
...@@ -277,15 +277,17 @@ public: ...@@ -277,15 +277,17 @@ public:
/// \param i The position of the ElementPtr to return /// \param i The position of the ElementPtr to return
virtual ConstElementPtr get(const int i) const; 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 /// Sets the ElementPtr at the given index. If the index is out
/// of bounds, this function throws an std::out_of_range exception. /// of bounds, this function throws an std::out_of_range exception.
/// \param i The position of the ElementPtr to set /// \param i The position of the ElementPtr to set
/// \param element The ElementPtr to set at the position /// \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 /// Adds an ElementPtr to the list
/// \param element The ElementPtr to add /// \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 /// Removes the element at the given position. If the index is out
/// of nothing happens. /// of nothing happens.
...@@ -603,29 +605,30 @@ public: ...@@ -603,29 +605,30 @@ public:
}; };
class ListElement : public Element { class ListElement : public Element {
std::vector<ConstElementPtr> l; std::vector<ElementPtr> l;
public: public:
ListElement(const Position& pos = ZERO_POSITION()) ListElement(const Position& pos = ZERO_POSITION())
: Element(list, pos) {} : Element(list, pos) {}
const std::vector<ConstElementPtr>& listValue() const { return (l); } const std::vector<ElementPtr>& listValue() const { return (l); }
using Element::getValue; using Element::getValue;
bool getValue(std::vector<ConstElementPtr>& t) const { bool getValue(std::vector<ElementPtr>& t) const {
t = l; t = l;
return (true); return (true);
} }
using Element::setValue; using Element::setValue;
bool setValue(const std::vector<ConstElementPtr>& v) { bool setValue(const std::vector<ElementPtr>& v) {
l = v; l = v;
return (true); return (true);
} }
using Element::get; using Element::get;
ConstElementPtr get(int i) const { return (l.at(i)); } ConstElementPtr get(int i) const { return (l.at(i)); }
ElementPtr getNonConst(int i) { return (l.at(i)); }
using Element::set; using Element::set;
void set(size_t i, ConstElementPtr e) { void set(size_t i, ElementPtr e) {
l.at(i) = e; l.at(i) = e;
} }
void add(ConstElementPtr e) { l.push_back(e); }; void add(ElementPtr e) { l.push_back(e); };
using Element::remove; using Element::remove;
void remove(int i) { l.erase(l.begin() + i); }; void remove(int i) { l.erase(l.begin() + i); };
void toJSON(std::ostream& ss) const; void toJSON(std::ostream& ss) const;
......
...@@ -210,7 +210,7 @@ testGetValueInt() { ...@@ -210,7 +210,7 @@ testGetValueInt() {
double d; double d;
bool b; bool b;
std::string s; std::string s;
std::vector<ConstElementPtr> v; std::vector<ElementPtr> v;
std::map<std::string, ConstElementPtr> m; std::map<std::string, ConstElementPtr> m;
el = Element::create(1); el = Element::create(1);
...@@ -270,7 +270,7 @@ testGetValueDouble() { ...@@ -270,7 +270,7 @@ testGetValueDouble() {
double d; double d;
bool b; bool b;
std::string s; std::string s;
std::vector<ConstElementPtr> v; std::vector<ElementPtr> v;
std::map<std::string, ConstElementPtr> m; std::map<std::string, ConstElementPtr> m;
el = Element::create(1.1); el = Element::create(1.1);
...@@ -297,7 +297,7 @@ testGetValueBool() { ...@@ -297,7 +297,7 @@ testGetValueBool() {
double d; double d;
bool b; bool b;
std::string s; std::string s;
std::vector<ConstElementPtr> v; std::vector<ElementPtr> v;
std::map<std::string, ConstElementPtr> m; std::map<std::string, ConstElementPtr> m;
el = Element::create(true); el = Element::create(true);
...@@ -324,7 +324,7 @@ testGetValueString() { ...@@ -324,7 +324,7 @@ testGetValueString() {
double d; double d;
bool b; bool b;
std::string s; std::string s;
std::vector<ConstElementPtr> v; std::vector<ElementPtr> v;
std::map<std::string, ConstElementPtr> m; std::map<std::string, ConstElementPtr> m;
el = Element::create("foo"); el = Element::create("foo");
...@@ -351,7 +351,7 @@ testGetValueList() { ...@@ -351,7 +351,7 @@ testGetValueList() {
double d; double d;
bool b; bool b;
std::string s; std::string s;
std::vector<ConstElementPtr> v; std::vector<ElementPtr> v;
std::map<std::string, ConstElementPtr> m; std::map<std::string, ConstElementPtr> m;
el = Element::createList(); el = Element::createList();
...@@ -378,7 +378,7 @@ testGetValueMap() { ...@@ -378,7 +378,7 @@ testGetValueMap() {
double d; double d;
bool b; bool b;
std::string s; std::string s;
std::vector<ConstElementPtr> v; std::vector<ElementPtr> v;
std::map<std::string, ConstElementPtr> m; std::map<std::string, ConstElementPtr> m;
el = Element::createMap(); el = Element::createMap();
...@@ -406,7 +406,7 @@ TEST(Element, create_and_value_throws) { ...@@ -406,7 +406,7 @@ TEST(Element, create_and_value_throws) {
double d = 0.0; double d = 0.0;
bool b = false; bool b = false;
std::string s("asdf"); std::string s("asdf");
std::vector<ConstElementPtr> v; std::vector<ElementPtr> v;
std::map<std::string, ConstElementPtr> m; std::map<std::string, ConstElementPtr> m;
ConstElementPtr tmp; ConstElementPtr tmp;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment