From b4f3b67b0c1f22d1fc50aecf9100f10a653b903e Mon Sep 17 00:00:00 2001 From: randomdan Date: Sun, 14 Aug 2011 22:27:20 +0000 Subject: Extend literal variables to support composing strings from other variables and literals like sqlWriter does --- project2/variables.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/project2/variables.cpp b/project2/variables.cpp index 7e7ea2c..b866165 100644 --- a/project2/variables.cpp +++ b/project2/variables.cpp @@ -123,17 +123,71 @@ class VariableLiteral : public VariableImpl { if (const xmlpp::Attribute * la = c->get_attribute("value")) { val = makeVariableType(la->get_value(), getVariableTypeFromName(c->get_attribute_value("type"))); } - else if (const xmlpp::TextNode * t = c->get_child_text()) { - val = makeVariableType(t->get_content(), getVariableTypeFromName(c->get_attribute_value("type"))); - } else { - val = VariableType(); + BOOST_FOREACH(const xmlpp::Node * n, c->get_children()) { + if (const xmlpp::Element * e = dynamic_cast(n)) { + vals.push_back(new VarPart(e)); + } + else if (const xmlpp::TextNode * t = dynamic_cast(n)) { + vals.push_back(new TextPart(t)); + } + } } } - virtual VariableType value() const { return val; } + virtual VariableType value() const + { + if (vals.empty()) { + return val; + } + if (vals.size() == 1) { + return *vals.front(); + } + Glib::ustring v; + BOOST_FOREACH(PartCPtr p, vals) { + p->appendTo(v); + } + return v; + } private: VariableType val; + class Part : public IntrusivePtrBase { + public: + virtual void appendTo(Glib::ustring & str) const = 0; + virtual operator VariableType() const = 0; + }; + class TextPart : public Part { + public: + TextPart(const xmlpp::TextNode * e) : + txt(e->get_content()) + { + } + void appendTo(Glib::ustring & str) const + { + str += txt; + } + operator VariableType() const + { + return txt; + } + const Glib::ustring txt; + }; + class VarPart : public Part, public Variable { + public: + VarPart(const xmlpp::Element * e) : Variable(e, boost::optional()) + { + } + void appendTo(Glib::ustring & str) const + { + str += (*this)().operator const Glib::ustring &(); + } + operator VariableType() const + { + return (*this)(); + } + }; + typedef boost::intrusive_ptr PartCPtr; + std::list vals; }; DECLARE_COMPONENT_LOADER("literal", VariableLiteral, VariableLoader); DECLARE_CUSTOM_COMPONENT_LOADER("", VariableLiteralDef, VariableLoaderImpl, VariableLoader); -- cgit v1.2.3