27 namespace lemon { |
27 namespace lemon { |
28 |
28 |
29 /// \addtogroup graphmaps |
29 /// \addtogroup graphmaps |
30 /// @{ |
30 /// @{ |
31 |
31 |
32 /// Registry class to register objects observes alterations in the graph. |
32 /// \brief Registry class to register objects observes alterations in |
33 |
33 /// the graph. |
|
34 /// |
34 /// This class is a registry for the objects which observe the |
35 /// This class is a registry for the objects which observe the |
35 /// alterations in a container. The alteration observers can be attached |
36 /// alterations in a container. The alteration observers can be attached |
36 /// to and detached from the registry. The observers have to inherit |
37 /// to and detached from the registry. The observers have to inherit |
37 /// from the \ref AlterationNotifier::ObserverBase and override |
38 /// from the \ref AlterationNotifier::ObserverBase and override |
38 /// the virtual functions in that. |
39 /// the virtual functions in that. |
39 /// |
40 /// |
40 /// The most important application of the alteration observing is the |
41 /// The most important application of the alteration observing is the |
41 /// dynamic map implementation when the observers are observing the |
42 /// dynamic map implementation. |
42 /// alterations in the graph. |
|
43 /// |
43 /// |
44 /// \param _Item The item type what the observers are observing, usually |
44 /// \param _Item The item type what the observers are observing, usually |
45 /// edge or node. |
45 /// edge or node. |
46 /// |
46 /// |
47 /// \author Balazs Dezso |
47 /// \author Balazs Dezso |
72 protected: |
72 protected: |
73 typedef AlterationNotifier Registry; |
73 typedef AlterationNotifier Registry; |
74 |
74 |
75 friend class AlterationNotifier; |
75 friend class AlterationNotifier; |
76 |
76 |
77 /// Default constructor. |
77 /// \brief Default constructor. |
78 |
78 /// |
79 /// Default constructor for ObserverBase. |
79 /// Default constructor for ObserverBase. |
80 /// |
80 /// |
81 ObserverBase() : registry(0) {} |
81 ObserverBase() : registry(0) {} |
82 |
82 |
83 virtual ~ObserverBase() {} |
83 virtual ~ObserverBase() {} |
84 |
84 |
85 /// Attaches the observer into an AlterationNotifier. |
85 /// \brief Attaches the observer into an AlterationNotifier. |
86 |
86 /// |
87 /// This member attaches the observer into an AlterationNotifier. |
87 /// This member attaches the observer into an AlterationNotifier. |
88 /// |
88 /// |
89 void attach(AlterationNotifier& r) { |
89 void attach(AlterationNotifier& r) { |
90 registry = &r; |
90 registry = &r; |
91 registry->attach(*this); |
91 registry->attach(*this); |
92 } |
92 } |
93 |
93 |
94 /// Detaches the observer into an AlterationNotifier. |
94 /// \brief Detaches the observer into an AlterationNotifier. |
95 |
95 /// |
96 /// This member detaches the observer from an AlterationNotifier. |
96 /// This member detaches the observer from an AlterationNotifier. |
97 /// |
97 /// |
98 void detach() { |
98 void detach() { |
99 if (registry) { |
99 if (registry) { |
100 registry->detach(*this); |
100 registry->detach(*this); |
129 /// |
129 /// |
130 /// The add() member function notificates the observer about an item |
130 /// The add() member function notificates the observer about an item |
131 /// is added to the container. It have to be overrided in the |
131 /// is added to the container. It have to be overrided in the |
132 /// subclasses. |
132 /// subclasses. |
133 |
133 |
134 virtual void add(const Item&) = 0; |
134 virtual void add(const Item&) = 0; |
135 |
135 |
|
136 /// \brief The member function to notificate the observer about |
|
137 /// simulitem is added to the container. |
|
138 /// |
|
139 /// The add() member function notificates the observer about an item |
|
140 /// is added to the container. It have to be overrided in the |
|
141 /// subclasses. |
|
142 |
|
143 virtual void add(const std::vector<Item>& items) { |
|
144 for (int i = 0; i < (int)items.size(); ++i) { |
|
145 add(items[i]); |
|
146 } |
|
147 } |
136 |
148 |
137 /// \brief The member function to notificate the observer about an |
149 /// \brief The member function to notificate the observer about an |
138 /// item is erased from the container. |
150 /// item is erased from the container. |
139 /// |
151 /// |
140 /// The erase() member function notificates the observer about an |
152 /// The erase() member function notificates the observer about an |
141 /// item is erased from the container. It have to be overrided in |
153 /// item is erased from the container. It have to be overrided in |
142 /// the subclasses. |
154 /// the subclasses. |
143 |
155 |
144 virtual void erase(const Item&) = 0; |
156 virtual void erase(const Item&) = 0; |
|
157 |
|
158 virtual void erase(const std::vector<Item>& items) { |
|
159 for (int i = 0; i < (int)items.size(); ++i) { |
|
160 add(items[i]); |
|
161 } |
|
162 } |
145 |
163 |
146 /// \brief The member function to notificate the observer about the |
164 /// \brief The member function to notificate the observer about the |
147 /// container is built. |
165 /// container is built. |
148 /// |
166 /// |
149 /// The build() member function notificates the observer about the |
167 /// The build() member function notificates the observer about the |
226 base.registry = 0; |
244 base.registry = 0; |
227 } |
245 } |
228 |
246 |
229 public: |
247 public: |
230 |
248 |
231 /// Notifies all the registered observers about an Item added to the container. |
249 /// \brief Notifies all the registered observers about an Item added to |
232 |
250 /// the container. |
233 /// It notifies all the registered observers about an Item added to the container. |
251 /// |
|
252 /// It notifies all the registered observers about an Item added to |
|
253 /// the container. |
234 /// |
254 /// |
235 void add(const Item& key) { |
255 void add(const Item& item) { |
236 typename Container::iterator it; |
256 typename Container::iterator it; |
237 for (it = container.begin(); it != container.end(); ++it) { |
257 for (it = container.begin(); it != container.end(); ++it) { |
238 (*it)->add(key); |
258 (*it)->add(item); |
239 } |
259 } |
240 } |
260 } |
241 |
261 |
242 /// Notifies all the registered observers about an Item erased from the container. |
262 /// \brief Notifies all the registered observers about more Item added to |
243 |
263 /// the container. |
244 /// It notifies all the registered observers about an Item erased from the container. |
264 /// |
|
265 /// It notifies all the registered observers about more Item added to |
|
266 /// the container. |
|
267 /// |
|
268 void add(const std::vector<Item>& items) { |
|
269 typename Container::iterator it; |
|
270 for (it = container.begin(); it != container.end(); ++it) { |
|
271 (*it)->add(items); |
|
272 } |
|
273 } |
|
274 |
|
275 /// \brief Notifies all the registered observers about an Item erased from |
|
276 /// the container. |
|
277 /// |
|
278 /// It notifies all the registered observers about an Item erased from |
|
279 /// the container. |
245 /// |
280 /// |
246 void erase(const Item& key) { |
281 void erase(const Item& key) { |
247 typename Container::iterator it; |
282 typename Container::iterator it; |
248 for (it = container.begin(); it != container.end(); ++it) { |
283 for (it = container.begin(); it != container.end(); ++it) { |
249 (*it)->erase(key); |
284 (*it)->erase(key); |
250 } |
285 } |
251 } |
286 } |
|
287 |
|
288 /// \brief Notifies all the registered observers about more Item erased |
|
289 /// from the container. |
|
290 /// |
|
291 /// It notifies all the registered observers about more Item erased from |
|
292 /// the container. |
|
293 /// |
|
294 void erase(const std::vector<Item>& items) { |
|
295 typename Container::iterator it; |
|
296 for (it = container.begin(); it != container.end(); ++it) { |
|
297 (*it)->erase(items); |
|
298 } |
|
299 } |
252 |
300 |
253 |
301 |
254 /// Notifies all the registered observers about the container is built. |
302 /// \brief Notifies all the registered observers about the container is |
255 |
303 /// built. |
|
304 /// |
256 /// Notifies all the registered observers about the container is built |
305 /// Notifies all the registered observers about the container is built |
257 /// from an empty container. |
306 /// from an empty container. |
258 void build() { |
307 void build() { |
259 typename Container::iterator it; |
308 typename Container::iterator it; |
260 for (it = container.begin(); it != container.end(); ++it) { |
309 for (it = container.begin(); it != container.end(); ++it) { |
261 (*it)->build(); |
310 (*it)->build(); |
262 } |
311 } |
263 } |
312 } |
264 |
313 |
265 |
314 |
266 /// Notifies all the registered observers about all Items are erased. |
315 /// \brief Notifies all the registered observers about all Items are |
267 |
316 /// erased. |
|
317 /// |
268 /// Notifies all the registered observers about all Items are erased |
318 /// Notifies all the registered observers about all Items are erased |
269 /// from the container. |
319 /// from the container. |
270 void clear() { |
320 void clear() { |
271 typename Container::iterator it; |
321 typename Container::iterator it; |
272 for (it = container.begin(); it != container.end(); ++it) { |
322 for (it = container.begin(); it != container.end(); ++it) { |