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