From 543f89f60644496d254d740decf99015eda9cbc2 Mon Sep 17 00:00:00 2001 From: randomdan Date: Sat, 26 Nov 2011 17:01:15 +0000 Subject: Get rid of the multi presenter mess in viewHost and replace it with a single presenter --- project2/cgi/cgiStagePresent.cpp | 4 +- project2/common/viewHost.cpp | 72 ++++------------------------------- project2/common/viewHost.h | 18 +-------- project2/common/xmlStorage.h | 24 ++++++++++++ project2/console/consoleAppEngine.cpp | 2 +- 5 files changed, 35 insertions(+), 85 deletions(-) diff --git a/project2/cgi/cgiStagePresent.cpp b/project2/cgi/cgiStagePresent.cpp index df05465..f1fd5e3 100644 --- a/project2/cgi/cgiStagePresent.cpp +++ b/project2/cgi/cgiStagePresent.cpp @@ -19,7 +19,7 @@ CgiApplicationEngine::PresentStage::run() runChecks(); try { executeViews(boost::bind(&PresenterLoader::createFrom, LoaderBase::getLoader(e->defaultPresenter), _1)); - return NextStage(NULL, this, boost::dynamic_pointer_cast(headPresenter()), headPresenter()); + return NextStage(NULL, this, boost::dynamic_pointer_cast(presenter), presenter); } catch (ResponseStagePtr p) { return NextStage(NULL, p, boost::dynamic_pointer_cast(p), boost::dynamic_pointer_cast(p)); @@ -37,7 +37,7 @@ CgiApplicationEngine::ResponseStage::ResponseStage(const CgiEnvironment * e) : CgiApplicationEngine::HttpHeaderPtr CgiApplicationEngine::PresentStage::getHeader() const { - ContentPresenter * cp = boost::dynamic_pointer_cast(headPresenter()).get(); + ContentPresenter * cp = boost::dynamic_pointer_cast(presenter).get(); Project2HttpHeader * header = new Project2HttpHeader("200 OK", cp ? cp->contentType : "text/plain"); header->addHeader("Cache-control", "no-cache"); return HttpHeaderPtr(header); diff --git a/project2/common/viewHost.cpp b/project2/common/viewHost.cpp index 7f7d6c7..01cf966 100644 --- a/project2/common/viewHost.cpp +++ b/project2/common/viewHost.cpp @@ -11,7 +11,7 @@ ViewHost::ViewHost(const boost::filesystem::path & file) : CheckHost(file) { loader.supportedStorers.insert(Storer::into(&views)); - loader.supportedStorers.insert(Storer::into(&pmp.presenters)); + loader.supportedStorers.insert(Storer::into(&presenter)); } ViewHost::~ViewHost() @@ -22,12 +22,12 @@ void ViewHost::executeViews(const DefaultPresenterProvider & dpp) const { parseDocument(); - if (pmp.presenters.empty()) { - pmp.presenters.insert(dpp(get_document()->get_root_node())); + if (!presenter) { + presenter = dpp(get_document()->get_root_node()); } BOOST_FOREACH(const Views::value_type & s, views) { - s->execute(&pmp); + s->execute(presenter.get()); } // Caches might open transactions BOOST_FOREACH(const DataSources::value_type & ds, datasources) { @@ -38,68 +38,10 @@ ViewHost::executeViews(const DefaultPresenterProvider & dpp) const void ViewHost::doTransforms() const { - BOOST_FOREACH (const PresenterPtr & p, pmp.presenters) { - TransformSourcePtr ts = boost::dynamic_pointer_cast(p); - if (ts) { - ts->doTransforms(); - } + TransformSourcePtr ts = boost::dynamic_pointer_cast(presenter); + if (ts) { + ts->doTransforms(); } } -PresenterPtr -ViewHost::headPresenter() const -{ - return *pmp.presenters.begin(); -} - -void -ViewHost::PresenterMultiplexer::declareNamespace(const Glib::ustring & prefix, const Glib::ustring & ns) const -{ - FOREACH_PRESENTER { p->declareNamespace(prefix, ns); } -} -void -ViewHost::PresenterMultiplexer::setNamespace(const Glib::ustring & prefix, const Glib::ustring & ns) const -{ - FOREACH_PRESENTER { p->setNamespace(prefix, ns); } -} -void -ViewHost::PresenterMultiplexer::pushSub(const Glib::ustring & name) const -{ - FOREACH_PRESENTER { p->pushSub(name); } -} -void -ViewHost::PresenterMultiplexer::pushSub(const Glib::ustring & name, const Glib::ustring & ns) const -{ - FOREACH_PRESENTER { p->pushSub(name, ns); } -} -void -ViewHost::PresenterMultiplexer::addAttribute(const Glib::ustring & name, const VariableType & value) const -{ - FOREACH_PRESENTER { p->addAttribute(name, value); } -} -void -ViewHost::PresenterMultiplexer::addAttribute(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const -{ - FOREACH_PRESENTER { p->addAttribute(name, ns, value); } -} -void -ViewHost::PresenterMultiplexer::addNamedValue(const Glib::ustring & name, const VariableType & value) const -{ - FOREACH_PRESENTER { p->addNamedValue(name, value); } -} -void -ViewHost::PresenterMultiplexer::addNamedValue(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const -{ - FOREACH_PRESENTER { p->addNamedValue(name, ns, value); } -} -void -ViewHost::PresenterMultiplexer::addText(const VariableType & value) const -{ - FOREACH_PRESENTER { p->addText(value); } -} -void -ViewHost::PresenterMultiplexer::popSub() const -{ - FOREACH_PRESENTER { p->popSub(); } -} diff --git a/project2/common/viewHost.h b/project2/common/viewHost.h index d5e8695..920bef0 100644 --- a/project2/common/viewHost.h +++ b/project2/common/viewHost.h @@ -11,21 +11,6 @@ class ViewHost : virtual public XmlScriptParser, virtual public CheckHost { public: - class PresenterMultiplexer : public Presenter { - public: - typedef std::set Presenters; - void declareNamespace(const Glib::ustring & prefix, const Glib::ustring & ns) const; - void setNamespace(const Glib::ustring & prefix, const Glib::ustring & ns) const; - void pushSub(const Glib::ustring & name) const; - void pushSub(const Glib::ustring & name, const Glib::ustring & ns) const; - void addAttribute(const Glib::ustring & name, const VariableType & value) const; - void addAttribute(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const; - void addNamedValue(const Glib::ustring & name, const VariableType & value) const; - void addNamedValue(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const; - void addText(const VariableType & value) const; - void popSub() const; - Presenters presenters; - }; typedef boost::function1 DefaultPresenterProvider; ViewHost(const boost::filesystem::path & file); @@ -33,10 +18,9 @@ class ViewHost : virtual public XmlScriptParser, virtual public CheckHost { void executeViews(const DefaultPresenterProvider &) const; void doTransforms() const; - PresenterPtr headPresenter() const; + mutable PresenterPtr presenter; private: - mutable PresenterMultiplexer pmp; typedef ANONORDEREDSTORAGEOF(View) Views; Views views; }; diff --git a/project2/common/xmlStorage.h b/project2/common/xmlStorage.h index 73fce0a..a700c64 100644 --- a/project2/common/xmlStorage.h +++ b/project2/common/xmlStorage.h @@ -10,6 +10,8 @@ SimpleMessageException(StoreFailed); +#define SINGLE(X) \ + boost::intrusive_ptr #define STORAGEOF(X) \ std::map > #define ANONORDEREDSTORAGEOF(X) \ @@ -21,6 +23,8 @@ class Storer; typedef boost::intrusive_ptr StorerPtr; class Storer : public virtual IntrusivePtrBase { public: + template + static StorerPtr into(SINGLE(X) * obj); template static StorerPtr into(STORAGEOF(X) * map); template @@ -55,6 +59,20 @@ class StorerImpl : public StorerBase { bool insert(const xmlpp::Element *, boost::intrusive_ptr O); }; template +class StorerImpl : public StorerBase { + public: + typedef SINGLE(X) Obj; + StorerImpl(SINGLE(X) * o) : obj(o) + { + } + bool insert(const xmlpp::Element *, boost::intrusive_ptr O) + { + *obj = O; + return true; + } + Obj * obj; +}; +template class StorerImpl : public StorerBase { public: typedef STORAGEOF(X) Map; @@ -96,6 +114,12 @@ class StorerImpl : public StorerBase +StorerPtr +Storer::into(SINGLE(X) * obj) { + return new StorerImpl(obj); +} + template StorerPtr Storer::into(STORAGEOF(X) * map) { diff --git a/project2/console/consoleAppEngine.cpp b/project2/console/consoleAppEngine.cpp index ef1a04f..cf0548d 100644 --- a/project2/console/consoleAppEngine.cpp +++ b/project2/console/consoleAppEngine.cpp @@ -41,7 +41,7 @@ ConsoleApplicationEngine::process() const runChecks(); execute(); executeViews(boost::bind(&PresenterLoader::createFrom, LoaderBase::getLoader("console"), _1)); - addAppData(headPresenter().get()); + addAppData(presenter.get()); } catch (const std::exception & e) { char * buf = __cxxabiv1::__cxa_demangle(typeid(e).name(), NULL, NULL, NULL); -- cgit v1.2.3