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

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

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

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

    // if not specified, disarms.
    // if specified, arms.
    void  updateTimeout(std::optional<Time::steady_dur> 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)> m_cb;
    void*                                                     m_data = nullptr;
    std::optional<Time::steady_tp>                            m_expires;
    bool                                                      m_wasCancelled = false;
};