6 using namespace std; |
6 using namespace std; |
7 |
7 |
8 namespace hugo { |
8 namespace hugo { |
9 |
9 |
10 /** |
10 /** |
11 Registry class to register edge or node maps in the graph. The |
11 * Registry class to register edge or node maps into the graph. The |
12 registry helps you to implement an observer pattern. If you add |
12 * registry helps you to implement an observer pattern. If you add |
13 or erase an edge or node you must notify all the maps about the |
13 * or erase an edge or node you must notify all the maps about the |
14 event. |
14 * event. |
15 */ |
15 */ |
16 template <typename G, typename K, typename KIt> |
16 template <typename G, typename K, typename KIt> |
17 class MapRegistry { |
17 class MapRegistry { |
18 public: |
18 public: |
19 typedef G Graph; |
19 typedef G Graph; |
20 typedef K Key; |
20 typedef K Key; |
21 typedef KIt KeyIt; |
21 typedef KIt KeyIt; |
22 |
22 |
23 |
23 |
24 |
24 |
25 ///. |
25 /** |
26 |
26 * MapBase is the base class of the registered maps. |
27 ///. |
27 * It defines the core modification operations on the maps and |
28 /// |
28 * implements some helper functions. |
|
29 */ |
29 class MapBase { |
30 class MapBase { |
30 public: |
31 public: |
31 typedef G Graph; |
32 typedef G Graph; |
32 typedef MapRegistry<G, K, KIt> Registry; |
33 typedef MapRegistry<G, K, KIt> Registry; |
33 typedef K Key; |
34 typedef K Key; |
34 typedef KIt KeyIt; |
35 typedef KIt KeyIt; |
35 |
36 |
36 friend class Registry; |
37 friend class Registry; |
37 |
38 |
38 /** |
39 /** |
39 Default constructor. |
40 * Default constructor for MapBase. |
40 */ |
41 */ |
41 |
42 |
42 MapBase() : graph(0), registry(0) {} |
43 MapBase() : graph(0), registry(0) {} |
43 |
44 |
44 /** |
45 /** |
45 Simple constructor to register into a graph registry. |
46 * Simple constructor to register into a graph registry. |
46 */ |
47 */ |
47 |
48 |
48 MapBase(Graph& g, Registry& r) : graph(&g), registry(0) { |
49 MapBase(const Graph& g, Registry& r) : graph(&g), registry(0) { |
49 r.attach(*this); |
50 r.attach(*this); |
50 } |
51 } |
51 |
52 |
52 /** |
53 /** |
53 Copy constructor with registering into the map. |
54 * Copy constructor to register into the registry. |
54 */ |
55 */ |
55 |
56 |
56 MapBase(const MapBase& copy) : registry(0), graph(copy.graph) { |
57 MapBase(const MapBase& copy) : registry(0), graph(copy.graph) { |
57 if (copy.registry) { |
58 if (copy.registry) { |
58 copy.registry->attach(*this); |
59 copy.registry->attach(*this); |
59 } |
60 } |
60 } |
61 } |
61 |
62 |
62 /** |
63 /** |
63 Assign operator. |
64 * Assign operator. |
64 */ |
65 */ |
65 |
66 |
66 const MapBase& operator=(const MapBase& copy) { |
67 const MapBase& operator=(const MapBase& copy) { |
67 if (registry) { |
68 if (registry) { |
68 registry->detach(*this); |
69 registry->detach(*this); |
132 |
141 |
133 }; |
142 }; |
134 |
143 |
135 protected: |
144 protected: |
136 |
145 |
|
146 /** |
|
147 * The container type of the maps. |
|
148 */ |
137 typedef std::vector<MapBase*> Container; |
149 typedef std::vector<MapBase*> Container; |
|
150 |
|
151 /** |
|
152 * The container of the registered maps. |
|
153 */ |
138 Container container; |
154 Container container; |
139 |
155 |
140 |
156 |
141 public: |
157 public: |
142 |
158 |
143 ///. |
159 /** |
|
160 * Default Constructor of the MapRegistry. It creates an empty registry. |
|
161 */ |
144 MapRegistry() {} |
162 MapRegistry() {} |
145 |
163 |
146 ///. |
164 /** |
|
165 * Copy Constructor of the MapRegistry. The new registry does not steal |
|
166 * the maps from the right value. The new registry will be an empty. |
|
167 */ |
147 MapRegistry(const MapRegistry&) {} |
168 MapRegistry(const MapRegistry&) {} |
148 |
169 |
149 ///. |
170 /** |
|
171 * Assign operator. The left value does not steal the maps |
|
172 * from the right value. The left value will be an empty registry. |
|
173 */ |
150 MapRegistry& operator=(const MapRegistry&) { |
174 MapRegistry& operator=(const MapRegistry&) { |
151 for (it = container.begin(); it != container.end(); ++it) { |
175 for (it = container.begin(); it != container.end(); ++it) { |
152 (*it)->destroy(); |
176 (*it)->destroy(); |
153 (*it)->graph = 0; |
177 (*it)->graph = 0; |
154 (*it)->registry = 0; |
178 (*it)->registry = 0; |
155 } |
179 } |
156 } |
180 } |
157 |
181 |
158 ///. |
182 /** |
|
183 * Destructor of the MapRegistry. |
|
184 */ |
159 ~MapRegistry() { |
185 ~MapRegistry() { |
160 typename Container::iterator it; |
186 typename Container::iterator it; |
161 for (it = container.begin(); it != container.end(); ++it) { |
187 for (it = container.begin(); it != container.end(); ++it) { |
162 (*it)->destroy(); |
188 (*it)->destroy(); |
163 (*it)->registry = 0; |
189 (*it)->registry = 0; |
166 } |
192 } |
167 |
193 |
168 |
194 |
169 public: |
195 public: |
170 |
196 |
171 ///. |
197 /** |
|
198 * Attach a map into thr registry. If the map has been attached |
|
199 * into an other registry it is detached from that automaticly. |
|
200 */ |
172 void attach(MapBase& map) { |
201 void attach(MapBase& map) { |
173 if (map.registry) { |
202 if (map.registry) { |
174 map.registry->detach(map); |
203 map.registry->detach(map); |
175 } |
204 } |
176 container.push_back(&map); |
205 container.push_back(&map); |
177 map.registry = this; |
206 map.registry = this; |
178 map.registry_index = container.size()-1; |
207 map.registry_index = container.size()-1; |
179 } |
208 } |
180 |
209 |
181 ///. |
210 /** |
|
211 * Detach the map from the registry. |
|
212 */ |
182 void detach(MapBase& map) { |
213 void detach(MapBase& map) { |
183 container.back()->registry_index = map.registry_index; |
214 container.back()->registry_index = map.registry_index; |
184 container[map.registry_index] = container.back(); |
215 container[map.registry_index] = container.back(); |
185 container.pop_back(); |
216 container.pop_back(); |
186 map.registry = 0; |
217 map.registry = 0; |
187 map.graph = 0; |
218 map.graph = 0; |
188 } |
219 } |
189 |
220 |
190 |
221 |
191 ///. |
222 /** |
|
223 * Notify all the registered maps about a Key added. |
|
224 */ |
192 virtual void add(Key& key) { |
225 virtual void add(Key& key) { |
193 typename Container::iterator it; |
226 typename Container::iterator it; |
194 for (it = container.begin(); it != container.end(); ++it) { |
227 for (it = container.begin(); it != container.end(); ++it) { |
195 (*it)->add(key); |
228 (*it)->add(key); |
196 } |
229 } |
197 } |
230 } |
198 |
231 |
199 ///. |
232 /** |
|
233 * Notify all the registered maps about a Key erased. |
|
234 */ |
200 virtual void erase(Key& key) { |
235 virtual void erase(Key& key) { |
201 typename Container::iterator it; |
236 typename Container::iterator it; |
202 for (it = container.begin(); it != container.end(); ++it) { |
237 for (it = container.begin(); it != container.end(); ++it) { |
203 (*it)->erase(key); |
238 (*it)->erase(key); |
204 } |
239 } |