| [e2e035e] | 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 |  | 
|---|
| [9b8fa4] | 20 | #include "CodePatterns/Range.hpp" | 
|---|
|  | 21 | #include "CodePatterns/Observer/defs.hpp" | 
|---|
|  | 22 | #include "CodePatterns/Observer/Observer.hpp" | 
|---|
| [e2e035e] | 23 |  | 
|---|
|  | 24 | /** | 
|---|
|  | 25 | * An Observable implements all neccessary method for being observed. | 
|---|
|  | 26 | * | 
|---|
|  | 27 | * That is, it provides methods for signing on and of from an | 
|---|
|  | 28 | * Observable that can be used by any observer. The actual | 
|---|
|  | 29 | * observer-mechanism is handled at a central static place | 
|---|
|  | 30 | * to avoid memory issues when many observable are around but only few | 
|---|
|  | 31 | * are actually observed. | 
|---|
|  | 32 | */ | 
|---|
|  | 33 | class Observable : public Observer { | 
|---|
|  | 34 | public: | 
|---|
|  | 35 | Observable(std::string _name); | 
|---|
|  | 36 | virtual ~Observable(); | 
|---|
|  | 37 |  | 
|---|
| [8fe1e2] | 38 | /** This class is only used to distinguish from size_t in the overload. | 
|---|
|  | 39 | * | 
|---|
|  | 40 | * It encapsulates a const int (the priority level) and checks valid bounds | 
|---|
|  | 41 | * in constructor. | 
|---|
|  | 42 | * | 
|---|
|  | 43 | */ | 
|---|
|  | 44 | class PriorityLevel { | 
|---|
|  | 45 | public: | 
|---|
|  | 46 | explicit PriorityLevel(const int i); | 
|---|
|  | 47 | ~PriorityLevel(); | 
|---|
|  | 48 |  | 
|---|
|  | 49 | const int level; | 
|---|
|  | 50 | private: | 
|---|
|  | 51 | static range<int> ValidRange; | 
|---|
|  | 52 | }; | 
|---|
|  | 53 |  | 
|---|
| [e2e035e] | 54 | /** | 
|---|
|  | 55 | * Sign an Observer on to this Observable. The Observer will be notified | 
|---|
|  | 56 | * whenever something inside the Observable changes. The Observer can | 
|---|
|  | 57 | * assign itself a priority for the changes in the range of -20:+20. | 
|---|
|  | 58 | * The Observer with lower priority will be called before the others, | 
|---|
|  | 59 | * same as with Unix nice-levels. This can be used when an Object | 
|---|
|  | 60 | * contains other objects that observe it (derived values), and these objects have | 
|---|
|  | 61 | * to recalculate their states before the changes should be propageted to the | 
|---|
|  | 62 | * UI. A default priority of 0 should be fine in most cases, since there is | 
|---|
|  | 63 | * ussually no need to order the update sequence. | 
|---|
|  | 64 | */ | 
|---|
| [8fe1e2] | 65 | virtual void signOn(Observer *target, PriorityLevel priority = PriorityDefault) const; | 
|---|
| [e2e035e] | 66 |  | 
|---|
|  | 67 | /** | 
|---|
|  | 68 | * Sign of a previously signed on Observer. After this no more | 
|---|
|  | 69 | * updates will be recieved from that observer. | 
|---|
|  | 70 | */ | 
|---|
|  | 71 | virtual void signOff(Observer *target) const; | 
|---|
|  | 72 |  | 
|---|
|  | 73 | /** | 
|---|
|  | 74 | * Sign on for specialized notifications | 
|---|
|  | 75 | */ | 
|---|
| [40f2e6] | 76 | virtual void signOn(Observer *target, size_t channelno) const; | 
|---|
| [e2e035e] | 77 |  | 
|---|
|  | 78 | /** | 
|---|
|  | 79 | * Stop receiving a specialized notification | 
|---|
|  | 80 | */ | 
|---|
| [40f2e6] | 81 | virtual void signOff(Observer *target, size_t channelno) const; | 
|---|
| [e2e035e] | 82 |  | 
|---|
|  | 83 | /** | 
|---|
|  | 84 | * Ask an Observer if it is currently in a blocked state, i.e. if | 
|---|
|  | 85 | * Changes are in Progress, that are not yet published. | 
|---|
|  | 86 | */ | 
|---|
|  | 87 | virtual bool isBlocked() const; | 
|---|
|  | 88 |  | 
|---|
|  | 89 | Notification_ptr getChannel(size_t no) const; | 
|---|
|  | 90 |  | 
|---|
|  | 91 | protected: | 
|---|
|  | 92 | virtual void update(Observable *publisher); | 
|---|
|  | 93 | virtual void subjectKilled(Observable *publisher); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | virtual void notifyAll(); | 
|---|
|  | 96 | protected: | 
|---|
|  | 97 | // Observer mechanism is done from a static central place | 
|---|
|  | 98 | /** | 
|---|
|  | 99 | * Internal method. | 
|---|
|  | 100 | * Do not call directly. Use OBSERVE macro instead | 
|---|
|  | 101 | */ | 
|---|
|  | 102 | static void start_observer_internal(Observable *publisher); | 
|---|
|  | 103 | /** | 
|---|
|  | 104 | * Internal method. | 
|---|
|  | 105 | * Do not call directly. Use OBSERVE macro instead | 
|---|
|  | 106 | */ | 
|---|
|  | 107 | static void finish_observer_internal(Observable *publisher); | 
|---|
|  | 108 |  | 
|---|
|  | 109 | static void enque_notification_internal(Observable *publisher, Notification_ptr notification); | 
|---|
|  | 110 |  | 
|---|
|  | 111 | typedef std::map<Observable*, Channels *> ChannelMap; | 
|---|
|  | 112 | static ChannelMap NotificationChannels; | 
|---|
|  | 113 |  | 
|---|
| [8fe1e2] | 114 | static PriorityLevel PriorityDefault; | 
|---|
|  | 115 |  | 
|---|
| [d85532] | 116 | protected: | 
|---|
| [e2e035e] | 117 | typedef std::multimap<int,Observer*> callees_t; | 
|---|
|  | 118 | typedef std::set<Notification*> notificationSet; | 
|---|
|  | 119 | static std::map<Observable*, int> depth; | 
|---|
|  | 120 | static std::map<Observable*,callees_t> callTable; | 
|---|
|  | 121 | static std::map<Observable*,notificationSet> notifications; | 
|---|
|  | 122 | static std::set<Observable*> busyObservables; | 
|---|
|  | 123 |  | 
|---|
|  | 124 | //! @cond | 
|---|
|  | 125 | // Structure for RAII-Style notification | 
|---|
|  | 126 | public: | 
|---|
|  | 127 | /** | 
|---|
|  | 128 | * This structure implements the Observer-mechanism RAII-Idiom. | 
|---|
|  | 129 | * It triggers certain functions on creation and destruction so that | 
|---|
|  | 130 | * Observer mechanisms can be linked to scope block. | 
|---|
|  | 131 | */ | 
|---|
|  | 132 | class _Observable_protector { | 
|---|
|  | 133 | public: | 
|---|
|  | 134 | _Observable_protector(Observable *); | 
|---|
|  | 135 | _Observable_protector(const _Observable_protector&); | 
|---|
|  | 136 | ~_Observable_protector(); | 
|---|
|  | 137 | private: | 
|---|
|  | 138 | Observable *protege; | 
|---|
|  | 139 | }; | 
|---|
|  | 140 | //! @endcond | 
|---|
|  | 141 | }; | 
|---|
|  | 142 |  | 
|---|
|  | 143 |  | 
|---|
|  | 144 | // extra macro is necessary to work with __LINE__ | 
|---|
|  | 145 | #define PASTE(a,b) PASTE_HELPER(a,b) | 
|---|
|  | 146 | #define PASTE_HELPER(a,b) a ## b | 
|---|
|  | 147 | #define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this) | 
|---|
| [40f2e6] | 148 | #define NOTIFY(channelno) do{Observable::enque_notification_internal(this,NotificationChannels[this]->getChannel(channelno));}while(0) | 
|---|
| [e2e035e] | 149 | #define LOCK_OBSERVABLE(observable) Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(&(observable)) | 
|---|
|  | 150 |  | 
|---|
|  | 151 | #endif /* OBSERVABLE_HPP_ */ | 
|---|