Separate timers in hpp and cpp files

multiple_init_problem
mbandic 2 months ago
parent 4f674467d2
commit 1ccac9dbf8
  1. 110
      lib/timers.hpp
  2. 109
      src/timers.cpp

@ -15,21 +15,13 @@ namespace asynco {
* Get the time in ms from the epoch * Get the time in ms from the epoch
*/ */
int64_t rtime_ms() { int64_t rtime_ms();
return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now()
.time_since_epoch())
.count();
}
/** /**
* Get the time in us from the epoch * Get the time in us from the epoch
*/ */
int64_t rtime_us() { int64_t rtime_us();
return chrono::duration_cast<chrono::microseconds>(chrono::system_clock::now()
.time_since_epoch())
.count();
}
/** /**
* Core timer class for construct time async functions * Core timer class for construct time async functions
@ -45,71 +37,40 @@ class timer {
/** /**
* A method to assign a callback wrapper and a reinitialization algorithm * A method to assign a callback wrapper and a reinitialization algorithm
*/ */
void init() { void init();
st.async_wait( [this] (const boost::system::error_code&) {
if (!_stop) {
callback();
if (repeate) {
st = boost::asio::steady_timer(_asynco_engine.io_context, boost::asio::chrono::milliseconds(time));
init();
}
_ticks++;
}
});
}
public: public:
/** /**
* The constructor creates the steady_timer and accompanying variables and runs a method to initialize the timer * The constructor creates the steady_timer and accompanying variables and runs a method to initialize the timer
*/ */
timer (function<void()> _callback, uint64_t _time, bool _repeate) : timer (function<void()> _callback, uint64_t _time, bool _repeate);
st(_asynco_engine.io_context, boost::asio::chrono::milliseconds(_time)),
_stop(false),
repeate(_repeate),
callback(_callback),
time(_time) {
init();
}
/** /**
* Stop timer * Stop timer
* The stop flag is set and timer remove it from the queue * The stop flag is set and timer remove it from the queue
*/ */
void stop() { void stop();
_stop = true;
st.cancel();
}
/** /**
* Run callback now * Run callback now
* Forces the callback function to run independently of the timer * Forces the callback function to run independently of the timer
*/ */
void now() { void now();
st.cancel();
}
/** /**
* Get the number of times the timer callback was runned * Get the number of times the timer callback was runned
*/ */
uint64_t ticks() { uint64_t ticks();
return _ticks;
}
/** /**
* The logic status of the timer stop state * The logic status of the timer stop state
*/ */
bool stoped() { bool stoped();
return _stop;
}
/** /**
* The destructor stops the timer * The destructor stops the timer
*/ */
~timer() { ~timer();
stop();
}
}; };
/** /**
@ -119,49 +80,37 @@ class periodic {
shared_ptr<timer> _timer; shared_ptr<timer> _timer;
public: public:
/** /**
* Constructor initializes a shared pointer of type timer * Constructor initializes a shared pointer of type timer
*/ */
periodic(function<void()> callback, uint64_t time) : periodic(function<void()> callback, uint64_t time);
_timer(make_shared<timer> (callback, time, true)) {
}
/** /**
* Stop periodic * Stop periodic
* The stop flag is set and periodic remove it from the queue * The stop flag is set and periodic remove it from the queue
*/ */
void stop() { void stop();
_timer->stop();
}
/** /**
* Run callback now * Run callback now
* Forces the callback function to run independently of the periodic * Forces the callback function to run independently of the periodic
*/ */
void now() { void now();
_timer->now();
}
/** /**
* Get the number of times the periodic callback was runned * Get the number of times the periodic callback was runned
*/ */
uint64_t ticks() { uint64_t ticks();
return _timer->ticks();
}
/** /**
* The logic status of the periodic stop state * The logic status of the periodic stop state
*/ */
bool stoped() { bool stoped();
return _timer->stoped();
}
/** /**
* The destructor stops the periodic * The destructor stops the periodic
*/ */
~periodic() { ~periodic();
stop();
}
}; };
/** /**
@ -171,49 +120,36 @@ class delayed {
shared_ptr<timer> _timer; shared_ptr<timer> _timer;
public: public:
/** /**
* Constructor initializes a shared pointer of type timer * Constructor initializes a shared pointer of type timer
*/ */
delayed(function<void()> callback, uint64_t time) : delayed(function<void()> callback, uint64_t time);
_timer(make_shared<timer> (callback, time, false)) {
}
/** /**
* Stop delayed * Stop delayed
* The stop flag is set and delayed remove it from the queue * The stop flag is set and delayed remove it from the queue
*/ */
void stop() { void stop();
_timer->stop();
}
/** /**
* Run callback now * Run callback now
* Forces the callback function to run independently of the delayed * Forces the callback function to run independently of the delayed
*/ */
void now() { void now();
_timer->now();
}
/** /**
* Get is the delayed callback runned * Get is the delayed callback runned
*/ */
bool expired() { bool expired();
return bool(_timer->ticks());
}
/** /**
* The logic status of the delayed stop state * The logic status of the delayed stop state
*/ */
bool stoped() { bool stoped();
return _timer->stoped();
}
/** /**
* The destructor stops the delayed * The destructor stops the delayed
*/ */
~delayed() { ~delayed();
stop();
}
}; };

@ -0,0 +1,109 @@
#include "../lib/timers.hpp"
namespace marcelb::asynco {
int64_t rtime_ms() {
return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now()
.time_since_epoch())
.count();
}
int64_t rtime_us() {
return chrono::duration_cast<chrono::microseconds>(chrono::system_clock::now()
.time_since_epoch())
.count();
}
void timer::init() {
st.async_wait( [this] (const boost::system::error_code&) {
if (!_stop) {
callback();
if (repeate) {
st = boost::asio::steady_timer(_asynco_engine.io_context, boost::asio::chrono::milliseconds(time));
init();
}
_ticks++;
}
});
}
timer::timer (function<void()> _callback, uint64_t _time, bool _repeate) :
st(_asynco_engine.io_context, boost::asio::chrono::milliseconds(_time)),
_stop(false),
repeate(_repeate),
callback(_callback),
time(_time) {
init();
}
void timer::stop() {
_stop = true;
st.cancel();
}
void timer::now() {
st.cancel();
}
uint64_t timer::ticks() {
return _ticks;
}
bool timer::stoped() {
return _stop;
}
timer::~timer() {
stop();
}
periodic::periodic(function<void()> callback, uint64_t time) :
_timer(make_shared<timer> (callback, time, true)) {
}
void periodic::stop() {
_timer->stop();
}
void periodic::now() {
_timer->now();
}
uint64_t periodic::ticks() {
return _timer->ticks();
}
bool periodic::stoped() {
return _timer->stoped();
}
periodic::~periodic() {
stop();
}
delayed::delayed(function<void()> callback, uint64_t time) :
_timer(make_shared<timer> (callback, time, false)) {
}
void delayed::stop() {
_timer->stop();
}
void delayed::now() {
_timer->now();
}
bool delayed::expired() {
return bool(_timer->ticks());
}
bool delayed::stoped() {
return _timer->stoped();
}
delayed::~delayed() {
stop();
}
};
Loading…
Cancel
Save