Code snippets‎ > ‎C++‎ > ‎Boost‎ > ‎

Signals2

#include <boost/signals2.hpp>
#include <iostream>
 
class Caller {
public:
  void execute_slot() {
    sig(*this);
  }
 
  template <class T>
  void add_slot(T &t) {
    sig.connect(t);
  }
 
  int nr() { 
    return 42; 
  }
 
private:
  boost::signals2::signal<void (Caller &)> sig;
};

class Callee {
public:
  void callback(Caller &caller) {
    std::cout << "Callback " << caller.nr() << std::endl;
  }
};

int main(int argc, char *argv[]) {
  Caller caller;
  Callee callee;
 
  caller.add_slot(boost::bind(&Callee::callback, boost::ref(callee), _1));
  caller.execute_slot();
}

Comments