anatomist  5.1.2
3D neuroimaging data viewer
Observable.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef ANA_OBSERVER_OBSERVABLE_H
36 #define ANA_OBSERVER_OBSERVABLE_H
37 
38 
39 //--- header files ------------------------------------------------------------
40 
42 #include <set>
43 #include <string>
44 
45 
46 namespace anatomist
47 {
48  //--- forward declarations --------------------------------------------------
49 
50  class Observer;
51 
52 
53  //--- class declarations ----------------------------------------------------
54 
67  class Observable : virtual public carto::SharedObject
68  {
69  public:
72 
74  virtual ~Observable();
75 
78 
83  void addObserver(Observer* observer);
84 
89  void deleteObserver(Observer* observer);
90 
97 
102  int countObservers() const;
103 
112  virtual void notifyObservers(void* arg = 0);
113 
116 
118 
121 
128  bool hasChanged() const;
130  bool obsHasChanged( int ) const;
132  bool obsHasChanged( const std::string & ) const;
133 
135  void setChanged() const;
136 
138 
139  protected:
151  void clearChanged() const;
152 
154  void obsSetChanged( int, bool = true ) const;
156  void obsSetChanged( const std::string &, bool = true ) const;
157 
159 
160  private:
161  struct Private;
162 
163  mutable bool _changed;
164  mutable bool _updating;
165  Private *d;
166 
168  std::set<Observer*> _observers;
169  };
170 
171 
172  //--- inline methods --------------------------------------------------------
173 
174  inline
175  bool
177  {
178  return _changed;
179  }
180 
181 
182  inline
183  void
185  {
186  _changed = true;
187  }
188 
189 }
190 
191 
192 #endif
This class can be subclassed to represent an object that the programmer wants to have observed.
Definition: Observable.h:68
void addObserver(Observer *observer)
Adds an observer to the set of observers for this object.
void setChanged() const
Indicates that this object has changed.
Definition: Observable.h:184
void obsSetChanged(int, bool=true) const
int-based change flags (use enums to address them)
bool hasChanged() const
Tests if this object has changed.
Definition: Observable.h:176
Observable()
Construct an Observable with zero observers.
virtual ~Observable()
does nothing
void obsSetChanged(const std::string &, bool=true) const
string-based change flags
void deleteObservers()
Clears the observer list so that this object no longer has any observers (doesn't call any observer m...
bool obsHasChanged(const std::string &) const
only valid during an Observer::update()
virtual void notifyUnregisterObservers()
Notifies observable destruction to all observers and unregisters them.
void clearChanged() const
Indicates that this object has no longer changed, or that it has already notified all of its observer...
int countObservers() const
Returns the number of observers of this object.
void deleteObserver(Observer *observer)
Deletes an observer from the set of observers of this object.
virtual void notifyObservers(void *arg=0)
If this object has changed, as indicated by the hasChanged method, then notify all of its observers.
bool obsHasChanged(int) const
only valid during an Observer::update()
A class can implement the Observer interface when it wants to be informed of changes in observable ob...
Definition: Observer.h:55