Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

SelectorBase Class Reference

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

#include <cxxtools/selector.h>

Inheritance diagram for SelectorBase:

Connectable EventLoopBase EventLoop

List of all members.

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.

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

Friends

class Selectable
class Timer


Detailed Description

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.


Member Function Documentation

void add ( Selectable &  s  ) 

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

void add ( Timer timer  ) 

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

Parameters:
timer The device to add

void remove ( Timer timer  ) 

Parameters:
timer The timer to remove

bool wait ( std::size_t  msecs = WaitInfinite  ) 

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

Parameters:
msecs timeout in miliseconds

void wake (  ) 

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.

virtual void onAdd ( Selectable &   )  [protected, pure virtual]

Do not throw exceptions.

Implemented in EventLoop.

virtual void onRemove ( Selectable &   )  [protected, pure virtual]

Do not throw exceptions.

Implemented in EventLoop.

virtual void onReinit ( Selectable &   )  [protected, pure virtual]

Do not throw exceptions.

Implemented in EventLoop.

virtual void onChanged ( Selectable &  s  )  [protected, pure virtual]

Do not throw exceptions.

Implemented in EventLoop.

Copyright © 2008 The Tntnet Development Team
Tntnet 1.6