Async 1.8.0
Async::AtTimer Class Reference

A timer that times out at a specified absolute time. More...

#include <AsyncAtTimer.h>

Inheritance diagram for Async::AtTimer:

Public Member Functions

 AtTimer (void)
 Default constructor.
 
 AtTimer (struct tm &tm, bool do_start=true)
 Constuctor.
 
 ~AtTimer (void)
 Destructor.
 
bool setTimeout (time_t t)
 Set the timeout time.
 
bool setTimeout (struct tm &tm)
 Set the timeout time.
 
void setExpireOffset (int offset_ms)
 Set the expire offset.
 
bool start (void)
 Start the timer.
 
void stop (void)
 Stop the timer.
 

Public Attributes

sigc::signal< void, AtTimer * > expired
 A signal that is emitted when the timer expires.
 

Detailed Description

A timer that times out at a specified absolute time.

Author
Tobias Blomberg / SM0SVX
Date
2013-04-06

This class is used to get a timeout at a specified absolute time. That is, you can specify a time of day, like 2013-04-06 12:43:00, when you would like the timer to expire.

This class use the gettimeofday() function as its time reference. If reading time using another function, like time(), in the expire callback, you can not be sure to get the same time value. The gettimeofday() and time() functions may return different values for the second. The offset usually seem to be small (~10ms) but this has not been tested very much. One way to get around the problem, if it's not possible to use the gettimeofday() function, is to set an offset using the setExpireOffset() method. An offset of 100ms will probably do.

#include <iostream>
#include <AsyncAtTimer.h>
using namespace std;
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
timer.setExpireOffset(100);
timer.expired.connect(mem_fun(*this, &MyClass::onTimerExpired));
timeoutNextMinute();
timer.start();
}
~MyClass(void)
{
}
private:
AtTimer timer;
void timeoutNextMinute(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm tm;
localtime_r(&tv.tv_sec, &tm);
tm.tm_min += 1;
tm.tm_sec = 0;
char timebuf[256];
cout << "Setting timer to expire at " << asctime_r(&tm, timebuf);
timer.setTimeout(tm);
}
void onTimerExpired(AtTimer *t)
{
struct timeval now;
gettimeofday(&now, NULL);
struct tm tm;
char timebuf[256];
cout << "Timer expired at (+" << now.tv_usec / 1000 << "msec) "
<< asctime_r(localtime_r(&now.tv_sec, &tm), timebuf) << endl;
timeoutNextMinute();
}
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
A timer that times out at a specified absolute time.
The core class for writing asyncronous cpp applications.
A timer that times out at a specified absolute time.
bool setTimeout(time_t t)
Set the timeout time.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
Namespace for the asynchronous programming classes.
Examples
AsyncAtTimer_demo.cpp.

Definition at line 136 of file AsyncAtTimer.h.

Constructor & Destructor Documentation

◆ AtTimer() [1/2]

Async::AtTimer::AtTimer ( void )

Default constructor.

After default construction the timer will be disabled.

◆ AtTimer() [2/2]

Async::AtTimer::AtTimer ( struct tm & tm,
bool do_start = true )
explicit

Constuctor.

Parameters
tmWhen the timer should expire in local time
do_startSet to true (default) if the timer should start upon creation

◆ ~AtTimer()

Async::AtTimer::~AtTimer ( void )

Destructor.

Member Function Documentation

◆ setExpireOffset()

void Async::AtTimer::setExpireOffset ( int offset_ms)

Set the expire offset.

Parameters
offset_msThe expire offset in milliseconds

Use this function to set an offset for the timer expiration. For example, if the offset is set to 100ms, the timer will expire 100ms after the time of day specification. It is also possible to set the offset to a negative value.

Referenced by Async::StateMachine< ContextT, StateTopT >::setTimeoutAt().

◆ setTimeout() [1/2]

bool Async::AtTimer::setTimeout ( struct tm & tm)

Set the timeout time.

Parameters
tmWhen the timer should expire in broken down local time
Returns
Returns true on success or else false

◆ setTimeout() [2/2]

bool Async::AtTimer::setTimeout ( time_t t)

Set the timeout time.

Parameters
tWhen the timer should expire in seconds since the epoch
Returns
Returns true on success or else false
Examples
AsyncAtTimer_demo.cpp.

Referenced by Async::StateMachine< ContextT, StateTopT >::setTimeoutAt().

◆ start()

bool Async::AtTimer::start ( void )

Start the timer.

Returns
Returns true on sucess or else false

Referenced by Async::StateMachine< ContextT, StateTopT >::setTimeoutAt().

◆ stop()

void Async::AtTimer::stop ( void )

Member Data Documentation

◆ expired

sigc::signal<void, AtTimer *> Async::AtTimer::expired

A signal that is emitted when the timer expires.

Parameters
timerA pointer to the timer that has expired

This signal is emitted when the timer expires. It is perfectly legal to delete the timer in the connected slot if it is known to be the only connected slot.

Definition at line 203 of file AsyncAtTimer.h.

Referenced by Async::StateMachine< ContextT, StateTopT >::StateMachine().


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