| [b2d8d0] | 1 | /* | 
|---|
|  | 2 | * Registry.hpp | 
|---|
|  | 3 | * | 
|---|
| [e4afb4] | 4 | *  Based on initial ActionRegistry code by Till Crueger. | 
|---|
| [b2d8d0] | 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 | * | 
|---|
| [e4afb4] | 23 | * The Registry is a class where instances of other classes are stored and be retrieved | 
|---|
|  | 24 | * by a string token when desired. For this purpose a Registry should always be a singleton | 
|---|
|  | 25 | * (i.e. use both this Registry and the Singleton pattern to declare a Registry class). It | 
|---|
|  | 26 | * basically is simply a singleton container of a map, where the pointers to the class | 
|---|
|  | 27 | * instances are stored by a string key and can be retrieved thereby. | 
|---|
| [b2d8d0] | 28 | * | 
|---|
| [e4afb4] | 29 | * The available functions are as follows if your class instances to be stored in Registry | 
|---|
|  | 30 | * are of type 'foo': | 
|---|
| [b2d8d0] | 31 | * | 
|---|
|  | 32 | * - <code>foo* Registry<foo>::getByName()</code> : returns the instance of a specific | 
|---|
|  | 33 | *    class foo instance as a pointer associated with the given name | 
|---|
|  | 34 | * - <code>bool Registry<foo>::isPresentByName()</code> : returns whether an instance | 
|---|
|  | 35 | *    of class foo is present under the given name. | 
|---|
|  | 36 | * - <code>map<string,foo*>::iterator Registry<foo>::getBeginIter()</code> : returns an | 
|---|
|  | 37 | *    iterator to the beginning of the storage map (STL). | 
|---|
|  | 38 | * - <code>map<string,foo*>::const_iterator Registry<foo>::getBeginIter()</code> : returns a | 
|---|
|  | 39 | *    constant iterator to the beginning of the storage map (STL). | 
|---|
|  | 40 | * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns an | 
|---|
|  | 41 | *    iterator to the one step past the last element of the storage map (STL). | 
|---|
|  | 42 | * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns a | 
|---|
|  | 43 | *    constant iterator to the one step past the last element of the storage map (STL). | 
|---|
|  | 44 | * | 
|---|
|  | 45 | * In order to use this pattern, additionally to the requirements of the Singleton pattern, | 
|---|
|  | 46 | * do this: | 
|---|
|  | 47 | *   -# in the declaration derive your class from Registry<foo>, where foo is the class to be | 
|---|
|  | 48 | *      stored | 
|---|
|  | 49 | *   -# in the definition add CONSTRUCT_REGISTRY(foo) to the code such that the templated | 
|---|
|  | 50 | *      functions get instantiated there (otherwise you'll get undefined reference errors). | 
|---|
|  | 51 | * | 
|---|
|  | 52 | */ | 
|---|
|  | 53 |  | 
|---|
|  | 54 | template <class T> | 
|---|
|  | 55 | class Registry | 
|---|
|  | 56 | { | 
|---|
|  | 57 | public: | 
|---|
|  | 58 | Registry(); | 
|---|
|  | 59 | ~Registry(); | 
|---|
|  | 60 |  | 
|---|
| [e4afb4] | 61 | typedef typename std::map<const std::string,T*> instance_map; | 
|---|
|  | 62 | typedef typename std::map<const std::string,T*>::iterator                  iterator; | 
|---|
|  | 63 | typedef typename std::map<const std::string,T*>::const_iterator            const_iterator; | 
|---|
|  | 64 |  | 
|---|
|  | 65 | T* getByName(const std::string name) const; | 
|---|
|  | 66 | bool isPresentByName(const std::string name) const; | 
|---|
| [b2d8d0] | 67 | void registerInstance(T*); | 
|---|
|  | 68 | void unregisterInstance(T*); | 
|---|
| [6e1e10] | 69 | void cleanup(); | 
|---|
| [b2d8d0] | 70 |  | 
|---|
| [e4afb4] | 71 | iterator getBeginIter(); | 
|---|
|  | 72 | const_iterator getBeginIter() const; | 
|---|
|  | 73 | iterator getEndIter(); | 
|---|
|  | 74 | const_iterator getEndIter() const; | 
|---|
| [b2d8d0] | 75 |  | 
|---|
|  | 76 | private: | 
|---|
| [e4afb4] | 77 | instance_map InstanceMap; | 
|---|
| [b2d8d0] | 78 | }; | 
|---|
|  | 79 |  | 
|---|
|  | 80 | template <class T> std::ostream& operator<<(std::ostream& ost, const Registry<T>& m); | 
|---|
|  | 81 |  | 
|---|
|  | 82 | #endif /* REGISTRY_HPP_ */ | 
|---|