topic.hpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Ifdefs
00010 *****************************************************************************/
00011 
00012 #ifndef ECL_SIGSLOTS_TOPIC_HPP_
00013 #define ECL_SIGSLOTS_TOPIC_HPP_
00014 
00015 /*****************************************************************************
00016 ** Includes
00017 *****************************************************************************/
00018 
00019 #include <string>
00020 #include <set>
00021 #include <ecl/config/macros.hpp>
00022 
00023 /*****************************************************************************
00024 ** Namespaces
00025 *****************************************************************************/
00026 
00027 namespace ecl {
00028 
00029 /*****************************************************************************
00030 ** Forward Declarations
00031 *****************************************************************************/
00032 
00033 template <typename Data>
00034 class SigSlot;
00035 
00036 /*****************************************************************************
00037 ** Interface
00038 *****************************************************************************/
00044 template <typename Data>
00045 class ECL_LOCAL Topic {
00046 public:
00047         /*********************
00048         ** Typedefs
00049         **********************/
00050         typedef std::set<SigSlot<Data>*> Subscribers;  
00056         Topic(const std::string& name) : topic_name(name) {}
00057 
00062         const Subscribers* subscribers() const { return &topic_subscribers; }
00063 
00068         void addSubscriber(SigSlot<Data>* sigslot) {
00069                 topic_subscribers.insert(sigslot);
00070         }
00075         void addPublisher(SigSlot<Data>* sigslot) {
00076                 topic_publishers.insert(sigslot);
00077         }
00086         void disconnect(SigSlot<Data>* sigslot) {
00087                 typename std::set<SigSlot<Data>*>::const_iterator iter = topic_publishers.find(sigslot);
00088                 if ( iter != topic_publishers.end() ) {
00089                         topic_publishers.erase(iter);
00090                 }
00091                 iter = topic_subscribers.find(sigslot);
00092                 if ( iter != topic_subscribers.end() ) {
00093                         topic_subscribers.erase(iter);
00094                 }
00095         }
00102         bool empty() const {
00103                 return ( ( topic_publishers.size() == 0 ) && ( topic_subscribers.size() == 0 ) );
00104         }
00105 
00106     /*********************
00107     ** Streaming
00108     **********************/
00115     template <typename OutputStream, typename TopicData>
00116     friend OutputStream& operator<<(OutputStream &ostream , const Topic<TopicData>& topic);
00117 
00118 private:
00119     std::string topic_name;
00120     std::set<SigSlot<Data>*> topic_publishers;
00121     std::set<SigSlot<Data>*> topic_subscribers;
00122 };
00123 
00124 
00125 /*****************************************************************************
00126 ** Implementation [Streaming]
00127 *****************************************************************************/
00128 
00129 template <typename OutputStream, typename TopicData>
00130 OutputStream& operator<<(OutputStream &ostream , const Topic<TopicData> &topic) {
00131 
00132         ostream << "  Name: " << topic.topic_name << "\n";
00133         ostream << "    # Subscribers: " << topic.topic_subscribers.size() << "\n";
00134         ostream << "    # Publishers : " << topic.topic_publishers.size() << "\n";
00135     ostream.flush();
00136 
00137     return ostream;
00138 }
00139 
00140 } // namespace ecl
00141 
00142 #endif /* ECL_SIGSLOTS_TOPIC_HPP_ */


ecl_sigslots
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:39