22 #include <vector> |
22 #include <vector> |
23 #include <list> |
23 #include <list> |
24 |
24 |
25 #include <lemon/core.h> |
25 #include <lemon/core.h> |
26 |
26 |
27 ///\ingroup graphbits |
27 //\ingroup graphbits |
28 ///\file |
28 //\file |
29 ///\brief Observer notifier for graph alteration observers. |
29 //\brief Observer notifier for graph alteration observers. |
30 |
30 |
31 namespace lemon { |
31 namespace lemon { |
32 |
32 |
33 /// \ingroup graphbits |
33 // \ingroup graphbits |
34 /// |
34 // |
35 /// \brief Notifier class to notify observes about alterations in |
35 // \brief Notifier class to notify observes about alterations in |
36 /// a container. |
36 // a container. |
37 /// |
37 // |
38 /// The simple graph's can be refered as two containers, one node container |
38 // The simple graph's can be refered as two containers, one node container |
39 /// and one edge container. But they are not standard containers they |
39 // and one edge container. But they are not standard containers they |
40 /// does not store values directly they are just key continars for more |
40 // does not store values directly they are just key continars for more |
41 /// value containers which are the node and edge maps. |
41 // value containers which are the node and edge maps. |
42 /// |
42 // |
43 /// The graph's node and edge sets can be changed as we add or erase |
43 // The graph's node and edge sets can be changed as we add or erase |
44 /// nodes and edges in the graph. LEMON would like to handle easily |
44 // nodes and edges in the graph. LEMON would like to handle easily |
45 /// that the node and edge maps should contain values for all nodes or |
45 // that the node and edge maps should contain values for all nodes or |
46 /// edges. If we want to check on every indicing if the map contains |
46 // edges. If we want to check on every indicing if the map contains |
47 /// the current indicing key that cause a drawback in the performance |
47 // the current indicing key that cause a drawback in the performance |
48 /// in the library. We use another solution we notify all maps about |
48 // in the library. We use another solution we notify all maps about |
49 /// an alteration in the graph, which cause only drawback on the |
49 // an alteration in the graph, which cause only drawback on the |
50 /// alteration of the graph. |
50 // alteration of the graph. |
51 /// |
51 // |
52 /// This class provides an interface to the container. The \e first() and \e |
52 // This class provides an interface to the container. The \e first() and \e |
53 /// next() member functions make possible to iterate on the keys of the |
53 // next() member functions make possible to iterate on the keys of the |
54 /// container. The \e id() function returns an integer id for each key. |
54 // container. The \e id() function returns an integer id for each key. |
55 /// The \e maxId() function gives back an upper bound of the ids. |
55 // The \e maxId() function gives back an upper bound of the ids. |
56 /// |
56 // |
57 /// For the proper functonality of this class, we should notify it |
57 // For the proper functonality of this class, we should notify it |
58 /// about each alteration in the container. The alterations have four type |
58 // about each alteration in the container. The alterations have four type |
59 /// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and |
59 // as \e add(), \e erase(), \e build() and \e clear(). The \e add() and |
60 /// \e erase() signals that only one or few items added or erased to or |
60 // \e erase() signals that only one or few items added or erased to or |
61 /// from the graph. If all items are erased from the graph or from an empty |
61 // from the graph. If all items are erased from the graph or from an empty |
62 /// graph a new graph is builded then it can be signaled with the |
62 // graph a new graph is builded then it can be signaled with the |
63 /// clear() and build() members. Important rule that if we erase items |
63 // clear() and build() members. Important rule that if we erase items |
64 /// from graph we should first signal the alteration and after that erase |
64 // from graph we should first signal the alteration and after that erase |
65 /// them from the container, on the other way on item addition we should |
65 // them from the container, on the other way on item addition we should |
66 /// first extend the container and just after that signal the alteration. |
66 // first extend the container and just after that signal the alteration. |
67 /// |
67 // |
68 /// The alteration can be observed with a class inherited from the |
68 // The alteration can be observed with a class inherited from the |
69 /// \e ObserverBase nested class. The signals can be handled with |
69 // \e ObserverBase nested class. The signals can be handled with |
70 /// overriding the virtual functions defined in the base class. The |
70 // overriding the virtual functions defined in the base class. The |
71 /// observer base can be attached to the notifier with the |
71 // observer base can be attached to the notifier with the |
72 /// \e attach() member and can be detached with detach() function. The |
72 // \e attach() member and can be detached with detach() function. The |
73 /// alteration handlers should not call any function which signals |
73 // alteration handlers should not call any function which signals |
74 /// an other alteration in the same notifier and should not |
74 // an other alteration in the same notifier and should not |
75 /// detach any observer from the notifier. |
75 // detach any observer from the notifier. |
76 /// |
76 // |
77 /// Alteration observers try to be exception safe. If an \e add() or |
77 // Alteration observers try to be exception safe. If an \e add() or |
78 /// a \e clear() function throws an exception then the remaining |
78 // a \e clear() function throws an exception then the remaining |
79 /// observeres will not be notified and the fulfilled additions will |
79 // observeres will not be notified and the fulfilled additions will |
80 /// be rolled back by calling the \e erase() or \e clear() |
80 // be rolled back by calling the \e erase() or \e clear() |
81 /// functions. Thence the \e erase() and \e clear() should not throw |
81 // functions. Thence the \e erase() and \e clear() should not throw |
82 /// exception. Actullay, it can be throw only \ref ImmediateDetach |
82 // exception. Actullay, it can be throw only \ref ImmediateDetach |
83 /// exception which detach the observer from the notifier. |
83 // exception which detach the observer from the notifier. |
84 /// |
84 // |
85 /// There are some place when the alteration observing is not completly |
85 // There are some place when the alteration observing is not completly |
86 /// reliable. If we want to carry out the node degree in the graph |
86 // reliable. If we want to carry out the node degree in the graph |
87 /// as in the \ref InDegMap and we use the reverseEdge that cause |
87 // as in the \ref InDegMap and we use the reverseEdge that cause |
88 /// unreliable functionality. Because the alteration observing signals |
88 // unreliable functionality. Because the alteration observing signals |
89 /// only erasing and adding but not the reversing it will stores bad |
89 // only erasing and adding but not the reversing it will stores bad |
90 /// degrees. The sub graph adaptors cannot signal the alterations because |
90 // degrees. The sub graph adaptors cannot signal the alterations because |
91 /// just a setting in the filter map can modify the graph and this cannot |
91 // just a setting in the filter map can modify the graph and this cannot |
92 /// be watched in any way. |
92 // be watched in any way. |
93 /// |
93 // |
94 /// \param _Container The container which is observed. |
94 // \param _Container The container which is observed. |
95 /// \param _Item The item type which is obserbved. |
95 // \param _Item The item type which is obserbved. |
96 |
96 |
97 template <typename _Container, typename _Item> |
97 template <typename _Container, typename _Item> |
98 class AlterationNotifier { |
98 class AlterationNotifier { |
99 public: |
99 public: |
100 |
100 |
101 typedef True Notifier; |
101 typedef True Notifier; |
102 |
102 |
103 typedef _Container Container; |
103 typedef _Container Container; |
104 typedef _Item Item; |
104 typedef _Item Item; |
105 |
105 |
106 /// \brief Exception which can be called from \e clear() and |
106 // \brief Exception which can be called from \e clear() and |
107 /// \e erase(). |
107 // \e erase(). |
108 /// |
108 // |
109 /// From the \e clear() and \e erase() function only this |
109 // From the \e clear() and \e erase() function only this |
110 /// exception is allowed to throw. The exception immediatly |
110 // exception is allowed to throw. The exception immediatly |
111 /// detaches the current observer from the notifier. Because the |
111 // detaches the current observer from the notifier. Because the |
112 /// \e clear() and \e erase() should not throw other exceptions |
112 // \e clear() and \e erase() should not throw other exceptions |
113 /// it can be used to invalidate the observer. |
113 // it can be used to invalidate the observer. |
114 struct ImmediateDetach {}; |
114 struct ImmediateDetach {}; |
115 |
115 |
116 /// \brief ObserverBase is the base class for the observers. |
116 // \brief ObserverBase is the base class for the observers. |
117 /// |
117 // |
118 /// ObserverBase is the abstract base class for the observers. |
118 // ObserverBase is the abstract base class for the observers. |
119 /// It will be notified about an item was inserted into or |
119 // It will be notified about an item was inserted into or |
120 /// erased from the graph. |
120 // erased from the graph. |
121 /// |
121 // |
122 /// The observer interface contains some pure virtual functions |
122 // The observer interface contains some pure virtual functions |
123 /// to override. The add() and erase() functions are |
123 // to override. The add() and erase() functions are |
124 /// to notify the oberver when one item is added or |
124 // to notify the oberver when one item is added or |
125 /// erased. |
125 // erased. |
126 /// |
126 // |
127 /// The build() and clear() members are to notify the observer |
127 // The build() and clear() members are to notify the observer |
128 /// about the container is built from an empty container or |
128 // about the container is built from an empty container or |
129 /// is cleared to an empty container. |
129 // is cleared to an empty container. |
130 |
|
131 class ObserverBase { |
130 class ObserverBase { |
132 protected: |
131 protected: |
133 typedef AlterationNotifier Notifier; |
132 typedef AlterationNotifier Notifier; |
134 |
133 |
135 friend class AlterationNotifier; |
134 friend class AlterationNotifier; |
136 |
135 |
137 /// \brief Default constructor. |
136 // \brief Default constructor. |
138 /// |
137 // |
139 /// Default constructor for ObserverBase. |
138 // Default constructor for ObserverBase. |
140 /// |
|
141 ObserverBase() : _notifier(0) {} |
139 ObserverBase() : _notifier(0) {} |
142 |
140 |
143 /// \brief Constructor which attach the observer into notifier. |
141 // \brief Constructor which attach the observer into notifier. |
144 /// |
142 // |
145 /// Constructor which attach the observer into notifier. |
143 // Constructor which attach the observer into notifier. |
146 ObserverBase(AlterationNotifier& nf) { |
144 ObserverBase(AlterationNotifier& nf) { |
147 attach(nf); |
145 attach(nf); |
148 } |
146 } |
149 |
147 |
150 /// \brief Constructor which attach the obserever to the same notifier. |
148 // \brief Constructor which attach the obserever to the same notifier. |
151 /// |
149 // |
152 /// Constructor which attach the obserever to the same notifier as |
150 // Constructor which attach the obserever to the same notifier as |
153 /// the other observer is attached to. |
151 // the other observer is attached to. |
154 ObserverBase(const ObserverBase& copy) { |
152 ObserverBase(const ObserverBase& copy) { |
155 if (copy.attached()) { |
153 if (copy.attached()) { |
156 attach(*copy.notifier()); |
154 attach(*copy.notifier()); |
157 } |
155 } |
158 } |
156 } |
159 |
157 |
160 /// \brief Destructor |
158 // \brief Destructor |
161 virtual ~ObserverBase() { |
159 virtual ~ObserverBase() { |
162 if (attached()) { |
160 if (attached()) { |
163 detach(); |
161 detach(); |
164 } |
162 } |
165 } |
163 } |
166 |
164 |
167 /// \brief Attaches the observer into an AlterationNotifier. |
165 // \brief Attaches the observer into an AlterationNotifier. |
168 /// |
166 // |
169 /// This member attaches the observer into an AlterationNotifier. |
167 // This member attaches the observer into an AlterationNotifier. |
170 /// |
|
171 void attach(AlterationNotifier& nf) { |
168 void attach(AlterationNotifier& nf) { |
172 nf.attach(*this); |
169 nf.attach(*this); |
173 } |
170 } |
174 |
171 |
175 /// \brief Detaches the observer into an AlterationNotifier. |
172 // \brief Detaches the observer into an AlterationNotifier. |
176 /// |
173 // |
177 /// This member detaches the observer from an AlterationNotifier. |
174 // This member detaches the observer from an AlterationNotifier. |
178 /// |
|
179 void detach() { |
175 void detach() { |
180 _notifier->detach(*this); |
176 _notifier->detach(*this); |
181 } |
177 } |
182 |
178 |
183 /// \brief Gives back a pointer to the notifier which the map |
179 // \brief Gives back a pointer to the notifier which the map |
184 /// attached into. |
180 // attached into. |
185 /// |
181 // |
186 /// This function gives back a pointer to the notifier which the map |
182 // This function gives back a pointer to the notifier which the map |
187 /// attached into. |
183 // attached into. |
188 /// |
|
189 Notifier* notifier() const { return const_cast<Notifier*>(_notifier); } |
184 Notifier* notifier() const { return const_cast<Notifier*>(_notifier); } |
190 |
185 |
191 /// Gives back true when the observer is attached into a notifier. |
186 // Gives back true when the observer is attached into a notifier. |
192 bool attached() const { return _notifier != 0; } |
187 bool attached() const { return _notifier != 0; } |
193 |
188 |
194 private: |
189 private: |
195 |
190 |
196 ObserverBase& operator=(const ObserverBase& copy); |
191 ObserverBase& operator=(const ObserverBase& copy); |
198 protected: |
193 protected: |
199 |
194 |
200 Notifier* _notifier; |
195 Notifier* _notifier; |
201 typename std::list<ObserverBase*>::iterator _index; |
196 typename std::list<ObserverBase*>::iterator _index; |
202 |
197 |
203 /// \brief The member function to notificate the observer about an |
198 // \brief The member function to notificate the observer about an |
204 /// item is added to the container. |
199 // item is added to the container. |
205 /// |
200 // |
206 /// The add() member function notificates the observer about an item |
201 // The add() member function notificates the observer about an item |
207 /// is added to the container. It have to be overrided in the |
202 // is added to the container. It have to be overrided in the |
208 /// subclasses. |
203 // subclasses. |
209 virtual void add(const Item&) = 0; |
204 virtual void add(const Item&) = 0; |
210 |
205 |
211 /// \brief The member function to notificate the observer about |
206 // \brief The member function to notificate the observer about |
212 /// more item is added to the container. |
207 // more item is added to the container. |
213 /// |
208 // |
214 /// The add() member function notificates the observer about more item |
209 // The add() member function notificates the observer about more item |
215 /// is added to the container. It have to be overrided in the |
210 // is added to the container. It have to be overrided in the |
216 /// subclasses. |
211 // subclasses. |
217 virtual void add(const std::vector<Item>& items) = 0; |
212 virtual void add(const std::vector<Item>& items) = 0; |
218 |
213 |
219 /// \brief The member function to notificate the observer about an |
214 // \brief The member function to notificate the observer about an |
220 /// item is erased from the container. |
215 // item is erased from the container. |
221 /// |
216 // |
222 /// The erase() member function notificates the observer about an |
217 // The erase() member function notificates the observer about an |
223 /// item is erased from the container. It have to be overrided in |
218 // item is erased from the container. It have to be overrided in |
224 /// the subclasses. |
219 // the subclasses. |
225 virtual void erase(const Item&) = 0; |
220 virtual void erase(const Item&) = 0; |
226 |
221 |
227 /// \brief The member function to notificate the observer about |
222 // \brief The member function to notificate the observer about |
228 /// more item is erased from the container. |
223 // more item is erased from the container. |
229 /// |
224 // |
230 /// The erase() member function notificates the observer about more item |
225 // The erase() member function notificates the observer about more item |
231 /// is erased from the container. It have to be overrided in the |
226 // is erased from the container. It have to be overrided in the |
232 /// subclasses. |
227 // subclasses. |
233 virtual void erase(const std::vector<Item>& items) = 0; |
228 virtual void erase(const std::vector<Item>& items) = 0; |
234 |
229 |
235 /// \brief The member function to notificate the observer about the |
230 // \brief The member function to notificate the observer about the |
236 /// container is built. |
231 // container is built. |
237 /// |
232 // |
238 /// The build() member function notificates the observer about the |
233 // The build() member function notificates the observer about the |
239 /// container is built from an empty container. It have to be |
234 // container is built from an empty container. It have to be |
240 /// overrided in the subclasses. |
235 // overrided in the subclasses. |
241 |
|
242 virtual void build() = 0; |
236 virtual void build() = 0; |
243 |
237 |
244 /// \brief The member function to notificate the observer about all |
238 // \brief The member function to notificate the observer about all |
245 /// items are erased from the container. |
239 // items are erased from the container. |
246 /// |
240 // |
247 /// The clear() member function notificates the observer about all |
241 // The clear() member function notificates the observer about all |
248 /// items are erased from the container. It have to be overrided in |
242 // items are erased from the container. It have to be overrided in |
249 /// the subclasses. |
243 // the subclasses. |
250 virtual void clear() = 0; |
244 virtual void clear() = 0; |
251 |
245 |
252 }; |
246 }; |
253 |
247 |
254 protected: |
248 protected: |
259 Observers _observers; |
253 Observers _observers; |
260 |
254 |
261 |
255 |
262 public: |
256 public: |
263 |
257 |
264 /// \brief Default constructor. |
258 // \brief Default constructor. |
265 /// |
259 // |
266 /// The default constructor of the AlterationNotifier. |
260 // The default constructor of the AlterationNotifier. |
267 /// It creates an empty notifier. |
261 // It creates an empty notifier. |
268 AlterationNotifier() |
262 AlterationNotifier() |
269 : container(0) {} |
263 : container(0) {} |
270 |
264 |
271 /// \brief Constructor. |
265 // \brief Constructor. |
272 /// |
266 // |
273 /// Constructor with the observed container parameter. |
267 // Constructor with the observed container parameter. |
274 AlterationNotifier(const Container& _container) |
268 AlterationNotifier(const Container& _container) |
275 : container(&_container) {} |
269 : container(&_container) {} |
276 |
270 |
277 /// \brief Copy Constructor of the AlterationNotifier. |
271 // \brief Copy Constructor of the AlterationNotifier. |
278 /// |
272 // |
279 /// Copy constructor of the AlterationNotifier. |
273 // Copy constructor of the AlterationNotifier. |
280 /// It creates only an empty notifier because the copiable |
274 // It creates only an empty notifier because the copiable |
281 /// notifier's observers have to be registered still into that notifier. |
275 // notifier's observers have to be registered still into that notifier. |
282 AlterationNotifier(const AlterationNotifier& _notifier) |
276 AlterationNotifier(const AlterationNotifier& _notifier) |
283 : container(_notifier.container) {} |
277 : container(_notifier.container) {} |
284 |
278 |
285 /// \brief Destructor. |
279 // \brief Destructor. |
286 /// |
280 // |
287 /// Destructor of the AlterationNotifier. |
281 // Destructor of the AlterationNotifier. |
288 /// |
|
289 ~AlterationNotifier() { |
282 ~AlterationNotifier() { |
290 typename Observers::iterator it; |
283 typename Observers::iterator it; |
291 for (it = _observers.begin(); it != _observers.end(); ++it) { |
284 for (it = _observers.begin(); it != _observers.end(); ++it) { |
292 (*it)->_notifier = 0; |
285 (*it)->_notifier = 0; |
293 } |
286 } |
294 } |
287 } |
295 |
288 |
296 /// \brief Sets the container. |
289 // \brief Sets the container. |
297 /// |
290 // |
298 /// Sets the container. |
291 // Sets the container. |
299 void setContainer(const Container& _container) { |
292 void setContainer(const Container& _container) { |
300 container = &_container; |
293 container = &_container; |
301 } |
294 } |
302 |
295 |
303 protected: |
296 protected: |
304 |
297 |
305 AlterationNotifier& operator=(const AlterationNotifier&); |
298 AlterationNotifier& operator=(const AlterationNotifier&); |
306 |
299 |
307 public: |
300 public: |
308 |
301 |
309 |
302 // \brief First item in the container. |
310 |
303 // |
311 /// \brief First item in the container. |
304 // Returns the first item in the container. It is |
312 /// |
305 // for start the iteration on the container. |
313 /// Returns the first item in the container. It is |
|
314 /// for start the iteration on the container. |
|
315 void first(Item& item) const { |
306 void first(Item& item) const { |
316 container->first(item); |
307 container->first(item); |
317 } |
308 } |
318 |
309 |
319 /// \brief Next item in the container. |
310 // \brief Next item in the container. |
320 /// |
311 // |
321 /// Returns the next item in the container. It is |
312 // Returns the next item in the container. It is |
322 /// for iterate on the container. |
313 // for iterate on the container. |
323 void next(Item& item) const { |
314 void next(Item& item) const { |
324 container->next(item); |
315 container->next(item); |
325 } |
316 } |
326 |
317 |
327 /// \brief Returns the id of the item. |
318 // \brief Returns the id of the item. |
328 /// |
319 // |
329 /// Returns the id of the item provided by the container. |
320 // Returns the id of the item provided by the container. |
330 int id(const Item& item) const { |
321 int id(const Item& item) const { |
331 return container->id(item); |
322 return container->id(item); |
332 } |
323 } |
333 |
324 |
334 /// \brief Returns the maximum id of the container. |
325 // \brief Returns the maximum id of the container. |
335 /// |
326 // |
336 /// Returns the maximum id of the container. |
327 // Returns the maximum id of the container. |
337 int maxId() const { |
328 int maxId() const { |
338 return container->maxId(Item()); |
329 return container->maxId(Item()); |
339 } |
330 } |
340 |
331 |
341 protected: |
332 protected: |