Home C-Bits Package Documentation Project Page

Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

cbits::CallbackTimer Class Reference

A timer object that invokes a user-supplied callback when it expires. More...

#include <CallbackTimer.h>

Inheritance diagram for cbits::CallbackTimer:

[legend]
Collaboration diagram for cbits::CallbackTimer:
[legend]
List of all members.

Public Types

enum  Type { SINGLE = 1, PERIODIC = 2 }
 Timer types. More...


Public Methods

virtual ~CallbackTimer ()
 Destructor. More...

const bool start ()
 Start the timer. More...

const bool stop ()
 Stop the timer. More...

virtual void run ()
 Runnable implementation. More...

const bool isStopped () const
 Test if the timer is stopped or expired. More...

const Thread *const getThread () const
 Accessor for the cbits::Thread object that's executing this timer. More...


Static Public Methods

CallbackTimer * create (const Type t, const long tm, CallbackTimerListener &cl)
 Create a callback timer instance. More...


Protected Methods

 CallbackTimer (const Type t, const long tm, CallbackTimerListener &cl)
 Constructor. More...

virtual const bool impl_lock_obj () const=0
 Lock the object. More...

virtual const bool impl_unlock_obj () const=0
 Unlock the object. More...

virtual const bool impl_wait (const long msec)=0
 Wait for the timer to expire. More...

virtual const bool impl_signal ()=0
 Abort the timer. More...


Private Attributes

Type _type
 Type of this timer. More...

long _tm_msec
 Timer period. More...

CallbackTimerListener_client
 Callback. More...

Thread_timer
 timer and callback thread. More...

bool _halt
 true if timer is stopped. More...


Detailed Description

A timer object that invokes a user-supplied callback when it expires.

If a PERIODIC timer is created, the user callback will be called at the end of each interval with the clock being 'reset' when the callback completes.

For instance, to create a timer objec that fires after 10 seconds:

	CallbackTimerListener cb = ...;
	CallbackTimer* timer = CallbackTimer::create( SINGLE, 10000, cb );
	timer->getThread()->join();
	// ... 
 

The 'join' method will return after the timer has fired and invoked the user supplied callback. If the CallbackTimer is of type PERIODIC, though, the join method will not return (in the example above).

Implementation Note:

A CallbackTimer derived class must be implemented for each new platform. The protected, pure virtual impl_XXX methods (see below) must be implemented in the derived class.


Member Enumeration Documentation

enum cbits::CallbackTimer::Type
 

Timer types.

Enumeration values:
SINGLE  Execute one interval.
PERIODIC  Execute repeatedly.


Constructor & Destructor Documentation

CallbackTimer::~CallbackTimer   [virtual]
 

Destructor.

If the timer is running, it is stopped and the callback will not be executed.

CallbackTimer::CallbackTimer const Type    t,
const long    tm,
CallbackTimerListener   cl
[protected]
 

Constructor.

Create an initialized timer. The timer must not 'run' until the start method is invoked.

Parameters:
t  Type
tm  timer interval
cl  callback


Member Function Documentation

CallbackTimer * CallbackTimer::create const Type    t,
const long    tm,
CallbackTimerListener   cl
[static]
 

Create a callback timer instance.

Create an initialized timer. The timer must not 'run' until the start method is invoked.

Returns:
a new callback timer.
@raises CallbackTimerException if the timer can not be created.
Parameters:
t  Timer type.
tm  interval (msec)
cl  Callback object.

const Thread* const cbits::CallbackTimer::getThread   const [inline]
 

Accessor for the cbits::Thread object that's executing this timer.

virtual const bool cbits::CallbackTimer::impl_lock_obj   const [protected, pure virtual]
 

Lock the object.

The implementation should protect the object against concurrent access by, for instance, locking an instance-scoped mutex.

Returns:
true if the object was successfully locked; otherwise, false.

Implemented in cbits::CallbackTimer_PTHREAD.

virtual const bool cbits::CallbackTimer::impl_signal   [protected, pure virtual]
 

Abort the timer.

The implementation must stop the timer, causing the impl_wait method to return with a value of false if the time interval has not expired.

Implemented in cbits::CallbackTimer_PTHREAD.

virtual const bool cbits::CallbackTimer::impl_unlock_obj   const [protected, pure virtual]
 

Unlock the object.

The implementation should release the 'lock' that was acquired by the 'lock' method.

Implemented in cbits::CallbackTimer_PTHREAD.

virtual const bool cbits::CallbackTimer::impl_wait const long    msec [protected, pure virtual]
 

Wait for the timer to expire.

The implementation must block the current thread for the time interval specified in the method call.

Parameters:
msec  the length of time to block the current thread.
Returns:
true if the time interval has expired, false for any other reason.

Implemented in cbits::CallbackTimer_PTHREAD.

const bool CallbackTimer::isStopped   const
 

Test if the timer is stopped or expired.

void CallbackTimer::run   [virtual]
 

Runnable implementation.

Not for client use.

Implements cbits::Runnable.

const bool CallbackTimer::start  
 

Start the timer.

Returns:
true if the timer was successfully started; otherwise, false.

const bool CallbackTimer::stop  
 

Stop the timer.

Returns:
true if the timer was stopped; otherwise, false.


Member Data Documentation

CallbackTimerListener& cbits::CallbackTimer::_client [private]
 

Callback.

bool cbits::CallbackTimer::_halt [private]
 

true if timer is stopped.

Thread* cbits::CallbackTimer::_timer [private]
 

timer and callback thread.

long cbits::CallbackTimer::_tm_msec [private]
 

Timer period.

Type cbits::CallbackTimer::_type [private]
 

Type of this timer.


The documentation for this class was generated from the following files:
Generated by
doxygen
Hosted by
SourceForge