Similar to my last post, this is a slightly different implementation
where the handler has a signal of its own. Now I can't seem to bind to
the handler functor, getting a compiler message like:
/usr/include/boost/bind.hpp:60: error: `bool (handler::*)(char)' is not
a class, struct, or union type
The goal is that when the keypress signal is emitted, it will execute
the handler which will receive the char (key), test it, and emit its
signal (sig) if appropriate.
Any help would again be much appreciated, the short code follow, with
the problematic bind statement commented:
Thanks in advance,
Darren Hart
compile: g++ test.cpp -o test -lboost_signals
// test.cpp ----------------------------------------------------
#include <iostream>
#include
#include
#include
#include
using namespace std;
struct widget
{
boost::signal sig;
boost::signal keypress;
bool test()
{
cout << "widget::test() - sig empty: " << (sig.empty() ? "true" :
"false") << endl;
sig();
return true;
}
};
struct handler
{
char key_;
boost::signal sig;
handler(char key) : key_(key) { }
bool operator()(char key)
{
cout << "handler::operator() - received a " << key << endl;
if (!sig.empty() && key == key_)
{
cout << "\texecuting slot\n\t";
sig();
}
cout << endl;
return true;
}
};
struct no_op
{
bool operator()() { cout << "no_op::operator() - doing nothing" <<
endl; return true; }
bool test() { cout << "no_op::test()" << endl; return true; }
};
int main()
{
// instantiate two widgets
widget widget_a;
widget widget_b;
// instanstiate the functions object (and test them)
no_op no_op_;
handler handler_a('A'); handler_a.sig.connect(no_op_);
no_op_();
handler_a('A');
// connect the function objects to the widgets
widget_a.sig.connect(no_op_);
// THE PROBLEM IS HERE - HOW DO I BIND TO handler_a.test(char) ?
widget_b.keypress.connect( boost::bind(&handler::operator(),
&handler_a) );
// END PROBLEM
cout << "signalling widget_b.keypress('B')" << endl;
widget_b.keypress('B'); // call the handler 3 times, but never execute
its slot
cout << "\nsignalling widget_b.keypress('A')" << endl;
widget_b.keypress('A'); // call the handler 3 times, executing the slot
return 0;
}