From b383a557107abb1a8684f9233286e35277dc7581 Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 15 Feb 2011 20:18:18 +0000 Subject: Tidied up XML loader using boost::multi_index --- project2/cgi/cgiAppEngine.cpp | 24 ++++----- project2/cgi/cgiAppEngine.h | 2 +- project2/commonObjects.cpp | 6 +-- project2/commonObjects.h | 6 +-- project2/console/consoleAppEngine.cpp | 20 ++++---- project2/console/consoleAppEngine.h | 2 +- project2/dataSource.h | 2 +- project2/if.cpp | 16 +++--- project2/if.h | 2 +- project2/iterate.cpp | 8 +-- project2/iterate.h | 2 +- project2/noOutputExecute.h | 3 +- project2/paramChecker.h | 3 +- project2/presenter.cpp | 4 +- project2/rowSet.cpp | 1 + project2/rowSet.h | 8 +-- project2/rowView.cpp | 2 +- project2/sqlMergeTask.cpp | 11 ++-- project2/streamRows.h | 1 + project2/view.h | 6 +-- project2/xmlObjectLoader.cpp | 2 +- project2/xmlObjectLoader.h | 97 +++++++++++++++-------------------- project2/xmlPresenter.h | 2 +- project2/xslRows.h | 1 + 24 files changed, 110 insertions(+), 121 deletions(-) diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp index 1d00c18..f3f8d56 100644 --- a/project2/cgi/cgiAppEngine.cpp +++ b/project2/cgi/cgiAppEngine.cpp @@ -96,9 +96,9 @@ CgiApplicationEngine::PresentStage::~PresentStage() CgiApplicationEngine::StagePtr CgiApplicationEngine::PresentStage::run() { - BOOST_FOREACH(OrderedParamCheckers::value_type pc, parameterChecks) { - if (!pc.second->performCheck()) { - return new PresentStage(appEngine, pc.second->present); + BOOST_FOREACH(ParamCheckers::value_type pc, parameterChecks.get()) { + if (!pc->performCheck()) { + return new PresentStage(appEngine, pc->present); } } execute(); @@ -164,25 +164,25 @@ CgiApplicationEngine::RequestStage::~RequestStage() CgiApplicationEngine::StagePtr CgiApplicationEngine::RequestStage::run() { - BOOST_FOREACH(OrderedParamCheckers::value_type pc, parameterChecks) { - if (!pc.second->performCheck()) { - return new PresentStage(appEngine, pc.second->present); + BOOST_FOREACH(const ParamCheckers::value_type & pc, parameterChecks.get()) { + if (!pc->performCheck()) { + return new PresentStage(appEngine, pc->present); } } try { - BOOST_FOREACH(NoOutputExecutes::value_type t, tasks) { - t.second->execute(); + BOOST_FOREACH(const NoOutputExecutes::value_type & t, tasks.get()) { + t->execute(); } // Commit data source transactions (without invoking a connection) - BOOST_FOREACH(DataSources::value_type ds, datasources) { - ds.second->commit(); + BOOST_FOREACH(const DataSources::value_type & ds, datasources) { + ds->commit(); } return new PresentStage(appEngine, present); } catch (...) { // Do something about the error - BOOST_FOREACH(DataSources::value_type ds, datasources) { - ds.second->rollback(); + BOOST_FOREACH(const DataSources::value_type & ds, datasources) { + ds->rollback(); } throw; } diff --git a/project2/cgi/cgiAppEngine.h b/project2/cgi/cgiAppEngine.h index 25c6537..2bef0f6 100644 --- a/project2/cgi/cgiAppEngine.h +++ b/project2/cgi/cgiAppEngine.h @@ -65,7 +65,7 @@ class CgiApplicationEngine : public ApplicationEngine { virtual StagePtr run(); std::string present; protected: - OrderedParamCheckers parameterChecks; + ParamCheckers parameterChecks; NoOutputExecutes tasks; }; diff --git a/project2/commonObjects.cpp b/project2/commonObjects.cpp index 90c8bdc..6d8fc08 100644 --- a/project2/commonObjects.cpp +++ b/project2/commonObjects.cpp @@ -3,9 +3,9 @@ RowSetPtr CommonObjects::getSource(const std::string & name) const { - RowSets::const_iterator i = rowSets.find(name); - if (i != rowSets.end()) { - return i->second; + RowSets::index::type::const_iterator i = rowSets.get().find(name); + if (i != rowSets.get().end()) { + return *i; } throw CommonObjects::DataSourceNotFound(); } diff --git a/project2/commonObjects.h b/project2/commonObjects.h index dc522ba..12b1f2b 100644 --- a/project2/commonObjects.h +++ b/project2/commonObjects.h @@ -13,11 +13,11 @@ class CommonObjects : public virtual IntrusivePtrBase { template const DataSourceType * dataSource(const std::string & name) const { - DataSources::const_iterator i = datasources.find(name); - if (i == datasources.end()) { + DataSources::index::type::const_iterator i = datasources.get().find(name); + if (i == datasources.get().end()) { throw DataSourceNotFound(); } - const DataSourceType * s = dynamic_cast(i->second.get()); + const DataSourceType * s = dynamic_cast(i->get()); if (!s) { throw DataSourceNotCompatible(); } diff --git a/project2/console/consoleAppEngine.cpp b/project2/console/consoleAppEngine.cpp index 6053ca9..0d0c71d 100644 --- a/project2/console/consoleAppEngine.cpp +++ b/project2/console/consoleAppEngine.cpp @@ -77,29 +77,29 @@ ConsoleApplicationEngine::~ConsoleApplicationEngine() void ConsoleApplicationEngine::process() const { - BOOST_FOREACH(OrderedParamCheckers::value_type pc, parameterChecks) { - if (!pc.second->performCheck()) { + BOOST_FOREACH(const ParamCheckers::value_type & pc, parameterChecks.get()) { + if (!pc->performCheck()) { throw std::runtime_error("Check failed"); } } try { - BOOST_FOREACH(NoOutputExecutes::value_type t, tasks) { - t.second->execute(); + BOOST_FOREACH(const NoOutputExecutes::value_type & t, tasks.get()) { + t->execute(); } // Commit data source transactions (without invoking a connection) - BOOST_FOREACH(DataSources::value_type ds, this->datasources) { - ds.second->commit(); + BOOST_FOREACH(const DataSources::value_type & ds, this->datasources) { + ds->commit(); } } catch (...) { // Do something about the error - BOOST_FOREACH(DataSources::value_type ds, this->datasources) { - ds.second->rollback(); + BOOST_FOREACH(const DataSources::value_type & ds, this->datasources) { + ds->rollback(); } throw; } - BOOST_FOREACH(Views::value_type v, views) { - v.second->execute(this); + BOOST_FOREACH(const Views::value_type & v, views) { + v->execute(this); } } diff --git a/project2/console/consoleAppEngine.h b/project2/console/consoleAppEngine.h index c18956f..44d90bd 100644 --- a/project2/console/consoleAppEngine.h +++ b/project2/console/consoleAppEngine.h @@ -44,7 +44,7 @@ class ConsoleApplicationEngine : public ApplicationEngine, public Presenter { private: mutable unsigned int indent; - OrderedParamCheckers parameterChecks; + ParamCheckers parameterChecks; NoOutputExecutes tasks; Views views; SessionPtr runtime; diff --git a/project2/dataSource.h b/project2/dataSource.h index 478feb6..890dc9a 100644 --- a/project2/dataSource.h +++ b/project2/dataSource.h @@ -8,7 +8,7 @@ class DataSource; typedef boost::intrusive_ptr DataSourcePtr; -typedef std::map DataSources; +typedef Storage::Objects DataSources; class DataSource : public virtual SourceObject { public: diff --git a/project2/if.cpp b/project2/if.cpp index 9d367bf..8fc210d 100644 --- a/project2/if.cpp +++ b/project2/if.cpp @@ -18,16 +18,16 @@ bool IfSet::passes() const { if (mode == And) { - BOOST_FOREACH(OrderedParamCheckers::value_type pc, checks) { - if (!pc.second->performCheck()) { + BOOST_FOREACH(const ParamCheckers::value_type & pc, checks.get()) { + if (!pc->performCheck()) { return false; } } return true; } else { - BOOST_FOREACH(OrderedParamCheckers::value_type pc, checks) { - if (pc.second->performCheck()) { + BOOST_FOREACH(const ParamCheckers::value_type & pc, checks.get()) { + if (pc->performCheck()) { return true; } } @@ -52,8 +52,8 @@ void If::execute(const Presenter * presenter) const { if (passes()) { Logger()->messagef(LOG_DEBUG, "IfSet passed, showing %zu views", subViews.size()); - BOOST_FOREACH(Views::value_type sq, subViews) { - sq.second->execute(presenter); + BOOST_FOREACH(const Views::value_type & sq, subViews) { + sq->execute(presenter); } } } @@ -62,8 +62,8 @@ void If::execute() const { if (passes()) { Logger()->message(LOG_DEBUG, "IfSet passed"); - BOOST_FOREACH(NoOutputExecutes::value_type sq, subNOEs) { - sq.second->execute(); + BOOST_FOREACH(const NoOutputExecutes::value_type & sq, subNOEs.get()) { + sq->execute(); } } } diff --git a/project2/if.h b/project2/if.h index 403f4b0..8ce08fb 100644 --- a/project2/if.h +++ b/project2/if.h @@ -13,7 +13,7 @@ class IfSet : public virtual IntrusivePtrBase { private: enum Mode { And, Or }; Mode mode; - OrderedParamCheckers checks; + ParamCheckers checks; }; class If : public Iterate, public RowView, public IfSet { diff --git a/project2/iterate.cpp b/project2/iterate.cpp index fa0bd77..fc24ed7 100644 --- a/project2/iterate.cpp +++ b/project2/iterate.cpp @@ -51,14 +51,14 @@ Iterate::execute() const void Iterate::executeChildren() const { - BOOST_FOREACH(NoOutputExecutes::value_type sq, subNOEs) { - if (dynamic_cast(sq.second.get())) { - sq.second->execute(); + BOOST_FOREACH(const NoOutputExecutes::value_type & sq, subNOEs.get()) { + if (dynamic_cast(sq.get())) { + sq->execute(); } else { RowSet::beginRow(NULL); try { - sq.second->execute(); + sq->execute(); RowSet::endRow(NULL); } catch (...) { diff --git a/project2/iterate.h b/project2/iterate.h index 58b4944..0152ffc 100644 --- a/project2/iterate.h +++ b/project2/iterate.h @@ -11,7 +11,7 @@ class Iterate; typedef boost::intrusive_ptr IteratePtr; -typedef std::map Iterates; +typedef Storage::Objects Iterates; class Iterate : public NoOutputExecute, public RowProcessor { public: diff --git a/project2/noOutputExecute.h b/project2/noOutputExecute.h index 9c476eb..4795ae6 100644 --- a/project2/noOutputExecute.h +++ b/project2/noOutputExecute.h @@ -2,13 +2,14 @@ #define NOOUTPUTEXECUTE_H #include "sourceObject.h" +#include "xmlObjectLoader.h" class ApplicationEngine; class PerRowValues; class NoOutputExecute; typedef boost::intrusive_ptr NoOutputExecutePtr; -typedef std::map NoOutputExecutes; +typedef Storage::Objects NoOutputExecutes; class NoOutputExecute : public virtual SourceObject { public: diff --git a/project2/paramChecker.h b/project2/paramChecker.h index 7d14346..179ff37 100644 --- a/project2/paramChecker.h +++ b/project2/paramChecker.h @@ -11,8 +11,7 @@ class ApplicationEngine; class ParamChecker; typedef boost::intrusive_ptr ParamCheckerPtr; -typedef std::map ParamCheckers; -typedef std::map OrderedParamCheckers; +typedef Storage::Objects ParamCheckers; class ParamChecker : public virtual SourceObject { public: diff --git a/project2/presenter.cpp b/project2/presenter.cpp index 888a577..37ae223 100644 --- a/project2/presenter.cpp +++ b/project2/presenter.cpp @@ -13,8 +13,8 @@ Presenter::~Presenter() void Presenter::execute() const { - BOOST_FOREACH(Views::value_type s, views) { - s.second->execute(this); + BOOST_FOREACH(const Views::value_type & s, views) { + s->execute(this); } // These were for debug... but why not pass them on? ApplicationEngine * appEngine = ApplicationEngine::getCurrent(); diff --git a/project2/rowSet.cpp b/project2/rowSet.cpp index 2e4b3f7..2dd4663 100644 --- a/project2/rowSet.cpp +++ b/project2/rowSet.cpp @@ -2,6 +2,7 @@ #include "rowUser.h" #include "commonObjects.h" #include "logger.h" +#include "variables.h" #include #include diff --git a/project2/rowSet.h b/project2/rowSet.h index b0bb668..bd17412 100644 --- a/project2/rowSet.h +++ b/project2/rowSet.h @@ -4,18 +4,18 @@ #include #include #include -#include #include #include "sourceObject.h" -#include "iHaveParameters.h" -#include "variables.h" +#include "xmlObjectLoader.h" #include class RowProcessor; class RowSet; +class RowUser; +class VariableType; typedef boost::intrusive_ptr RowSetPtr; typedef boost::intrusive_ptr ConstRowSetPtr; -typedef std::map RowSets; +typedef Storage::Objects RowSets; class RowSet : public virtual SourceObject { public: diff --git a/project2/rowView.cpp b/project2/rowView.cpp index f6e7b67..be1800c 100644 --- a/project2/rowView.cpp +++ b/project2/rowView.cpp @@ -82,7 +82,7 @@ void RowView::executeChildren() const { BOOST_FOREACH(Views::value_type sq, subViews) { - sq.second->execute(presenter); + sq->execute(presenter); } } diff --git a/project2/sqlMergeTask.cpp b/project2/sqlMergeTask.cpp index b37cbf6..8847913 100644 --- a/project2/sqlMergeTask.cpp +++ b/project2/sqlMergeTask.cpp @@ -3,6 +3,7 @@ #include "rdbmsDataSource.h" #include "exceptions.h" #include "sqlVariableBinder.h" +#include "xmlObjectLoader.h" #include #include #include @@ -91,7 +92,7 @@ SqlMergeTask::loadComplete(const CommonObjects * co) destdb = &co->dataSource(dataSource())->getWritable(); insCmd = insertCommand(); BOOST_FOREACH(const Iterates::value_type & i, sources) { - attach(i.second, insCmd); + attach(i, insCmd); } } @@ -287,9 +288,9 @@ attach(IteratePtr i, ModifyCommand * insert) i->subNOEs.insert(NoOutputExecutes::value_type(0, new Populate(insert))); } else { - BOOST_FOREACH(const NoOutputExecutes::value_type & n, i->subNOEs) { - attach(boost::dynamic_pointer_cast(n.second), insert); - attach(boost::dynamic_pointer_cast(n.second), insert); + BOOST_FOREACH(const NoOutputExecutes::value_type & n, i->subNOEs.get()) { + attach(boost::dynamic_pointer_cast(n), insert); + attach(boost::dynamic_pointer_cast(n), insert); } } } @@ -299,7 +300,7 @@ SqlMergeTask::copyToTempTable() const { if (useView) return; BOOST_FOREACH(const Iterates::value_type & i, sources) { - i.second->execute(); + i->execute(); } BOOST_FOREACH(const std::string & sql, sqls) { Buffer ins; diff --git a/project2/streamRows.h b/project2/streamRows.h index 473a0d8..ffe2dc6 100644 --- a/project2/streamRows.h +++ b/project2/streamRows.h @@ -2,6 +2,7 @@ #define STREAMROWS_H #include "rowSet.h" +#include "variables.h" #include #include #include diff --git a/project2/view.h b/project2/view.h index 8080824..cfee85b 100644 --- a/project2/view.h +++ b/project2/view.h @@ -1,15 +1,13 @@ #ifndef VIEW_H #define VIEW_H -#include -#include -#include #include "sourceObject.h" +#include "xmlObjectLoader.h" class Presenter; class View; typedef boost::intrusive_ptr ViewPtr; -typedef std::map Views; +typedef Storage::Objects Views; class View : public virtual SourceObject { public: diff --git a/project2/xmlObjectLoader.cpp b/project2/xmlObjectLoader.cpp index f14f773..17e59d4 100644 --- a/project2/xmlObjectLoader.cpp +++ b/project2/xmlObjectLoader.cpp @@ -1,7 +1,7 @@ #include "xmlObjectLoader.h" #include "exceptions.h" #include "logger.h" -#include +#include unsigned int LoaderBase::depth = 0; std::set LoaderBase::loadedObjects; diff --git a/project2/xmlObjectLoader.h b/project2/xmlObjectLoader.h index 7dec077..4d0346e 100644 --- a/project2/xmlObjectLoader.h +++ b/project2/xmlObjectLoader.h @@ -1,85 +1,73 @@ #ifndef XMLOBJECTLOADER_H #define XMLOBJECTLOADER_H -#include #include #include #include #include -#include #include #include #include "intrusivePtrBase.h" #include "sourceObject.h" +#include +#include +#include Glib::ustring xmlChildText(const xmlpp::Node * p, const Glib::ustring & n); +class StoreFailed : public std::exception { }; + +struct bySOName { }; +struct bySOOrder { }; + class ElementLoader; class CommonObjects; +template +class Storage { + public: + typedef boost::multi_index::multi_index_container< + boost::intrusive_ptr, + boost::multi_index::indexed_by< + boost::multi_index::ordered_unique< + boost::multi_index::tag, BOOST_MULTI_INDEX_MEMBER(SourceObject, const unsigned int, order)>, + boost::multi_index::ordered_unique< + boost::multi_index::tag, BOOST_MULTI_INDEX_MEMBER(SourceObject, const std::string, name)> + > > Objects; + typedef Objects * ObjectsPtr; +}; +class Storer; +typedef boost::intrusive_ptr StorerPtr; class Storer : public virtual IntrusivePtrBase { public: - template - static boost::intrusive_ptr into(std::map > * map); + template + static StorerPtr into(X * map); virtual bool save(SourceObjectPtr o) const = 0; }; -template -class StorerImplBase : public Storer { - public: - typedef K Key; - StorerImplBase(std::map > * m) : map(m) - { - } - std::map > * map; -}; -template -class StorerImpl; - -template -class StorerImpl : public StorerImplBase { +template +class StorerImpl : public Storer { public: - typedef unsigned int Key; - typedef boost::intrusive_ptr Ptr; - StorerImpl(std::map * m) : StorerImplBase(m) - { - } - bool save(SourceObjectPtr obj) const + StorerImpl(typename Storage::ObjectsPtr m) : map(m) { - Ptr O = boost::dynamic_pointer_cast(obj); - if (O) { - StorerImplBase::map->insert(std::pair(O->order, O)); - return true; - } - return false; } -}; -template -class StorerImpl : public StorerImplBase { - public: - typedef std::string Key; - typedef boost::intrusive_ptr Ptr; - StorerImpl(std::map * m) : StorerImplBase(m) - { - } - bool save(SourceObjectPtr obj) const - { - Ptr O = boost::dynamic_pointer_cast(obj); + bool save(SourceObjectPtr obj) const { + boost::intrusive_ptr O = boost::dynamic_pointer_cast(obj); if (O) { - StorerImplBase::map->insert(std::pair(O->name, O)); + map->insert(O); return true; } return false; } + typename Storage::ObjectsPtr map; }; -template -boost::intrusive_ptr -Storer::into(std::map > * map) -{ - return new StorerImpl(map); +template +StorerPtr +Storer::into(X * map) { + return new StorerImpl(map); } enum UnsupportedHandling { ErrorOnUnsupported, WarnOnUnsupported, IgnoreUnsupported }; @@ -88,15 +76,13 @@ class LoaderBase { public: LoaderBase(const Glib::ustring & ns, bool recursive); virtual ~LoaderBase(); - void collectAll(const CommonObjects * co, const xmlpp::Element * node, bool childrenOnly, UnsupportedHandling uh = ErrorOnUnsupported) const; - void collectAll(const xmlpp::Element * node, bool childrenOnly, UnsupportedHandling uh = ErrorOnUnsupported) const; - - template - static void save(std::map > * map, boost::intrusive_ptr obj); - template - static void save(std::map > * map, boost::intrusive_ptr obj); + void collectAll(const CommonObjects * co, const xmlpp::Element * node, bool childrenOnly, + UnsupportedHandling uh = ErrorOnUnsupported) const; + void collectAll(const xmlpp::Element * node, bool childrenOnly, + UnsupportedHandling uh = ErrorOnUnsupported) const; std::set > supportedStorers; + static void onIdle(); static void onIteration(); static void onPeriodic(); @@ -109,6 +95,7 @@ class LoaderBase { static std::set loadedObjects; const bool recursive; + public: const Glib::ustring ns; }; diff --git a/project2/xmlPresenter.h b/project2/xmlPresenter.h index 72fc3aa..1c520a8 100644 --- a/project2/xmlPresenter.h +++ b/project2/xmlPresenter.h @@ -25,7 +25,7 @@ class XmlPresenter : public Presenter { const Glib::ustring contentType; protected: - OrderedParamCheckers parameterChecks; + ParamCheckers parameterChecks; private: XmlDocumentPtr responseDoc; diff --git a/project2/xslRows.h b/project2/xslRows.h index e3aae63..8f13c89 100644 --- a/project2/xslRows.h +++ b/project2/xslRows.h @@ -6,6 +6,7 @@ #include #include #include "rowSet.h" +#include "variables.h" class XslRows : public RowSet { public: -- cgit v1.2.3