Re: Signals & Slots
- From: Chris Vine <chris cvine freeserve co uk>
- To: gtkmm-list gnome org
- Cc: libsigc-list gnome org, boost-users lists boost org
- Subject: Re: Signals & Slots
- Date: Fri, 19 Nov 2004 23:54:15 +0000
On Friday 19 November 2004 09:38, Foster, Gareth wrote:
> I think if you just follow through the different typical tasks you might
> undertake with the two libraries as Murray wrote:
>
> "
> 1. Create a signal
> 2. Connect a slot (callback function) to a signal.
> 2.2 For a member method.
> 2.3 For a non-member or static function.
> 3. Disconnect a slot.
> 4. Bind an extra parameter, so that e.g. a slot with 4 parameters can be
> used with a signal with 3 parameters. I don't personally find the more
> complex adaptors interesting.
> "
>
> Then write up a document where, in each case, the code needed for both
> libraries is given, and then I small discusion about any differences or
> similarities. Perhaps in each case, giving some approximate figure to
> indicate the similarity would be useful.
>
> Presumably, this could then end up in the appendix of, or being referenced
> by, a proposal for a standard signals/slots interface. I don't know what is
> involved to be honest.
>
> I suppose, it needn't be just the one person doing the comparison, there
> are four tasks in the list (and maybe a few more besides), so four people
> could spread the work load, and maybe a 5th could add final conclusions and
> draw together the others notes.
The attached file covers most of the things you can do with boost signals. It
is fairly flexible. (This is tested with boost 1.30).
Chris.
#include <boost/signal.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <functional>
// a plain function with argument
void sig_handler_func(const char* msg) {
std::cout << "sig_handler called with message: " << msg << std::endl;
}
// a plain void function
void sig_handler_func_void(void) {
std::cout << "sig_handler called" << std::endl;
}
// a functor with argument
struct Sig_handler {
typedef void result_type;
void operator()(const char* msg) const {sig_handler_func(msg);}
};
// a functor without argument
struct Sig_handler_void {
void operator()(void) const {sig_handler_func_void();}
};
// a class with methods
struct Sig_class {
void handler(const char* msg) const {sig_handler_func(msg);}
void handler_void(void) const {sig_handler_func_void();}
};
int main(void) {
// define some signals
boost::signal<void(const char*)> sig;
boost::signal<void(void)> voidsig;
// boost::function objects
boost::function<void(const char*)> f1;
boost::function<void(void)> f2;
f1 = sig_handler_func;
f2 = sig_handler_func_void;
// an object with methods
Sig_class sig_class;
// a boost::shared_ptr
boost::shared_ptr<Sig_class> sig_class_sp(new Sig_class);
//connect to signal passing one argument
sig.connect(Sig_handler());
sig.connect(&sig_handler_func);
sig.connect(f1);
sig.connect(std::ptr_fun(sig_handler_func));
sig.connect(boost::function<void(const char*)>(sig_handler_func));
sig.connect(boost::bind(&Sig_class::handler, Sig_class(), _1));
sig.connect(boost::bind(&Sig_class::handler, sig_class, _1));
sig.connect(boost::bind(&Sig_class::handler, &sig_class, _1));
sig.connect(boost::bind(&Sig_class::handler, boost::ref(sig_class), _1));
sig.connect(boost::bind(&Sig_class::handler, sig_class_sp, _1));
// connect bound functions to voidsig
voidsig.connect(boost::bind(Sig_handler(), "bound hello"));
voidsig.connect(boost::bind(sig_handler_func, "bound hello"));
voidsig.connect(boost::bind(f1, "bound hello"));
voidsig.connect(boost::bind(std::ptr_fun(sig_handler_func), "bound hello"));
voidsig.connect(boost::bind(boost::function<void(const char*)>(sig_handler_func), "bound hello"));
voidsig.connect(boost::bind(&Sig_class::handler, Sig_class(), "bound hello"));
voidsig.connect(boost::bind(&Sig_class::handler, sig_class, "bound hello"));
voidsig.connect(boost::bind(&Sig_class::handler, &sig_class, "bound hello"));
voidsig.connect(boost::bind(&Sig_class::handler, boost::ref(sig_class), "bound hello"));
voidsig.connect(boost::bind(&Sig_class::handler, sig_class_sp, "bound hello"));
// connect void functions to voidsig
voidsig.connect(Sig_handler_void());
voidsig.connect(&sig_handler_func_void);
voidsig.connect(f2);
voidsig.connect(boost::function<void(void)>(sig_handler_func_void));
voidsig.connect(boost::bind(&Sig_class::handler_void, Sig_class()));
voidsig.connect(boost::bind(&Sig_class::handler_void, sig_class));
voidsig.connect(boost::bind(&Sig_class::handler_void, &sig_class));
voidsig.connect(boost::bind(&Sig_class::handler_void, boost::ref(sig_class)));
voidsig.connect(boost::bind(&Sig_class::handler_void, sig_class_sp));
// now emit the signals
sig("hello");
voidsig();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]