Async  1.5.0
Public Member Functions | Public Attributes | List of all members
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. More...
 
 AtTimer (struct tm &tm, bool do_start=true)
 Constuctor. More...
 
 ~AtTimer (void)
 Destructor. More...
 
bool setTimeout (time_t t)
 Set the timeout time. More...
 
bool setTimeout (struct tm &tm)
 Set the timeout time. More...
 
void setExpireOffset (int offset_ms)
 Set the expire offset. More...
 
bool start (void)
 Start the timer. More...
 
void stop (void)
 Stop the timer. More...
 

Public Attributes

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

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(&tv.tv_sec);
tm->tm_min += 1;
tm->tm_sec = 0;
cout << "Setting timer to expire at " << asctime(tm);
timer.setTimeout(*tm);
}
void onTimerExpired(AtTimer *t)
{
struct timeval now;
gettimeofday(&now, NULL);
struct tm *tm = localtime(&now.tv_sec);
cout << "Timer expired at (+" << now.tv_usec / 1000 << "msec) "
<< asctime(tm) << endl;
timeoutNextMinute();
}
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
Examples:
AsyncAtTimer_demo.cpp.

Definition at line 135 of file AsyncAtTimer.h.

Constructor & Destructor Documentation

◆ AtTimer() [1/2]

Async::AtTimer::AtTimer ( void  )

Default constructor.

◆ AtTimer() [2/2]

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

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.

◆ setTimeout() [1/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.

◆ setTimeout() [2/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

◆ start()

bool Async::AtTimer::start ( void  )

Start the timer.

Returns
Returns true on sucess or else false

◆ stop()

void Async::AtTimer::stop ( void  )

Stop the timer.

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 200 of file AsyncAtTimer.h.


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