VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
G::Slot Namespace Reference

A typesafe callback library that isolates event sinks from event sources. More...

Classes

class  SlotImpBase
 Used as a base class to all slot implementation classes (such as G::Slot::SlotImp1), allowing them to be used as bodies to the associated slot reference-counting handle class (eg. More...
 
class  SignalImp
 A static helper class used by G::Slot signal classes. More...
 
class  SlotImp0
 A slot implementation class for zero-parameter callbacks. More...
 
class  SlotCallback0
 Provides a function to down-cast from SlotImpBase to SlotImp0. More...
 
class  Slot0
 A slot class for zero-parameter callbacks. More...
 
class  Signal0
 A signal class for zero-parameter callbacks. More...
 
class  SlotImp1
 A slot implementation class for one-parameter callbacks. More...
 
class  SlotCallback1
 Provides a function to down-cast from SlotImpBase to SlotImp1. More...
 
class  Slot1
 A slot class for one-parameter callbacks. More...
 
class  Signal1
 A signal class for one-parameter callbacks. More...
 
class  SlotImp2
 A slot implementation class for two-parameter callbacks. More...
 
class  SlotCallback2
 Provides a function to down-cast from SlotImpBase to SlotImp2. More...
 
class  Slot2
 A slot class for two-parameter callbacks. More...
 
class  Signal2
 A signal class for two-parameter callbacks. More...
 
class  SlotImp3
 A slot implementation class for three-parameter callbacks. More...
 
class  SlotCallback3
 Provides a function to down-cast from SlotImpBase to SlotImp3. More...
 
class  Slot3
 A slot class for three-parameter callbacks. More...
 
class  Signal3
 A signal class for three-parameter callbacks. More...
 

Functions

template<typename T >
Slot0 slot (T &object, void(T::*fn)())
 A slot factory function overloaded for a zero-parameter callback.
 
template<typename T , typename P >
Slot1< P > slot (T &object, void(T::*fn)(P))
 A slot factory function overloaded for a one-parameter callback.
 
template<typename T , typename P1 , typename P2 >
Slot2< P1, P2 > slot (T &object, void(T::*fn)(P1, P2))
 A slot factory function overloaded for a two-parameter callback.
 
template<typename T , typename P1 , typename P2 , typename P3 >
Slot3< P1, P2, P3 > slot (T &object, void(T::*fn)(P1, P2, P3))
 A slot factory function overloaded for a three-parameter callback.
 

Detailed Description

A typesafe callback library that isolates event sinks from event sources.

The slot/signal pattern is used in several C++ libraries including libsigc++, Qt and boost; it is completely unrelated to ANSI-C or POSIX signals (signal(), sigaction(2)).

This implementation was inspired by libsigc++, but simplified by:

  • not doing multicast
  • not detecting dangling references
  • not supporting global function callbacks
  • using only void returns

Event-generating classes expose a "signal" object which client objects can connect() to in order to receive events. The client receives events through a "slot" member function.

The key to the implementation is that the slot implementation class (eg. G::Slot::SlotImp1) is templated on the callback parameter P and the callback sink class T, whereas the slot public interface (eg. G::Slot::Slot1) is templated only on P. This means that the event source, using the public interface, does not need to know the type of the event sink. It is the slot callback classes (eg. G::Slot::Callback1) that resolve the mismatch, by doing a down-cast to the T-specific slot implementation class.

A slot is a reference-counting handle to a slot implementation body, with the G::Slot::SlotImpBase class doing the reference counting. Slots also hold a function pointer pointing to the relevant downcasting method provided by one of the callback classes.

The overloaded factory function G::Slot::slot() creates a slot handle, passing it a suitable SlotImpBase body and callback casting function. The dynamic type of the SlotImpBase pointer is a slot implementation class that knows about the specific sink class T, and the callback casting function knows how to access the derived class. The combination of SlotImpBase polymorphism and casting function isolates the slot class from the sink class.

The signal classes contain a slot that is initially uninitialised; the connect() method is used to initialise the slot so that it points to the sink class's event handling function.

Usage:

class Source
{
public:
private:
void Source::raiseEvent()
{
int n = 123 ;
m_signal.emit( n ) ;
}
} ;
class Sink
{
public:
void onEvent( int n ) ;
Sink( Source & source )
{
source.m_signal.connect( G::Slot::slot(*this,&Sink::onEvent) ) ;
}
} ;