ocra-recipes
Doxygen documentation for the ocra-recipes repository
ObserverSubject.h
Go to the documentation of this file.
1 
14 #ifndef _OCRABASE_OBSERVER_SUBJECT_H_
15 #define _OCRABASE_OBSERVER_SUBJECT_H_
16 
18 #include "ocra/optim/ocra_events.h"
19 
20 #include <boost/static_assert.hpp>
21 #include <boost/type_traits/is_base_of.hpp>
22 #include <boost/type_traits/is_convertible.hpp>
23 #include <boost/type_traits/is_class.hpp>
24 
25 namespace ocra
26 {
27  class Subject
28  : public SubjectBase<EVT_RESIZE>
29  , public SubjectBase<EVT_CHANGE_DEPENDENCIES>
30  , public SubjectBase<EVT_CHANGE_VALUE>
31  {
32  public:
33  virtual ~Subject() {}
34 
36  template<int EVT, class Derived, class Base>
37  void connect(Derived& object, void (Base::*newCallback)(int)) const
38  {
39  typedef boost::is_class<Derived> you_must_pass_the_first_arg_by_reference;
40  typedef boost::is_base_of<Base, Derived> callback_must_be_a_method_of_observer_or_must_be_inherited;
41  typedef boost::is_convertible<Derived*, Base*> if_inherited_callback_then_public_inheritance_needed;
42 
43  BOOST_STATIC_ASSERT(you_must_pass_the_first_arg_by_reference ::value);
44  BOOST_STATIC_ASSERT(callback_must_be_a_method_of_observer_or_must_be_inherited ::value);
45  BOOST_STATIC_ASSERT(if_inherited_callback_then_public_inheritance_needed ::value);
46 
47  Base& objectBase = object;
48  SubjectBase<EVT>::connect(objectBase, newCallback);
49  }
50 
52  template<int EVT>
53  void connect(void (*newCallback)(int)) const
54  {
55  SubjectBase<EVT>::connect(newCallback);
56  }
57 
59  template<int EVT, class Derived, class Base>
60  void disconnect(Derived& object, void (Base::*callbackToErase)(int)) const
61  {
62  typedef boost::is_class<Derived> you_must_pass_the_first_arg_by_reference;
63  typedef boost::is_base_of<Base, Derived> callback_must_be_a_method_of_observer_or_must_be_inherited;
64  typedef boost::is_convertible<Derived*, Base*> if_inherited_callback_then_public_inheritance_needed;
65 
66  BOOST_STATIC_ASSERT(you_must_pass_the_first_arg_by_reference ::value);
67  BOOST_STATIC_ASSERT(callback_must_be_a_method_of_observer_or_must_be_inherited ::value);
68  BOOST_STATIC_ASSERT(if_inherited_callback_then_public_inheritance_needed ::value);
69 
70  Base& objectBase = object;
71  SubjectBase<EVT>::disconnect(objectBase, callbackToErase);
72  }
73 
75  template<int EVT>
76  void disconnect(void (*callbackToErase)(int)) const
77  {
78  SubjectBase<EVT>::disconnect(callbackToErase);
79  }
80 
82  template<int EVT>
83  void propagate() const
84  {
86  }
87 
89  template<int EVT>
90  void propagate(int timestamp) const
91  {
92  SubjectBase<EVT>::propagate(timestamp);
93  }
94  };
95 
96  class Observer
97  : public ObserverBase<EVT_RESIZE>
98  , public ObserverBase<EVT_CHANGE_DEPENDENCIES>
99  , public ObserverBase<EVT_CHANGE_VALUE>
100  {
101  public:
102  virtual ~Observer() {}
103 
105  template<int EVT>
106  void bind(SubjectBase<EVT>& subject)
107  {
108  ObserverBase<EVT>::bind(subject);
109  }
110 
111  template<int EVT>
113  {
115  }
116  };
117 
119  : public Observer
120  , public Subject
121  {
122  public:
124  {
125  bind<EVT_RESIZE>(*this);
126  bind<EVT_CHANGE_DEPENDENCIES>(*this);
127  bind<EVT_CHANGE_VALUE>(*this);
128  }
129 
130  virtual ~ObserverSubject() {}
131  };
132 }
133 
134 #endif//_OCRABASE_OBSERVER_SUBJECT_H_
135 
136 // cmake:sourcegroup=utils
void propagate(int timestamp) const
void disconnect(void(*callbackToErase)(int)) const
Disconnect free function.
virtual ~Subject()
void connect(T &object, typename SubjectBaseTraits< EVT, T >::callback_type newCallback) const
Call this method to register a non-static method as a callback.
void propagate() const
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void disconnect(T &object, typename SubjectBaseTraits< EVT, T >::callback_type callback) const
Disconnect non-static method.
Declaration file of the Observer and Subject base classes.
void connect(void(*newCallback)(int)) const
Call this method to register a free function as a callback.
void bind(SubjectBase< EVT > &subject)
Call this method to automatically propagate observed events to observers connected to the subject giv...
Base class for Observers with propagation system.
void bind(subject_type &subject)
Call this method to automatically propagate observed events to observers connected to the subject giv...
void disconnect(Derived &object, void(Base::*callbackToErase)(int)) const
Disconnect non-static method.
void connect(Derived &object, void(Base::*newCallback)(int)) const
Call this method to register a non-static method as a callback.
virtual ~Observer()
void stopPropagation()
Call this method from your callbacks to avoid propagation to the bound subject (if any)...