Observer.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_Observer_H
21 #define MIRTK_Observer_H
22 
23 #include "mirtk/Object.h"
24 
25 #include "mirtk/OrderedSet.h"
26 #include "mirtk/Event.h"
27 
28 
29 namespace mirtk {
30 
31 
32 // Type of objects that can be observed
33 class Observable;
34 
35 
36 /**
37  * Observer of an observable object
38  */
39 class Observer : public Object
40 {
41  mirtkAbstractMacro(Observer);
42 
43  // ---------------------------------------------------------------------------
44  // Observable/Observer interaction
45 
46  friend class Observable;
47 
48  void StartObserving(Observable *);
49  void StopObserving (Observable *);
50 
51  // ---------------------------------------------------------------------------
52  // Types
53 
54  /// Set of objects observed by this instance
55  typedef OrderedSet<Observable *> ObservableSet;
56 
57  /// Iterator of observable objects
58  typedef ObservableSet::iterator ObservableIterator;
59 
60  // ---------------------------------------------------------------------------
61  // Attributes
62 
63  /// Observed objects
64  mirtkAttributeMacro(ObservableSet, Observables);
65 
66  /// Copy attributes of this class from another instance
67  void CopyAttributes(const Observer &);
68 
69  // ---------------------------------------------------------------------------
70  // Construction/Destruction
71 protected:
72 
73  /// Default constructor
74  Observer();
75 
76  /// Copy constructor
77  Observer(const Observer &);
78 
79  /// Assignment operator
80  Observer &operator =(const Observer &);
81 
82  /// Destructor
83  virtual ~Observer();
84 
85  // ---------------------------------------------------------------------------
86  // Observation
87 public:
88 
89  /// Stop observing any of the currently monitored observables
90  void ClearObservables();
91 
92  /// Receives event messages from observed objects
93  virtual void HandleEvent(Observable *, Event, const void * = NULL) = 0;
94 
95 };
96 
97 
98 } // namespace mirtk
99 
100 #endif // MIRTK_Observer_H
virtual ~Observer()
Destructor.
void ClearObservables()
Stop observing any of the currently monitored observables.
Definition: IOConfig.h:41
Observer()
Default constructor.
virtual void HandleEvent(Observable *, Event, const void *=NULL)=0
Receives event messages from observed objects.
Event
Events that can be observed.
Definition: Event.h:32
Observer & operator=(const Observer &)
Assignment operator.