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>
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_ */
const Subscribers * subscribers() const
List of subscribers (listeners) to a topic.
Definition: topic.hpp:62
Topic(const std::string &name)
Uniquely construct with the specified name.
Definition: topic.hpp:56
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
bool empty() const
Checks to see if there is no publishers/subscribers.
Definition: topic.hpp:102
void disconnect(SigSlot< Data > *sigslot)
Disconnect a sigslot.
Definition: topic.hpp:86
std::set< SigSlot< Data > * > topic_publishers
Definition: topic.hpp:120
void addPublisher(SigSlot< Data > *sigslot)
Add a publisher.
Definition: topic.hpp:75
void addSubscriber(SigSlot< Data > *sigslot)
Add a subscriber.
Definition: topic.hpp:68
std::string topic_name
Definition: topic.hpp:119
Not for direct use, provides the power behind both signals and slots.
Definition: sigslot.hpp:46
std::set< SigSlot< Data > * > topic_subscribers
Definition: topic.hpp:121
#define ECL_LOCAL


ecl_sigslots
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:57