/* * Registry.hpp * * Based on Registry by Till Crueger. * * The registry pattern is basically just a singleton map, wherein instantiations * of a class can be registered, unregistered and retrieved. * * Created on: Jul 28, 2010 * Author: heber */ #ifndef REGISTRY_HPP_ #define REGISTRY_HPP_ #include /** * This template produces a generic registry pattern. * *

Registry Howto

* * The registry is a class where instances of other classes are stored and be retrieved * when desired. For this purpose a registry should always be a singleton (i.e. use both * this registry and the singleton pattern to declare a registry class). It basically * is simply a singleton container of a map, where the pointers to the class instances are * stored by a string key and can be retrieved thereby. * * The available functions are, if your class to be stored in registry is foo : * * - foo* Registry::getByName() : returns the instance of a specific * class foo instance as a pointer associated with the given name * - bool Registry::isPresentByName() : returns whether an instance * of class foo is present under the given name. * - map::iterator Registry::getBeginIter() : returns an * iterator to the beginning of the storage map (STL). * - map::const_iterator Registry::getBeginIter() : returns a * constant iterator to the beginning of the storage map (STL). * - map::const_iterator Registry::getEndIter() : returns an * iterator to the one step past the last element of the storage map (STL). * - map::const_iterator Registry::getEndIter() : returns a * constant iterator to the one step past the last element of the storage map (STL). * * In order to use this pattern, additionally to the requirements of the Singleton pattern, * do this: * -# in the declaration derive your class from Registry, where foo is the class to be * stored * -# in the definition add CONSTRUCT_REGISTRY(foo) to the code such that the templated * functions get instantiated there (otherwise you'll get undefined reference errors). * */ template class Registry { public: Registry(); ~Registry(); T* getByName(const std::string name); bool isPresentByName(const std::string name); void registerInstance(T*); void unregisterInstance(T*); typename std::map::iterator getBeginIter(); typename std::map::const_iterator getBeginIter() const; typename std::map::iterator getEndIter(); typename std::map::const_iterator getEndIter() const; private: typename std::map InstanceMap; }; template std::ostream& operator<<(std::ostream& ost, const Registry& m); #endif /* REGISTRY_HPP_ */