~singpolyma/biboumi

ref: f0f3d3d4e2fb7c02a5b2ad517632ec5071fefb45 biboumi/src/utils/timed_events.cpp -rw-r--r-- 1.2 KiB
f0f3d3d4 — louiz’ Remove the now useless “find … touch” hack in gitlab-ci.yml 5 years ago
                                                                                
5402a256 louiz’
e1a7114c Florent Le Coz
5402a256 louiz’
ccb4ee09 louiz’
5402a256 louiz’
e1a7114c Florent Le Coz
5402a256 louiz’
e1a7114c Florent Le Coz
5402a256 louiz’
e1a7114c Florent Le Coz
5402a256 louiz’
e1a7114c Florent Le Coz
ccb4ee09 louiz’
5402a256 louiz’
e1a7114c Florent Le Coz
0d1e0629 louiz’
e1a7114c Florent Le Coz
cd92baae louiz’
e1a7114c Florent Le Coz
66609cfb louiz’
e1a7114c Florent Le Coz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <utility>
#include <utils/timed_events.hpp>

TimedEvent::TimedEvent(std::chrono::steady_clock::time_point&& time_point,
                       std::function<void()> callback, std::string name):
  time_point(time_point),
  callback(std::move(callback)),
  repeat(false),
  repeat_delay(0),
  name(std::move(name))
{
}

TimedEvent::TimedEvent(std::chrono::milliseconds&& duration,
                       std::function<void()> callback, std::string name):
  time_point(std::chrono::steady_clock::now() + duration),
  callback(std::move(callback)),
  repeat(true),
  repeat_delay(duration),
  name(std::move(name))
{
}

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 diff = std::chrono::duration_cast<std::chrono::milliseconds>(this->time_point - std::chrono::steady_clock::now());
  return std::max(diff, 0ms);
}

void TimedEvent::execute() const
{
  this->callback();
}

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