From 9b513b94cb98dd3dd074bfc78382911d74242c7d Mon Sep 17 00:00:00 2001 From: randomdan Date: Sat, 16 Mar 2013 17:35:08 +0000 Subject: Folderise variables and another test --- project2/common/scripts.cpp | 2 +- project2/common/tests/validDateCheck.cpp | 66 ++++++++++++++++++++++ project2/common/validDateCheck.cpp | 66 ---------------------- project2/common/variables-modconfig.cpp | 87 ----------------------------- project2/common/variables-modfixed.cpp | 13 ----- project2/common/variables-modfixed.h | 17 ------ project2/common/variables-modliteral.cpp | 78 -------------------------- project2/common/variables-modliteral.h | 40 ------------- project2/common/variables-modlocalparam.cpp | 32 ----------- project2/common/variables-modlookup.cpp | 74 ------------------------ project2/common/variables-modparam.cpp | 31 ---------- project2/common/variables-modsession.cpp | 31 ---------- project2/common/variables-moduri.cpp | 31 ---------- project2/common/variables.cpp | 2 +- project2/common/variables/config.cpp | 87 +++++++++++++++++++++++++++++ project2/common/variables/fixed.cpp | 13 +++++ project2/common/variables/fixed.h | 17 ++++++ project2/common/variables/literal.cpp | 78 ++++++++++++++++++++++++++ project2/common/variables/literal.h | 40 +++++++++++++ project2/common/variables/localparam.cpp | 31 ++++++++++ project2/common/variables/lookup.cpp | 74 ++++++++++++++++++++++++ project2/common/variables/param.cpp | 31 ++++++++++ project2/common/variables/session.cpp | 31 ++++++++++ project2/common/variables/uri.cpp | 31 ++++++++++ project2/xml/xmlScriptParser.cpp | 2 +- 25 files changed, 502 insertions(+), 503 deletions(-) create mode 100644 project2/common/tests/validDateCheck.cpp delete mode 100644 project2/common/validDateCheck.cpp delete mode 100644 project2/common/variables-modconfig.cpp delete mode 100644 project2/common/variables-modfixed.cpp delete mode 100644 project2/common/variables-modfixed.h delete mode 100644 project2/common/variables-modliteral.cpp delete mode 100644 project2/common/variables-modliteral.h delete mode 100644 project2/common/variables-modlocalparam.cpp delete mode 100644 project2/common/variables-modlookup.cpp delete mode 100644 project2/common/variables-modparam.cpp delete mode 100644 project2/common/variables-modsession.cpp delete mode 100644 project2/common/variables-moduri.cpp create mode 100644 project2/common/variables/config.cpp create mode 100644 project2/common/variables/fixed.cpp create mode 100644 project2/common/variables/fixed.h create mode 100644 project2/common/variables/literal.cpp create mode 100644 project2/common/variables/literal.h create mode 100644 project2/common/variables/localparam.cpp create mode 100644 project2/common/variables/lookup.cpp create mode 100644 project2/common/variables/param.cpp create mode 100644 project2/common/variables/session.cpp create mode 100644 project2/common/variables/uri.cpp diff --git a/project2/common/scripts.cpp b/project2/common/scripts.cpp index 997f558..e2f1370 100644 --- a/project2/common/scripts.cpp +++ b/project2/common/scripts.cpp @@ -1,5 +1,5 @@ #include "scripts.h" -#include "variables-modfixed.h" +#include "variables/fixed.h" ScriptNode::ScriptNode(ScriptReaderPtr s) : script(s) diff --git a/project2/common/tests/validDateCheck.cpp b/project2/common/tests/validDateCheck.cpp new file mode 100644 index 0000000..dabbdaa --- /dev/null +++ b/project2/common/tests/validDateCheck.cpp @@ -0,0 +1,66 @@ +#include +#include "../logger.h" +#include "../scriptLoader.h" +#include "../commonObjects.h" +#include "../test.h" +#include "../variables.h" +#include "../scripts.h" + +class ValidDateTest : public Test { + public: + ValidDateTest(ScriptNodePtr p) : + SourceObject(p), + Test(p), + applyTo(p, "apply-to"), + format(p, "format"), + warnLev(p->value("warn", true).as() ? LOG_WARNING : LOG_INFO) + { + } + + ~ValidDateTest() + { + } + + bool + passes() const + { + struct tm tm, ftm; + memset(&tm, 0, sizeof(struct tm)); + mktime(&tm); + const char * at = applyTo(); + const char * f = format(); + const char * s = strptime(at, f, &tm); + if (!s || *s) { + Logger()->messagef(warnLev, "%s: check failed (parse) for '%s' against '%s'", + __PRETTY_FUNCTION__, at, f); + return false; + } + ftm = tm; + if (mktime(&ftm) == -1) { + Logger()->messagef(warnLev, "%s: check failed (normalise) for '%s' against '%s'", + __PRETTY_FUNCTION__, at, f); + return false; + } + if (tm.tm_year != ftm.tm_year || + tm.tm_mon != ftm.tm_mon || + tm.tm_mday != ftm.tm_mday || + tm.tm_hour != (ftm.tm_hour - ftm.tm_isdst) || + tm.tm_min != ftm.tm_min || + tm.tm_sec != ftm.tm_sec) { + Logger()->messagef(LOG_INFO, "tm: %d %d %d %d %d %d", + tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); + Logger()->messagef(LOG_INFO, "ftm: %d %d %d %d %d %d", + ftm.tm_year, ftm.tm_mon, ftm.tm_mday, ftm.tm_hour, ftm.tm_min, ftm.tm_sec); + Logger()->messagef(warnLev, "%s: check failed (verify) for '%s' against '%s'", + __PRETTY_FUNCTION__, at, f); + return false; + } + return true; + } + Variable applyTo; + Variable format; + int warnLev; +}; + +DECLARE_LOADER("validdatetest", ValidDateTest); + diff --git a/project2/common/validDateCheck.cpp b/project2/common/validDateCheck.cpp deleted file mode 100644 index be983b4..0000000 --- a/project2/common/validDateCheck.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include "logger.h" -#include "scriptLoader.h" -#include "commonObjects.h" -#include "test.h" -#include "variables.h" -#include "scripts.h" - -class ValidDateTest : public Test { - public: - ValidDateTest(ScriptNodePtr p) : - SourceObject(p), - Test(p), - applyTo(p, "apply-to"), - format(p, "format"), - warnLev(p->value("warn", true).as() ? LOG_WARNING : LOG_INFO) - { - } - - ~ValidDateTest() - { - } - - bool - passes() const - { - struct tm tm, ftm; - memset(&tm, 0, sizeof(struct tm)); - mktime(&tm); - const char * at = applyTo(); - const char * f = format(); - const char * s = strptime(at, f, &tm); - if (!s || *s) { - Logger()->messagef(warnLev, "%s: check failed (parse) for '%s' against '%s'", - __PRETTY_FUNCTION__, at, f); - return false; - } - ftm = tm; - if (mktime(&ftm) == -1) { - Logger()->messagef(warnLev, "%s: check failed (normalise) for '%s' against '%s'", - __PRETTY_FUNCTION__, at, f); - return false; - } - if (tm.tm_year != ftm.tm_year || - tm.tm_mon != ftm.tm_mon || - tm.tm_mday != ftm.tm_mday || - tm.tm_hour != (ftm.tm_hour - ftm.tm_isdst) || - tm.tm_min != ftm.tm_min || - tm.tm_sec != ftm.tm_sec) { - Logger()->messagef(LOG_INFO, "tm: %d %d %d %d %d %d", - tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - Logger()->messagef(LOG_INFO, "ftm: %d %d %d %d %d %d", - ftm.tm_year, ftm.tm_mon, ftm.tm_mday, ftm.tm_hour, ftm.tm_min, ftm.tm_sec); - Logger()->messagef(warnLev, "%s: check failed (verify) for '%s' against '%s'", - __PRETTY_FUNCTION__, at, f); - return false; - } - return true; - } - Variable applyTo; - Variable format; - int warnLev; -}; - -DECLARE_LOADER("validdatetest", ValidDateTest); - diff --git a/project2/common/variables-modconfig.cpp b/project2/common/variables-modconfig.cpp deleted file mode 100644 index 4d9553a..0000000 --- a/project2/common/variables-modconfig.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include "variables.h" -#include "scriptLoader.h" -#include "scriptStorage.h" -#include -#include "appEngine.h" - -typedef std::map ConfigOptions; -static ConfigOptions cfgOpts; - -SimpleMessageException(NoSuchConfigurationValue); - -/// Variable implementation to access platform configuration values -class VariableConfig : public VariableImplDyn { - public: - VariableConfig(ScriptNodePtr e) : - VariableImplDyn(e), - name(e->value("name").as()) - { - } - VariableType value() const - { - const ConfigOptions::const_iterator i = cfgOpts.find(name); - if (i != cfgOpts.end()) { - return i->second; - } - if (defaultValue) { - return defaultValue.get()(); - } - throw NoSuchConfigurationValue(name); - } - private: - const Glib::ustring name; -}; - -class VariableConfigLoader : public VariableLoader::For { - public: - class AppSettings : public Options::Option { - public: - void reset() const { - cfgOpts.clear(); - } - void consume(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const { - if (boost::algorithm::starts_with(n, "application.")) { - Glib::ustring k(n.substr(12)); - const ConfigOptions::iterator i = cfgOpts.find(k); - if (i == cfgOpts.end()) { - if (p.empty() || p == Environment::getCurrent()->platform()) { - cfgOpts.insert(ConfigOptions::value_type(k, v)); - } - } - else { - if (p == Environment::getCurrent()->platform()) { - i->second = v; - } - } - } - } - bool accepts(const Glib::ustring & n) const { - return (boost::algorithm::starts_with(n, "application.")); - } - bool paramRequired() const { - return true; - } - Glib::ustring name() const { - return "application.*"; - } - Glib::ustring description() const { - return "Load settings into the client application"; - } - }; - VariableConfigLoader() : - opts("Variables - ModConfig options") - { - opts(new AppSettings()); - } - - const Options * options() const { - return &opts; - } - - private: - Options opts; -}; - -DECLARE_CUSTOM_COMPONENT_LOADER("config", VariableConfigLoader, VariableConfigLoader, VariableLoader); - diff --git a/project2/common/variables-modfixed.cpp b/project2/common/variables-modfixed.cpp deleted file mode 100644 index 45fa74e..0000000 --- a/project2/common/variables-modfixed.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "variables-modfixed.h" - -VariableFixed::VariableFixed(VariableType v) : - var(v) -{ -} - -VariableType -VariableFixed::value() const -{ - return var; -} - diff --git a/project2/common/variables-modfixed.h b/project2/common/variables-modfixed.h deleted file mode 100644 index eceb548..0000000 --- a/project2/common/variables-modfixed.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef VARIABLES_MODFIXED_H -#define VARIABLES_MODFIXED_H - -#include "variables.h" - -/// Variable implementation which has some fixed value -class VariableFixed : public VariableImpl { - public: - VariableFixed(VariableType v); - VariableType value() const; - - private: - VariableType var; -}; - -#endif - diff --git a/project2/common/variables-modliteral.cpp b/project2/common/variables-modliteral.cpp deleted file mode 100644 index 33c9ad5..0000000 --- a/project2/common/variables-modliteral.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include "variables-modliteral.h" -#include "scripts.h" -#include -#include -#include - -/// 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 -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, &vals, _1), - boost::bind(&append, &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, VariableLoader::For, VariableLoader); - - diff --git a/project2/common/variables-modliteral.h b/project2/common/variables-modliteral.h deleted file mode 100644 index 77a43cc..0000000 --- a/project2/common/variables-modliteral.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef VARIABLES_MODLITERAL_H -#define VARIABLES_MODLITERAL_H - -#include "variables.h" -#include - -class VariableLiteral : public VariableImpl { - public: - VariableLiteral(const Glib::ustring & src, const VT_typeID format = DefaultType); - VariableLiteral(ScriptNodePtr); - virtual VariableType value() const; - class Part; - typedef boost::intrusive_ptr PartCPtr; - typedef std::list Vals; - - 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 Glib::ustring & e); - void appendTo(Glib::ustring & str) const; - operator VariableType() const; - const Glib::ustring txt; - }; - class VarPart : public Part, public Variable { - public: - VarPart(ScriptNodePtr e); - void appendTo(Glib::ustring & str) const; - operator VariableType() const; - }; - private: - VariableType val; - Vals vals; -}; - -#endif - diff --git a/project2/common/variables-modlocalparam.cpp b/project2/common/variables-modlocalparam.cpp deleted file mode 100644 index 38cfbba..0000000 --- a/project2/common/variables-modlocalparam.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include "variables.h" -#include "scriptLoader.h" -#include "logger.h" -#include "scriptStorage.h" -#include "iHaveParameters.h" - -/// Variable implementation to access call parameters -class VariableLocalParam : public VariableImplDyn { - public: - VariableLocalParam(ScriptNodePtr e) : - VariableImplDyn(e), - name(e->value("name").as()) - { - } - VariableType value() const - { - try { - return IHaveParameters::getScopedParameter(name); - } - catch (ParamNotFound) { - if (!defaultValue) { - throw; - } - return (*defaultValue)(); - } - } - private: - const Glib::ustring name; -}; -DECLARE_COMPONENT_LOADER("local", VariableLocalParam, VariableLoader); - diff --git a/project2/common/variables-modlookup.cpp b/project2/common/variables-modlookup.cpp deleted file mode 100644 index 64ff93d..0000000 --- a/project2/common/variables-modlookup.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include "variables.h" -#include "safeMapFind.h" -#include "logger.h" -#include "rowProcessor.h" -#include "rowSet.h" -#include -#include "scriptLoader.h" -#include "scriptStorage.h" - - -/// Variable implementation that looks up it's value in a map of key(s)/value pairs -class VariableLookup : public VariableImplDyn, public RowProcessor { - private: - typedef std::vector Key; - typedef std::map Map; - public: - class NotFound : public std::runtime_error { - public: - NotFound(const Key & k) : - std::runtime_error(mklist(k)) { - } - static std::string mklist(const Key & k) { - std::string l("("); - for (Key::const_iterator kp = k.begin(); kp != k.end(); ++kp) { - if (kp != k.begin()) l += ", "; - l += kp->operator const std::string &(); - } - l += ")"; - return l; - } - }; - VariableLookup(ScriptNodePtr e) : - VariableImplDyn(e), - RowProcessor(e), - name(e->value("name").as()) - { - e->script->loader.addLoadTarget(e, Storer::into(&rowSets)); - } - VariableType value() const - { - if (map.empty()) { - fillCache(); - } - Key k; - k.reserve(parameters.size()); - BOOST_FOREACH(const Parameters::value_type & p, parameters) { - k.push_back(p.second); - } - return safeMapLookup(map, k); - } - private: - void fillCache() const - { - BOOST_FOREACH(const RowSets::value_type & rs, rowSets) { - rs->execute(filter, this); - } - Logger()->messagef(LOG_DEBUG, "%s: %s has filled cached with %zu items", - __PRETTY_FUNCTION__, name.c_str(), map.size()); - } - void rowReady(const RowState * rs) const - { - Key k; - BOOST_FOREACH(const Parameters::value_type & p, parameters) { - k.push_back(rs->getCurrentValue(p.first)); - } - map[k] = rs->getCurrentValue(name); - } - mutable Map map; - typedef ANONSTORAGEOF(RowSet) RowSets; - RowSets rowSets; - const Glib::ustring name; -}; -DECLARE_COMPONENT_LOADER("lookup", VariableLookup, VariableLoader); diff --git a/project2/common/variables-modparam.cpp b/project2/common/variables-modparam.cpp deleted file mode 100644 index 3019b96..0000000 --- a/project2/common/variables-modparam.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include "variables.h" -#include "scriptLoader.h" -#include "scriptStorage.h" -#include "appEngine.h" - -/// Variable implementation to access call parameters -class VariableParam : public VariableImplDyn { - public: - VariableParam(ScriptNodePtr e) : - VariableImplDyn(e), - name(e->value("name").as()) - { - } - VariableType value() const - { - try { - return ApplicationEngine::getCurrent()->env()->getParamQuery(name); - } - catch (ParamNotFound) { - if (!defaultValue) { - throw; - } - return (*defaultValue)(); - } - } - private: - const Glib::ustring name; -}; -DECLARE_COMPONENT_LOADER("param", VariableParam, VariableLoader); - diff --git a/project2/common/variables-modsession.cpp b/project2/common/variables-modsession.cpp deleted file mode 100644 index 551f448..0000000 --- a/project2/common/variables-modsession.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include "variables.h" -#include "scriptLoader.h" -#include "scriptStorage.h" -#include "appEngine.h" - -/// Variable implementation to access session contents -class VariableSession : public VariableImplDyn { - public: - VariableSession(ScriptNodePtr e) : - VariableImplDyn(e), - name(e->value("name").as()) - { - } - VariableType value() const - { - try { - return ApplicationEngine::getCurrent()->session()->GetValue(name); - } - catch (Session::VariableNotFound) { - if (!defaultValue) { - throw; - } - return (*defaultValue)(); - } - } - private: - const Glib::ustring name; -}; -DECLARE_COMPONENT_LOADER("session", VariableSession, VariableLoader); - diff --git a/project2/common/variables-moduri.cpp b/project2/common/variables-moduri.cpp deleted file mode 100644 index 81b8698..0000000 --- a/project2/common/variables-moduri.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include "variables.h" -#include "scriptLoader.h" -#include "scriptStorage.h" -#include "appEngine.h" - -/// Variable implementation to access URI path fragments -class VariableUri : public VariableImplDyn { - public: - VariableUri(ScriptNodePtr e) : - VariableImplDyn(e), - index(e, "index") - { - } - VariableType value() const - { - try { - return ApplicationEngine::getCurrent()->env()->getParamUri(index()); - } - catch (UriElementOutOfRange) { - if (!defaultValue) { - throw; - } - return (*defaultValue)(); - } - } - private: - Variable index; -}; -DECLARE_COMPONENT_LOADER("uri", VariableUri, VariableLoader); - diff --git a/project2/common/variables.cpp b/project2/common/variables.cpp index 4b42aa3..7dcf243 100644 --- a/project2/common/variables.cpp +++ b/project2/common/variables.cpp @@ -1,7 +1,7 @@ #include #include "logger.h" #include "variables.h" -#include "variables-modfixed.h" +#include "variables/fixed.h" #include "iHaveParameters.h" #include "scriptLoader.h" #include "exceptions.h" diff --git a/project2/common/variables/config.cpp b/project2/common/variables/config.cpp new file mode 100644 index 0000000..67014c4 --- /dev/null +++ b/project2/common/variables/config.cpp @@ -0,0 +1,87 @@ +#include +#include "../variables.h" +#include "../scriptLoader.h" +#include "../scriptStorage.h" +#include +#include "../appEngine.h" + +typedef std::map ConfigOptions; +static ConfigOptions cfgOpts; + +SimpleMessageException(NoSuchConfigurationValue); + +/// Variable implementation to access platform configuration values +class VariableConfig : public VariableImplDyn { + public: + VariableConfig(ScriptNodePtr e) : + VariableImplDyn(e), + name(e->value("name").as()) + { + } + VariableType value() const + { + const ConfigOptions::const_iterator i = cfgOpts.find(name); + if (i != cfgOpts.end()) { + return i->second; + } + if (defaultValue) { + return defaultValue.get()(); + } + throw NoSuchConfigurationValue(name); + } + private: + const Glib::ustring name; +}; + +class VariableConfigLoader : public VariableLoader::For { + public: + class AppSettings : public Options::Option { + public: + void reset() const { + cfgOpts.clear(); + } + void consume(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const { + if (boost::algorithm::starts_with(n, "application.")) { + Glib::ustring k(n.substr(12)); + const ConfigOptions::iterator i = cfgOpts.find(k); + if (i == cfgOpts.end()) { + if (p.empty() || p == Environment::getCurrent()->platform()) { + cfgOpts.insert(ConfigOptions::value_type(k, v)); + } + } + else { + if (p == Environment::getCurrent()->platform()) { + i->second = v; + } + } + } + } + bool accepts(const Glib::ustring & n) const { + return (boost::algorithm::starts_with(n, "application.")); + } + bool paramRequired() const { + return true; + } + Glib::ustring name() const { + return "application.*"; + } + Glib::ustring description() const { + return "Load settings into the client application"; + } + }; + VariableConfigLoader() : + opts("Variables - ModConfig options") + { + opts(new AppSettings()); + } + + const Options * options() const { + return &opts; + } + + private: + Options opts; +}; + +DECLARE_CUSTOM_COMPONENT_LOADER("config", VariableConfigLoader, VariableConfigLoader, VariableLoader); + diff --git a/project2/common/variables/fixed.cpp b/project2/common/variables/fixed.cpp new file mode 100644 index 0000000..fdb67ed --- /dev/null +++ b/project2/common/variables/fixed.cpp @@ -0,0 +1,13 @@ +#include "fixed.h" + +VariableFixed::VariableFixed(VariableType v) : + var(v) +{ +} + +VariableType +VariableFixed::value() const +{ + return var; +} + diff --git a/project2/common/variables/fixed.h b/project2/common/variables/fixed.h new file mode 100644 index 0000000..ec8be1a --- /dev/null +++ b/project2/common/variables/fixed.h @@ -0,0 +1,17 @@ +#ifndef VARIABLES_MODFIXED_H +#define VARIABLES_MODFIXED_H + +#include "../variables.h" + +/// Variable implementation which has some fixed value +class VariableFixed : public VariableImpl { + public: + VariableFixed(VariableType v); + VariableType value() const; + + private: + VariableType var; +}; + +#endif + diff --git a/project2/common/variables/literal.cpp b/project2/common/variables/literal.cpp new file mode 100644 index 0000000..11fb78a --- /dev/null +++ b/project2/common/variables/literal.cpp @@ -0,0 +1,78 @@ +#include "literal.h" +#include "../scripts.h" +#include +#include + +/// 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 +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, &vals, _1), + boost::bind(&append, &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, VariableLoader::For, VariableLoader); + + + diff --git a/project2/common/variables/literal.h b/project2/common/variables/literal.h new file mode 100644 index 0000000..4128d1c --- /dev/null +++ b/project2/common/variables/literal.h @@ -0,0 +1,40 @@ +#ifndef VARIABLES_MODLITERAL_H +#define VARIABLES_MODLITERAL_H + +#include "../variables.h" +#include + +class VariableLiteral : public VariableImpl { + public: + VariableLiteral(const Glib::ustring & src, const VT_typeID format = DefaultType); + VariableLiteral(ScriptNodePtr); + virtual VariableType value() const; + class Part; + typedef boost::intrusive_ptr PartCPtr; + typedef std::list Vals; + + 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 Glib::ustring & e); + void appendTo(Glib::ustring & str) const; + operator VariableType() const; + const Glib::ustring txt; + }; + class VarPart : public Part, public Variable { + public: + VarPart(ScriptNodePtr e); + void appendTo(Glib::ustring & str) const; + operator VariableType() const; + }; + private: + VariableType val; + Vals vals; +}; + +#endif + diff --git a/project2/common/variables/localparam.cpp b/project2/common/variables/localparam.cpp new file mode 100644 index 0000000..1b789a7 --- /dev/null +++ b/project2/common/variables/localparam.cpp @@ -0,0 +1,31 @@ +#include +#include "../variables.h" +#include "../scriptLoader.h" +#include "../scriptStorage.h" +#include "../iHaveParameters.h" + +/// Variable implementation to access call parameters +class VariableLocalParam : public VariableImplDyn { + public: + VariableLocalParam(ScriptNodePtr e) : + VariableImplDyn(e), + name(e->value("name").as()) + { + } + VariableType value() const + { + try { + return IHaveParameters::getScopedParameter(name); + } + catch (ParamNotFound) { + if (!defaultValue) { + throw; + } + return (*defaultValue)(); + } + } + private: + const Glib::ustring name; +}; +DECLARE_COMPONENT_LOADER("local", VariableLocalParam, VariableLoader); + diff --git a/project2/common/variables/lookup.cpp b/project2/common/variables/lookup.cpp new file mode 100644 index 0000000..892968e --- /dev/null +++ b/project2/common/variables/lookup.cpp @@ -0,0 +1,74 @@ +#include +#include "../variables.h" +#include "../safeMapFind.h" +#include "../logger.h" +#include "../rowProcessor.h" +#include "../rowSet.h" +#include +#include "../scriptLoader.h" +#include "../scriptStorage.h" + + +/// Variable implementation that looks up it's value in a map of key(s)/value pairs +class VariableLookup : public VariableImplDyn, public RowProcessor { + private: + typedef std::vector Key; + typedef std::map Map; + public: + class NotFound : public std::runtime_error { + public: + NotFound(const Key & k) : + std::runtime_error(mklist(k)) { + } + static std::string mklist(const Key & k) { + std::string l("("); + for (Key::const_iterator kp = k.begin(); kp != k.end(); ++kp) { + if (kp != k.begin()) l += ", "; + l += kp->operator const std::string &(); + } + l += ")"; + return l; + } + }; + VariableLookup(ScriptNodePtr e) : + VariableImplDyn(e), + RowProcessor(e), + name(e->value("name").as()) + { + e->script->loader.addLoadTarget(e, Storer::into(&rowSets)); + } + VariableType value() const + { + if (map.empty()) { + fillCache(); + } + Key k; + k.reserve(parameters.size()); + BOOST_FOREACH(const Parameters::value_type & p, parameters) { + k.push_back(p.second); + } + return safeMapLookup(map, k); + } + private: + void fillCache() const + { + BOOST_FOREACH(const RowSets::value_type & rs, rowSets) { + rs->execute(filter, this); + } + Logger()->messagef(LOG_DEBUG, "%s: %s has filled cached with %zu items", + __PRETTY_FUNCTION__, name.c_str(), map.size()); + } + void rowReady(const RowState * rs) const + { + Key k; + BOOST_FOREACH(const Parameters::value_type & p, parameters) { + k.push_back(rs->getCurrentValue(p.first)); + } + map[k] = rs->getCurrentValue(name); + } + mutable Map map; + typedef ANONSTORAGEOF(RowSet) RowSets; + RowSets rowSets; + const Glib::ustring name; +}; +DECLARE_COMPONENT_LOADER("lookup", VariableLookup, VariableLoader); diff --git a/project2/common/variables/param.cpp b/project2/common/variables/param.cpp new file mode 100644 index 0000000..7292b0a --- /dev/null +++ b/project2/common/variables/param.cpp @@ -0,0 +1,31 @@ +#include +#include "../variables.h" +#include "../scriptLoader.h" +#include "../scriptStorage.h" +#include "../appEngine.h" + +/// Variable implementation to access call parameters +class VariableParam : public VariableImplDyn { + public: + VariableParam(ScriptNodePtr e) : + VariableImplDyn(e), + name(e->value("name").as()) + { + } + VariableType value() const + { + try { + return ApplicationEngine::getCurrent()->env()->getParamQuery(name); + } + catch (ParamNotFound) { + if (!defaultValue) { + throw; + } + return (*defaultValue)(); + } + } + private: + const Glib::ustring name; +}; +DECLARE_COMPONENT_LOADER("param", VariableParam, VariableLoader); + diff --git a/project2/common/variables/session.cpp b/project2/common/variables/session.cpp new file mode 100644 index 0000000..fc33d8e --- /dev/null +++ b/project2/common/variables/session.cpp @@ -0,0 +1,31 @@ +#include +#include "../variables.h" +#include "../scriptLoader.h" +#include "../scriptStorage.h" +#include "../appEngine.h" + +/// Variable implementation to access session contents +class VariableSession : public VariableImplDyn { + public: + VariableSession(ScriptNodePtr e) : + VariableImplDyn(e), + name(e->value("name").as()) + { + } + VariableType value() const + { + try { + return ApplicationEngine::getCurrent()->session()->GetValue(name); + } + catch (Session::VariableNotFound) { + if (!defaultValue) { + throw; + } + return (*defaultValue)(); + } + } + private: + const Glib::ustring name; +}; +DECLARE_COMPONENT_LOADER("session", VariableSession, VariableLoader); + diff --git a/project2/common/variables/uri.cpp b/project2/common/variables/uri.cpp new file mode 100644 index 0000000..c5e596f --- /dev/null +++ b/project2/common/variables/uri.cpp @@ -0,0 +1,31 @@ +#include +#include "../variables.h" +#include "../scriptLoader.h" +#include "../scriptStorage.h" +#include "../appEngine.h" + +/// Variable implementation to access URI path fragments +class VariableUri : public VariableImplDyn { + public: + VariableUri(ScriptNodePtr e) : + VariableImplDyn(e), + index(e, "index") + { + } + VariableType value() const + { + try { + return ApplicationEngine::getCurrent()->env()->getParamUri(index()); + } + catch (UriElementOutOfRange) { + if (!defaultValue) { + throw; + } + return (*defaultValue)(); + } + } + private: + Variable index; +}; +DECLARE_COMPONENT_LOADER("uri", VariableUri, VariableLoader); + diff --git a/project2/xml/xmlScriptParser.cpp b/project2/xml/xmlScriptParser.cpp index 71c4bb5..abdffdd 100644 --- a/project2/xml/xmlScriptParser.cpp +++ b/project2/xml/xmlScriptParser.cpp @@ -2,7 +2,7 @@ #include "xmlScriptParser.h" #include "scripts.h" #include "commonObjects.h" -#include "variables-modliteral.h" +#include "variables/literal.h" #include static const std::string XIncludeNS("http://www.w3.org/2001/XInclude"); -- cgit v1.2.3