SEDNL  .
Simple Event Driven Network Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Types | Public Member Functions | List of all members
SedNL::Slot< Arguments > Class Template Reference

A slot which can contain a callback. More...

#include <Slot.hpp>

Public Types

typedef void(* Callback )(Arguments...)
 Type of a callback function stored by this slot.
 
template<typename T >
using MemberCallback = void(T::*)(Arguments...)
 Type of a member function (of a T object) callback that can be stored by this slot.
 

Public Member Functions

void reset () noexcept
 Remove an eventualy stored callback.
 
void operator() (Arguments...args)
 Call the current callback, or do nothing.
 
 operator bool () const noexcept
 Check if the slot isn't empty. More...
 
template<typename T >
void set_function (T function)
 Set the callback. More...
 
template<typename T >
void set_function (T &instance, MemberCallback< T > callback)
 Set the callback from an object. More...
 
template<typename T >
void set_function (T *instance, MemberCallback< T > callback)
 Set the callback from a pointer to an object. More...
 

Detailed Description

template<typename... Arguments>
class SedNL::Slot< Arguments >

A slot which can contain a callback.

A slot is an emplacement that can store callback.

A callback can be :

Slots are the way you bind your callbacks to event consumers.

void on_connect(Connection &c)
{
std::cout << "New connection" << std::endl;
}
class MyClass
{
public:
MyClass()
:v(42)
{};
void on_hello(Connection& c, const Event& e)
{
c.send("hello_back", make_event("hello_back", 42));
};
};
// ...
MyClass my_class;
EventListener listener;
//listener.on_connect() is a reference to a Slot<Connection&>.
listener.on_connect().set_function(on_connect);
EventConsumer consumer;
//consumer.bind("hello") is a reference to a Slot<Connection&, const Event&>.
consumer.bind("hello").set_function(&my_class, MyClass::on_hello);

Definition at line 42 of file Connection.hpp.

Member Function Documentation

template<typename... Arguments>
SedNL::Slot< Arguments >::operator bool ( ) const
explicitnoexcept

Check if the slot isn't empty.

Returns
True if the slot contain a callback, False otherwise.

Definition at line 65 of file Slot.ipp.

template<typename... Arguments>
template<class T >
void SedNL::Slot< Arguments >::set_function ( function)

Set the callback.

Set the callback from a function pointer, a lambda or a functor. Actualy, it just wrap T to the internal std::function constructor, so you can also give a std::function, for example from a call to std::bind.

Parameters
[in]functionThe callback to store.

Definition at line 39 of file Slot.ipp.

template<typename... Arguments>
template<class T >
void SedNL::Slot< Arguments >::set_function ( T &  instance,
MemberCallback< T >  callback 
)

Set the callback from an object.

Set a member function as a callback. The instance given shouldn't die before the callback was removed with reset(), or the destructor ~Slot() called. Otherwise, it will result in undefined behavior.

Warning : This set_function() version store a copy of the instance. If you want to give a reference, see set_function(T*, MemberCallback<T>).

Parameters
[in]instanceThe instance that will be copied.
[in]callbackThe member function.

Definition at line 46 of file Slot.ipp.

template<typename... Arguments>
template<class T >
void SedNL::Slot< Arguments >::set_function ( T *  instance,
MemberCallback< T >  callback 
)

Set the callback from a pointer to an object.

This set_function() version store the pointer of the instance, and a pointer to member function.

Same use as set_function(T&, MemberCallback<T>) except that the instance given isn't copied. It's the most common use case.

Parameters
[in]instanceThe instance pointer that will be stored.
[in]callbackThe member function.

Definition at line 53 of file Slot.ipp.


The documentation for this class was generated from the following files: