Re: [sigc] Re: [Boost-users] Signals & Slots
- From: Carl Nygard <cjnygard fast net>
- To: Murray Cumming <murrayc murrayc com>
- Cc: libsigc++ list <libsigc-list gnome org>, boost-users lists boost org, Doug Gregor <dgregor cs indiana edu>
- Subject: Re: [sigc] Re: [Boost-users] Signals & Slots
- Date: Sat, 08 Jan 2005 17:54:01 -0500
On Sat, 2005-01-08 at 12:34 +0100, Murray Cumming wrote:
> On Sat, 2005-01-08 at 06:05 -0500, Carl Nygard wrote:
> > On Sat, 2005-01-08 at 09:23 +0100, Murray Cumming wrote:
> > > On Fri, 2005-01-07 at 20:10 -0500, Carl Nygard wrote:
> > > > sigc::connect() -- can it take a function object or ptr-to-function or
> > > > must they be wrapped with sigc::ptr_fun() or sigc::mem_fun()?
> > >
> > > You can use a pointer-to-function without using sigc::ptr_fun(). I
> > > tested that just now.
> > >
> > > A pointer-to-member-function without sigc::mem_fun() would be useless
> > > without the object instance.
> >
> > Yeah, but it can assume operator()() if given the address of a class
> > object instance, in the case of a function object. That's what I meant
> > by "function object ... without... sigc::mem_fun()"? Is that supposed
> > to work, or is the explicit sigc::mem_fun() required?
>
> Yes, this works:
>
> class foo
> {
> public:
> void operator()(int i)
> {
> std::cout << "foo(int "<< i << ")" << std::endl;
> }
> };
>
> int main()
> {
> sigc::signal<void, int> sig;
>
> foo instance;
> sig.connect(instance);
>
> sig.emit(1);
> }
>
>
> Again, I am surprised. I'm not sure whether I like it, but I guess
> people should be less surprised as they get used to functors.
>
does the following code surprise you at all? compile with:
g++ -DALL -o example1 example1.cxx `pkg-config sigc++-2.0 --cflags --libs`
#include <iostream>
#ifdef BOOST
#include <boost/signals.hpp>
#define TRACKABLE boost::signals::trackable
#define SIGNAL boost::signal
typedef SIGNAL<void (int)> Sig;
#else
#include <sigc++/signal.h>
#define TRACKABLE sigc::trackable
#define SIGNAL sigc::signal
typedef SIGNAL<void,int> Sig;
#endif
#ifdef ALL
#define NORMAL
#define SCOPE
#ifdef BOOST
#define BOOSTREF
#else
#define PTR2
#endif
#define PTR
#endif
class Functor : public TRACKABLE
{
public:
Functor() : id(count++) {}
Functor(const Functor& obj) : id(count++) {}
void operator()(int i) const {
std::cout << "foo[" << id << "](int "<< i << ")";
}
int id;
static int count;
};
int Functor::count = 0;
void Test(const std::string& msg, const Sig& sig)
{
std::cout << " " << msg << ":";
sig(0);
std::cout << std::endl;
}
int main(int argc, char** argv)
{
#ifdef NORMAL
{
std::cout << "Normal" << std::endl;
Sig sig;
Functor f;
std::cout << " Connecting: "; f(0); std::cout << std::endl;
sig.connect(f);
Test("Normal", sig);
}
#endif
#ifdef SCOPE
{
std::cout << "Scoped" << std::endl;
Sig sig;
{
Functor f;
std::cout << " Connecting: "; f(0); std::cout << std::endl;
sig.connect(f);
Test("Scoped", sig);
}
Test("Scoped-disconn", sig);
}
#endif
#ifdef BOOSTREF
{
std::cout << "Boostref" << std::endl;
Sig sig;
{
Functor f;
std::cout << " Connecting: "; f(0); std::cout << std::endl;
sig.connect(boost::ref(f));
Test("Boost::ref", sig);
}
Test("Boost::ref-disconn", sig);
}
#endif
#ifdef PTR
{
std::cout << "Ptr" << std::endl;
Sig sig;
Functor* f = new Functor;
std::cout << " Connecting: "; (*f)(0); std::cout << std::endl;
sig.connect(*f);
Test("Ptr", sig);
delete f;
Test("Ptr-disconn", sig);
}
#endif
#ifdef PTR2
{
std::cout << "Ptr2" << std::endl;
Sig sig;
Functor* f = new Functor;
std::cout << " Connecting: "; (*f)(0); std::cout << std::endl;
sig.connect(sigc::mem_fun(f, &Functor::operator()));
Test("Ptr2", sig);
delete f;
Test("Ptr2-disconn", sig);
}
#endif
}
Normal
Connecting: foo[0](int 0)
Normal:foo[2](int 0)
Scoped
Connecting: foo[3](int 0)
Scoped:foo[5](int 0)
Scoped-disconn:foo[5](int 0)
Ptr
Connecting: foo[6](int 0)
Ptr:foo[8](int 0)
Ptr-disconn:foo[8](int 0)
Ptr2
Connecting: foo[9](int 0)
Ptr2:foo[9](int 0)
Ptr2-disconn:
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]