Boost logo

Boost Users :

From: Seweryn Habdank-Wojewódzki (shw_at_[hidden])
Date: 2007-06-21 18:06:03


Hi

How to connect method with more arguments (pointers) to signal using e.g.
boost::bind?

The code below works, but if I eliminate Data_type class from desing, and try
to work directly on _two_ pointers I have a problem with connection methods
to signal slots.

Could anyone tell me how to do that?

Best regards.

#include <boost/bind.hpp>
#include <boost/signals.hpp>
#include <iostream>
#include <utility>

// some class
class Test {
public:
  explicit Test(int x) : x_ (x) {}
  int const x() const { return x_; }
  operator int() { return x_; }
private:
  int x_;
};

// class wrap references
class Data_type {
public:
  Data_type(double & d, Test & t) : d_ (d), t_ (t) {}
  double & d() const { return d_; }
  Test & t() const { return t_; }
private:
  double & d_;
  Test & t_;
};

// class react on signals
class some_class {
public:
  bool some_function(Data_type & dt) {
    bool test = (dt.d() > 3.14);
    std::cout << std::boolalpha << "Check " << dt.d()
      << " > 3.14 " << test << " and " << dt.t().x() << '\n';
    return test;
  }
  
  bool another_function(Data_type & dt) {
    bool test = (dt.d() > 3.14);
    std::cout << std::boolalpha << "Check " << dt.d()
      << " > 3.14 " << test << " and " << dt.t().x() << '\n';
    return test;
  }
};

// signal source
class Caller {
public:
  Caller() : sig0(), sig1() {}
  ~Caller(){}
  
  typedef boost::signal <bool (Data_type & dt)> signal_type;
  typedef signal_type::slot_type slot_type;

  signal_type sig0;
  signal_type sig1;

  void emit (double & d, Test & t) {
    // conversion from 2 parameters to 1
    Data_type tmp (d, t);
    sig0 (tmp);
    sig1 (tmp);
  }

  boost::signals::connection connect_to_sig0 (slot_type const & s) {
    return sig0.connect (s);
  }

  boost::signals::connection connect_to_sig1 (slot_type const & s) {
    return sig1.connect (s);
  }
};

int main() {
  using std::cout;
  using namespace boost;

  some_class sc;
  Caller c;

  // if I will eliminate Data_type class
  // and change methods that they could take 2 arguments
  // then bind can not meet requirements of slot_type
  c.connect_to_sig0 (bind (&some_class::some_function, &sc, _1));
  c.connect_to_sig1 (bind (&some_class::another_function, &sc, _1));

  Test * t = new Test (4);
  double * d = new double (3.14);
  c.emit (*d, *t);
}
        

-- 
|\/\/|   Seweryn Habdank-Wojewódzki
 \/\/ 

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net