libsigc++: Slots

libsigc++ 3.2.0

Classes
Slots
Functors

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

## Classes
class | sigc::slot< T_return(T_arg...)>
| Converts an arbitrary functor to a unified type which is opaque. More...
class | sigc::slot_base
| Base type for slots. More...

## Detailed Description

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

A slot can be constructed from any function object or function, regardless of whether it is a global function, a member method, static, or virtual.

# Creating Slots

Use the sigc::mem_fun() or sigc::ptr_fun() template functions to get a sigc::slot, like so:

sigc::slot<void(int)> sl = sigc::mem_fun(someobj, & SomeClass::somemethod);
sigc::mem_fun
decltype(auto) mem_fun(T_return(T_obj::*func)(T_arg...))
Creates a functor of type sigc::mem_functor which wraps a method.
Definition: mem_fun.h:188

or

sigc::slot<void(int)> sl = sigc::ptr_fun(&somefunction);
sigc::ptr_fun
decltype(auto) ptr_fun(T_return(*func)(T_args...))
Creates a functor of type sigc::pointer_functor which wraps an existing non-member function.
Definition: ptr_fun.h:109

or, in gtkmm,

m_Button.signal_clicked().connect( sigc::mem_fun(*this, &MyWindow::on_button_clicked) );

The compiler will complain if SomeClass::somemethod, etc. have the wrong signature.

You can also pass slots as method parameters where you might normally pass a function pointer.

# Member Methods and Automatic Disconnection

See sigc::mem_fun() about deriving from sigc::trackable to prevent member methods from being called after the instance has been destroyed.

# auto

sigc::mem_fun() and sigc::ptr_fun() return functors, but those functors are not slots.

sigc::slot<void(int)> sl = sigc::mem_fun(someobj, & SomeClass::somemethod);

is not equivalent to

auto sl = sigc::mem_fun(someobj, &SomeClass::somemethod); // Not a slot!

If you don't explicitly use a sigc::slot then the slot could call a method on an instance after it has been destroyed even if the method is in a class that derives from sigc::trackable.

# C++ Lambdas

A C++11 lambda expression is a functor (function object). It is automatically wrapped in a slot, if it is connected to a signal.

auto on_response = [&someobj] (int response_id)
{
someobj.somemethod(response_id);
somefunction(response_id);
};
m_Dialog.signal_response().connect(on_response);

If you connect a C++11 lambda expression or a std::function<> instance to a signal or assign it to a slot, if your functor contains references to sigc::trackable derived objects, those objects will not be tracked, unless you also use sigc::track_obj().