1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/time/Time.hpp" 5 #include "hmbdc/Exception.hpp" 6 #include "hmbdc/Compile.hpp" 14 #include <boost/intrusive/set.hpp> 17 namespace hmbdc {
namespace time {
19 namespace timers_detail {
20 void noop(TimerManager&, SysTime
const&);
24 : boost::intrusive::set_base_hook<
25 boost::intrusive::link_mode<boost::intrusive::normal_link>
30 SysTime getFireAt()
const {
return fireAt_; }
33 using Callback = std::function<void (TimerManager&, SysTime const&)>;
34 Timer(Callback cb = timers_detail::noop)
38 void setCallback(Callback cb) {
42 bool operator < (
Timer const& other)
const 43 {
return fireAt_ < other.fireAt_; }
44 bool operator <= (
SysTime const& t)
const 45 {
return !(t < fireAt_); }
59 namespace timers_detail {
76 timer.fireAt_ = fireAt;
77 timers_.insert(timer);
87 auto range = timers_.equal_range(timer);
88 for (
auto it = range.first; it != range.second; ++it) {
100 auto res = std::numeric_limits<Duration>::max();
101 if (timers_.begin() != timers_.end()) {
102 res = std::max(
Duration(0), timers_.begin()->fireAt_ - SysTime::now());
108 using Timers = boost::intrusive::multiset<Timer>;
121 { tm.
schedule(now + interval, *
this); };
125 DailyTimer(Callback callback = timers_detail::noop)
130 Duration day = Duration::seconds(86400);
131 SysTime newFireTime = getFireAt() + day;
132 while (newFireTime < now) newFireTime += day;
Definition: Timers.hpp:23
Definition: Timers.hpp:66
Definition: Timers.hpp:124
void cancel(Timer &timer)
cancel a timer previously scheduled with the TimerManager
Definition: Timers.hpp:86
Definition: Timers.hpp:137
void schedule(SysTime fireAt, Timer &timer)
schedule the timer to start at a specific time
Definition: Timers.hpp:75
Definition: Timers.hpp:113