sigslot.hpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Ifdefs
12 *****************************************************************************/
13 
14 #ifndef ECL_SIGSLOTS_SIGSLOT_HPP_
15 #define ECL_SIGSLOTS_SIGSLOT_HPP_
16 
17 /*****************************************************************************
18 ** Includes
19 *****************************************************************************/
20 
21 #include <map>
22 #include <set>
23 #include <string>
24 #include <ecl/config/macros.hpp>
25 #include <ecl/threads/mutex.hpp>
27 #include <ecl/utilities/void.hpp>
28 #include "manager.hpp"
29 
30 /*****************************************************************************
31 ** Namespaces
32 *****************************************************************************/
33 
34 namespace ecl {
35 
36 /*****************************************************************************
37 ** Interface [General]
38 *****************************************************************************/
45 template <typename Data=Void>
46 class SigSlot {
47 public:
48  /*********************
49  ** Typedefs
50  **********************/
51  typedef typename Topic<Data>::Subscribers Subscribers;
52  typedef typename std::map<std::string, const Subscribers*> PublicationMap;
54  /*********************
55  ** C&D
56  **********************/
62  function = new PartiallyBoundUnaryMemberFunction< SigSlot<Data> ,Data,void >( &SigSlot<Data>::emit, *this);
63  }
69  SigSlot(void (*f)(Data)) : processing_count(0), number_of_handles(1) {
70  function = new UnaryFreeFunction<Data>(f);
71  }
79  template<typename C>
80  SigSlot(void (C::*f)(Data), C &c) : processing_count(0), number_of_handles(1) {
81  function = new PartiallyBoundUnaryMemberFunction<C,Data,void>(f,c);
82  }
83 
91  ~SigSlot() {
92  disconnect(); // stop any new processing from connected signals
93  mutex.lock(); // acts like a barrier - holds up here if function still processing stuff.
94  delete function;
95  }
96 
97  const unsigned int& handles() const { return number_of_handles; }
98  void incrHandles() { ++number_of_handles; }
99  void decrHandles() { --number_of_handles; }
106  void emit(Data data) {
107  typename PublicationMap::const_iterator topic_iter;
108  typename Subscribers::const_iterator slots_iter;
109  for ( topic_iter = publications.begin(); topic_iter != publications.end(); ++topic_iter ) {
110  const Subscribers* subscribers = topic_iter->second;
111  for ( slots_iter = subscribers->begin(); slots_iter != subscribers->end(); ++slots_iter ) {
112  SigSlot<Data> *sigslot = *slots_iter;
113  sigslot->process(data);
114  }
115  }
116  }
123  void process(Data data) {
124  mutex.trylock(); // Only lock if its not already locked.
126  (*function)(data);
127  if ( --processing_count == 0 ) {
128  mutex.unlock();
129  }
130  }
138  void connectSignal(const std::string& topic) {
139  // Logic:
140  // - if already publishing to this topic
141  // - don't do anything
142  // - else
143  // - if topic doesn't exist
144  // - Manager will automatically create a new topic
145  // - Manager returns the subscribers handle
146  // - Topic name and subscribers handle are stored locally here in publications
147  publications.insert( std::pair<std::string, const Subscribers*>(topic, SigSlotsManager<Data>::connectSignal(topic,this)) );
148  }
156  void connectSlot(const std::string& topic) {
157  std::pair< std::set<std::string>::iterator,bool > ret;
158  // std::cout << "Topic: " << topic << std::endl;
159  ret = subscriptions.insert(topic); // Doesn't matter if it already exists.
160  if ( ret.second ) {
162  } // else { already subscribed to this topic }
163  }
167  void disconnect(const std::string &topic) {
168  std::set<std::string>::const_iterator listen_iter = subscriptions.find(topic);
169  publications.erase(topic); // Doesn't matter if it finds it or not.
171  }
177  void disconnect() {
178  std::set<std::string>::iterator iter;
179  for ( iter = subscriptions.begin(); iter != subscriptions.end(); ++iter ) {
181  }
182  subscriptions.clear();
183  typename std::map<std::string,const Subscribers*>::iterator emit_iter;
184  for ( emit_iter = publications.begin(); emit_iter != publications.end(); ++emit_iter ) {
185  SigSlotsManager<Data>::disconnect(emit_iter->first, this);
186  }
187  publications.clear();
188  }
189 
190  const std::set<std::string>& subscribedTopics() { return subscriptions; }
191 
192 private:
193  Mutex mutex;
194  unsigned int processing_count; // number of running process()'
195  unsigned int number_of_handles; // number of handles to this sigslot (allows copying)
196  std::set<std::string> subscriptions; // topics this sigslot is listening to
197  PublicationMap publications; // topics this sigslot is posting to, as well as the subscribers on the other end
198 
199  UnaryFunction<Data,void> *function;
200 };
201 
202 /*****************************************************************************
203 ** Interface [Void]
204 *****************************************************************************/
205 
213 template<>
214 class SigSlot<Void> {
215 public:
216  // typedef std::set<SigSlot<Void>*> Subscribers
218  typedef std::map<std::string, const Subscribers*> PublicationMap;
226  }
233  function = new NullaryFreeFunction<void>(f);
234  }
242  template<typename C>
243  SigSlot(void (C::*f)(void), C &c) : processing_count(0), number_of_handles(1) {
244  function = new BoundNullaryMemberFunction<C,void>(f,c);
245  }
246 
254  ~SigSlot() {
255  disconnect(); // stop any new processing from connected signals
256  mutex.lock(); // acts like a barrier - holds up here if function still processing stuff.
257  delete function;
258  }
259 
260  const unsigned int& handles() const { return number_of_handles; }
261  void incrHandles() { ++number_of_handles; }
262  void decrHandles() { --number_of_handles; }
269  void emit() {
270  PublicationMap::const_iterator topic_iter;
271  Subscribers::const_iterator slots_iter;
272  for ( topic_iter = publications.begin(); topic_iter != publications.end(); ++topic_iter ) {
273  const Subscribers* subscribers = topic_iter->second;
274  for ( slots_iter = subscribers->begin(); slots_iter != subscribers->end(); ++slots_iter ) {
275  SigSlot *sigslot = *slots_iter;
276  sigslot->process();
277  }
278  }
279  }
286  void process(Void void_arg = Void()) {
287  mutex.trylock(); // Only lock if its not already locked.
289  (*function)();
290  if ( --processing_count == 0 ) {
291  mutex.unlock();
292  }
293  }
301  void connectSignal(const std::string& topic) {
302  // Logic:
303  // - if already publishing to this topic
304  // - don't do anything
305  // - else
306  // - if topic doesn't exist
307  // - Manager will automatically create a new topic
308  // - Manager returns the subscribers handle
309  // - Topic name and subscribers handle are stored locally here in publications
310  publications.insert( std::pair<std::string, const Subscribers*>(topic, SigSlotsManager<Void>::connectSignal(topic,this)) );
311  }
315  void connectSlot(const std::string& topic){
316  std::pair< std::set<std::string>::iterator,bool > ret;
317  ret = subscriptions.insert(topic); // Doesn't matter if it already exists.
318  if ( ret.second ) {
320  } // else { already subscribed to this topic }
321  }
329  void disconnect(const std::string &topic) {
330  std::set<std::string>::const_iterator listen_iter = subscriptions.find(topic);
331  publications.erase(topic); // Doesn't matter if it finds it or not.
333  }
339  void disconnect() {
340  std::set<std::string>::iterator iter;
341  for ( iter = subscriptions.begin(); iter != subscriptions.end(); ++iter ) {
343  }
344  subscriptions.clear();
345  std::map<std::string,const Subscribers*>::iterator emit_iter;
346  for ( emit_iter = publications.begin(); emit_iter != publications.end(); ++emit_iter ) {
347  SigSlotsManager<Void>::disconnect(emit_iter->first, this);
348  }
349  publications.clear();
350  }
351 
352 private:
353  Mutex mutex;
354  unsigned int processing_count; // number of running process()'
355  unsigned int number_of_handles; // number of handles to this sigslot (allows copying)
356  std::set<std::string> subscriptions; // topics this sigslot is listening to
357  PublicationMap publications; // topics this sigslot is posting to, as well as the subscribers on the other end
358 
359  NullaryFunction<void> *function;
360 };
361 
362 } // namespace ecl
363 
364 #endif /* ECL_SIGSLOTS_SIGSLOT_HPP_ */
ecl::NullaryFreeFunction< void >
ecl::SigSlot::process
void process(Data data)
Process the callback function loaded into this sigslot.
Definition: sigslot.hpp:137
ecl::SigSlot::disconnect
void disconnect()
Disconnect the sigslot from all topics.
Definition: sigslot.hpp:191
ecl::BoundNullaryMemberFunction< C, void >
ecl::UnaryFreeFunction
ecl::SigSlot
Not for direct use, provides the power behind both signals and slots.
Definition: sigslot.hpp:52
ecl::Void
manager.hpp
File comment.
function_objects.hpp
ecl::VoidFunction
void(* VoidFunction)()
ecl::SigSlot::~SigSlot
~SigSlot()
Disconnects the sigslot completely.
Definition: sigslot.hpp:105
ecl::SigSlotsManager
The sigslots connection manager.
Definition: manager.hpp:55
ecl::UnaryFunction
ecl::SigSlot::publications
PublicationMap publications
Definition: sigslot.hpp:211
ecl::Topic::Subscribers
std::set< SigSlot< Data > * > Subscribers
A list of subscribers (slots) to a given topic.
Definition: topic.hpp:58
ecl::SigSlot::Subscribers
Topic< Data >::Subscribers Subscribers
A list of subscribers (slots) to a given topic.
Definition: sigslot.hpp:65
f
void f(int i)
ecl::SigSlotsManager::connectSignal
static const Subscribers * connectSignal(const std::string &topic, SigSlot< Data > *sigslot)
Definition: manager.hpp:96
NullaryFunction< void >
mutex.hpp
ecl::SigSlot::connectSlot
void connectSlot(const std::string &topic)
Connect a slot to the specified topic.
Definition: sigslot.hpp:170
ecl::SigSlot::PublicationMap
std::map< std::string, const Subscribers * > PublicationMap
Stores publishing topics and their followers.
Definition: sigslot.hpp:66
ecl::SigSlotsManager::connectSlot
static void connectSlot(const std::string &topic, SigSlot< Data > *sigslot)
Definition: manager.hpp:115
ecl::BoundNullaryMemberFunction
ecl::SigSlot::subscriptions
std::set< std::string > subscriptions
Definition: sigslot.hpp:210
ecl::SigSlot::mutex
Mutex mutex
Definition: sigslot.hpp:207
void.hpp
ecl::SigSlot::processing_count
unsigned int processing_count
Definition: sigslot.hpp:208
ecl::SigSlot::subscribedTopics
const std::set< std::string > & subscribedTopics()
Definition: sigslot.hpp:204
ecl::SigSlot::emit
void emit(Data data)
Emit a signal along with the specified data.
Definition: sigslot.hpp:120
ecl::SigSlot::SigSlot
SigSlot()
Definition: sigslot.hpp:75
ecl::SigSlot::connectSignal
void connectSignal(const std::string &topic)
Connect a signal to the specified topic.
Definition: sigslot.hpp:152
macros.hpp
ecl
ecl::PartiallyBoundUnaryMemberFunction
ecl::SigSlot< Void >::SigSlot
SigSlot()
Definition: sigslot.hpp:232
ecl::SigSlotsManager::disconnect
static void disconnect(const std::string &topic, SigSlot< Data > *sigslot)
Disconnect the sigslot from the specified topic.
Definition: manager.hpp:134
ecl::SigSlot::number_of_handles
unsigned int number_of_handles
Definition: sigslot.hpp:209


ecl_sigslots
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:47