diff options
author | randomdan <randomdan@localhost> | 2014-01-04 18:52:36 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2014-01-04 18:52:36 +0000 |
commit | 75ad5d45cce54c8208869c97a613f43a65b1dbf4 (patch) | |
tree | 33268bd0b09e649e66f52cd7b8df2694b5881a65 /project2/common/instanceStore.h | |
parent | Tidy up the code for adding a help option (diff) | |
download | project2-75ad5d45cce54c8208869c97a613f43a65b1dbf4.tar.bz2 project2-75ad5d45cce54c8208869c97a613f43a65b1dbf4.tar.xz project2-75ad5d45cce54c8208869c97a613f43a65b1dbf4.zip |
Explicit instantiations of instance store/map/set to avoid multiple instantiatation in different compilation units
Diffstat (limited to 'project2/common/instanceStore.h')
-rw-r--r-- | project2/common/instanceStore.h | 90 |
1 files changed, 15 insertions, 75 deletions
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 Type, class StoreType> class InstanceStore { public: - static const StoreType & GetAll() - { - return *getInstances(); - } - - static void Add(const typename StoreType::value_type & p) - { - getInstances()->insert(p); - } - - template <typename EraseByType> - static void Remove(const EraseByType & p) - { - getInstances()->erase(p); - prune(); - } - - static void OnEach(const boost::function<void(typename StoreType::value_type &)> & 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<void(Type *)> & 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 Type, class KeyType> class InstanceMap : public InstanceStore<Type, std::map<KeyType, boost::shared_ptr<Type>>> { public: typedef std::map<KeyType, boost::shared_ptr<Type>> Store; + typedef InstanceStore<Type, std::map<KeyType, boost::shared_ptr<Type>>> IStore; typedef typename Store::value_type Value; - static void Add(const KeyType & k, Type * p) { - InstanceStore<Type, Store>::Add(Value(k, boost::shared_ptr<Type>(p))); - } - - static void Add(const KeyType & k, const boost::shared_ptr<Type> & p) { - InstanceStore<Type, Store>::Add(Value(k, p)); - } + static void Add(const KeyType & k, Type * p); + static void Add(const KeyType & k, const boost::shared_ptr<Type> & p); + static void Remove(const KeyType &); template <class E> static boost::shared_ptr<Type> Get(const KeyType & n) { return safeMapLookup<E>(InstanceStore<Type, Store>::GetAll(), n); } + + static void OnEach(const boost::function<void(const Value &)> & func, bool ContinueOnError = false); }; /// Anonymous collection of instances template <class Type> class InstanceSet : public InstanceStore<Type, std::set<boost::shared_ptr<Type>>> { + public: + typedef InstanceStore<Type, std::set<boost::shared_ptr<Type>>> IStore; + static void OnAll(const boost::function<void(Type *)> & func, bool ContinueOnError = false); + static void Remove(const boost::shared_ptr<Type> &); }; #endif |