Added the SimpleController class, and removed the first version of SimAnn in favour of the second.
2 * src/lemon/observer_registry.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_ALTERATION_OBSERVER_REGISTRY_H
18 #define LEMON_ALTERATION_OBSERVER_REGISTRY_H
25 ///\brief Observer registry for graph alteration observers.
31 /// \addtogroup graphmaps
34 /// Registry class to register objects observes alterations in the graph.
36 /// This class is a registry for the objects which observe the
37 /// alterations in a container. The alteration observers can be attached
38 /// to and detached from the registry. The observers have to inherit
39 /// from the \ref AlterationObserverRegistry::ObserverBase and override
40 /// the virtual functions in that.
42 /// The most important application of the alteration observing is the
43 /// dynamic map implementation when the observers are observing the
44 /// alterations in the graph.
46 /// \param _Item The item type what the observers are observing, usually
49 /// \author Balazs Dezso
51 template <typename _Item>
52 class AlterationObserverRegistry {
56 /// ObserverBase is the base class for the observers.
58 /// ObserverBase is the abstract base class for the observers.
59 /// It will be notified about an item was inserted into or
60 /// erased from the graph.
62 /// The observer interface contains some pure virtual functions
63 /// to override. The add() and erase() functions are
64 /// to notify the oberver when one item is added or
67 /// The build() and clear() members are to notify the observer
68 /// about the container is builded from an empty container or
69 /// is cleared to an empty container.
71 /// \author Balazs Dezso
75 typedef AlterationObserverRegistry Registry;
77 friend class Registry;
79 /// Default constructor.
81 /// Default constructor for ObserverBase.
83 ObserverBase() : registry(0) {}
85 virtual ~ObserverBase() {}
87 /// Attaches the observer into an AlterationObserverRegistry.
89 /// This member attaches the observer into an AlterationObserverRegistry.
91 void attach(AlterationObserverRegistry& r) {
93 registry->attach(*this);
96 /// Detaches the observer into an AlterationObserverRegistry.
98 /// This member detaches the observer from an AlterationObserverRegistry.
102 registry->detach(*this);
107 /// Gives back a pointer to the registry what the map attached into.
109 /// This function gives back a pointer to the registry what the map
112 Registry* getRegistry() const { return const_cast<Registry*>(registry); }
114 /// Gives back true when the observer is attached into a registry.
115 bool attached() const { return registry != 0; }
119 ObserverBase(const ObserverBase& copy);
120 ObserverBase& operator=(const ObserverBase& copy);
129 /// \brief The member function to notificate the observer about an
130 /// item is added to the container.
132 /// The add() member function notificates the observer about an item
133 /// is added to the container. It have to be overrided in the
136 virtual void add(const Item&) = 0;
139 /// \brief The member function to notificate the observer about an
140 /// item is erased from the container.
142 /// The erase() member function notificates the observer about an
143 /// item is erased from the container. It have to be overrided in
146 virtual void erase(const Item&) = 0;
148 /// \brief The member function to notificate the observer about the
149 /// container is builded.
151 /// The build() member function notificates the observer about the
152 /// container is builded from an empty container. It have to be
153 /// overrided in the subclasses.
155 virtual void build() = 0;
157 /// \brief The member function to notificate the observer about all
158 /// items are erased from the container.
160 /// The clear() member function notificates the observer about all
161 /// items are erased from the container. It have to be overrided in
164 virtual void clear() = 0;
171 typedef std::vector<ObserverBase*> Container;
178 /// Default constructor.
181 /// The default constructor of the AlterationObserverRegistry.
182 /// It creates an empty registry.
183 AlterationObserverRegistry() {}
185 /// Copy Constructor of the AlterationObserverRegistry.
187 /// Copy constructor of the AlterationObserverRegistry.
188 /// It creates only an empty registry because the copiable
189 /// registry's observers have to be registered still into that registry.
190 AlterationObserverRegistry(const AlterationObserverRegistry&) {}
194 /// Assign operator for the AlterationObserverRegistry.
195 /// It makes the registry only empty because the copiable
196 /// registry's observers have to be registered still into that registry.
197 AlterationObserverRegistry& operator=(const AlterationObserverRegistry&) {
198 typename Container::iterator it;
199 for (it = container.begin(); it != container.end(); ++it) {
206 /// Destructor of the AlterationObserverRegistry.
208 ~AlterationObserverRegistry() {
209 typename Container::iterator it;
210 for (it = container.begin(); it != container.end(); ++it) {
218 void attach(ObserverBase& observer) {
219 container.push_back(&observer);
220 observer.registry = this;
221 observer.registry_index = container.size()-1;
224 void detach(ObserverBase& base) {
225 container.back()->registry_index = base.registry_index;
226 container[base.registry_index] = container.back();
227 container.pop_back();
233 /// Notifies all the registered observers about an Item added to the container.
235 /// It notifies all the registered observers about an Item added to the container.
237 void add(const Item& key) {
238 typename Container::iterator it;
239 for (it = container.begin(); it != container.end(); ++it) {
244 /// Notifies all the registered observers about an Item erased from the container.
246 /// It notifies all the registered observers about an Item erased from the container.
248 void erase(const Item& key) {
249 typename Container::iterator it;
250 for (it = container.begin(); it != container.end(); ++it) {
256 /// Notifies all the registered observers about the container is builded.
258 /// Notifies all the registered observers about the container is builded
259 /// from an empty container.
261 typename Container::iterator it;
262 for (it = container.begin(); it != container.end(); ++it) {
268 /// Notifies all the registered observers about all Items are erased.
270 /// Notifies all the registered observers about all Items are erased
271 /// from the container.
273 typename Container::iterator it;
274 for (it = container.begin(); it != container.end(); ++it) {
281 /// Class to extend a graph functionality with the possibility of alteration observing.
283 /// AlterableGraphExtender extends the _Base graphs functionality with the possibility of
284 /// alteration observing. It defines two observer registrys for the nodes and mapes.
286 /// \param _Base is the base class to extend.
288 /// \pre _Base is conform to the BaseGraphComponent concept.
290 /// \post AlterableGraphExtender<_Base> is conform to the AlterableGraphComponent concept.
292 /// \author Balazs Dezso
294 template <typename _Base>
295 class AlterableGraphExtender : public _Base {
298 typedef AlterableGraphExtender Graph;
299 typedef _Base Parent;
301 typedef typename Parent::Node Node;
302 typedef typename Parent::Edge Edge;
304 /// The node observer registry.
305 typedef AlterationObserverRegistry<Edge> EdgeObserverRegistry;
306 /// The edge observer registry.
307 typedef AlterationObserverRegistry<Node> NodeObserverRegistry;
312 mutable EdgeObserverRegistry edge_observers;
314 mutable NodeObserverRegistry node_observers;
318 EdgeObserverRegistry& getEdgeObserverRegistry() const {
319 return edge_observers;
322 NodeObserverRegistry& getNodeObserverRegistry() const {
323 return node_observers;
326 ~AlterableGraphExtender() {
327 node_observers.clear();
328 edge_observers.clear();