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/common/instanceStore.h | 90 +++++++---------------------------------- 1 file changed, 15 insertions(+), 75 deletions(-) (limited to 'project2/common/instanceStore.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 -- cgit v1.2.3