ReFRACtor
observer.h
Go to the documentation of this file.
1 #ifndef OBSERVER_H
2 #define OBSERVER_H
3 #include "null_deleter.h"
4 #include "generic_object.h"
5 #include <boost/shared_ptr.hpp>
6 #include <boost/weak_ptr.hpp>
7 #include <boost/foreach.hpp>
8 #include <list>
9 #include <vector>
10 #include <iostream>
11 namespace FullPhysics {
12 template<class T> class Observable;
13 /****************************************************************/
29 template<class T> class Observer : public virtual GenericObject {
30 public:
32  this_obj = to_ptr(*this);
33  }
34  virtual ~Observer() {}
35 
36 //-----------------------------------------------------------------------
38 //-----------------------------------------------------------------------
39 
40  virtual void notify_update(const T& Observed_object) {};
41 
42 //-----------------------------------------------------------------------
45 //-----------------------------------------------------------------------
46 
47  virtual void notify_add(T& Observed_object) {}
48  virtual void notify_add() {}
49 
50 //-----------------------------------------------------------------------
53 //-----------------------------------------------------------------------
54 
55  virtual void notify_remove(T& Observed_object) {}
56  virtual void notify_remove() {}
57 
58 private:
59  // Shared ptr that we can use to get weak_ptr to. This object has
60  // exactly the same lifetime as the containing object.
62  friend class Observable<T>;
63 };
64 
65 /****************************************************************/
91 template<class T> class Observable : public virtual GenericObject {
92 public:
93  virtual ~Observable() {}
94 
95 //-----------------------------------------------------------------------
97 //-----------------------------------------------------------------------
98 
99  virtual void add_observer(Observer<T>& Obs) = 0;
100 
101 //-----------------------------------------------------------------------
104 //-----------------------------------------------------------------------
105 
106  void add_observer_and_keep_reference
108  { ref_list.push_back(Obs); add_observer(*Obs); }
109 
110 //-----------------------------------------------------------------------
112 //-----------------------------------------------------------------------
113 
114  virtual void remove_observer(Observer<T>& Obs) = 0;
115 protected:
116 //-----------------------------------------------------------------------
120 //-----------------------------------------------------------------------
121  void notify_update_do(const T& Self)
122  {
123  clean_dead_ptr();
124  BOOST_FOREACH(boost::weak_ptr<Observer<T> >& t, olist) {
125  boost::shared_ptr<Observer<T> > t2 = t.lock();
126  if(t2)
127  t2->notify_update(Self);
128  }
129  }
130 
131 //-----------------------------------------------------------------------
132 // Helper class for comparing against the weak_ptrs used to keep track
133 // of observables
134 //-----------------------------------------------------------------------
135 
136  class PointerEqual {
137  public:
138  PointerEqual(Observer<T>* t) : t_(t) {}
139  bool operator()(const boost::weak_ptr<Observer<T> >& w)
140  { return w.lock().get() == t_; }
142  };
143 
144 //-----------------------------------------------------------------------
146 //-----------------------------------------------------------------------
147 
148  void add_observer_do(Observer<T>& Obs, T& t)
149  {
150  PointerEqual p(Obs.this_obj.get());
151  bool already_added = std::find_if(olist.begin(), olist.end(), p) != olist.end();
152  if (!already_added) {
153  olist.push_back(boost::weak_ptr<Observer<T> >(Obs.this_obj));
154  Obs.notify_add(t);
155  Obs.notify_add();
156  }
157  }
158 
160  {
161  PointerEqual p(Obs.this_obj.get());
162  bool already_added = std::find_if(olist.begin(), olist.end(), p) != olist.end();
163  if (!already_added) {
164  olist.push_back(boost::weak_ptr<Observer<T> >(Obs.this_obj));
165  Obs.notify_add();
166  }
167  }
168 
169 //-----------------------------------------------------------------------
171 //-----------------------------------------------------------------------
172 
174  {
175  PointerEqual p(Obs.this_obj.get());
176  olist.remove_if(p);
177  Obs.notify_remove(t);
178  Obs.notify_remove();
179  }
180 
182  {
183  PointerEqual p(Obs.this_obj.get());
184  olist.remove_if(p);
185  Obs.notify_remove();
186  }
187 
188  class PointerDead {
189  public:
190  bool operator()(const boost::weak_ptr<Observer<T> >& w)
191  { return w.expired(); }
192  };
193 //-----------------------------------------------------------------------
195 //-----------------------------------------------------------------------
196  void clean_dead_ptr() {
197  PointerDead p;
198  olist.remove_if(p);
199  }
200  std::list<boost::weak_ptr<Observer<T> > > olist;
201  std::vector<boost::shared_ptr<Observer<T> > > ref_list;
202 };
203 }
204 #endif
205 
bool operator()(const boost::weak_ptr< Observer< T > > &w)
Definition: observer.h:190
void remove_observer_do(Observer< T > &Obs)
Definition: observer.h:181
virtual void notify_add(T &Observed_object)
Called when an object is added to an Observable.
Definition: observer.h:47
virtual void notify_remove(T &Observed_object)
Called when an object is removed from an Observable.
Definition: observer.h:55
std::vector< boost::shared_ptr< Observer< T > > > ref_list
Definition: observer.h:201
void clean_dead_ptr()
Remove any dead pointers.
Definition: observer.h:196
std::list< boost::weak_ptr< Observer< T > > > olist
Definition: observer.h:200
void add_observer_do(Observer< T > &Obs, T &t)
Add an observer.
Definition: observer.h:148
boost::shared_ptr< T > to_ptr(T &t)
Helper routine to get a shared_ptr from a reference.
Definition: null_deleter.h:24
void remove_observer_do(Observer< T > &Obs, T &t)
Remove an observer.
Definition: observer.h:173
void notify_update_do(const T &Self)
Function to call to notify Observers of a state change.
Definition: observer.h:121
virtual ~Observable()
Definition: observer.h:93
virtual void notify_update(const T &Observed_object)
Called when the Observed object is updated.
Definition: observer.h:40
Mixin for a class that allows other classes to observe it state.
Definition: observer.h:12
bool operator()(const boost::weak_ptr< Observer< T > > &w)
Definition: observer.h:139
virtual void notify_add()
Definition: observer.h:48
void add_observer_do(Observer< T > &Obs)
Definition: observer.h:159
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
virtual ~Observer()
Definition: observer.h:34
For use with SWIG, it is useful to have a base class that everything can be cast to.
Simple Mixin to be and Observer of another object of class T.
Definition: observer.h:29
virtual void notify_remove()
Definition: observer.h:56

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:11