| [b2d8d0] | 1 | /*
 | 
|---|
 | 2 |  * Registry_impl.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Jul 28, 2010
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef REGISTRY_IMPL_HPP_
 | 
|---|
 | 9 | #define REGISTRY_IMPL_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 12 | 
 | 
|---|
 | 13 | #include "Patterns/Registry.hpp"
 | 
|---|
 | 14 | #include "Patterns/Singleton_impl.hpp"
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include "Helpers/Assert.hpp"
 | 
|---|
| [311d688] | 17 | #include <iosfwd>
 | 
|---|
| [b2d8d0] | 18 | 
 | 
|---|
 | 19 | /** Constructor for class Registry.
 | 
|---|
 | 20 |  */
 | 
|---|
 | 21 | template <class T> Registry<T>::Registry()
 | 
|---|
 | 22 | {}
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | /** Destructor for class Registry.
 | 
|---|
 | 25 |  */
 | 
|---|
 | 26 | template <class T> Registry<T>::~Registry()
 | 
|---|
| [2a6a2c] | 27 | {}
 | 
|---|
| [b2d8d0] | 28 | 
 | 
|---|
 | 29 | /** Returns pointer to an instance named by \a name.
 | 
|---|
 | 30 |  * \param name name of instance
 | 
|---|
 | 31 |  * \return pointer to instance
 | 
|---|
 | 32 |  */
 | 
|---|
 | 33 | template <class T> T* Registry<T>::getByName(const std::string name){
 | 
|---|
 | 34 |   typename std::map<const std::string,T*>::iterator iter;
 | 
|---|
 | 35 |   iter = InstanceMap.find(name);
 | 
|---|
 | 36 |   ASSERT(iter!=InstanceMap.end(),"Query for an instance not stored in registry");
 | 
|---|
 | 37 |   return iter->second;
 | 
|---|
 | 38 | }
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | /** States whether instance is present or not.
 | 
|---|
 | 41 |  * \note This is needed as Registry<T>::getByName() ASSERT()s that instance is in std::map.
 | 
|---|
 | 42 |  * \param name name of instance
 | 
|---|
 | 43 |  * \return true - v present, false - instance absent
 | 
|---|
 | 44 |  */
 | 
|---|
 | 45 | template <class T>bool Registry<T>::isPresentByName(const std::string name){
 | 
|---|
 | 46 |   typename std::map<const std::string,T*>::iterator iter;
 | 
|---|
 | 47 |   iter = InstanceMap.find(name);
 | 
|---|
 | 48 |   return iter!=InstanceMap.end();
 | 
|---|
 | 49 | }
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | /** Registers an instance with the Registry.
 | 
|---|
 | 52 |  * \param *instance pointer to T.
 | 
|---|
 | 53 |  */
 | 
|---|
 | 54 | template <class T>void Registry<T>::registerInstance(T* instance){
 | 
|---|
 | 55 |   std::pair<typename std::map<const std::string,T*>::iterator,bool> ret;
 | 
|---|
 | 56 |   //cout << "Trying to register instance with name " << instance->getName() << "." << endl;
 | 
|---|
 | 57 |   ret = InstanceMap.insert(typename std::pair<const std::string,T*>(instance->getName(),instance));
 | 
|---|
 | 58 |   ASSERT(ret.second,"Two instances with the same name added to registry");
 | 
|---|
 | 59 | }
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | /** Unregisters an instance.
 | 
|---|
 | 62 |  * \param *instance pointer to instance.
 | 
|---|
 | 63 |  */
 | 
|---|
 | 64 | template <class T>void Registry<T>::unregisterInstance(T* instance){
 | 
|---|
 | 65 |   //cout << "Unregistering instance with name " << instance->getName() << "." << endl;
 | 
|---|
 | 66 |   InstanceMap.erase(instance->getName());
 | 
|---|
 | 67 | }
 | 
|---|
 | 68 | 
 | 
|---|
| [6e1e10] | 69 | /** Removes every instance from the registry.
 | 
|---|
 | 70 |  */
 | 
|---|
 | 71 | template <class T>void Registry<T>::cleanup()
 | 
|---|
 | 72 | {
 | 
|---|
 | 73 |   typename std::map<const std::string,T*>::iterator iter;
 | 
|---|
 | 74 |   for(iter=InstanceMap.begin();iter!=InstanceMap.end();++iter) {
 | 
|---|
 | 75 |     delete iter->second;
 | 
|---|
 | 76 |   }
 | 
|---|
 | 77 |   InstanceMap.clear();
 | 
|---|
 | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | 
 | 
|---|
| [b2d8d0] | 81 | /** Returns an iterator pointing to the start of the std::map of instance's.
 | 
|---|
 | 82 |  * \return begin iterator
 | 
|---|
 | 83 |  */
 | 
|---|
 | 84 | template <class T>
 | 
|---|
 | 85 | typename std::map<const std::string,T*>::iterator Registry<T>::getBeginIter()
 | 
|---|
 | 86 | {
 | 
|---|
 | 87 |   return InstanceMap.begin();
 | 
|---|
 | 88 | }
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 | /** Returns an iterator pointing to the end of the std::map of instance's.
 | 
|---|
 | 91 |  * \return end iterator
 | 
|---|
 | 92 |  */
 | 
|---|
 | 93 | template <class T>
 | 
|---|
 | 94 | typename std::map<const std::string,T*>::iterator Registry<T>::getEndIter()
 | 
|---|
 | 95 | {
 | 
|---|
 | 96 |   return InstanceMap.end();
 | 
|---|
 | 97 | }
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 | /** Returns a const iterator pointing to the start of the std::map of instance's.
 | 
|---|
 | 100 |  * \return constant begin iterator
 | 
|---|
 | 101 |  */
 | 
|---|
 | 102 | template <class T>
 | 
|---|
 | 103 | typename std::map<const std::string,T*>::const_iterator Registry<T>::getBeginIter() const
 | 
|---|
 | 104 | {
 | 
|---|
 | 105 |   return InstanceMap.begin();
 | 
|---|
 | 106 | }
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | /** Returns a const iterator pointing to the end of the std::map of instance's.
 | 
|---|
 | 109 |  * \return constant end iterator
 | 
|---|
 | 110 |  */
 | 
|---|
 | 111 | template <class T>
 | 
|---|
 | 112 | typename std::map<const std::string,T*>::const_iterator Registry<T>::getEndIter() const
 | 
|---|
 | 113 | {
 | 
|---|
 | 114 |   return InstanceMap.end();
 | 
|---|
 | 115 | }
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | /** Prints the contents of the Registry \a &m to \a &ost.
 | 
|---|
 | 118 |  * \param &ost output stream
 | 
|---|
 | 119 |  * \param &m reference to Registry
 | 
|---|
 | 120 |  * \return reference to the above out stream for concatenation
 | 
|---|
 | 121 |  */
 | 
|---|
 | 122 | template <class T>
 | 
|---|
 | 123 | std::ostream& operator<<(std::ostream& ost, const Registry<T>& m)
 | 
|---|
 | 124 | {
 | 
|---|
 | 125 |   ost << "Registry contains:" << std::endl;
 | 
|---|
 | 126 |   for (typename std::map<const std::string,T*>::const_iterator iter = m.getBeginIter(); iter != m.getEndIter(); ++iter) {
 | 
|---|
 | 127 |     ost << "\t" << iter->first << " with pointer " << iter->second << std::endl;
 | 
|---|
 | 128 |   }
 | 
|---|
 | 129 |   return ost;
 | 
|---|
 | 130 | };
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | /**
 | 
|---|
 | 133 |  * This define allows simple instantiation of the necessary registryfunctions
 | 
|---|
 | 134 |  * at a chosen place.
 | 
|---|
 | 135 |  */
 | 
|---|
 | 136 | #define CONSTRUCT_REGISTRY(name) \
 | 
|---|
 | 137 |     template name* Registry<name>::getByName(const std::string name); \
 | 
|---|
 | 138 |     template bool Registry<name>::isPresentByName(const std::string name); \
 | 
|---|
 | 139 |     template void Registry<name>::registerInstance(name*); \
 | 
|---|
 | 140 |     template void Registry<name>::unregisterInstance(name*); \
 | 
|---|
 | 141 |     template std::map<const std::string,name*>::iterator Registry<name>::getBeginIter(); \
 | 
|---|
 | 142 |     template std::map<const std::string,name*>::const_iterator Registry<name>::getBeginIter() const; \
 | 
|---|
 | 143 |     template std::map<const std::string,name*>::iterator Registry<name>::getEndIter(); \
 | 
|---|
 | 144 |     template std::map<const std::string,name*>::const_iterator Registry<name>::getEndIter() const;
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 | #endif /* REGISTRY_IMPL_HPP_ */
 | 
|---|