| [a80f419] | 1 | /*
 | 
|---|
 | 2 |  * Observer.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Jan 19, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef OBSERVER_HPP_
 | 
|---|
 | 9 | #define OBSERVER_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
| [70672e3] | 11 | // include config.h
 | 
|---|
 | 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 13 | #include <config.h>
 | 
|---|
 | 14 | #endif
 | 
|---|
 | 15 | 
 | 
|---|
| [a80f419] | 16 | #include <map>
 | 
|---|
 | 17 | #include <set>
 | 
|---|
 | 18 | #include <string>
 | 
|---|
 | 19 | #include <sstream>
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | /**
 | 
|---|
 | 22 |  * Basic structure for the observer pattern
 | 
|---|
 | 23 |  *
 | 
|---|
 | 24 |  * Observers register themselves with the observables to be notified when something changes.
 | 
|---|
 | 25 |  * In the Observable code that changes, attributes should be started with OBSERVE;. This macro
 | 
|---|
 | 26 |  * locks the observer mechanism while changes are done. At the end of the scope in which the
 | 
|---|
 | 27 |  * macro was placed the lock is released. When the last lock is released all changes are
 | 
|---|
 | 28 |  * propagated to the observers.
 | 
|---|
 | 29 |  *
 | 
|---|
 | 30 |  * Each observerable can have sub-observables. When one of these sub-observables changes and
 | 
|---|
 | 31 |  * notifies its observers the observable that contains them will also notify its observers.
 | 
|---|
 | 32 |  * This passing on of updates is blocked, when the main-observable is in the process of
 | 
|---|
 | 33 |  * updating many of its internal sub-observables. This means the update is not passed, if
 | 
|---|
 | 34 |  * it is produced while the main-observable itself is within any Observation block.
 | 
|---|
 | 35 |  */
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | // Deactivate any logging when we are not in debug mode
 | 
|---|
 | 38 | #ifdef NDEBUG
 | 
|---|
 | 39 | #undef LOG_OBSERVER
 | 
|---|
 | 40 | #endif
 | 
|---|
 | 41 | 
 | 
|---|
| [74e0f7] | 42 | class Channels;
 | 
|---|
| [a80f419] | 43 | class Notification;
 | 
|---|
| [74e0f7] | 44 | class Observable;
 | 
|---|
| [a80f419] | 45 | 
 | 
|---|
 | 46 | // Pointers to notifications are used for unique identification
 | 
|---|
 | 47 | // using this typedef makes it hard for others to mess up this
 | 
|---|
 | 48 | // identification process
 | 
|---|
 | 49 | typedef Notification *const Notification_ptr;
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | template<class _Set>
 | 
|---|
 | 52 | class ObservedIterator;
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | /**
 | 
|---|
 | 55 |  * An Observer is notified by all Observed objects, when anything changes.
 | 
|---|
 | 56 |  *
 | 
|---|
 | 57 |  * If a simple change is done to an Object the Obervable will call the update() method
 | 
|---|
 | 58 |  * of all signed on observers, passing itself as a parameter for identification. The
 | 
|---|
 | 59 |  * Observers should then react to the changes and update themselves accordingly.
 | 
|---|
 | 60 |  *
 | 
|---|
 | 61 |  * If an observed Object is destroyed it will call the subjectKilled() method
 | 
|---|
 | 62 |  * of all signed on Observers, again passing itself as a parameter for identification.
 | 
|---|
 | 63 |  * The Observers should handle the destruction of an observed Object gracefully, i.e.
 | 
|---|
 | 64 |  * set themselves inactive, display something else, etc. There is no need
 | 
|---|
 | 65 |  * to sign of from the dying object, since this will be handled by the Observable destructor.
 | 
|---|
 | 66 |  */
 | 
|---|
 | 67 | class Observer
 | 
|---|
 | 68 | {
 | 
|---|
 | 69 |   friend class Observable;
 | 
|---|
 | 70 |   friend class Notification;
 | 
|---|
 | 71 |   template<class> friend class ObservedIterator;
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |   // indicates the constructor called from Observables
 | 
|---|
 | 74 |   struct BaseConstructor{};
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | public:
 | 
|---|
 | 77 |   Observer(BaseConstructor);
 | 
|---|
 | 78 |   Observer(std::string);
 | 
|---|
 | 79 |   virtual ~Observer();
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 | protected:
 | 
|---|
 | 82 |   /**
 | 
|---|
 | 83 |    * This method is called upon changes of the Observable
 | 
|---|
 | 84 |    */
 | 
|---|
 | 85 |   virtual void update(Observable *publisher)=0;
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 |   /**
 | 
|---|
 | 88 |    * This method is called when a special named change
 | 
|---|
 | 89 |    * of the Observable occured
 | 
|---|
 | 90 |    */
 | 
|---|
 | 91 |   virtual void recieveNotification(Observable *publisher, Notification_ptr notification);
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 |   /**
 | 
|---|
 | 94 |    * This method is called when the observed object is destroyed.
 | 
|---|
 | 95 |    */
 | 
|---|
 | 96 |   virtual void subjectKilled(Observable *publisher)=0;
 | 
|---|
 | 97 | };
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 | /**
 | 
|---|
 | 100 |  * An Observable implements all neccessary method for being observed.
 | 
|---|
 | 101 |  *
 | 
|---|
 | 102 |  * That is, it provides methods for signing on and of from an
 | 
|---|
 | 103 |  * Observable that can be used by any observer. The actual
 | 
|---|
 | 104 |  * observer-mechanism is handled at a central static place
 | 
|---|
 | 105 |  * to avoid memory issues when many observable are around but only few
 | 
|---|
 | 106 |  * are actually observed.
 | 
|---|
 | 107 |  */
 | 
|---|
 | 108 | class Observable : public Observer {
 | 
|---|
 | 109 | public:
 | 
|---|
 | 110 |   Observable(std::string _name);
 | 
|---|
 | 111 |   virtual ~Observable();
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |   /**
 | 
|---|
 | 114 |    * Sign an Observer on to this Observable. The Observer will be notified
 | 
|---|
 | 115 |    * whenever something inside the Observable changes. The Observer can
 | 
|---|
 | 116 |    * assign itself a priority for the changes in the range of -20:+20.
 | 
|---|
 | 117 |    * The Observer with lower priority will be called before the others,
 | 
|---|
 | 118 |    * same as with Unix nice-levels. This can be used when an Object
 | 
|---|
 | 119 |    * contains other objects that observe it (derived values), and these objects have
 | 
|---|
 | 120 |    * to recalculate their states before the changes should be propageted to the
 | 
|---|
 | 121 |    * UI. A default priority of 0 should be fine in most cases, since there is
 | 
|---|
 | 122 |    * ussually no need to order the update sequence.
 | 
|---|
 | 123 |    */
 | 
|---|
| [9e776f] | 124 |   virtual void signOn(Observer *target, int priority=0) const;
 | 
|---|
| [a80f419] | 125 | 
 | 
|---|
 | 126 |   /**
 | 
|---|
 | 127 |    * Sign of a previously signed on Observer. After this no more
 | 
|---|
 | 128 |    * updates will be recieved from that observer.
 | 
|---|
 | 129 |    */
 | 
|---|
| [9e776f] | 130 |   virtual void signOff(Observer *target) const;
 | 
|---|
| [a80f419] | 131 | 
 | 
|---|
 | 132 |   /**
 | 
|---|
 | 133 |    * Sign on for specialized notifications
 | 
|---|
 | 134 |    */
 | 
|---|
| [9e776f] | 135 |   virtual void signOn(Observer *target, Notification_ptr notification) const;
 | 
|---|
| [a80f419] | 136 | 
 | 
|---|
 | 137 |   /**
 | 
|---|
 | 138 |    * Stop receiving a specialized notification
 | 
|---|
 | 139 |    */
 | 
|---|
| [9e776f] | 140 |   virtual void signOff(Observer *target, Notification_ptr notification) const;
 | 
|---|
| [a80f419] | 141 | 
 | 
|---|
 | 142 |   /**
 | 
|---|
 | 143 |    * Ask an Observer if it is currently in a blocked state, i.e. if
 | 
|---|
 | 144 |    * Changes are in Progress, that are not yet published.
 | 
|---|
 | 145 |    */
 | 
|---|
| [9e776f] | 146 |   virtual bool isBlocked() const;
 | 
|---|
| [a80f419] | 147 | 
 | 
|---|
| [74e0f7] | 148 |   Notification_ptr getChannel(size_t no) const;
 | 
|---|
 | 149 | 
 | 
|---|
| [a80f419] | 150 | protected:
 | 
|---|
 | 151 |   virtual void update(Observable *publisher);
 | 
|---|
 | 152 |   virtual void subjectKilled(Observable *publisher);
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 |   virtual void notifyAll();
 | 
|---|
 | 155 | protected:
 | 
|---|
 | 156 | // Observer mechanism is done from a static central place
 | 
|---|
 | 157 |   /**
 | 
|---|
 | 158 |    * Internal method.
 | 
|---|
 | 159 |    * Do not call directly. Use OBSERVE macro instead
 | 
|---|
 | 160 |    */
 | 
|---|
 | 161 |   static void start_observer_internal(Observable *publisher);
 | 
|---|
 | 162 |   /**
 | 
|---|
 | 163 |    * Internal method.
 | 
|---|
 | 164 |    * Do not call directly. Use OBSERVE macro instead
 | 
|---|
 | 165 |    */
 | 
|---|
 | 166 |   static void finish_observer_internal(Observable *publisher);
 | 
|---|
 | 167 | 
 | 
|---|
 | 168 |   static void enque_notification_internal(Observable *publisher, Notification_ptr notification);
 | 
|---|
 | 169 | 
 | 
|---|
| [74e0f7] | 170 |   Channels *NotificationChannels;
 | 
|---|
 | 171 | 
 | 
|---|
| [a80f419] | 172 | private:
 | 
|---|
 | 173 |   typedef std::multimap<int,Observer*> callees_t;
 | 
|---|
 | 174 |   typedef std::set<Notification*> notificationSet;
 | 
|---|
 | 175 |   static std::map<Observable*, int> depth;
 | 
|---|
 | 176 |   static std::map<Observable*,callees_t> callTable;
 | 
|---|
 | 177 |   static std::map<Observable*,notificationSet> notifications;
 | 
|---|
 | 178 |   static std::set<Observable*> busyObservables;
 | 
|---|
 | 179 | 
 | 
|---|
 | 180 |   //! @cond
 | 
|---|
 | 181 |   // Structure for RAII-Style notification
 | 
|---|
 | 182 | public:
 | 
|---|
 | 183 |   /**
 | 
|---|
 | 184 |    * This structure implements the Observer-mechanism RAII-Idiom.
 | 
|---|
 | 185 |    * It triggers certain functions on creation and destruction so that
 | 
|---|
 | 186 |    * Observer mechanisms can be linked to scope block.
 | 
|---|
 | 187 |    */
 | 
|---|
 | 188 |   class _Observable_protector {
 | 
|---|
 | 189 |   public:
 | 
|---|
 | 190 |     _Observable_protector(Observable *);
 | 
|---|
 | 191 |     _Observable_protector(const _Observable_protector&);
 | 
|---|
 | 192 |     ~_Observable_protector();
 | 
|---|
 | 193 |   private:
 | 
|---|
 | 194 |     Observable *protege;
 | 
|---|
 | 195 |   };
 | 
|---|
 | 196 |   //! @endcond
 | 
|---|
 | 197 | };
 | 
|---|
 | 198 | 
 | 
|---|
| [74e0f7] | 199 | /** Notifications are sort of new channels of an Observable.
 | 
|---|
 | 200 |  * Via the NOTIFY() macro updates can be transmitted in a specific channel.
 | 
|---|
 | 201 |  * Observers can subscribe to Notification in much the same way as they can to
 | 
|---|
 | 202 |  * the Observable itself. Usually, Notifications
 | 
|---|
 | 203 |  */
 | 
|---|
| [a80f419] | 204 | class Notification {
 | 
|---|
 | 205 |   friend class Observable;
 | 
|---|
| [74e0f7] | 206 |   friend class Channels;
 | 
|---|
| [a80f419] | 207 | public:
 | 
|---|
 | 208 |   Notification(Observable *_owner);
 | 
|---|
| [74e0f7] | 209 |   Notification(Observable *_owner, size_t _channelno);
 | 
|---|
| [a80f419] | 210 |   virtual ~Notification();
 | 
|---|
| [74e0f7] | 211 | 
 | 
|---|
 | 212 |   size_t getChannelNo() const { return channelno; }
 | 
|---|
| [a80f419] | 213 | protected:
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 |   void addObserver(Observer *target);
 | 
|---|
 | 216 |   void removeObserver(Observer *target);
 | 
|---|
 | 217 | 
 | 
|---|
 | 218 |   void notifyAll();
 | 
|---|
 | 219 | private:
 | 
|---|
 | 220 |   Observable * const owner;
 | 
|---|
 | 221 |   std::set<Observer*> targets;
 | 
|---|
| [74e0f7] | 222 |   size_t channelno;
 | 
|---|
 | 223 | };
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 | /** Channels aggregate all possible Notifications of an Observable.
 | 
|---|
 | 226 |  *
 | 
|---|
 | 227 |  * Usually, one implements an enumeration of the channel number which is
 | 
|---|
 | 228 |  * visible to the outside only.
 | 
|---|
 | 229 |  *
 | 
|---|
 | 230 |  */
 | 
|---|
 | 231 | class Channels {
 | 
|---|
 | 232 | public:
 | 
|---|
 | 233 |   Channels(Observable *_owner);
 | 
|---|
 | 234 |   virtual ~Channels();
 | 
|---|
 | 235 | 
 | 
|---|
 | 236 |   void addChannel(size_t no);
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 |   Notification_ptr getChannel(size_t no) const;
 | 
|---|
 | 239 |   size_t getType(Notification_ptr channel) const;
 | 
|---|
 | 240 | 
 | 
|---|
 | 241 | protected:
 | 
|---|
 | 242 |   void removeChannel(size_t no);
 | 
|---|
 | 243 | 
 | 
|---|
 | 244 | private:
 | 
|---|
 | 245 |   Observable * const owner;
 | 
|---|
 | 246 | 
 | 
|---|
 | 247 |   typedef std::map< size_t, Notification_ptr> NotificationTypetoRefMap;
 | 
|---|
 | 248 | 
 | 
|---|
 | 249 |   NotificationTypetoRefMap ChannelMap;
 | 
|---|
| [a80f419] | 250 | };
 | 
|---|
 | 251 | 
 | 
|---|
 | 252 | #ifdef LOG_OBSERVER
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 | /**
 | 
|---|
 | 255 |  * This class is used to log the working of the observer mechanism
 | 
|---|
 | 256 |  *
 | 
|---|
 | 257 |  * TODO: make this conditional dependent on compiler Flag.
 | 
|---|
 | 258 |  */
 | 
|---|
 | 259 | class ObserverLog{
 | 
|---|
 | 260 |   friend class Observable;
 | 
|---|
 | 261 |   friend class Observer;
 | 
|---|
 | 262 |   template <typename> friend class Cacheable;
 | 
|---|
 | 263 | public:
 | 
|---|
 | 264 |   ObserverLog();
 | 
|---|
 | 265 |   ~ObserverLog();
 | 
|---|
 | 266 |   std::string getLog();                        // get everything that has been logged
 | 
|---|
 | 267 |   std::string getName(void*);                  // get the name of an actor
 | 
|---|
 | 268 |   bool isObservable(void*);
 | 
|---|
 | 269 | private:
 | 
|---|
 | 270 |   int count;                                   // number to reference each actor in this framework
 | 
|---|
 | 271 |   std::map<void*,std::string> names;           // List of names assigned to actors
 | 
|---|
 | 272 |   std::set<void*> observables;                 // List of pointers to Observables. Needed to distinguish Observers and Observables
 | 
|---|
 | 273 |   void addName(void*, std::string);            // Assign a name to an Actor
 | 
|---|
 | 274 |   void addObservable(void*);
 | 
|---|
 | 275 |   void deleteName(void*);                      // delete the name of an Actor
 | 
|---|
 | 276 |   void deleteObservable(void*);
 | 
|---|
 | 277 |   std::stringstream &addMessage(int depth=0);  // Add a Message to the logging
 | 
|---|
 | 278 |   std::stringstream log;                       // The internal log object
 | 
|---|
 | 279 | };
 | 
|---|
 | 280 | 
 | 
|---|
 | 281 | ObserverLog &observerLog();
 | 
|---|
 | 282 | 
 | 
|---|
 | 283 | #endif
 | 
|---|
 | 284 | 
 | 
|---|
 | 285 | // extra macro is necessary to work with __LINE__
 | 
|---|
 | 286 | #define PASTE(a,b) PASTE_HELPER(a,b)
 | 
|---|
 | 287 | #define PASTE_HELPER(a,b) a ## b
 | 
|---|
 | 288 | #define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this)
 | 
|---|
| [74e0f7] | 289 | #define NOTIFY(channelno) do{Observable::enque_notification_internal(this,NotificationChannels->getChannel(channelno));}while(0)
 | 
|---|
| [a80f419] | 290 | #define LOCK_OBSERVABLE(observable) Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(&(observable))
 | 
|---|
 | 291 | 
 | 
|---|
 | 292 | #endif /* OBSERVER_HPP_ */
 | 
|---|