| 1 | /* | 
|---|
| 2 | * Observable.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Dec 1, 2011 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef OBSERVABLE_HPP_ | 
|---|
| 9 | #define OBSERVABLE_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 | // include config.h | 
|---|
| 12 | #ifdef HAVE_CONFIG_H | 
|---|
| 13 | #include <config.h> | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 | #include <map> | 
|---|
| 17 | #include <set> | 
|---|
| 18 | #include <string> | 
|---|
| 19 | #include <boost/function.hpp> | 
|---|
| 20 |  | 
|---|
| 21 | #include "CodePatterns/Range.hpp" | 
|---|
| 22 | #include "CodePatterns/Observer/defs.hpp" | 
|---|
| 23 | #include "CodePatterns/Observer/Observer.hpp" | 
|---|
| 24 |  | 
|---|
| 25 | class Graveyard; | 
|---|
| 26 |  | 
|---|
| 27 | /** | 
|---|
| 28 | * An Observable implements all neccessary method for being observed. | 
|---|
| 29 | * | 
|---|
| 30 | * That is, it provides methods for signing on and of from an | 
|---|
| 31 | * Observable that can be used by any observer. The actual | 
|---|
| 32 | * observer-mechanism is handled at a central static place | 
|---|
| 33 | * to avoid memory issues when many observable are around but only few | 
|---|
| 34 | * are actually observed. | 
|---|
| 35 | * | 
|---|
| 36 | * \note We have to clean our Channels from static NotificationChannels and | 
|---|
| 37 | * we call Channels::subjectKilled() to let Observers that have only signed | 
|---|
| 38 | * on to single channel still know when their observable has died. | 
|---|
| 39 | * | 
|---|
| 40 | * Note that one may allow an Observable to live some over-time by using | 
|---|
| 41 | * the Graveyard. This allows any Observer to still access the instance | 
|---|
| 42 | * in order to properly sign off. It is destroyed when no Observer is left. | 
|---|
| 43 | */ | 
|---|
| 44 | class Observable : public Observer { | 
|---|
| 45 | public: | 
|---|
| 46 | Observable(std::string _name); | 
|---|
| 47 | virtual ~Observable(); | 
|---|
| 48 |  | 
|---|
| 49 | /** This class is only used to distinguish from size_t in the overload. | 
|---|
| 50 | * | 
|---|
| 51 | * It encapsulates a const int (the priority level) and checks valid bounds | 
|---|
| 52 | * in constructor. | 
|---|
| 53 | * | 
|---|
| 54 | */ | 
|---|
| 55 | class PriorityLevel { | 
|---|
| 56 | public: | 
|---|
| 57 | explicit PriorityLevel(const int i); | 
|---|
| 58 | ~PriorityLevel(); | 
|---|
| 59 |  | 
|---|
| 60 | const int level; | 
|---|
| 61 | private: | 
|---|
| 62 | static range<int> ValidRange; | 
|---|
| 63 | }; | 
|---|
| 64 |  | 
|---|
| 65 | /** | 
|---|
| 66 | * Sign an Observer on to this Observable. The Observer will be notified | 
|---|
| 67 | * whenever something inside the Observable changes. The Observer can | 
|---|
| 68 | * assign itself a priority for the changes in the range of -20:+20. | 
|---|
| 69 | * The Observer with lower priority will be called before the others, | 
|---|
| 70 | * same as with Unix nice-levels. This can be used when an Object | 
|---|
| 71 | * contains other objects that observe it (derived values), and these objects have | 
|---|
| 72 | * to recalculate their states before the changes should be propageted to the | 
|---|
| 73 | * UI. A default priority of 0 should be fine in most cases, since there is | 
|---|
| 74 | * ussually no need to order the update sequence. | 
|---|
| 75 | */ | 
|---|
| 76 | virtual void signOn(Observer *target, PriorityLevel priority = PriorityDefault) const; | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 | * Sign of a previously signed on Observer. After this no more | 
|---|
| 80 | * updates will be recieved from that observer. | 
|---|
| 81 | */ | 
|---|
| 82 | virtual void signOff(Observer *target) const; | 
|---|
| 83 |  | 
|---|
| 84 | /** | 
|---|
| 85 | * Sign on for specialized notifications | 
|---|
| 86 | */ | 
|---|
| 87 | virtual void signOn(Observer *target, size_t channelno) const; | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 | * Stop receiving a specialized notification | 
|---|
| 91 | */ | 
|---|
| 92 | virtual void signOff(Observer *target, size_t channelno) const; | 
|---|
| 93 |  | 
|---|
| 94 | /** | 
|---|
| 95 | * Ask an Observer if it is currently in a blocked state, i.e. if | 
|---|
| 96 | * Changes are in Progress, that are not yet published. | 
|---|
| 97 | */ | 
|---|
| 98 | virtual bool isBlocked() const; | 
|---|
| 99 |  | 
|---|
| 100 | Notification_ptr getChannel(size_t no) const; | 
|---|
| 101 |  | 
|---|
| 102 | size_t getNumberOfObservers() const; | 
|---|
| 103 |  | 
|---|
| 104 | protected: | 
|---|
| 105 | virtual void update(Observable *publisher); | 
|---|
| 106 | virtual void subjectKilled(Observable *publisher); | 
|---|
| 107 |  | 
|---|
| 108 | virtual void notifyAll(); | 
|---|
| 109 | protected: | 
|---|
| 110 | // Observer mechanism is done from a static central place | 
|---|
| 111 | /** | 
|---|
| 112 | * Internal method. | 
|---|
| 113 | * Do not call directly. Use OBSERVE macro instead | 
|---|
| 114 | */ | 
|---|
| 115 | static void start_observer_internal(Observable *publisher); | 
|---|
| 116 | /** | 
|---|
| 117 | * Internal method. | 
|---|
| 118 | * Do not call directly. Use OBSERVE macro instead | 
|---|
| 119 | */ | 
|---|
| 120 | static void finish_observer_internal(Observable *publisher); | 
|---|
| 121 |  | 
|---|
| 122 | static void enque_notification_internal(Observable *publisher, Notification_ptr notification); | 
|---|
| 123 |  | 
|---|
| 124 | typedef std::map<Observable*, Channels *> ChannelMap; | 
|---|
| 125 | static ChannelMap NotificationChannels; | 
|---|
| 126 |  | 
|---|
| 127 | static PriorityLevel PriorityDefault; | 
|---|
| 128 |  | 
|---|
| 129 | protected: | 
|---|
| 130 | typedef std::multimap<int,Observer*> callees_t; | 
|---|
| 131 | typedef std::set<Notification*> notificationSet; | 
|---|
| 132 | static std::map<Observable*, int> depth; | 
|---|
| 133 | static std::map<Observable*,callees_t> callTable; | 
|---|
| 134 | static std::map<Observable*,notificationSet> notifications; | 
|---|
| 135 | static std::set<Observable*> busyObservables; | 
|---|
| 136 |  | 
|---|
| 137 | private: | 
|---|
| 138 | friend class Zombie; | 
|---|
| 139 | friend class Graveyard; | 
|---|
| 140 |  | 
|---|
| 141 | typedef boost::function<void (const Observable*)> graveyard_informer_t; | 
|---|
| 142 |  | 
|---|
| 143 | /** Bound function to call when Observer are signing off (needs to be a ptr | 
|---|
| 144 | * as we must be able to rebound it. | 
|---|
| 145 | * | 
|---|
| 146 | * \warning Do not delete this pointer, the instance is either a static one | 
|---|
| 147 | * or handled someplace else (e.g. in the Graveyard). | 
|---|
| 148 | */ | 
|---|
| 149 | graveyard_informer_t * graveyard_informer; | 
|---|
| 150 |  | 
|---|
| 151 | //!> default informer that does nothing | 
|---|
| 152 | static graveyard_informer_t noop_informer; | 
|---|
| 153 |  | 
|---|
| 154 | /** Sets the bound function for over-time life-time management. | 
|---|
| 155 | * | 
|---|
| 156 | * \param _graveyard ptr Graveyard to inform of leaving Observers | 
|---|
| 157 | */ | 
|---|
| 158 | void setGraveyardInformer(graveyard_informer_t * _graveyard_informer) | 
|---|
| 159 | { | 
|---|
| 160 | graveyard_informer = _graveyard_informer; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | //! @cond | 
|---|
| 164 | // Structure for RAII-Style notification | 
|---|
| 165 | public: | 
|---|
| 166 | /** | 
|---|
| 167 | * This structure implements the Observer-mechanism RAII-Idiom. | 
|---|
| 168 | * It triggers certain functions on creation and destruction so that | 
|---|
| 169 | * Observer mechanisms can be linked to scope block. | 
|---|
| 170 | */ | 
|---|
| 171 | class _Observable_protector { | 
|---|
| 172 | public: | 
|---|
| 173 | _Observable_protector(Observable *); | 
|---|
| 174 | _Observable_protector(const _Observable_protector&); | 
|---|
| 175 | ~_Observable_protector(); | 
|---|
| 176 | private: | 
|---|
| 177 | Observable *protege; | 
|---|
| 178 | }; | 
|---|
| 179 | //! @endcond | 
|---|
| 180 | }; | 
|---|
| 181 |  | 
|---|
| 182 |  | 
|---|
| 183 | // extra macro is necessary to work with __LINE__ | 
|---|
| 184 | #define PASTE(a,b) PASTE_HELPER(a,b) | 
|---|
| 185 | #define PASTE_HELPER(a,b) a ## b | 
|---|
| 186 | #define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this) | 
|---|
| 187 | #define NOTIFY(channelno) do{Observable::enque_notification_internal(this,NotificationChannels[this]->getChannel(channelno));}while(0) | 
|---|
| 188 | #define LOCK_OBSERVABLE(observable) Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(&(observable)) | 
|---|
| 189 |  | 
|---|
| 190 | #endif /* OBSERVABLE_HPP_ */ | 
|---|