tyhenry / lsignal

C++ signal and slot system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lsignal: C++ signal/slot system.

lsignal (or lightweight signal) is a very little and fast C++ thread-safe implementation of signal and slot system which is based on modern C++11 code.

Requirements

C++ compiler with support C++11.

How to use

Include lsignal.h in your project.

Essential classes

signal

This is a template class which holds callbacks and can emit signals with certain arguments. See examples of declarations:

Declaration Description
lsignal::signal<void()> s; Signal without parameters, return type - void
lsignal::signal<int(int,int)> t; Signal with two parameters, return type - int
lsignal::signal<std::string()> u; Signal without parameters, return type - std::string

You can connect to signal any callback which looks like callable object but be aware than signature of callback must be equal signature of corresponding signal:

Callback Description
s.connect(foo); foo is a common function
s.connect(bar); bar is a lambda function
s.connect(baz); baz is a class with operator()
s.connect(&qx, &qux::func); qx is a instance of class qux

Result of this function is a instance of class connection.

When signal is emitted return value will be the result of executing last connected callback. If you want to receive all results of callbacks you should pass aggregate function as last parameter:

lsignal::signal<int(int,int)> s;
...
auto agg = [](const std::vector<int>& v) -> int { ... };
s(2, 3, agg);
connection

connection contains link between signal and callback. Available next operations:

Method Description
is_locked Check if connection is locked
set_lock If connection is locked then callback won't be called
disconnect Remove callback from signal

Also you can pass connection directly to signal::disconnect for disconnecting this connection.

slot

This class similar to connection but is used for owhership policy. Look example:

class foo : public lsignal::slot
{
    ...
};
...
foo f;

// disconnect when f was destroyed
s.connect([](){ ... }, &f);

Performance

Synthetic test (one or more empty callbacks) showed that calling lsignal from two to five times faster than calling boost::signal2 which was created with dummy (empty) mutex.

About

C++ signal and slot system

License:MIT License


Languages

Language:C++ 98.5%Language:Makefile 1.5%