14 #ifndef _OCRA_OBSERVER_SUBJECT_BASE_H_ 15 #define _OCRA_OBSERVER_SUBJECT_BASE_H_ 20 #include <boost/any.hpp> 21 #include <boost/type_traits/is_class.hpp> 22 #include <boost/type_traits/is_base_of.hpp> 23 #include <boost/static_assert.hpp> 24 #include <boost/ptr_container/ptr_vector.hpp> 69 result = callback == boost::any_cast<void (T::*)(int)>(
getCallback());
71 catch(boost::bad_any_cast&) {
82 result = callback == boost::any_cast<void (*)(int)>(
getCallback());
84 catch(boost::bad_any_cast&) {
98 virtual void trigger(
int timestamp) = 0;
109 template<
class T,
int EVT,
bool T_isDerivedFromObserverBase>
class Invoker 117 template<
class T,
int EVT>
122 Invoker(T&
object,
void (T::*callback)(
int)): object_(object), callback_(callback), timestamp_(-1) {}
126 object_.InvokerBase<EVT>::observer_base_type::trigger(object_, callback_, timestamp, timestamp_);
135 void (T::*callback_)(int);
143 template<
class T,
int EVT>
148 Invoker(T&
object,
void (T::*callback)(
int)): object_(object), callback_(callback) {}
150 void trigger(
int timestamp) {
if(callback_) (object_.*callback_)(timestamp); }
158 void (T::*callback_)(int);
170 Invoker(
void (*callback)(
int)): callback_(callback) {}
172 void trigger(
int timestamp) {
if(callback_) (*callback_)(timestamp); }
179 void (*callback_)(int);
243 void bind(subject_type& subject) { subject_ = &subject; }
258 void trigger(T&
object,
void (T::*callback)(
int),
int timestamp,
int& savedTimestamp)
260 if(timestamp > savedTimestamp)
262 savedTimestamp = timestamp;
265 (
object.*callback)(timestamp);
266 if(propagate_ && subject_)
267 subject_->propagate(timestamp);
271 subject_type* subject_;
274 template<
class,
int,
bool>
friend class Invoker;
292 template<
int EVT,
class T>
296 typedef void (T::*callback_type)(int);
306 typedef void (*callback_type)(int);
329 BOOST_STATIC_ASSERT(boost::is_class<T>::value);
343 BOOST_STATIC_ASSERT(boost::is_class<T>::value);
348 observers_.erase_if( std::bind2nd(comparator, elementToErase) );
355 std::mem_fun_ref<bool, invoker_base_type, void (*)(int)>(&invoker_base_type::isCallbackEqualTo);
356 observers_.erase_if( std::bind2nd(comparator, callbackToErase) );
362 std::mem_fun1_ref_t<void, invoker_base_type, int>
trigger = std::mem_fun_ref(&invoker_base_type::trigger);
363 std::for_each(observers_.begin(), observers_.end(), std::bind2nd(trigger, timestamp));
369 mutable boost::ptr_vector<invoker_base_type> observers_;
SubjectBase< EVT > subject_type
void(observer_base_type::* member_callback_type)(int)
virtual boost::any getCallback()=0
callback_type invoker_id_type
SubjectBaseTraitsBase< EVT >::invoker_base_type invoker_base_type
Invoker< void, EVT, false > invoker_type
void connect(typename SubjectBaseTraits< EVT, void >::callback_type newCallback) const
Call this method to register a free function as a callback.
void trigger(int timestamp)
InvokerBase< EVT > invoker_base_type
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(* free_callback_type)(int)
void trigger(int timestamp)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
bool isObserverEqualTo(std::pair< T *, void(T::*)(int)> observer)
bool isCallbackEqualTo(void(*callback)(int))
std::mem_fun1_ref_t< bool, invoker_base_type, invoker_id_type > invoker_comparator_type
Invoker(void(*callback)(int))
std::mem_fun1_ref_t< bool, invoker_base_type, invoker_id_type > invoker_comparator_type
virtual void * getObject()=0
void disconnect(T &object, typename SubjectBaseTraits< EVT, T >::callback_type callback) const
Disconnect non-static method.
ObserverBase< EVT > observer_base_type
Invoker(T &object, void(T::*callback)(int))
bool isCallbackEqualTo(void(T::*callback)(int))
Base class for objects who can raise events,.
void disconnect(typename SubjectBaseTraits< EVT, void >::callback_type callbackToErase) const
Disconnect free function.
InvokerBase< EVT > invoker_type
void propagate(int timestamp) const
Base class for Observers with propagation system.
virtual void trigger(int timestamp)=0
void trigger(int timestamp)
Invoker(T &object, void(T::*callback)(int))
std::pair< T *, callback_type > invoker_id_type
void bind(subject_type &subject)
Call this method to automatically propagate observed events to observers connected to the subject giv...
SubjectBaseTraitsBase< EVT >::invoker_base_type invoker_base_type
void stopPropagation()
Call this method from your callbacks to avoid propagation to the bound subject (if any)...
Invoker< T, EVT, boost::is_base_of< ObserverBase< EVT >, T >::value > invoker_type