manager.hpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Ifdefs
12 *****************************************************************************/
13 
14 #ifndef ECL_SIGSLOTS_MANAGER_HPP_
15 #define ECL_SIGSLOTS_MANAGER_HPP_
16 
17 /*****************************************************************************
18 ** Includes
19 *****************************************************************************/
20 
21 #include <iostream>
22 #include <map>
23 #include <string>
25 #include <ecl/config/macros.hpp>
26 #include <ecl/utilities/void.hpp>
27 #include "topic.hpp"
28 
29 /*****************************************************************************
30 ** Namespaces
31 *****************************************************************************/
32 
33 namespace ecl {
34 
35 /*****************************************************************************
36 ** Interface
37 *****************************************************************************/
48 template<typename Data = Void>
49 class SigSlotsManager {
50 public:
51 
52  friend class SigSlot<Data>;
53 
59  static void printStatistics() {
60  std::cout << "Topics" << std::endl;
61  typename std::map< std::string, Topic<Data> >::iterator iter;
62  for ( iter = topics().begin(); iter != topics().end(); ++iter ) {
63  std::cout << iter->second;
64  }
65  }
66 
67 private:
71  typedef typename Topic<Data>::Subscribers Subscribers;
72 
82  static const Subscribers* connectSignal(const std::string& topic, SigSlot<Data>* sigslot) {
83  // Try and insert a new topic in case it doesn't already exist
84  // In any case, we always get the iterator back (to new or existing)
85  //
86  // Maybe improve the efficiency of this by specifying position
87  // refer to http://www.cplusplus.com/reference/stl/map/insert/
88  std::pair< typename std::map< std::string, Topic<Data> >::iterator,bool> ret = topics().insert( std::pair< std::string, Topic<Data> >(topic, Topic<Data>(topic)) );
89  Topic<Data>& current_topic = (ret.first)->second;
90  current_topic.addPublisher(sigslot);
91  return current_topic.subscribers();
92  }
93 
101  static void connectSlot(const std::string& topic, SigSlot<Data>* sigslot) {
102  // Try and insert a new topic in case it doesn't already exist
103  // In any case, we always get the iterator back (to new or existing)
104  //
105  // Maybe improve the efficiency of this by specifying position
106  // refer to http://www.cplusplus.com/reference/stl/map/insert/
107  std::pair< typename std::map< std::string, Topic<Data> >::iterator,bool> ret = topics().insert(std::pair< std::string, Topic<Data> >(topic, Topic<Data>(topic)) );
108  Topic<Data>& current_topic = (ret.first)->second;
109  current_topic.addSubscriber(sigslot);
110  }
111 
120  static void disconnect(const std::string& topic, SigSlot<Data>* sigslot) {
121  typename std::map<std::string, Topic<Data> >::iterator iter = topics().find(topic);
122  if ( iter != topics().end() ) {
123  iter->second.disconnect(sigslot);
124  }
125  if ( iter->second.empty() ) {
126  topics().erase(iter);
127  }
128  }
129 
136  static bool isTopic(const std::string& topic) {
137  return !( topics().find(topic) == topics().end() );
138  }
139 
147  static std::map< std::string, Topic<Data> >& topics() {
148  static std::map< std::string, Topic<Data> > topic_list;
149  return topic_list;
150  }
151 
162  static const Subscribers& subscribers(const std::string& topic) {
163  typename std::map< std::string, Topic<Data> >::const_iterator iter = topics().find(topic);
164  /*
165  * Note that this is called only by SigSlotsManager::connectSignal which
166  * makes sure the topic name exists, so we don't need to do any error
167  * handling here.
168  */
169  // ecl_assert_throw( iter != topics().end(), StandardException(LOC,InvalidInputError,std::string("No sigslots topic with name:")+topic) );
170  return *iter->second.subscribers();
171  }
172 
173 };
174 
175 } // namespace ecl
176 
177 
178 #endif /* ECL_SIGSLOTS_MANAGER_HPP_ */
ecl::SigSlotsManager::printStatistics
static void printStatistics()
Print some statistics on the current status of the manager.
Definition: manager.hpp:73
topic.hpp
Simple structure holding publishers and subscribers to a topic.
ecl::Topic::subscribers
const Subscribers * subscribers() const
List of subscribers (listeners) to a topic.
Definition: topic.hpp:70
ecl::SigSlot
Not for direct use, provides the power behind both signals and slots.
Definition: sigslot.hpp:52
ecl::SigSlotsManager::isTopic
static bool isTopic(const std::string &topic)
Check to see if the specified topic exists (and is being used).
Definition: manager.hpp:150
ecl::Topic
Stores publisher and subscriber lists to a uniquely string identified topic.
Definition: topic.hpp:51
ecl::Topic::Subscribers
std::set< SigSlot< Data > * > Subscribers
A list of subscribers (slots) to a given topic.
Definition: topic.hpp:58
ecl::SigSlotsManager::connectSignal
static const Subscribers * connectSignal(const std::string &topic, SigSlot< Data > *sigslot)
Definition: manager.hpp:96
ecl::SigSlotsManager::SigSlot< Data >
friend class SigSlot< Data >
Definition: manager.hpp:66
ecl::Topic::addSubscriber
void addSubscriber(SigSlot< Data > *sigslot)
Add a subscriber.
Definition: topic.hpp:76
ecl::SigSlotsManager::subscribers
static const Subscribers & subscribers(const std::string &topic)
Provides a list of subscribers (listeners) associated with a topic.
Definition: manager.hpp:176
ecl::SigSlotsManager::connectSlot
static void connectSlot(const std::string &topic, SigSlot< Data > *sigslot)
Definition: manager.hpp:115
ecl::SigSlotsManager::topics
static std::map< std::string, Topic< Data > > & topics()
Hack to create a static variable internally without.
Definition: manager.hpp:161
ecl::Topic::addPublisher
void addPublisher(SigSlot< Data > *sigslot)
Add a publisher.
Definition: topic.hpp:83
void.hpp
standard_exception.hpp
ecl::SigSlotsManager::Subscribers
Topic< Data >::Subscribers Subscribers
A list of subscribers (slots) to a given topic.
Definition: manager.hpp:85
macros.hpp
ecl
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_sigslots
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:47