[ create a new paste ] login | about

Link: http://codepad.org/3xRQNnWB    [ raw code | output | fork ]

pyrtsa_ - C++, pasted on May 21:
#include <boost/foreach.hpp>
#include <boost/ptr_container/ptr_set.hpp>
#include <iostream>

struct listener {
    virtual ~listener() {}
    virtual void activate() = 0;
    bool operator<(listener const & l) const { return this < &l; }
};

template <typename Range>
void listen_all(Range & listeners) {
    BOOST_FOREACH(listener & l, listeners)
        l.activate();
}

struct foo_listener : listener {
    explicit foo_listener(std::string const & s) : name(s) {}
    virtual void activate() {
        std::cout << "Whoopee, says " << name << std::endl;
    }
private:
    std::string name;
};

int main() {
    boost::ptr_set<listener> listeners;
    listeners.insert(new foo_listener("first"));
    listeners.insert(new foo_listener("second"));
    listen_all(listeners);
}


Output:
1
2
Whoopee, says first
Whoopee, says second


Create a new paste based on this one


Comments: