source: src/CodePatterns/Observer/Observable.hpp@ 1f96ec

Last change on this file since 1f96ec was 959c82, checked in by Frederik Heber <heber@…>, 10 years ago

Extracted all static Observable maps (and mutex) into singleton GlobalObservableInfo.

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