dispatcher.h
Go to the documentation of this file.
1 #ifndef H_CAN_DISPATCHER
2 #define H_CAN_DISPATCHER
3 
5 #include <list>
6 #include <boost/thread/mutex.hpp>
7 #include <boost/unordered_map.hpp>
8 #include <boost/utility.hpp>
9 #include <boost/foreach.hpp>
10 #include <boost/weak_ptr.hpp>
11 
12 namespace can{
13 
14 template< typename Listener > class SimpleDispatcher{
15 public:
16  typedef typename Listener::Callable Callable;
17  typedef typename Listener::Type Type;
19 protected:
20  class DispatcherBase;
21  typedef boost::shared_ptr<DispatcherBase> DispatcherBaseSharedPtr;
22  class DispatcherBase : boost::noncopyable{
23  class GuardedListener: public Listener{
24  boost::weak_ptr<DispatcherBase> guard_;
25  public:
26  GuardedListener(DispatcherBaseSharedPtr g, const Callable &callable): Listener(callable), guard_(g){}
27  virtual ~GuardedListener() {
28  DispatcherBaseSharedPtr d = guard_.lock();
29  if(d){
30  d->remove(this);
31  }
32  }
33  };
34 
35  boost::mutex &mutex_;
36  std::list<const Listener* > listeners_;
37  public:
38  DispatcherBase(boost::mutex &mutex) : mutex_(mutex) {}
39  void dispatch_nolock(const Type &obj) const{
40  for(typename std::list<const Listener* >::const_iterator it=listeners_.begin(); it != listeners_.end(); ++it){
41  (**it)(obj);
42  }
43  }
44  void remove(Listener *d){
45  boost::mutex::scoped_lock lock(mutex_);
46  listeners_.remove(d);
47  }
48  size_t numListeners(){
49  boost::mutex::scoped_lock lock(mutex_);
50  return listeners_.size();
51  }
52 
53  static ListenerConstSharedPtr createListener(DispatcherBaseSharedPtr dispatcher, const Callable &callable){
54  ListenerConstSharedPtr l(new GuardedListener(dispatcher,callable));
55  dispatcher->listeners_.push_back(l.get());
56  return l;
57  }
58  };
59  boost::mutex mutex_;
60  DispatcherBaseSharedPtr dispatcher_;
61 public:
62  SimpleDispatcher() : dispatcher_(new DispatcherBase(mutex_)) {}
63  ListenerConstSharedPtr createListener(const Callable &callable){
64  boost::mutex::scoped_lock lock(mutex_);
65  return DispatcherBase::createListener(dispatcher_, callable);
66  }
67  void dispatch(const Type &obj){
68  boost::mutex::scoped_lock lock(mutex_);
69  dispatcher_->dispatch_nolock(obj);
70  }
71  size_t numListeners(){
72  return dispatcher_->numListeners();
73  }
74  operator Callable() { return Callable(this,&SimpleDispatcher::dispatch); }
75 };
76 
77 template<typename K, typename Listener, typename Hash = boost::hash<K> > class FilteredDispatcher: public SimpleDispatcher<Listener>{
79  boost::unordered_map<K, typename BaseClass::DispatcherBaseSharedPtr, Hash> filtered_;
80 public:
81  using BaseClass::createListener;
82  typename BaseClass::ListenerConstSharedPtr createListener(const K &key, const typename BaseClass::Callable &callable){
83  boost::mutex::scoped_lock lock(BaseClass::mutex_);
84  typename BaseClass::DispatcherBaseSharedPtr &ptr = filtered_[key];
85  if(!ptr) ptr.reset(new typename BaseClass::DispatcherBase(BaseClass::mutex_));
86  return BaseClass::DispatcherBase::createListener(ptr, callable);
87  }
88  void dispatch(const typename BaseClass::Type &obj){
89  boost::mutex::scoped_lock lock(BaseClass::mutex_);
90  typename BaseClass::DispatcherBaseSharedPtr &ptr = filtered_[obj];
91  if(ptr) ptr->dispatch_nolock(obj);
92  BaseClass::dispatcher_->dispatch_nolock(obj);
93  }
94  operator typename BaseClass::Callable() { return typename BaseClass::Callable(this,&FilteredDispatcher::dispatch); }
95 };
96 
97 } // namespace can
98 #endif
boost::unordered_map< K, typename BaseClass::DispatcherBaseSharedPtr, Hash > filtered_
Definition: dispatcher.h:79
void dispatch(const typename BaseClass::Type &obj)
Definition: dispatcher.h:88
Definition: asio_base.h:11
boost::weak_ptr< DispatcherBase > guard_
Definition: dispatcher.h:24
static ListenerConstSharedPtr createListener(DispatcherBaseSharedPtr dispatcher, const Callable &callable)
Definition: dispatcher.h:53
DispatcherBase(boost::mutex &mutex)
Definition: dispatcher.h:38
boost::mutex mutex_
Definition: dispatcher.h:59
boost::shared_ptr< DispatcherBase > DispatcherBaseSharedPtr
Definition: dispatcher.h:20
Listener::ListenerConstSharedPtr ListenerConstSharedPtr
Definition: dispatcher.h:18
void dispatch(const Type &obj)
Definition: dispatcher.h:67
DispatcherBaseSharedPtr dispatcher_
Definition: dispatcher.h:60
Listener::Callable Callable
Definition: dispatcher.h:16
void dispatch_nolock(const Type &obj) const
Definition: dispatcher.h:39
boost::shared_ptr< const Listener > ListenerConstSharedPtr
Definition: interface.h:99
BaseClass::ListenerConstSharedPtr createListener(const K &key, const typename BaseClass::Callable &callable)
Definition: dispatcher.h:82
ListenerConstSharedPtr createListener(const Callable &callable)
Definition: dispatcher.h:63
SimpleDispatcher< Listener > BaseClass
Definition: dispatcher.h:78
GuardedListener(DispatcherBaseSharedPtr g, const Callable &callable)
Definition: dispatcher.h:26
std::list< const Listener * > listeners_
Definition: dispatcher.h:36
Listener::Type Type
Definition: dispatcher.h:17


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Sat May 4 2019 02:40:41