Debian Codegraph @

src/Base/Observer.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Library General Public License (LGPL) *
10 * as published by the Free Software Foundation; either version 2 of *
11 * the License, or (at your option) any later version. *
12 * for detail see the LICENCE text file. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU Library General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Library General Public *
20 * License along with FreeCAD; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22 * USA *
23 * *
24 ***************************************************************************/
25
26
27 #ifndef BASE_OBSERVER_H
28 #define BASE_OBSERVER_H
29
30 #include <cstring>
31 #include <set>
32 #include "Console.h"
33 #include "Exception.h"
34
35
36 namespace Base
37 {
38
39 template<class MessageType>
40 class Subject;
41
42
43 /** Observer class
44 * Implementation of the well known Observer Design Pattern.
45 * The observed object, which inherit FCSubject, will call all
46 * its observers in case of changes. A observer class has to
47 * Attach itself to the observed object.
48 * @see FCSubject
49 */
50 template<class MsgType>
51 class Observer
52 {
53 public:
54 /**
55 * A constructor.
56 * No special function so far.
57 */
58 Observer() = default;
59
60 /**
61 * A destructor.
62 * No special function so far.
63 */
64 virtual ~Observer() = default;
65
66 /**
67 * This method need to be reimplemented from the concrete Observer
68 * and get called by the observed class
69 * @param rCaller a reference to the calling object
70 * @param rcReason
71 * \todo undocumented parameter 2
72 */
73 virtual void OnChange(Subject<MsgType>& rCaller, MsgType rcReason) = 0;
74
75 /**
76 * This method need to be reimplemented from the concrete Observer
77 * and get called by the observed class
78 * @param rCaller a reference to the calling object
79 */
80 virtual void OnDestroy(Subject<MsgType>& rCaller)
81 {
82 (void)rCaller;
83 }
84
85 /**
86 * This method can be reimplemented from the concrete Observer
87 * and returns the name of the observer. Needed to use the Get
88 * Method of the Subject.
89 */
90 virtual const char* name()
91 {
92 return nullptr;
93 }
94
95 protected:
96 FC_DEFAULT_COPY_MOVE(Observer)
97 };
98
99 /** Subject class
100 * Implementation of the well known Observer Design Pattern.
101 * The observed object, which inherit FCSubject, will call all
102 * its observers in case of changes. A observer class has to
103 * Attach itself to the observed object.
104 * @see FCObserver
105 */
106 template<class MsgType>
107 class Subject
108 {
109 public:
110 using ObserverType = Observer<MsgType>;
111 using MessageType = MsgType;
112 using SubjectType = Subject<MsgType>;
113
114 /**
115 * A constructor.
116 * No special function so far.
117 */
118 Subject() = default;
119
120 /**
121 * A destructor.
122 * No special function so far.
123 */
124 virtual ~Subject()
125 {
126 if (_ObserverSet.size() > 0) {
127 Base::Console().developerWarning(
128 std::string("~Subject()"),
129 "Not detached all observers yet\n"
130 );
131 }
132 }
133
134 /** Attach an Observer
135 * Attach an Observer to the list of Observers which get
136 * called when Notify is called.
137 * @param ToObserv A pointer to a concrete Observer
138 * @see Notify
139 */
140 void Attach(Observer<MsgType>* ToObserv)
141 {
142 #ifdef FC_DEBUG
143 size_t count = _ObserverSet.size();
144 _ObserverSet.insert(ToObserv);
145 if (_ObserverSet.size() == count) {
146 Base::Console().developerWarning(
147 std::string("Subject::Attach"),
148 "Observer %p already attached\n",
149 static_cast<void*>(ToObserv)
150 );
151 }
152 #else
153 _ObserverSet.insert(ToObserv);
154 #endif
155 }
156
157 /** Detach an Observer
158 * Detach an Observer from the list of Observers which get
159 * called when Notify is called.
160 * @param ToObserv A pointer to a concrete Observer
161 * @see Notify
162 */
163 void Detach(Observer<MsgType>* ToObserv)
164 {
165 #ifdef FC_DEBUG
166 size_t count = _ObserverSet.size();
167 _ObserverSet.erase(ToObserv);
168 if (_ObserverSet.size() == count) {
169 Base::Console().developerWarning(
170 std::string("Subject::Detach"),
171 "Observer %p already detached\n",
172 static_cast<void*>(ToObserv)
173 );
174 }
175 #else
176 _ObserverSet.erase(ToObserv);
177 #endif
178 }
179
180 /** Notify all Observers
181 * Send a message to all Observers attached to this subject.
182 * The Message depends on the implementation of a concrete
183 * Oberserver and Subject.
184 * @see Notify
185 */
186 void Notify(MsgType rcReason)
187 {
188 for (typename std::set<Observer<MsgType>*>::iterator Iter = _ObserverSet.begin();
189 Iter != _ObserverSet.end();
190 ++Iter) {
191 try {
192 (*Iter)->OnChange(*this, rcReason); // send OnChange-signal
193 }
194 catch (Base::Exception& e) {
195 Base::Console().error(
196 "Unhandled Base::Exception caught when notifying observer.\n"
197 "The error message is: %s\n",
198 e.what()
199 );
200 }
201 catch (std::exception& e) {
202 Base::Console().error(
203 "Unhandled std::exception caught when notifying observer\n"
204 "The error message is: %s\n",
205 e.what()
206 );
207 }
208 catch (...) {
209 Base::Console().error(
210 "Unhandled unknown exception caught in when notifying observer.\n"
211 );
212 }
213 }
214 }
215
216 /** Get an Observer by name
217 * Get a observer by name if the observer reimplements the Name() mthode.
218 * @see Observer
219 */
220 Observer<MsgType>* Get(const char* Name)
221 {
222 const char* OName = nullptr;
223 for (typename std::set<Observer<MsgType>*>::iterator Iter = _ObserverSet.begin();
224 Iter != _ObserverSet.end();
225 ++Iter) {
226 OName = (*Iter)->name(); // get the name
227 if (OName && strcmp(OName, Name) == 0) {
228 return *Iter;
229 }
230 }
231
232 return nullptr;
233 }
234
235 /** Clears the list of all registered observers.
236 * @note Using this function in your code may be an indication of design problems.
237 */
238 void ClearObserver()
239 {
240 _ObserverSet.clear();
241 }
242
243 protected:
244 FC_DEFAULT_COPY_MOVE(Subject)
245
246 private:
247 /// Vector of attached observers
248 std::set<Observer<MsgType>*> _ObserverSet;
249 };
250
251 // Workaround for MSVC
252 #if defined(FreeCADBase_EXPORTS) && defined(_MSC_VER)
253 # define Base_EXPORT
254 #else
255 # define Base_EXPORT BaseExport
256 #endif
257
258 #if !defined(__MINGW32__)
259 extern template class Base_EXPORT Observer<const char*>;
260 extern template class Base_EXPORT Subject<const char*>;
261 #endif
262
263 } // namespace Base
264
265
266 #endif // BASE_OBSERVER_H