summaryrefslogtreecommitdiff
path: root/src/managers/eventLoop/EventLoopTimer.hpp
blob: e5a8c5e38ef3770556c7a3632dc321bdf3b4de39 (plain)
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
#pragma once

#include <chrono>
#include <functional>
#include <optional>

#include "../../helpers/memory/Memory.hpp"

class CEventLoopTimer {
  public:
    CEventLoopTimer(std::optional<std::chrono::steady_clock::duration> timeout, std::function<void(SP<CEventLoopTimer> self, void* data)> cb_, void* data_);

    // if not specified, disarms.
    // if specified, arms.
    void  updateTimeout(std::optional<std::chrono::steady_clock::duration> timeout);

    void  cancel();
    bool  passed();
    bool  armed();

    float leftUs();

    bool  cancelled();
    // resets expires
    void call(SP<CEventLoopTimer> self);

  private:
    std::function<void(SP<CEventLoopTimer> self, void* data)> cb;
    void*                                                     data = nullptr;
    std::optional<std::chrono::steady_clock::time_point>      expires;
    bool                                                      wasCancelled = false;
};