Reports activity on a set of devices. More...

#include <cxxtools/selector.h>

Inheritance diagram for cxxtools::SelectorBase:
cxxtools::Connectable cxxtools::NonCopyable cxxtools::EventLoopBase cxxtools::Selector cxxtools::EventLoop

Public Member Functions

virtual ~SelectorBase ()
 Destructor.
void add (Selectable &s)
 Adds an IOResult.
void remove (Selectable &s)
 Cancel an IOResult.
void add (Timer &timer)
 Adds a Timer.
void remove (Timer &timer)
 Removes a Timer.
bool wait (std::size_t msecs=WaitInfinite)
 Wait for activity.
void wake ()
 Wakes the selctor from waiting.
- Public Member Functions inherited from cxxtools::Connectable
 Connectable ()
 Default constructor.
virtual ~Connectable ()
 Closes all connections.
virtual void onConnectionOpen (const Connection &c)
 Registers a Connection with the Connectable.
virtual void onConnectionClose (const Connection &c)
 Unregisters a Connection from the Connectable.
size_t connectionCount () const

Static Public Attributes

static const std::size_t WaitInfinite = static_cast<const std::size_t>(-1)

Protected Member Functions

 SelectorBase ()
 Default constructor.
void onAddTimer (Timer &timer)
void onRemoveTimer (Timer &timer)
void onTimerChanged (Timer &timer)
virtual void onAdd (Selectable &)=0
 A Selectable is added to this Selector.
virtual void onRemove (Selectable &)=0
 A Selectable is removed from this Selector.
virtual void onReinit (Selectable &)=0
 A Selectable is reinitialised and needs to be updated.
virtual void onChanged (Selectable &s)=0
 A Selectable in this Selector has changed.
virtual bool onWait (std::size_t msecs)=0
virtual void onWake ()=0
- Protected Member Functions inherited from cxxtools::Connectable
 Connectable (const Connectable &c)
 Copy constructor.
Connectableoperator= (const Connectable &rhs)
 Assignment operator.
const std::list< Connection > & connections () const
 Returns a list of all current connections.
std::list< Connection > & connections ()
 Returns a list of all current connections.
void clear ()
- Protected Member Functions inherited from cxxtools::NonCopyable
 NonCopyable ()

Friends

class Selectable
class Timer

Additional Inherited Members

- Protected Attributes inherited from cxxtools::Connectable
std::list< Connection_connections
 A list of all current connections.

Detailed Description

Reports activity on a set of devices.

A Selector can be used to monitor a set of Selectables and Timers and wait for activity on them. The wait call can be performed with a timeout and the respective timeout signal is sent if it occurs. Clients can be notified about Timer and Selectable activity by connecting to the appropriate signals of the Timer and Selectable classes.

The following example uses a Selector to wait on acitvity on a Timer, which is set to time-out after 1000 msecs.

// slot to handle timer activity
void onTimer();
int main()
{
using cxxtools::System;
Timer timer;
timer.start(1000);
connect(timer.timeout, ontimer);
Selector selector;
selector.addTimer(timer);
selector wait();
return 0;
}

A Selector is the heart of the EventLoop, which calls Selector::wait continously. The EventLoop and Application classes provide the same API as the Selector itself.

Constructor & Destructor Documentation

virtual cxxtools::SelectorBase::~SelectorBase ( )
virtual

Destructor.

cxxtools::SelectorBase::SelectorBase ( )
protected

Default constructor.

Member Function Documentation

void cxxtools::SelectorBase::add ( Selectable s)

Adds an IOResult.

Adds an IOResult to the selector. IOResult are removed automatically when they get destroyed.

void cxxtools::SelectorBase::add ( Timer timer)

Adds a Timer.

Adds a Timer to the selector. Timers are removed automatically when they get destroyed.

Parameters
timerThe device to add
virtual void cxxtools::SelectorBase::onAdd ( Selectable )
protectedpure virtual

A Selectable is added to this Selector.

Do not throw exceptions.

Implemented in cxxtools::Selector, and cxxtools::EventLoop.

void cxxtools::SelectorBase::onAddTimer ( Timer timer)
protected
virtual void cxxtools::SelectorBase::onChanged ( Selectable s)
protectedpure virtual

A Selectable in this Selector has changed.

Do not throw exceptions.

Implemented in cxxtools::Selector, and cxxtools::EventLoop.

virtual void cxxtools::SelectorBase::onReinit ( Selectable )
protectedpure virtual

A Selectable is reinitialised and needs to be updated.

Do not throw exceptions.

Implemented in cxxtools::Selector, and cxxtools::EventLoop.

virtual void cxxtools::SelectorBase::onRemove ( Selectable )
protectedpure virtual

A Selectable is removed from this Selector.

Do not throw exceptions.

Implemented in cxxtools::Selector, and cxxtools::EventLoop.

void cxxtools::SelectorBase::onRemoveTimer ( Timer timer)
protected
void cxxtools::SelectorBase::onTimerChanged ( Timer timer)
protected
virtual bool cxxtools::SelectorBase::onWait ( std::size_t  msecs)
protectedpure virtual
virtual void cxxtools::SelectorBase::onWake ( )
protectedpure virtual
void cxxtools::SelectorBase::remove ( Selectable s)

Cancel an IOResult.

void cxxtools::SelectorBase::remove ( Timer timer)

Removes a Timer.

Parameters
timerThe timer to remove
bool cxxtools::SelectorBase::wait ( std::size_t  msecs = WaitInfinite)

Wait for activity.

This method will wait for activity on the registered Selectables and Timers. Use Selector::WaitInfinite to wait without timeout.

Parameters
msecstimeout in miliseconds
Returns
true on timeout
void cxxtools::SelectorBase::wake ( )

Wakes the selctor from waiting.

This method can be used to end a Selector::wait call before the timeout expires. It is supposed to be used from another thread and thus is thread-safe.

Friends And Related Function Documentation

friend class Selectable
friend
friend class Timer
friend

Member Data Documentation

const std::size_t cxxtools::SelectorBase::WaitInfinite = static_cast<const std::size_t>(-1)
static

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