Re: [sigc] libsigc++-2.0 functor and adaptor names



Am 2003.10.29 17:53 schrieb(en) Jeff Franks:
martin-ml hippogriff de wrote:

You should use the methods sigc::ptr_fun() and sigc::mem_fun(). sigc::slot<> is a structure that has an implicit templated ctor that
converts arbitrary functors (e.g. the ones returned by sigc::ptr_fun
()
and sigc::mem_fun()) into a functor with a well-defined parameter
list.

E.g. sigc::signal<void,int>::conect() takes functors of type
sigc::slot<void,int>. A sigc::slot<void,int> instantiation is created
on the fly from the functors you pass into connect(). The type of this
functor might be sigc::pointer_functor<void,int>,
sigc::pointer_functor<void,float>, sigc::bound_mem_functor<void,int> or
any other type on which operator()(int) can be invoked.


Can I just check this - sigc::slot's are what I use in typedefs, as data members, and as functon arguments when implementing an API, and users of the API pass in a sigc::ptr_fun() and sigc::mem_fun() to create those slots.

That's correct.

The reason I ask is because I found you can pass a sigc::slot<> as well and it seems to work ok.

Of course this also works. You can pass any functor into the templated
sigc::slot<> ctor (if operator()(int) can be invoked on it in the
example above). However, there is no function that returns a
sigc::slot<> object directly.

Note that sigc::slot<> also has a copy constructor, so passing a
slot with the same template signature creates a (deep) copy. Passing
a  slot with a different template signature (or any other functor)
adds an extra function call during slot invocation:

  void foo(int i);
  sigc::slot<void, int> s1(sigc::ptr_fun(&foo)); // template ctor
  s1(1); // => sigc::pointer_functor::operator()(1) => foo(1)
  sigc::slot<void, int> s2(s1); // copy ctor
  s2(2); // => sigc::pointer_functor::operator()(2) => foo(2)
  sigc::slot<void, char> s3(s1); // template ctor
  s3(3); // => sigc::slot<void, int>::operator()(3)
               => sigc::pointer_functor::operator()(3) => foo(3)
  slot::slot<void, char> s4(sigc::ptr_fun(&foo)); // template ctor
  s4(4); // sigc::pointer_functor::operator()(4) => foo(4)

By the way, sigc::slot<>s are the only functors that install
destroy notification callbacks in all objects of sigc::trackable
derived types which are bound in the stored functor.
A sigc::slot<> object can have a parent (e.g. a signal) that is
notified from the sigc::slot<>'s destroy notification calback.

Regards,

  Martin



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]