From 0fd4f3740a2581963643292e6b9f201ee0aa9466 Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 29 Nov 2011 13:22:48 +0000 Subject: Allow specifying different loader classes when setting up script parsers (allows defining presenter modules from within a script) --- project2/common/checkHost.cpp | 2 +- project2/common/commonObjects.cpp | 2 +- project2/common/if.cpp | 6 +-- project2/common/iterate.cpp | 2 +- project2/common/rowProcessor.cpp | 2 +- project2/common/rowView.cpp | 2 +- project2/common/structExceptHandling.cpp | 2 +- project2/common/taskHost.cpp | 2 +- project2/common/variables-modlookup.cpp | 2 +- project2/common/viewHost.cpp | 4 +- project2/common/xmlObjectLoader.cpp | 12 +++--- project2/common/xmlObjectLoader.h | 4 +- project2/common/xmlScriptParser.cpp | 2 +- project2/common/xmlStorage.h | 68 +++++++++++++++++--------------- project2/sql/sqlMergeTask.cpp | 2 +- project2/sql/sqlTask.cpp | 2 +- 16 files changed, 62 insertions(+), 54 deletions(-) diff --git a/project2/common/checkHost.cpp b/project2/common/checkHost.cpp index 7f908c0..343b255 100644 --- a/project2/common/checkHost.cpp +++ b/project2/common/checkHost.cpp @@ -6,7 +6,7 @@ CheckHost::CheckHost(const boost::filesystem::path & file) : XmlScriptParser(file, false) { - loader.supportedStorers.insert(Storer::into(¶meterChecks)); + loader.supportedStorers.insert(Storer::into(¶meterChecks)); } CheckHost::~CheckHost() diff --git a/project2/common/commonObjects.cpp b/project2/common/commonObjects.cpp index 8ad1a97..4ce8c82 100644 --- a/project2/common/commonObjects.cpp +++ b/project2/common/commonObjects.cpp @@ -22,7 +22,7 @@ CommonObjects::loadDataSource(const std::string & name) const Environment::getCurrent()->datasourceRoot, name), true); LoaderBase loader(true); - loader.supportedStorers.insert(Storer::into(&datasources)); + loader.supportedStorers.insert(Storer::into(&datasources)); loader.collectAll(xml.get_document()->get_root_node(), false); return safeMapFind(datasources, name); diff --git a/project2/common/if.cpp b/project2/common/if.cpp index f3c7102..a810146 100644 --- a/project2/common/if.cpp +++ b/project2/common/if.cpp @@ -14,7 +14,7 @@ IfSet::IfSet(const xmlpp::Element * e) : mode(e->get_attribute_value("mode") == "or" ? Or : And) { LoaderBase loader(true); - loader.supportedStorers.insert(Storer::into(&checks)); + loader.supportedStorers.insert(Storer::into(&checks)); loader.collectAll(e, true, IgnoreUnsupported); } @@ -48,8 +48,8 @@ If::If(const xmlpp::Element * e) : IfSet(e) { LoaderBase loader(true); - loader.supportedStorers.insert(Storer::into(&normal)); - loader.supportedStorers.insert(Storer::into(&subViews)); + loader.supportedStorers.insert(Storer::into(&normal)); + loader.supportedStorers.insert(Storer::into(&subViews)); loader.collectAll(e, true, IgnoreUnsupported); } diff --git a/project2/common/iterate.cpp b/project2/common/iterate.cpp index ddde42c..ba8a4b9 100644 --- a/project2/common/iterate.cpp +++ b/project2/common/iterate.cpp @@ -14,7 +14,7 @@ Iterate::Iterate(const xmlpp::Element * p) : RowProcessor(p) { LoaderBase loader(true); - loader.supportedStorers.insert(Storer::into(&normal)); + loader.supportedStorers.insert(Storer::into(&normal)); loader.collectAll(p, true, IgnoreUnsupported); } diff --git a/project2/common/rowProcessor.cpp b/project2/common/rowProcessor.cpp index b2d3cbd..e186c43 100644 --- a/project2/common/rowProcessor.cpp +++ b/project2/common/rowProcessor.cpp @@ -13,7 +13,7 @@ RowProcessor::RowProcessor(const xmlpp::Element * p) : IRSE(Variable(p, "ignoreRowSourceError", false, false)()) { LoaderBase loader(true); - loader.supportedStorers.insert(Storer::into(&caches)); + loader.supportedStorers.insert(Storer::into(&caches)); loader.collectAll(p, true, IgnoreUnsupported); } diff --git a/project2/common/rowView.cpp b/project2/common/rowView.cpp index a35619d..6b0bbed 100644 --- a/project2/common/rowView.cpp +++ b/project2/common/rowView.cpp @@ -23,7 +23,7 @@ RowView::RowView(const xmlpp::Element * p) : } } LoaderBase loader(true); - loader.supportedStorers.insert(Storer::into(&subViews)); + loader.supportedStorers.insert(Storer::into(&subViews)); loader.collectAll(p, true, IgnoreUnsupported); } diff --git a/project2/common/structExceptHandling.cpp b/project2/common/structExceptHandling.cpp index 5b5f1f5..0f176c9 100644 --- a/project2/common/structExceptHandling.cpp +++ b/project2/common/structExceptHandling.cpp @@ -10,7 +10,7 @@ static void loadHelper(const char * name, const xmlpp::Element * root, ANONORDEREDSTORAGEOF(NoOutputExecute) * noes) { LoaderBase loader(true); - loader.supportedStorers.insert(Storer::into(noes)); + loader.supportedStorers.insert(Storer::into(noes)); BOOST_FOREACH(const xmlpp::Node * node, root->find(name)) { const xmlpp::Element * elem = dynamic_cast(node); if (elem) { diff --git a/project2/common/taskHost.cpp b/project2/common/taskHost.cpp index cecc883..98a1fa5 100644 --- a/project2/common/taskHost.cpp +++ b/project2/common/taskHost.cpp @@ -11,7 +11,7 @@ TaskHost::TaskHost(const boost::filesystem::path & file) : CheckHost(file), IHaveSubTasks(get_document()->get_root_node()) { - loader.supportedStorers.insert(Storer::into(&tasks)); + loader.supportedStorers.insert(Storer::into(&tasks)); } TaskHost::~TaskHost() diff --git a/project2/common/variables-modlookup.cpp b/project2/common/variables-modlookup.cpp index 408c98b..82c3539 100644 --- a/project2/common/variables-modlookup.cpp +++ b/project2/common/variables-modlookup.cpp @@ -36,7 +36,7 @@ class VariableLookup : public VariableImplDyn, public RowProcessor { name(e->get_attribute_value("name")) { LoaderBase loader(true); - loader.supportedStorers.insert(Storer::into(&rowSets)); + loader.supportedStorers.insert(Storer::into(&rowSets)); loader.collectAll(e, false); } VariableType value() const diff --git a/project2/common/viewHost.cpp b/project2/common/viewHost.cpp index 01cf966..9d3c14b 100644 --- a/project2/common/viewHost.cpp +++ b/project2/common/viewHost.cpp @@ -10,8 +10,8 @@ ViewHost::ViewHost(const boost::filesystem::path & file) : XmlScriptParser(file, false), CheckHost(file) { - loader.supportedStorers.insert(Storer::into(&views)); - loader.supportedStorers.insert(Storer::into(&presenter)); + loader.supportedStorers.insert(Storer::into(&views)); + loader.supportedStorers.insert(Storer::into(&presenter)); } ViewHost::~ViewHost() diff --git a/project2/common/xmlObjectLoader.cpp b/project2/common/xmlObjectLoader.cpp index 6859f22..0e55cb4 100644 --- a/project2/common/xmlObjectLoader.cpp +++ b/project2/common/xmlObjectLoader.cpp @@ -30,14 +30,14 @@ LoaderBase::LoaderBase(bool r) : recursive(r), ns(Environment::getCurrent()->xmlNamespace) { - supportedStorers.insert(Storer::into(&libraries)); + supportedStorers.insert(Storer::into(&libraries)); } LoaderBase::LoaderBase(const Glib::ustring & n, bool r) : recursive(r), ns(n) { - supportedStorers.insert(Storer::into(&libraries)); + supportedStorers.insert(Storer::into(&libraries)); } LoaderBase::~LoaderBase() @@ -65,11 +65,13 @@ LoaderBase::collectAll(const xmlpp::Element * node, bool childrenOnly, Unsupport if (!childrenOnly && node->get_namespace_uri() == ns) { Glib::ustring name = node->get_name(); unsigned int stored = 0; - SourceObjectPtr o = getLoader(name)->go(node); created += 1; - loadedObjects.insert(o); BOOST_FOREACH(std::set >::value_type s, supportedStorers) { - if (s->save(o, node)) { + boost::intrusive_ptr o = s->save(node); + if (boost::intrusive_ptr t = boost::dynamic_pointer_cast(o)) { + loadedObjects.insert(t); + } + if (o) { stored += 1; } } diff --git a/project2/common/xmlObjectLoader.h b/project2/common/xmlObjectLoader.h index 4795717..202dbeb 100644 --- a/project2/common/xmlObjectLoader.h +++ b/project2/common/xmlObjectLoader.h @@ -120,14 +120,14 @@ class ComponentLoader { /// Helper for loading and maintaining Project2 script components class ElementLoader : public ComponentLoader { public: - virtual SourceObjectPtr go(const xmlpp::Element * xml) const = 0; + virtual SourceObjectPtr createFrom(const xmlpp::Element * xml) const = 0; }; /// Helper for loading and maintaining Project2 script components (typed implementation) template class ElementLoaderImpl : public ElementLoader { public: - SourceObjectPtr go(const xmlpp::Element * xml) const + SourceObjectPtr createFrom(const xmlpp::Element * xml) const { return new X(xml); } diff --git a/project2/common/xmlScriptParser.cpp b/project2/common/xmlScriptParser.cpp index 762618a..1db1841 100644 --- a/project2/common/xmlScriptParser.cpp +++ b/project2/common/xmlScriptParser.cpp @@ -33,7 +33,7 @@ XmlScriptParser::loadDocument(const boost::filesystem::path & file) throw IncludesError(file.string()); } } - loader.supportedStorers.insert(Storer::into(&rowSets)); + loader.supportedStorers.insert(Storer::into(&rowSets)); } void diff --git a/project2/common/xmlStorage.h b/project2/common/xmlStorage.h index a700c64..ceae899 100644 --- a/project2/common/xmlStorage.h +++ b/project2/common/xmlStorage.h @@ -2,6 +2,7 @@ #define XMLSTORAGE_H #include "sourceObject.h" +#include "xmlObjectLoader.h" #include "exceptions.h" #include #include @@ -23,43 +24,48 @@ class Storer; typedef boost::intrusive_ptr StorerPtr; class Storer : public virtual IntrusivePtrBase { public: - template + template static StorerPtr into(SINGLE(X) * obj); - template + template static StorerPtr into(STORAGEOF(X) * map); - template + template static StorerPtr into(ANONSTORAGEOF(X) * set); - template + template static StorerPtr into(ANONORDEREDSTORAGEOF(X) * list); - virtual bool save(SourceObjectPtr o, const xmlpp::Element *) = 0; + virtual boost::intrusive_ptr save(const xmlpp::Element *) = 0; }; -template +template class StorerBase : public Storer { public: typedef M * Map; - bool save(SourceObjectPtr obj, const xmlpp::Element * p) { - boost::intrusive_ptr O = boost::dynamic_pointer_cast(obj); - if (O) { - if (insert(p, O)) { - return true; + boost::intrusive_ptr save(const xmlpp::Element * p) { + try { + boost::intrusive_ptr O = boost::dynamic_pointer_cast( + LoaderBase::getLoader(p->get_name())->createFrom(p)); + if (O) { + if (!insert(p, O)) { + throw StoreFailed(p->get_attribute_value("name")); + } } - throw StoreFailed(obj->name); + return O; + } + catch (NotSupported) { + return NULL; } - return false; } virtual bool insert(const xmlpp::Element *, boost::intrusive_ptr) = 0; }; -template -class StorerImpl : public StorerBase { +template +class StorerImpl : public StorerBase { public: StorerImpl(M * m); bool insert(const xmlpp::Element *, boost::intrusive_ptr O); }; -template -class StorerImpl : public StorerBase { +template +class StorerImpl : public StorerBase { public: typedef SINGLE(X) Obj; StorerImpl(SINGLE(X) * o) : obj(o) @@ -72,8 +78,8 @@ class StorerImpl : public StorerBase { } Obj * obj; }; -template -class StorerImpl : public StorerBase { +template +class StorerImpl : public StorerBase { public: typedef STORAGEOF(X) Map; StorerImpl(STORAGEOF(X) * m) : map(m) @@ -85,8 +91,8 @@ class StorerImpl : public StorerBase { } Map * map; }; -template -class StorerImpl : public StorerBase { +template +class StorerImpl : public StorerBase { public: typedef ANONSTORAGEOF(X) Map; StorerImpl(ANONSTORAGEOF(X) * m) : map(m) @@ -99,8 +105,8 @@ class StorerImpl : public StorerBase { } Map * map; }; -template -class StorerImpl : public StorerBase { +template +class StorerImpl : public StorerBase { public: typedef ANONORDEREDSTORAGEOF(X) Map; StorerImpl(ANONORDEREDSTORAGEOF(X) * m) : map(m) @@ -114,28 +120,28 @@ class StorerImpl : public StorerBase +template StorerPtr Storer::into(SINGLE(X) * obj) { - return new StorerImpl(obj); + return new StorerImpl(obj); } -template +template StorerPtr Storer::into(STORAGEOF(X) * map) { - return new StorerImpl(map); + return new StorerImpl(map); } -template +template StorerPtr Storer::into(ANONSTORAGEOF(X) * set) { - return new StorerImpl(set); + return new StorerImpl(set); } -template +template StorerPtr Storer::into(ANONORDEREDSTORAGEOF(X) * list) { - return new StorerImpl(list); + return new StorerImpl(list); } #endif diff --git a/project2/sql/sqlMergeTask.cpp b/project2/sql/sqlMergeTask.cpp index f98d57e..96a895a 100644 --- a/project2/sql/sqlMergeTask.cpp +++ b/project2/sql/sqlMergeTask.cpp @@ -58,7 +58,7 @@ SqlMergeTask::SqlMergeTask(const xmlpp::Element * p) : dtablet(stringf("tmp_%s_%d", dtable.c_str(), getpid())) { LoaderBase loader(true); - loader.supportedStorers.insert(Storer::into(&sources)); + loader.supportedStorers.insert(Storer::into(&sources)); loader.collectAll(p, true); if (!sources.empty() && useView()) { diff --git a/project2/sql/sqlTask.cpp b/project2/sql/sqlTask.cpp index 63f73a9..9f881de 100644 --- a/project2/sql/sqlTask.cpp +++ b/project2/sql/sqlTask.cpp @@ -10,7 +10,7 @@ DECLARE_LOADER("sqltask", SqlTask); StaticMessageException(RunOnNotSpecified, "runon attribute must be specified"); -class SqlIfChangesStorer : public StorerBase { +class SqlIfChangesStorer : public StorerBase { public: SqlIfChangesStorer(Map c, Map nc) : changes(c), -- cgit v1.2.3