diff options
author | Florent Le Coz <louiz@louiz.org> | 2014-05-27 01:12:46 +0200 |
---|---|---|
committer | Florent Le Coz <louiz@louiz.org> | 2014-05-28 17:31:28 +0200 |
commit | 5999e6e0c32e6897b88f59f0743b4bb1fc9c521c (patch) | |
tree | 7c8523be49b221e5bd0bb5753e8695fae41ecb84 /src/utils/timed_events.cpp | |
parent | e033b6a3ae2923b040eb08cfa5376efe0a3da898 (diff) | |
download | biboumi-5999e6e0c32e6897b88f59f0743b4bb1fc9c521c.tar.gz biboumi-5999e6e0c32e6897b88f59f0743b4bb1fc9c521c.tar.bz2 biboumi-5999e6e0c32e6897b88f59f0743b4bb1fc9c521c.tar.xz biboumi-5999e6e0c32e6897b88f59f0743b4bb1fc9c521c.zip |
Introduce the timed events
Diffstat (limited to 'src/utils/timed_events.cpp')
-rw-r--r-- | src/utils/timed_events.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/utils/timed_events.cpp b/src/utils/timed_events.cpp new file mode 100644 index 0000000..10ef4c1 --- /dev/null +++ b/src/utils/timed_events.cpp @@ -0,0 +1,54 @@ +#include <utils/timed_events.hpp> + +TimedEvent::TimedEvent(std::chrono::steady_clock::time_point&& time_point, + std::function<void()> callback): + time_point(std::move(time_point)), + callback(callback), + repeat(false), + repeat_delay(0) +{ +} + +TimedEvent::TimedEvent(std::chrono::milliseconds&& duration, + std::function<void()> callback): + time_point(std::chrono::steady_clock::now() + duration), + callback(callback), + repeat(true), + repeat_delay(std::move(duration)) +{ +} + +TimedEvent::TimedEvent(TimedEvent&& other): + time_point(std::move(other.time_point)), + callback(std::move(other.callback)), + repeat(other.repeat), + repeat_delay(std::move(other.repeat_delay)) +{ +} + +TimedEvent::~TimedEvent() +{ +} + +bool TimedEvent::is_after(const TimedEvent& other) const +{ + return this->is_after(other.time_point); +} + +bool TimedEvent::is_after(const std::chrono::steady_clock::time_point& time_point) const +{ + return this->time_point >= time_point; +} + +std::chrono::milliseconds TimedEvent::get_timeout() const +{ + auto now = std::chrono::steady_clock::now(); + if (now > this->time_point) + return std::chrono::milliseconds(0); + return std::chrono::duration_cast<std::chrono::milliseconds>(this->time_point - now); +} + +void TimedEvent::execute() +{ + this->callback(); +} |