timed_events.cpp 1.46 KB
Newer Older
louiz’'s avatar
louiz’ committed
1 2 3
#include <utils/timed_events.hpp>

TimedEvent::TimedEvent(std::chrono::steady_clock::time_point&& time_point,
4
                       std::function<void()> callback, const std::string& name):
louiz’'s avatar
louiz’ committed
5 6 7
  time_point(std::move(time_point)),
  callback(callback),
  repeat(false),
8 9
  repeat_delay(0),
  name(name)
louiz’'s avatar
louiz’ committed
10 11 12 13
{
}

TimedEvent::TimedEvent(std::chrono::milliseconds&& duration,
14
                       std::function<void()> callback, const std::string& name):
louiz’'s avatar
louiz’ committed
15 16 17
  time_point(std::chrono::steady_clock::now() + duration),
  callback(callback),
  repeat(true),
18 19
  repeat_delay(std::move(duration)),
  name(name)
louiz’'s avatar
louiz’ committed
20 21 22 23 24 25 26
{
}

TimedEvent::TimedEvent(TimedEvent&& other):
  time_point(std::move(other.time_point)),
  callback(std::move(other.callback)),
  repeat(other.repeat),
27 28
  repeat_delay(std::move(other.repeat_delay)),
  name(std::move(other.name))
louiz’'s avatar
louiz’ committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
{
}

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();
}
58 59 60 61 62

const std::string& TimedEvent::get_name() const
{
  return this->name;
}