From 313c4560ed8a12db4517da1fc0946bce0fff6310 Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 22 Dec 2011 17:25:41 +0000 Subject: Generalise code from structuredExceptionHandler for loaded sub component sets and use it to standardise sqlTask's conditional children --- project2/common/scriptLoader.cpp | 29 +++++++++++++++++++++++++++-- project2/common/scriptLoader.h | 3 ++- project2/common/scriptStorage.h | 20 ++++++++++---------- project2/common/structExceptHandling.cpp | 32 +++----------------------------- project2/sql/sqlTask.cpp | 22 ++-------------------- 5 files changed, 44 insertions(+), 62 deletions(-) diff --git a/project2/common/scriptLoader.cpp b/project2/common/scriptLoader.cpp index 42f826e..6a0f0b3 100644 --- a/project2/common/scriptLoader.cpp +++ b/project2/common/scriptLoader.cpp @@ -41,7 +41,32 @@ LoaderBase::addLoadTarget(ScriptNodePtr src, boost::intrusive_ptr target targets[src].push_back(target); } - std::set > * & +void +LoaderBase::addLoadTargetSub(ScriptNodePtr src, const Glib::ustring & name, bool required, boost::intrusive_ptr target) +{ + if (ScriptNodePtr c = getSub(src, name, required)) { + src->script->loader.addLoadTarget(c, target); + } +} + +ScriptNodePtr +LoaderBase::getSub(ScriptNodePtr root, const Glib::ustring & name, bool required) +{ + if (required) { + return root->child(name); + } + else { + try { + return root->child(name); + } + catch (const ValueNotFound &) { + return NULL; + } + } +} + + +std::set > * & LoaderBase::componentLoaders() { static std::set > * _compLoaders = NULL; @@ -73,7 +98,7 @@ LoaderBase::collectAll(ScriptNodePtr node, bool childrenOnly, const StorerPtrs & throw NotSupported(name); } BOOST_FOREACH(const StorerPtr & s, sts) { - if (s->save(o, node)) { + if (s->save(o, name)) { stored += 1; break; } diff --git a/project2/common/scriptLoader.h b/project2/common/scriptLoader.h index d916821..6878983 100644 --- a/project2/common/scriptLoader.h +++ b/project2/common/scriptLoader.h @@ -13,7 +13,6 @@ #include #include -enum UnsupportedHandling { ErrorOnUnsupported, WarnOnUnsupported, IgnoreUnsupported }; class ElementLoader; class ComponentLoader; class CommonObjects; @@ -31,6 +30,7 @@ class LoaderBase { void collectAll(const CommonObjects * co, bool childrenOnly, ScriptNodePtr script); void addLoadTarget(ScriptNodePtr src, boost::intrusive_ptr target); + void addLoadTargetSub(ScriptNodePtr src, const Glib::ustring & name, bool required, boost::intrusive_ptr target); static void onAllComponents(const boost::function1 &); @@ -86,6 +86,7 @@ class LoaderBase { private: void collectAll(ScriptNodePtr script, bool childrenOnly, const StorerPtrs & sts) const; + static ScriptNodePtr getSub(ScriptNodePtr root, const Glib::ustring & name, bool required); Targets targets; static unsigned int depth; template friend class ElementLoaderImpl; diff --git a/project2/common/scriptStorage.h b/project2/common/scriptStorage.h index d5daf95..0187cb4 100644 --- a/project2/common/scriptStorage.h +++ b/project2/common/scriptStorage.h @@ -35,7 +35,7 @@ class Storer : public virtual IntrusivePtrBase { static StorerPtr into(ANONORDEREDSTORAGEOF(X) * list); virtual boost::intrusive_ptr create(ScriptNodePtr) const = 0; - virtual bool save(boost::intrusive_ptr, ScriptNodePtr) = 0; + virtual bool save(boost::intrusive_ptr, const Glib::ustring &) = 0; }; template @@ -45,23 +45,23 @@ class StorerBase : public Storer { boost::intrusive_ptr create(ScriptNodePtr p) const { return LoaderBase::getLoader(p->get_name())->createFrom(p); } - bool save(boost::intrusive_ptr o, ScriptNodePtr p) { + bool save(boost::intrusive_ptr o, const Glib::ustring & name) { boost::intrusive_ptr O = boost::dynamic_pointer_cast(o); if (O) { - if (!insert(p, O)) { - throw StoreFailed(p->get_name()); + if (!insert(O)) { + throw StoreFailed(name); } } return O; } - virtual bool insert(ScriptNodePtr, boost::intrusive_ptr) = 0; + virtual bool insert(boost::intrusive_ptr) = 0; }; template class StorerImpl : public StorerBase { public: StorerImpl(M * m); - bool insert(ScriptNodePtr, boost::intrusive_ptr O); + bool insert(boost::intrusive_ptr O); }; template class StorerImpl : public StorerBase { @@ -70,7 +70,7 @@ class StorerImpl : public StorerBase { StorerImpl(SINGLE(X) * o) : obj(o) { } - bool insert(ScriptNodePtr, boost::intrusive_ptr O) + bool insert(boost::intrusive_ptr O) { *obj = O; return true; @@ -84,7 +84,7 @@ class StorerImpl : public StorerBase { StorerImpl(STORAGEOF(X) * m) : map(m) { } - bool insert(ScriptNodePtr, boost::intrusive_ptr O) + bool insert(boost::intrusive_ptr O) { return map->insert(typename Map::value_type(O->name, O)).second; } @@ -97,7 +97,7 @@ class StorerImpl : public StorerBase O) + bool insert(boost::intrusive_ptr O) { map->insert(O); return true; @@ -111,7 +111,7 @@ class StorerImpl : public StorerBase O) + bool insert(boost::intrusive_ptr O) { map->push_back(O); return true; diff --git a/project2/common/structExceptHandling.cpp b/project2/common/structExceptHandling.cpp index 3820ce8..0881d6d 100644 --- a/project2/common/structExceptHandling.cpp +++ b/project2/common/structExceptHandling.cpp @@ -7,39 +7,13 @@ DECLARE_LOADER("handler", StructuredExceptionHandler); -static -ScriptNodePtr -loadHelperSub(const char * name, bool required, ScriptNodePtr root) -{ - if (required) { - return root->child(name); - } - else { - try { - return root->child(name); - } - catch (const ValueNotFound &) { - return NULL; - } - } -} -static -void -loadHelper(const char * name, bool required, ScriptNodePtr root, ANONORDEREDSTORAGEOF(NoOutputExecute) * noes) -{ - ScriptNodePtr c = loadHelperSub(name, required, root); - if (c) { - root->script->loader.addLoadTarget(c, Storer::into(noes)); - } -} - StructuredExceptionHandler::StructuredExceptionHandler(ScriptNodePtr e) : SourceObject(e), IHaveSubTasks(e) { - loadHelper("try", true, e, &normal); - loadHelper("catch", false, e, &catches); - loadHelper("finally", false, e, &finallies); + e->script->loader.addLoadTargetSub(e, "try", true, Storer::into(&normal)); + e->script->loader.addLoadTargetSub(e, "catch", false, Storer::into(&catches)); + e->script->loader.addLoadTargetSub(e, "finally", false, Storer::into(&finallies)); } void diff --git a/project2/sql/sqlTask.cpp b/project2/sql/sqlTask.cpp index 6855cc0..852069d 100644 --- a/project2/sql/sqlTask.cpp +++ b/project2/sql/sqlTask.cpp @@ -10,25 +10,6 @@ DECLARE_LOADER("sqltask", SqlTask); StaticMessageException(RunOnNotSpecified, "runon attribute must be specified"); -class SqlIfChangesStorer : public StorerBase { - public: - SqlIfChangesStorer(Map c, Map nc) : - changes(c), - noChanges(nc) - { - } - bool insert(ScriptNodePtr p, NoOutputExecutePtr O) { - try { - ((p->value("runon").as() == "changes") ? changes : noChanges)->push_back(O); - return true; - } - catch (const ValueNotFound &) { - throw RunOnNotSpecified(); - } - } - Map changes, noChanges; -}; - SqlTask::SqlTask(ScriptNodePtr p) : SourceObject(p), Task(p), @@ -36,7 +17,8 @@ SqlTask::SqlTask(ScriptNodePtr p) : filter(p, "filter", ""), sqlCommand(p->child("sql")) { - p->script->loader.addLoadTarget(p, new SqlIfChangesStorer(&changesNOEs, &noChangesNOEs)); + p->script->loader.addLoadTargetSub(p, "changes", false, Storer::into(&changesNOEs)); + p->script->loader.addLoadTargetSub(p, "nochanges", false, Storer::into(&noChangesNOEs)); } SqlTask::~SqlTask() -- cgit v1.2.3