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