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>
24 #include <ecl/exceptions/standard_exception.hpp>
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_ */
static void connectSlot(const std::string &topic, SigSlot< Data > *sigslot)
static const Subscribers & subscribers(const std::string &topic)
std::set< SigSlot< Data > * > Subscribers
static std::map< std::string, Topic< Data > > & topics()
Topic< Data >::Subscribers Subscribers
EndOfLine endl
static void printStatistics()
static void disconnect(const std::string &topic, SigSlot< Data > *sigslot)
static bool isTopic(const std::string &topic)
friend friend class SigSlot
static const Subscribers * connectSignal(const std::string &topic, SigSlot< Data > *sigslot)


xbot_driver
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:27:37