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

Last change on this file since 1c291d was 1c291d, checked in by Frederik Heber <heber@…>, 13 years ago

Channels and Notifications pass on subjectKilled().

  • if we only sign on to a single notification channel, we miss on subjectKilled() completely. Hence, we have to chance of properly signing off again or, at least, to know that we must not anymore.
  • Hence, Observable's dstor now calls subjectKilled() on its Channels which passes the call on to all its Notifications that in turn use the subjectKilled() slot of their targets along with the passed-through Observable instance.
  • also added observerLog verbosity when signing on/off to channels.
  • explained this in header documentation of Observable, Channels, and Notification.
  • TESTFIX: ObserverUnitTest did not properly signOff() before deleting instances in relayNotificationTest().
  • Property mode set to 100644
File size: 4.8 KB
Line 
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
20#include "CodePatterns/Range.hpp"
21#include "CodePatterns/Observer/defs.hpp"
22#include "CodePatterns/Observer/Observer.hpp"
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 * \note We have to clean our Channels from static NotificationChannels and
34 * we call Channels::subjectKilled() to let Observers that have only signed
35 * on to single channel still know when their observable has died.
36 */
37class Observable : public Observer {
38public:
39 Observable(std::string _name);
40 virtual ~Observable();
41
42 /** This class is only used to distinguish from size_t in the overload.
43 *
44 * It encapsulates a const int (the priority level) and checks valid bounds
45 * in constructor.
46 *
47 */
48 class PriorityLevel {
49 public:
50 explicit PriorityLevel(const int i);
51 ~PriorityLevel();
52
53 const int level;
54 private:
55 static range<int> ValidRange;
56 };
57
58 /**
59 * Sign an Observer on to this Observable. The Observer will be notified
60 * whenever something inside the Observable changes. The Observer can
61 * assign itself a priority for the changes in the range of -20:+20.
62 * The Observer with lower priority will be called before the others,
63 * same as with Unix nice-levels. This can be used when an Object
64 * contains other objects that observe it (derived values), and these objects have
65 * to recalculate their states before the changes should be propageted to the
66 * UI. A default priority of 0 should be fine in most cases, since there is
67 * ussually no need to order the update sequence.
68 */
69 virtual void signOn(Observer *target, PriorityLevel priority = PriorityDefault) const;
70
71 /**
72 * Sign of a previously signed on Observer. After this no more
73 * updates will be recieved from that observer.
74 */
75 virtual void signOff(Observer *target) const;
76
77 /**
78 * Sign on for specialized notifications
79 */
80 virtual void signOn(Observer *target, size_t channelno) const;
81
82 /**
83 * Stop receiving a specialized notification
84 */
85 virtual void signOff(Observer *target, size_t channelno) const;
86
87 /**
88 * Ask an Observer if it is currently in a blocked state, i.e. if
89 * Changes are in Progress, that are not yet published.
90 */
91 virtual bool isBlocked() const;
92
93 Notification_ptr getChannel(size_t no) const;
94
95protected:
96 virtual void update(Observable *publisher);
97 virtual void subjectKilled(Observable *publisher);
98
99 virtual void notifyAll();
100protected:
101// Observer mechanism is done from a static central place
102 /**
103 * Internal method.
104 * Do not call directly. Use OBSERVE macro instead
105 */
106 static void start_observer_internal(Observable *publisher);
107 /**
108 * Internal method.
109 * Do not call directly. Use OBSERVE macro instead
110 */
111 static void finish_observer_internal(Observable *publisher);
112
113 static void enque_notification_internal(Observable *publisher, Notification_ptr notification);
114
115 typedef std::map<Observable*, Channels *> ChannelMap;
116 static ChannelMap NotificationChannels;
117
118 static PriorityLevel PriorityDefault;
119
120protected:
121 typedef std::multimap<int,Observer*> callees_t;
122 typedef std::set<Notification*> notificationSet;
123 static std::map<Observable*, int> depth;
124 static std::map<Observable*,callees_t> callTable;
125 static std::map<Observable*,notificationSet> notifications;
126 static std::set<Observable*> busyObservables;
127
128 //! @cond
129 // Structure for RAII-Style notification
130public:
131 /**
132 * This structure implements the Observer-mechanism RAII-Idiom.
133 * It triggers certain functions on creation and destruction so that
134 * Observer mechanisms can be linked to scope block.
135 */
136 class _Observable_protector {
137 public:
138 _Observable_protector(Observable *);
139 _Observable_protector(const _Observable_protector&);
140 ~_Observable_protector();
141 private:
142 Observable *protege;
143 };
144 //! @endcond
145};
146
147
148// extra macro is necessary to work with __LINE__
149#define PASTE(a,b) PASTE_HELPER(a,b)
150#define PASTE_HELPER(a,b) a ## b
151#define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this)
152#define NOTIFY(channelno) do{Observable::enque_notification_internal(this,NotificationChannels[this]->getChannel(channelno));}while(0)
153#define LOCK_OBSERVABLE(observable) Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(&(observable))
154
155#endif /* OBSERVABLE_HPP_ */
Note: See TracBrowser for help on using the repository browser.