4.6.0
Freundlich's C++ toolkit
|
An implementation of a managed signals and slots system (replacement for boost's signals).
fcppt::signal is a library similar to boost.signals2
. It was created when boost.signals
(version 1) was the only signals library available in boost, which was really slow. See the old benchmark in Benchmarks. One of the major differences between fcppt::signal and boost.signals2
is that boost.signals2
is thread-safe, while fcppt::signal is not, which makes up for some of the speed difference.
Here is a simple, motivating example:
A signal is of type fcppt::signal::object. This type has a template parameter which contains the signal's function type. Note that signals may have a return type; see below for more on that.
To attach a callback function to the signal, you have to call the fcppt::signal::object::connect
member function. Arbitrarily many attached functions are allowed. The connect
function returns an fcppt::signal::auto_connection, which is a unique pointer to a connection. The reason behind this is that connections may keep additional information that is important when they are destroyed (see Disconnect callbacks). The callback function is disconnected from the signal when the connection object dies.
If a signal has a return value other than void
, you cannot use fcppt::signal::object's default constructor. Instead, there is a constructor that takes a function of type
where result_type
is the return type of the signal's function type. This function is used to combine the results of the different callbacks that are connected to the signal. The initial value of this operation is passed when the signal itself is called.
Here is an example which should clarify how combiners are used:
Note that there are setters for both the initial value and the combiner.
One unique feature of fcppt::signal::object is the ability to receive a callback from the signal when a connection dies. To demonstrate how this works, let's say you want to associate a name with a signal. Users can then attach to such a named signal (think about a Quake-style console where you can register new console commands). However, when the last connection to a named signal dies, the signal should die with it. To do that, we define:
To receive disconnect "events", we use fcppt::signal::object's second template parameter and set it to fcppt::signal::unregister::base. Using this base class, we have the empty method at our disposal to check if there are connections attached to the signal.
To register to a named signal, we define the following function:
Note that the remove callbacks' signature is just void()
. Our main might look like this:
Out benchmark is as follows:
vector
. The results were as follows:
Signals-Implementation | gcc-4.6 (-O3 ) | clang-3.0 (-O3 ) | icc-12.1.0 (-O3 ) |
---|---|---|---|
boost.signals | 1.426s | 1.444s | 1.679s |
boost.signals2 | 0.871s | 1.644s | 1.728s |
libsigc++ | 0.614s | 0.615s | 0.647s |
fcppt.signal | 0.526s | 0.505s | 0.539s |
Or graphically:
Classes | |
class | fcppt::signal::base< T > |
Default base class for signals. Provides no unlinking capabilities. More... | |
class | fcppt::signal::connection |
A connection returned by a connect call. More... | |
class | fcppt::signal::object< Result(Args...), Base > |
Represents a signal with a non-void return value. More... | |
class | fcppt::signal::object< void(Args...), Base > |
Represents a signal without a return value. More... | |
class | fcppt::signal::unregister::base< T > |
A base class for signals providing unlinking. More... | |
Typedefs | |
using | fcppt::signal::auto_connection = fcppt::unique_ptr<fcppt::signal::connection> |
A unique pointer to a connection. | |
using | fcppt::signal::optional_auto_connection = fcppt::optional::object<fcppt::signal::auto_connection> |
An optional auto connection. | |
using | fcppt::signal::unregister::function = fcppt::function<void()> |
The unregister function object. | |
A unique pointer to a connection.
using fcppt::signal::unregister::function = fcppt::function<void()> |
The unregister function object.
using fcppt::signal::optional_auto_connection = fcppt::optional::object<fcppt::signal::auto_connection> |
An optional auto connection.