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