Program Listing for File topic.hpp

Return to documentation for file (/tmp/ws/src/ecl_core/ecl_sigslots/include/ecl/sigslots/topic.hpp)

/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef ECL_SIGSLOTS_TOPIC_HPP_
#define ECL_SIGSLOTS_TOPIC_HPP_

/*****************************************************************************
** Includes
*****************************************************************************/

#include <string>
#include <set>
#include <ecl/config/macros.hpp>

/*****************************************************************************
** Namespaces
*****************************************************************************/

namespace ecl {

/*****************************************************************************
** Forward Declarations
*****************************************************************************/

template <typename Data>
class SigSlot;

/*****************************************************************************
** Interface
*****************************************************************************/
template <typename Data>
class ECL_LOCAL Topic {
public:
    /*********************
    ** Typedefs
    **********************/
    typedef std::set<SigSlot<Data>*> Subscribers;
    Topic(const std::string& name) : topic_name(name) {}

    const Subscribers* subscribers() const { return &topic_subscribers; }

    void addSubscriber(SigSlot<Data>* sigslot) {
        topic_subscribers.insert(sigslot);
    }
    void addPublisher(SigSlot<Data>* sigslot) {
        topic_publishers.insert(sigslot);
    }
    void disconnect(SigSlot<Data>* sigslot) {
        typename std::set<SigSlot<Data>*>::const_iterator iter = topic_publishers.find(sigslot);
        if ( iter != topic_publishers.end() ) {
            topic_publishers.erase(iter);
        }
        iter = topic_subscribers.find(sigslot);
        if ( iter != topic_subscribers.end() ) {
            topic_subscribers.erase(iter);
        }
    }
    bool empty() const {
        return ( ( topic_publishers.size() == 0 ) && ( topic_subscribers.size() == 0 ) );
    }

    /*********************
    ** Streaming
    **********************/
    template <typename OutputStream, typename TopicData>
    friend OutputStream& operator<<(OutputStream &ostream , const Topic<TopicData>& topic);

private:
    std::string topic_name;
    std::set<SigSlot<Data>*> topic_publishers;
    std::set<SigSlot<Data>*> topic_subscribers;
};


/*****************************************************************************
** Implementation [Streaming]
*****************************************************************************/

template <typename OutputStream, typename TopicData>
OutputStream& operator<<(OutputStream &ostream , const Topic<TopicData> &topic) {

    ostream << "  Name: " << topic.topic_name << "\n";
    ostream << "    # Subscribers: " << topic.topic_subscribers.size() << "\n";
    ostream << "    # Publishers : " << topic.topic_publishers.size() << "\n";
    ostream.flush();

    return ostream;
}

} // namespace ecl

#endif /* ECL_SIGSLOTS_TOPIC_HPP_ */