libsigc++ 2.0: Functors

Main Page Groups Namespaces

# Functors

Collaboration diagram for Functors:

Functors are copyable types that define operator()(). More...

## Modules
| mem_fun()
| mem_fun() is used to convert a pointer to a method to a functor.

| ptr_fun()
| ptr_fun() is used to convert a pointer to a function to a functor.

| Slots
| Slots are type-safe representations of callback methods and functions.

## Classes
struct | sigc::functor_base
| A hint to the compiler. More...
## Defines
#define | SIGC_FUNCTORS_HAVE_RESULT_TYPE
| If you want to mix functors from a different library with libsigc++ and these functors define `result_type` simply use this macro inside namespace sigc like so:.
#define | SIGC_FUNCTOR_TRAIT(T_functor, T_return)
| If you want to mix functors from a different library with libsigc++ and these functors don't define `result_type` use this macro inside namespace sigc to expose the return type of the functors like so:.
## Functions
template<class T_action, class T_functor>
void | sigc::visit_each (const T_action& _A_action, const T_functor& _A_functor)
| This function performs a functor on each of the targets of a functor.
template<class T_type, class T_action, class T_functor>
void | sigc::visit_each_type (const T_action& _A_action, const T_functor& _A_functor)
| This function performs a functor on each of the targets of a functor limited to a restricted type.

## Detailed Description

Functors are copyable types that define operator()().

Types that define operator()() overloads with different return types are referred to as multi-type functors. Multi-type functors are only partly supported in libsigc++.

Closures are functors that store all information needed to invoke a callback from operator()().

Adaptors are functors that alter the signature of a functor's operator()().

libsigc++ defines numerous functors, closures and adaptors. Since libsigc++ is a callback libaray, most functors are also closures. The documentation doesn't distinguish between functors and closures.

The basic functor types libsigc++ provides are created with ptr_fun() and mem_fun() and can be converted into slots implicitly. The set of adaptors that ships with libsigc++ is documented in the equally named module.

## Define Documentation

#define SIGC_FUNCTOR_TRAIT | ( | T_functor,
|  | T_return |  | ) |

Value:

    template <>                                    \
    struct functor_trait<T_functor,false>          \
    {                                              \
      typedef T_return result_type;                \
      typedef T_functor functor_type;              \
    };

If you want to mix functors from a different library with libsigc++ and these functors don't define `result_type` use this macro inside namespace sigc to expose the return type of the functors like so:.

     namespace sigc {
       SIGC_FUNCTOR_TRAIT(first_functor_type, return_type_of_first_functor_type)
       SIGC_FUNCTOR_TRAIT(second_functor_type, return_type_of_second_functor_type)
       ...
     }

#define SIGC_FUNCTORS_HAVE_RESULT_TYPE

Value:

    template <class T_functor>                             \
    struct functor_trait<T_functor,false>                  \
    {                                                      \
      typedef typename T_functor::result_type result_type; \
      typedef T_functor functor_type;                      \
    };

If you want to mix functors from a different library with libsigc++ and these functors define `result_type` simply use this macro inside namespace sigc like so:.

     namespace sigc { SIGC_FUNCTORS_HAVE_RESULT_TYPE }

## Function Documentation

template <class T_action, class T_functor>

void sigc::visit_each | ( | const T_action & | _A_action,
|  | const T_functor & | _A_functor |
| ) |  |  | `[inline]`

This function performs a functor on each of the targets of a functor.

All unknown types just call _A_action on them. Add overloads that specialize the T_functor argument for your own functor types, so that subobjects get visited. This is needed to enable auto-disconnection support for your functor types.

Example:

         struct some_functor
         {
           void operator()() {}
           some_possibly_sigc_trackable_derived_type some_data_member;
           some_other_functor_type some_other_functor;
         }

         namespace sigc
         {
           template <class T_action>
           void visit_each(const T_action& _A_action,
                           const some_functor& _A_target)
           {
             visit_each(_A_action, _A_target.some_data_member);
             visit_each(_A_action, _A_target.some_other_functor);
           }
         }

template <class T_type, class T_action, class T_functor>

void sigc::visit_each_type | ( | const T_action & | _A_action,
|  | const T_functor & | _A_functor |
| ) |  |  | `[inline]`

This function performs a functor on each of the targets of a functor limited to a restricted type.