From 75ad5d45cce54c8208869c97a613f43a65b1dbf4 Mon Sep 17 00:00:00 2001 From: randomdan Date: Sat, 4 Jan 2014 18:52:36 +0000 Subject: Explicit instantiations of instance store/map/set to avoid multiple instantiatation in different compilation units --- project2/cgi/cgiOutputOptions.cpp | 2 + project2/cgi/cgiRouter.cpp | 5 ++ project2/cgi/testCgi.cpp | 1 + project2/common/instanceStore.h | 90 ++++-------------------- project2/common/instanceStore.impl.h | 113 ++++++++++++++++++++++++++++++ project2/common/logger.cpp | 4 +- project2/common/options.cpp | 3 + project2/common/optionsSource.cpp | 4 ++ project2/common/presenter.cpp | 3 + project2/common/scriptLoader.cpp | 3 + project2/common/scripts.cpp | 1 + project2/common/sessionContainer.cpp | 3 + project2/common/sourceObject.cpp | 4 ++ project2/common/transform.cpp | 4 ++ project2/common/variables.cpp | 3 + project2/compression/decompressStream.cpp | 3 + project2/daemon/lib/daemon.cpp | 3 + project2/files/fsRows.cpp | 3 + project2/sql/connectionLoader.cpp | 5 ++ project2/xml/xmlDocumentCache.cpp | 2 +- project2/xml/xmlPresenter.cpp | 3 + 21 files changed, 185 insertions(+), 77 deletions(-) create mode 100644 project2/cgi/cgiRouter.cpp create mode 100644 project2/common/instanceStore.impl.h create mode 100644 project2/sql/connectionLoader.cpp diff --git a/project2/cgi/cgiOutputOptions.cpp b/project2/cgi/cgiOutputOptions.cpp index 55e1de4..de3d291 100644 --- a/project2/cgi/cgiOutputOptions.cpp +++ b/project2/cgi/cgiOutputOptions.cpp @@ -1,5 +1,6 @@ #include "cgiOutputOptions.h" #include "scripts.h" +#include "instanceStore.impl.h" bool OutputOptions::core; bool OutputOptions::session; @@ -36,4 +37,5 @@ OutputOptionsLoader::create(ScriptNodePtr e) const { } DECLARE_CUSTOM_COMPONENT_LOADER("outputoptions", OutputOptions, OutputOptionsLoader, OutputOptionsLoader) +INSTANTIATESTORE(std::string, OutputOptionsLoader); diff --git a/project2/cgi/cgiRouter.cpp b/project2/cgi/cgiRouter.cpp new file mode 100644 index 0000000..dc77c40 --- /dev/null +++ b/project2/cgi/cgiRouter.cpp @@ -0,0 +1,5 @@ +#include "cgiRouter.h" +#include "instanceStore.impl.h" + +INSTANTIATESTORE(std::string, RouterLoader); + diff --git a/project2/cgi/testCgi.cpp b/project2/cgi/testCgi.cpp index 58b823f..80c3f01 100644 --- a/project2/cgi/testCgi.cpp +++ b/project2/cgi/testCgi.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include "options.h" #include "safeMapFind.h" diff --git a/project2/common/instanceStore.h b/project2/common/instanceStore.h index 36f4e73..5557b43 100644 --- a/project2/common/instanceStore.h +++ b/project2/common/instanceStore.h @@ -12,75 +12,12 @@ template class InstanceStore { public: - static const StoreType & GetAll() - { - return *getInstances(); - } - - static void Add(const typename StoreType::value_type & p) - { - getInstances()->insert(p); - } - - template - static void Remove(const EraseByType & p) - { - getInstances()->erase(p); - prune(); - } - - static void OnEach(const boost::function & func, bool ContinueOnError = false) - { - BOOST_FOREACH(const auto & l, GetAll()) { - if (ContinueOnError) { - try { - func(l.get()); - } - catch (...) { - } - } - else { - func(l.get()); - } - } - prune(); - } + static const StoreType & GetAll(); + static void Add(const typename StoreType::value_type & p); - static void OnAll(const boost::function & func, bool ContinueOnError = false) - { - BOOST_FOREACH(const auto & l, GetAll()) { - if (ContinueOnError) { - try { - func(l.get()); - } - catch (...) { - } - } - else { - func(l.get()); - } - } - prune(); - } - - private: - static void prune() - { - auto & ps = getInstances(); - if (ps->empty()) { - delete ps; - ps = NULL; - } - } - - static StoreType * & getInstances() - { - static StoreType * instances = NULL; - if (!instances) { - instances = new StoreType(); - } - return instances; - } + protected: + static void prune(); + static StoreType * & getInstances(); }; /// Keyed collection of instances @@ -88,25 +25,28 @@ template class InstanceMap : public InstanceStore>> { public: typedef std::map> Store; + typedef InstanceStore>> IStore; typedef typename Store::value_type Value; - static void Add(const KeyType & k, Type * p) { - InstanceStore::Add(Value(k, boost::shared_ptr(p))); - } - - static void Add(const KeyType & k, const boost::shared_ptr & p) { - InstanceStore::Add(Value(k, p)); - } + static void Add(const KeyType & k, Type * p); + static void Add(const KeyType & k, const boost::shared_ptr & p); + static void Remove(const KeyType &); template static boost::shared_ptr Get(const KeyType & n) { return safeMapLookup(InstanceStore::GetAll(), n); } + + static void OnEach(const boost::function & func, bool ContinueOnError = false); }; /// Anonymous collection of instances template class InstanceSet : public InstanceStore>> { + public: + typedef InstanceStore>> IStore; + static void OnAll(const boost::function & func, bool ContinueOnError = false); + static void Remove(const boost::shared_ptr &); }; #endif diff --git a/project2/common/instanceStore.impl.h b/project2/common/instanceStore.impl.h new file mode 100644 index 0000000..f626d47 --- /dev/null +++ b/project2/common/instanceStore.impl.h @@ -0,0 +1,113 @@ +#include "instanceStore.h" +#include + +template +const StoreType & +InstanceStore::GetAll() +{ + return *getInstances(); +} + +template +void +InstanceStore::Add(const typename StoreType::value_type & p) +{ + getInstances()->insert(p); +} + +template +void +InstanceStore::prune() +{ + auto & ps = getInstances(); + if (ps->empty()) { + delete ps; + ps = NULL; + } +} + +template +StoreType * & +InstanceStore::getInstances() +{ + static StoreType * instances = NULL; + if (!instances) { + instances = new StoreType(); + } + return instances; +} + +template +void +InstanceMap::Add(const KeyType & k, Type * p) +{ + IStore::Add(Value(k, boost::shared_ptr(p))); +} + +template +void +InstanceMap::Add(const KeyType & k, const boost::shared_ptr & p) +{ + IStore::Add(Value(k, p)); +} + +template +void +InstanceMap::Remove(const KeyType & k) +{ + IStore::getInstances()->erase(k); + IStore::prune(); +} + +template +void +InstanceMap::OnEach(const boost::function & func, bool ContinueOnError) +{ + BOOST_FOREACH(const auto & l, IStore::GetAll()) { + if (ContinueOnError) { + try { + func(l); + } + catch (...) { + } + } + else { + func(l); + } + } + IStore::prune(); +} + +template +void +InstanceSet::OnAll(const boost::function & func, bool ContinueOnError) +{ + BOOST_FOREACH(const auto & l, IStore::GetAll()) { + if (ContinueOnError) { + try { + func(l.get()); + } + catch (...) { + } + } + else { + func(l.get()); + } + } + IStore::prune(); +} + +template +void +InstanceSet::Remove(const boost::shared_ptr & p) +{ + IStore::getInstances()->erase(p); + IStore::prune(); +} + +#define INSTANTIATESTORE(K, T) \ +template class InstanceStore>>; \ +template class InstanceMap; \ +template class InstanceStore>>; \ +template class InstanceSet + diff --git a/project2/common/logger.cpp b/project2/common/logger.cpp index 781b44a..a9c797a 100644 --- a/project2/common/logger.cpp +++ b/project2/common/logger.cpp @@ -3,7 +3,7 @@ #include "logger.h" #include - +#include "instanceStore.impl.h" Log Logger::log; @@ -97,3 +97,5 @@ LogDriverBase::~LogDriverBase() { } +INSTANTIATESTORE(std::string, LogDriverLoader); + diff --git a/project2/common/options.cpp b/project2/common/options.cpp index fd93576..3000069 100644 --- a/project2/common/options.cpp +++ b/project2/common/options.cpp @@ -1,6 +1,7 @@ #include #include "options.h" #include +#include "instanceStore.impl.h" class NamedOption : public Options::Option { public: @@ -169,3 +170,5 @@ Options::InstanceTarget::assign(const VariableType & value) const assigner(value); } +INSTANTIATESTORE(std::string, Options); + diff --git a/project2/common/optionsSource.cpp b/project2/common/optionsSource.cpp index 0f24f9e..58c9b1f 100644 --- a/project2/common/optionsSource.cpp +++ b/project2/common/optionsSource.cpp @@ -1,5 +1,7 @@ #include "optionsSource.h" #include "logger.h" +#include +#include "instanceStore.impl.h" class DefaultConfigConsumer : public ConfigConsumer { public: @@ -41,3 +43,5 @@ OptionsSource::loadSources(const Options::CurrentPlatform & platform) } } +INSTANTIATESTORE(std::string, OptionsSource); + diff --git a/project2/common/presenter.cpp b/project2/common/presenter.cpp index 4e12307..35f5f2c 100644 --- a/project2/common/presenter.cpp +++ b/project2/common/presenter.cpp @@ -1,6 +1,7 @@ #include #include "presenter.h" #include "dataSource.h" +#include "instanceStore.impl.h" #include NameValuePairPresenter::NameValuePairPresenter() @@ -120,3 +121,5 @@ MultiRowSetPresenter::finalizeContent() const { } +INSTANTIATESTORE(std::string, PresenterLoader); + diff --git a/project2/common/scriptLoader.cpp b/project2/common/scriptLoader.cpp index fe8799b..93ed639 100644 --- a/project2/common/scriptLoader.cpp +++ b/project2/common/scriptLoader.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "instanceStore.impl.h" unsigned int LoaderBase::depth = 0; std::set LoaderBase::loadedObjects; @@ -137,3 +138,5 @@ LoaderBase::collectAll(const CommonObjects * co, bool childrenOnly, ScriptNodePt } } +INSTANTIATESTORE(std::string, ScriptReaderLoader); + diff --git a/project2/common/scripts.cpp b/project2/common/scripts.cpp index a4a036d..a7a6c23 100644 --- a/project2/common/scripts.cpp +++ b/project2/common/scripts.cpp @@ -2,6 +2,7 @@ #include "variables/fixed.h" #include #include +#include ScriptReader::ScriptCache ScriptReader::scriptCache; diff --git a/project2/common/sessionContainer.cpp b/project2/common/sessionContainer.cpp index 41c3f98..ae82dd9 100644 --- a/project2/common/sessionContainer.cpp +++ b/project2/common/sessionContainer.cpp @@ -1,5 +1,6 @@ #include #include "sessionContainer.h" +#include "instanceStore.impl.h" SessionContainer::SessionContainer() { @@ -27,3 +28,5 @@ SessionContainer::GetSession(const boost::uuids::uuid & id) const return s; } +INSTANTIATESTORE(std::string, SessionContainerLoader); + diff --git a/project2/common/sourceObject.cpp b/project2/common/sourceObject.cpp index b451919..a76bdec 100644 --- a/project2/common/sourceObject.cpp +++ b/project2/common/sourceObject.cpp @@ -1,4 +1,5 @@ #include +#include "instanceStore.impl.h" #include "sourceObject.h" #include "exceptions.h" #include "safeMapFind.h" @@ -54,3 +55,6 @@ SourceObject::findComponent(const std::string & name) const return safeMapLookup(script->namedComponents, name); } +INSTANTIATESTORE(std::string, ComponentLoader); +INSTANTIATESTORE(std::string, ElementLoader); + diff --git a/project2/common/transform.cpp b/project2/common/transform.cpp index d392110..5f64de7 100644 --- a/project2/common/transform.cpp +++ b/project2/common/transform.cpp @@ -4,6 +4,7 @@ #include "ostreamWrapper.h" #include "scriptStorage.h" #include +#include "instanceStore.impl.h" class TransformTargetStorer : public Storer { public: @@ -106,3 +107,6 @@ class TransformStaticContentToStdStream : public TransformImpl #include #include +#include "instanceStore.impl.h" VariableImplDyn::VariableImplDyn(ScriptNodePtr e) { @@ -124,3 +125,5 @@ Variable::makeParent(const Glib::ustring & name, bool attr, unsigned int dep) return Variable(new VariableParent(name, attr, dep)); } +INSTANTIATESTORE(std::string, VariableLoader); + diff --git a/project2/compression/decompressStream.cpp b/project2/compression/decompressStream.cpp index ad2e01d..c1d52ab 100644 --- a/project2/compression/decompressStream.cpp +++ b/project2/compression/decompressStream.cpp @@ -4,6 +4,7 @@ #include "scripts.h" #include "variables.h" #include "scriptStorage.h" +#include "instanceStore.impl.h" class DecompressStream : public Stream { public: @@ -28,3 +29,5 @@ class DecompressStream : public Stream { }; DECLARE_LOADER("decompstream", DecompressStream); +INSTANTIATESTORE(std::string, DecompressorLoader); + diff --git a/project2/daemon/lib/daemon.cpp b/project2/daemon/lib/daemon.cpp index 1a70fc1..2f1760f 100644 --- a/project2/daemon/lib/daemon.cpp +++ b/project2/daemon/lib/daemon.cpp @@ -1,4 +1,5 @@ #include "daemon.h" +#include "instanceStore.impl.h" Daemon::Daemon() { @@ -8,3 +9,5 @@ Daemon::~Daemon() { } +INSTANTIATESTORE(std::string, DaemonLoader); + diff --git a/project2/files/fsRows.cpp b/project2/files/fsRows.cpp index aca1626..657973f 100644 --- a/project2/files/fsRows.cpp +++ b/project2/files/fsRows.cpp @@ -14,6 +14,7 @@ #include #include #include +#include "instanceStore.impl.h" typedef boost::filesystem::directory_iterator DirEnt; @@ -236,3 +237,5 @@ FsRows::SearchState::fileType() const throw NotSupported(__PRETTY_FUNCTION__); } +INSTANTIATESTORE(std::string, FsRows::SpecBaseLoader); + diff --git a/project2/sql/connectionLoader.cpp b/project2/sql/connectionLoader.cpp new file mode 100644 index 0000000..671fafc --- /dev/null +++ b/project2/sql/connectionLoader.cpp @@ -0,0 +1,5 @@ +#include "connectionLoader.h" +#include "instanceStore.impl.h" + +INSTANTIATESTORE(std::string, ConnectionLoader); + diff --git a/project2/xml/xmlDocumentCache.cpp b/project2/xml/xmlDocumentCache.cpp index cbfaf74..52165fb 100644 --- a/project2/xml/xmlDocumentCache.cpp +++ b/project2/xml/xmlDocumentCache.cpp @@ -116,5 +116,5 @@ class XmlDocumentCacheClearer : public ComponentLoader { Logger()->messagef(LOG_DEBUG, "%s: Cleared XML document cache", __PRETTY_FUNCTION__); } }; -DECLARE_CUSTOM_COMPONENT_LOADER("XmlDocumentCacheClearer", XmlDocumentCacheClearer, XmlDocumentCacheClearer, XmlDocumentCacheClearer); +DECLARE_COMPONENT("XmlDocumentCacheClearer", XmlDocumentCacheClearer); diff --git a/project2/xml/xmlPresenter.cpp b/project2/xml/xmlPresenter.cpp index d0281f7..cf31f21 100644 --- a/project2/xml/xmlPresenter.cpp +++ b/project2/xml/xmlPresenter.cpp @@ -8,6 +8,7 @@ #include #include #include +#include "instanceStore.impl.h" DECLARE_OPTIONS(XmlPresenter, "XML Presenter options") ("presenter.xml.typeid.null", Options::value(&typeidNull, false), @@ -268,3 +269,5 @@ XmlDocMutator::XmlDocMutator(ScriptNodePtr) { } +INSTANTIATESTORE(std::string, XmlDocMutatorLoader); + -- cgit v1.2.3