diff options
Diffstat (limited to 'project2/common/variables-modliteral.cpp')
-rw-r--r-- | project2/common/variables-modliteral.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/project2/common/variables-modliteral.cpp b/project2/common/variables-modliteral.cpp new file mode 100644 index 0000000..8c165ee --- /dev/null +++ b/project2/common/variables-modliteral.cpp @@ -0,0 +1,78 @@ +#include "variables-modliteral.h" +#include "scripts.h" +#include <stdio.h> +#include <boost/foreach.hpp> +#include <boost/bind.hpp> + +/// Variable implementation whose value is a literal value of some known type +VariableLiteral::VariableLiteral(const Glib::ustring & src, const VT_typeID format) : + val(VariableType::make(src, format)) +{ +} + +template <typename X, typename Y> +static +void +append(VariableLiteral::Vals * vals, const Y & y) +{ + vals->push_back(new X(y)); +} + +VariableLiteral::VariableLiteral(ScriptNodePtr s) { + try { + val = VariableType::make(s->value("value"), VariableType::getTypeFromName(s->value("type", ""))); + } + catch (const ValueNotFound &) { + s->composeWithCallbacks( + boost::bind(&append<TextPart, Glib::ustring>, &vals, _1), + boost::bind(&append<VarPart, ScriptNodePtr>, &vals, _1)); + } +} + +VariableType +VariableLiteral::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; +} + +VariableLiteral::TextPart::TextPart(const Glib::ustring & e) : + txt(e) +{ +} +void +VariableLiteral::TextPart::appendTo(Glib::ustring & str) const +{ + str += txt; +} +VariableLiteral::TextPart::operator VariableType() const +{ + return txt; +} + +VariableLiteral::VarPart::VarPart(ScriptNodePtr e) : Variable(e) +{ +} +void +VariableLiteral::VarPart::appendTo(Glib::ustring & str) const +{ + str += (*this)().operator const Glib::ustring &(); +} +VariableLiteral::VarPart::operator VariableType() const +{ + return (*this)(); +} + +DECLARE_COMPONENT_LOADER("literal", VariableLiteral, VariableLoader); +DECLARE_CUSTOM_COMPONENT_LOADER("", VariableLiteralDef, VariableLoaderImpl<VariableLiteral>, VariableLoader); + + |