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

#include <cxxtools/selector.h>

Inheritance diagram for cxxtools::SelectorBase:
cxxtools::Connectable 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 (Milliseconds msecs=WaitInfinite)
 Wait for activity.
bool waitUntil (Timespan t)
 Wait for activity.
void wake ()
 Wakes the selctor from waiting.
Timespan waitTimer ()
 Wait for activity.
- 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 int WaitInfinite = -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 onWaitUntil (Timespan timeout)=0
virtual void onWake ()=0
- Protected Member Functions inherited from cxxtools::Connectable
 Connectable (const Connectable &)
 Copy constructor.
Connectableoperator= (const Connectable &)
 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 ()

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()
{
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::onWaitUntil ( Timespan  timeout)
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 ( Milliseconds  msecs = WaitInfinite)

Wait for activity.

This method will wait for activity on the registered Selectables and Timers and execute the registered callbacks. It will return when either the timeout has expired or at least one event has occured.

When the timeout is 0, then just active events are triggered.

When the timeout is negative, it will wait until the next event without timeout. Use Selector::WaitInfinite to wait without timeout.

Note that the class Milliseconds has a implicit constructor which accepts a double value, so that you can pass just a number to the method to get a timeout in milliseconds.

Parameters
msecstimeout
Returns
true on timeout
Timespan cxxtools::SelectorBase::waitTimer ( )

Wait for activity.

This method executes just the pending events and returns the time when the next timer is due. When there are no active timers a negative timespan is returned.

bool cxxtools::SelectorBase::waitUntil ( Timespan  t)

Wait for activity.

This method is like wait but the timeout is specified as a timestamp. The method will wait until the specified time.

When the timespan is negative, the method will wait until the next event without timeout.

When the timespan is smaller or equal the current time, it will just check for active events and execute them.

Parameters
ttimeout as a timespan since 1970-01-01 00:00:00
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 int cxxtools::SelectorBase::WaitInfinite = -1
static

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