| 1 | /* | 
|---|
| 2 | * Registry.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Based on Registry<Action> by Till Crueger. | 
|---|
| 5 | * | 
|---|
| 6 | *  The registry pattern is basically just a singleton map, wherein instantiations | 
|---|
| 7 | *  of a class can be registered, unregistered and retrieved. | 
|---|
| 8 | * | 
|---|
| 9 | *  Created on: Jul 28, 2010 | 
|---|
| 10 | *      Author: heber | 
|---|
| 11 | */ | 
|---|
| 12 |  | 
|---|
| 13 | #ifndef REGISTRY_HPP_ | 
|---|
| 14 | #define REGISTRY_HPP_ | 
|---|
| 15 |  | 
|---|
| 16 | #include <map> | 
|---|
| 17 |  | 
|---|
| 18 | /** | 
|---|
| 19 | * This template produces a generic registry pattern. | 
|---|
| 20 | * | 
|---|
| 21 | * <h1> Registry Howto </h1> | 
|---|
| 22 | * | 
|---|
| 23 | * The registry is a class where instances of other classes are stored and be retrieved | 
|---|
| 24 | * when desired. For this purpose a registry should always be a singleton (i.e. use both | 
|---|
| 25 | * this registry and the singleton pattern to declare a registry class). It basically | 
|---|
| 26 | * is simply a singleton container of a map, where the pointers to the class instances are | 
|---|
| 27 | * stored by a string key and can be retrieved thereby. | 
|---|
| 28 | * | 
|---|
| 29 | * The available functions are, if your class to be stored in registry is foo : | 
|---|
| 30 | * | 
|---|
| 31 | * - <code>foo* Registry<foo>::getByName()</code> : returns the instance of a specific | 
|---|
| 32 | *    class foo instance as a pointer associated with the given name | 
|---|
| 33 | * - <code>bool Registry<foo>::isPresentByName()</code> : returns whether an instance | 
|---|
| 34 | *    of class foo is present under the given name. | 
|---|
| 35 | * - <code>map<string,foo*>::iterator Registry<foo>::getBeginIter()</code> : returns an | 
|---|
| 36 | *    iterator to the beginning of the storage map (STL). | 
|---|
| 37 | * - <code>map<string,foo*>::const_iterator Registry<foo>::getBeginIter()</code> : returns a | 
|---|
| 38 | *    constant iterator to the beginning of the storage map (STL). | 
|---|
| 39 | * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns an | 
|---|
| 40 | *    iterator to the one step past the last element of the storage map (STL). | 
|---|
| 41 | * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns a | 
|---|
| 42 | *    constant iterator to the one step past the last element of the storage map (STL). | 
|---|
| 43 | * | 
|---|
| 44 | * In order to use this pattern, additionally to the requirements of the Singleton pattern, | 
|---|
| 45 | * do this: | 
|---|
| 46 | *   -# in the declaration derive your class from Registry<foo>, where foo is the class to be | 
|---|
| 47 | *      stored | 
|---|
| 48 | *   -# in the definition add CONSTRUCT_REGISTRY(foo) to the code such that the templated | 
|---|
| 49 | *      functions get instantiated there (otherwise you'll get undefined reference errors). | 
|---|
| 50 | * | 
|---|
| 51 | */ | 
|---|
| 52 |  | 
|---|
| 53 | template <class T> | 
|---|
| 54 | class Registry | 
|---|
| 55 | { | 
|---|
| 56 | public: | 
|---|
| 57 | Registry(); | 
|---|
| 58 | ~Registry(); | 
|---|
| 59 |  | 
|---|
| 60 | T* getByName(const std::string name); | 
|---|
| 61 | bool isPresentByName(const std::string name); | 
|---|
| 62 | void registerInstance(T*); | 
|---|
| 63 | void unregisterInstance(T*); | 
|---|
| 64 |  | 
|---|
| 65 | typename std::map<const std::string,T*>::iterator getBeginIter(); | 
|---|
| 66 | typename std::map<const std::string,T*>::const_iterator getBeginIter() const; | 
|---|
| 67 | typename std::map<const std::string,T*>::iterator getEndIter(); | 
|---|
| 68 | typename std::map<const std::string,T*>::const_iterator getEndIter() const; | 
|---|
| 69 |  | 
|---|
| 70 | private: | 
|---|
| 71 | typename std::map<const std::string,T*> InstanceMap; | 
|---|
| 72 | }; | 
|---|
| 73 |  | 
|---|
| 74 | template <class T> std::ostream& operator<<(std::ostream& ost, const Registry<T>& m); | 
|---|
| 75 |  | 
|---|
| 76 | #endif /* REGISTRY_HPP_ */ | 
|---|