From adcd84938d66dfaae45c0638aec3a09fb4be6d87 Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 24 Apr 2012 22:44:13 +0000 Subject: Handy little lazy pointer class for last minute default initialisation --- project2/cgi/cgiAppEngine.h | 3 ++- project2/cgi/cgiStagePresent.cpp | 6 ++---- project2/common/scriptStorage.h | 24 ++++++++---------------- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/project2/cgi/cgiAppEngine.h b/project2/cgi/cgiAppEngine.h index 854013a..66d5a69 100644 --- a/project2/cgi/cgiAppEngine.h +++ b/project2/cgi/cgiAppEngine.h @@ -8,6 +8,7 @@ #include "taskHost.h" #include "viewHost.h" #include "transform.h" +#include "lazyPointer.h" #include "xmlPresenter.h" #include "presenterCache.h" #include "sessionContainer.h" @@ -104,7 +105,7 @@ class CgiApplicationEngine : public ApplicationEngine, public TransformChainLink virtual HttpHeaderPtr getHeader() const; protected: HttpHeaderPtr header; - mutable MultiRowSetPresenterPtr presenter; + LazyPointer presenter; }; /// Stage to return previous cached output diff --git a/project2/cgi/cgiStagePresent.cpp b/project2/cgi/cgiStagePresent.cpp index 98031e4..9574e84 100644 --- a/project2/cgi/cgiStagePresent.cpp +++ b/project2/cgi/cgiStagePresent.cpp @@ -9,7 +9,8 @@ CgiApplicationEngine::PresentStage::PresentStage(ScriptReaderPtr s) : CgiApplicationEngine::ResponseStage(s->root()), CommonObjects(s->root()), CheckHost(s->root()), - ViewHost(s->root()) + ViewHost(s->root()), + presenter([root,this] { return PresenterLoader::getFor(this->env()->defaultPresenter)->create(root); }) { s->loader.addLoadTarget(s->root(), Storer::into(&outputOptions)); s->loader.addLoadTarget(s->root(), Storer::into(&presenter)); @@ -51,9 +52,6 @@ CgiApplicationEngine::PresentStage::run() MultiRowSetPresenterPtr CgiApplicationEngine::PresentStage::getPresenter() const { - if (!presenter) { - presenter = PresenterLoader::getFor(env()->defaultPresenter)->create(root); - } return presenter; } diff --git a/project2/common/scriptStorage.h b/project2/common/scriptStorage.h index 63ee71c..12cf3a6 100644 --- a/project2/common/scriptStorage.h +++ b/project2/common/scriptStorage.h @@ -14,8 +14,6 @@ SimpleMessageException(StoreFailed); -#define SINGLE(X) \ - boost::intrusive_ptr #define STORAGEOF(X) \ std::map > #define ANONORDEREDSTORAGEOF(X) \ @@ -29,8 +27,8 @@ class StorerBase; typedef boost::intrusive_ptr StorerPtr; class Storer : public virtual IntrusivePtrBase { public: - template - static boost::intrusive_ptr > into(SINGLE(X) * obj, const C & ... c); + template + static boost::intrusive_ptr > into(PtrOfX * obj, const C & ... c); template static boost::intrusive_ptr > into(STORAGEOF(X) * map, const C & ... c); template @@ -70,14 +68,8 @@ class StorerBase : public Storer { template class StorerImpl : public StorerBase { public: - StorerImpl(M * m); - bool insert(boost::intrusive_ptr O); -}; -template -class StorerImpl : public StorerBase { - public: - typedef SINGLE(X) Obj; - StorerImpl(SINGLE(X) * o, const typename StorerBase::Creator & c) : StorerBase(c), obj(o) + typedef M Obj; + StorerImpl(M * o, const typename StorerBase::Creator & c) : StorerBase(c), obj(o) { } bool insert(boost::intrusive_ptr O) @@ -129,10 +121,10 @@ class StorerImpl : public StorerBase { Map * map; }; -template -boost::intrusive_ptr > -Storer::into(SINGLE(X) * obj, const C & ... c) { - return new StorerImpl(obj, boost::bind(&L::create, _1, _2, c...)); +template +boost::intrusive_ptr > +Storer::into(PtrOfX * obj, const C & ... c) { + return new StorerImpl(obj, boost::bind(&L::create, _1, _2, c...)); } template -- cgit v1.2.3