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