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>
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:
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_ */
static void connectSlot(const std::string &topic, SigSlot< Data > *sigslot)
Definition: manager.hpp:101
const Subscribers * subscribers() const
List of subscribers (listeners) to a topic.
Definition: topic.hpp:62
static const Subscribers & subscribers(const std::string &topic)
Provides a list of subscribers (listeners) associated with a topic.
Definition: manager.hpp:162
Stores publisher and subscriber lists to a uniquely string identified topic.
Definition: topic.hpp:45
std::set< SigSlot< Data > * > Subscribers
A list of subscribers (slots) to a given topic.
Definition: topic.hpp:50
static std::map< std::string, Topic< Data > > & topics()
Hack to create a static variable internally without.
Definition: manager.hpp:147
Simple structure holding publishers and subscribers to a topic.
Topic< Data >::Subscribers Subscribers
A list of subscribers (slots) to a given topic.
Definition: manager.hpp:71
void addPublisher(SigSlot< Data > *sigslot)
Add a publisher.
Definition: topic.hpp:75
The sigslots connection manager.
Definition: manager.hpp:49
static void printStatistics()
Print some statistics on the current status of the manager.
Definition: manager.hpp:59
static void disconnect(const std::string &topic, SigSlot< Data > *sigslot)
Disconnect the sigslot from the specified topic.
Definition: manager.hpp:120
void addSubscriber(SigSlot< Data > *sigslot)
Add a subscriber.
Definition: topic.hpp:68
static bool isTopic(const std::string &topic)
Check to see if the specified topic exists (and is being used).
Definition: manager.hpp:136
Not for direct use, provides the power behind both signals and slots.
Definition: sigslot.hpp:46
static const Subscribers * connectSignal(const std::string &topic, SigSlot< Data > *sigslot)
Definition: manager.hpp:82


ecl_sigslots
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:48