diff options
Diffstat (limited to 'project2/variables.cpp')
-rw-r--r-- | project2/variables.cpp | 105 |
1 files changed, 33 insertions, 72 deletions
diff --git a/project2/variables.cpp b/project2/variables.cpp index 4e18fc0..516e187 100644 --- a/project2/variables.cpp +++ b/project2/variables.cpp @@ -4,7 +4,6 @@ #include "exceptions.h" #include "appEngine.h" #include "session.h" -#include "rowUser.h" #include "rowSet.h" #include <libxml++/nodes/textnode.h> #include <stdexcept> @@ -132,117 +131,79 @@ class VariableLiteral : public VariableImpl { } } - virtual const VariableType & value() const { return val; } + virtual VariableType value() const { return val; } private: VariableType val; }; DECLARE_COMPONENT_LOADER("literal", VariableLiteral, VariableLoader); DECLARE_CUSTOM_COMPONENT_LOADER("", VariableLiteralDef, VariableLoaderImpl<VariableLiteral>, VariableLoader); -VariableImplDyn::VariableImplDyn(const xmlpp::Element * e) : - cacheValid(false) +VariableImplDyn::VariableImplDyn(const xmlpp::Element * e) { - try { - if (e) { - defaultValue = Variable(e, "default"); - } - } - catch (NoVariableDefinition) { - // That's cool... no default + if (e) { + defaultValue = Variable(e, "default", false); } } -static void assignHelper(VariableType & dest, const VariableType & src) { - dest = src; -} - /// Variable implementation to access fields in row sets -class VariableParent : public VariableImplDyn, public RowUser { +class VariableParent : public VariableImplDyn { public: - VariableParent(const xmlpp::Element * e, const RowUser * d = NULL) : + VariableParent(const xmlpp::Element * e) : VariableImplDyn(e), - row(NULL), depth(atoi(e->get_attribute_value("depth").c_str())), attr(e->get_attribute("attribute")), - name(attr ? e->get_attribute_value("attribute") : e->get_attribute_value("name")), - dep(d) + name(attr ? e->get_attribute_value("attribute") : e->get_attribute_value("name")) { } VariableParent(const Glib::ustring & n, bool a, unsigned int d) : VariableImplDyn(NULL), - row(NULL), depth(d), attr(a), - name(n), - dep(NULL) + name(n) { } - ~VariableParent() - { - if (row) { - row->finish(this); - if (dep) { - row->finish(dep); - } - } - } - const VariableType & value() const + VariableType value() const { - if (!cacheValid) { - try { - if (!row) { - RowSet::RowValuesStack::const_reverse_iterator r = RowSet::Stack().rbegin(); - for (size_t p = depth; p--; r++) ; - if (r == RowSet::Stack().rend()) { - throw RowSet::ParentOutOfRange(depth); - } - row = *r; - row->use(this); - if (dep) { - row->use(dep); - } - bind(); + try { + if (!getValue) { + RowSet::RowValuesStack::const_reverse_iterator r = RowSet::Stack().rbegin(); + for (size_t p = depth; p--; r++) ; + if (r == RowSet::Stack().rend()) { + throw RowSet::ParentOutOfRange(depth); } - getValue(cache, row); + ; + bind(*r); } - catch (RowSet::ParentOutOfRange) { - if (!defaultValue) { - throw; - } - cache = (*defaultValue)(); + return getValue(); + } + catch (RowSet::ParentOutOfRange) { + if (!defaultValue) { + throw; } - catch (RowSet::FieldDoesNotExist) { - if (!defaultValue) { - throw; - } - cache = (*defaultValue)(); + return (*defaultValue)(); + } + catch (RowSet::FieldDoesNotExist) { + if (!defaultValue) { + throw; } - cacheValid = true; + return (*defaultValue)(); } - return cache; - } - void rowChanged() const - { - cacheValid = false; } protected: - void bind() const + void bind(ConstRowSetPtr row) const { if (attr) { - getValue = boost::bind(&assignHelper, _1, boost::bind(row->resolveAttr(name))); + getValue = boost::bind(row->resolveAttr(name)); } else { typedef VariableType (RowSet::*gCV)(const Glib::ustring &) const; - getValue = boost::bind(&assignHelper, _1, - boost::bind((gCV)&RowSet::getCurrentValue, _2, name)); + getValue = boost::bind((gCV)&RowSet::getCurrentValue, row, name); } } - mutable ConstRowSetPtr row; const size_t depth; const bool attr; const Glib::ustring name; - const RowUser * dep; - mutable boost::function2<void, VariableType &, ConstRowSetPtr> getValue; + mutable boost::function0<VariableType> getValue; }; DECLARE_COMPONENT_LOADER("parent", VariableParent, VariableLoader); @@ -253,7 +214,7 @@ class VariableFixed : public VariableImpl { var(v) { } - const VariableType & value() const + VariableType value() const { return var; } |