topic.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_SIGSLOTS_TOPIC_HPP_
13 #define ECL_SIGSLOTS_TOPIC_HPP_
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <string>
20 #include <set>
21 #include <ecl/config/macros.hpp>
22 
23 /*****************************************************************************
24 ** Namespaces
25 *****************************************************************************/
26 
27 namespace ecl {
28 
29 /*****************************************************************************
30 ** Forward Declarations
31 *****************************************************************************/
32 
33 template <typename Data>
34 class SigSlot;
35 
36 /*****************************************************************************
37 ** Interface
38 *****************************************************************************/
44 template <typename Data>
45 class ECL_LOCAL Topic {
46 public:
47  /*********************
48  ** Typedefs
49  **********************/
50  typedef std::set<SigSlot<Data>*> Subscribers;
56  Topic(const std::string& name) : topic_name(name) {}
57 
62  const Subscribers* subscribers() const { return &topic_subscribers; }
63 
68  void addSubscriber(SigSlot<Data>* sigslot) {
69  topic_subscribers.insert(sigslot);
70  }
75  void addPublisher(SigSlot<Data>* sigslot) {
76  topic_publishers.insert(sigslot);
77  }
86  void disconnect(SigSlot<Data>* sigslot) {
87  typename std::set<SigSlot<Data>*>::const_iterator iter = topic_publishers.find(sigslot);
88  if ( iter != topic_publishers.end() ) {
89  topic_publishers.erase(iter);
90  }
91  iter = topic_subscribers.find(sigslot);
92  if ( iter != topic_subscribers.end() ) {
93  topic_subscribers.erase(iter);
94  }
95  }
102  bool empty() const {
103  return ( ( topic_publishers.size() == 0 ) && ( topic_subscribers.size() == 0 ) );
104  }
105 
106  /*********************
107  ** Streaming
108  **********************/
115  template <typename OutputStream, typename TopicData>
116  friend OutputStream& operator<<(OutputStream &ostream , const Topic<TopicData>& topic);
117 
118 private:
119  std::string topic_name;
120  std::set<SigSlot<Data>*> topic_publishers;
121  std::set<SigSlot<Data>*> topic_subscribers;
122 };
123 
124 
125 /*****************************************************************************
126 ** Implementation [Streaming]
127 *****************************************************************************/
128 
129 template <typename OutputStream, typename TopicData>
130 OutputStream& operator<<(OutputStream &ostream , const Topic<TopicData> &topic) {
131 
132  ostream << " Name: " << topic.topic_name << "\n";
133  ostream << " # Subscribers: " << topic.topic_subscribers.size() << "\n";
134  ostream << " # Publishers : " << topic.topic_publishers.size() << "\n";
135  ostream.flush();
136 
137  return ostream;
138 }
139 
140 } // namespace ecl
141 
142 #endif /* ECL_SIGSLOTS_TOPIC_HPP_ */
ECL_LOCAL
#define ECL_LOCAL
ecl::SigSlot
Not for direct use, provides the power behind both signals and slots.
Definition: sigslot.hpp:52
ecl::Topic::topic_publishers
std::set< SigSlot< Data > * > topic_publishers
Definition: topic.hpp:128
ecl::Topic
Stores publisher and subscriber lists to a uniquely string identified topic.
Definition: topic.hpp:51
ecl::Topic::topic_subscribers
std::set< SigSlot< Data > * > topic_subscribers
Definition: topic.hpp:129
ecl::operator<<
OutputStream & operator<<(OutputStream &ostream, const TimeStampBase &time_stamp)
macros.hpp
ecl
ecl::Topic::topic_name
std::string topic_name
Definition: topic.hpp:127


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