1
/***************************************************************************
2
* (c) J�rgen Riegel (juergen.riegel@web.de) 2002 *
4
* This file is part of the FreeCAD CAx development system. *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU Library General Public License (LGPL) *
8
* as published by the Free Software Foundation; either version 2 of *
9
* the License, or (at your option) any later version. *
10
* for detail see the LICENCE text file. *
12
* FreeCAD is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU Library General Public License for more details. *
17
* You should have received a copy of the GNU Library General Public *
18
* License along with FreeCAD; if not, write to the Free Software *
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22
* Juergen Riegel 2002 *
23
***************************************************************************/
26
#ifndef BASE_OBSERVER_H
27
#define BASE_OBSERVER_H
29
// Std. configurations
40
template <class MessageType> class Subject;
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.
50
template <class _MessageType>
57
* No special function so far.
63
* No special function so far.
65
virtual ~Observer(){};
68
* This method need to be reimplemented from the concrete Observer
69
* and get called by the observed class
70
* @param pCaller a referenc to the calling object
72
virtual void OnChange(Subject<_MessageType> &rCaller,_MessageType rcReason)=0;
75
* This method need to be reimplemented from the concrete Observer
76
* and get called by the observed class
77
* @param pCaller a referenc to the calling object
79
virtual void OnDestroy(Subject<_MessageType> & /*rCaller*/){}
82
* This method can be reimplemented from the concrete Observer
83
* and returns the name of the observer. Needed to use the Get
84
* Method of the Subject.
86
virtual const char *Name(void){return 0L;}
90
* Implementation of the well known Observer Design Pattern.
91
* The observed object, which inherit FCSubject, will call all
92
* its observers in case of changes. A observer class has to
93
* Attach itself to the observed object.
96
template <class _MessageType>
101
typedef Observer<_MessageType> ObserverType;
102
typedef _MessageType MessageType;
103
typedef Subject<_MessageType> SubjectType;
107
* No special function so far.
113
* No special function so far.
117
if (_ObserverSet.size() > 0)
119
printf("Not detached all observers yet\n");
124
/** Attach an Observer
125
* Attach an Observer to the list of Observers which get
126
* called when Notify is called.
127
* @param ToObserv A pointer to a concrete Observer
130
void Attach(Observer<_MessageType> *ToObserv)
133
size_t count = _ObserverSet.size();
134
printf("Attach observer %p\n", ToObserv);
135
_ObserverSet.insert(ToObserv);
136
if ( _ObserverSet.size() == count )
137
printf("Observer %p already attached\n", ToObserv);
139
_ObserverSet.insert(ToObserv);
143
/** Detach an Observer
144
* Detach an Observer from the list of Observers which get
145
* called when Notify is called.
146
* @param ToObserv A pointer to a concrete Observer
149
void Detach(Observer<_MessageType> *ToObserv)
152
size_t count = _ObserverSet.size();
153
printf("Detach observer %p\n", ToObserv);
154
_ObserverSet.erase(ToObserv);
155
if ( _ObserverSet.size() == count )
156
printf("Observer %p already detached\n", ToObserv);
158
_ObserverSet.erase(ToObserv);
162
/** Notify all Observers
163
* Send a message to all Observers attached to this subject.
164
* The Message depends on the implementation of a concrete
165
* Oberserver and Subject.
168
void Notify(_MessageType rcReason)
170
for(typename std::set<Observer<_MessageType> * >::iterator Iter=_ObserverSet.begin();Iter!=_ObserverSet.end();Iter++)
171
(*Iter)->OnChange(*this,rcReason); // send OnChange-signal
174
/** Get an Observer by name
175
* Get a observer by name if the observer reimplements the Name() mthode.
178
Observer<_MessageType> * Get(const char *Name)
181
for(typename std::set<Observer<_MessageType> * >::iterator Iter=_ObserverSet.begin();Iter!=_ObserverSet.end();Iter++)
183
OName = (*Iter)->Name(); // get the name
184
if(OName && strcmp(OName,Name) == 0)
191
/** Clears the list of all registered observers.
192
* @note Using this function in your code may be an indication of design problems.
196
_ObserverSet.clear();
201
/// Vector of attached observers
202
std::set<Observer <_MessageType> *> _ObserverSet;
209
#endif // BASE_OBSERVER_H