summaryrefslogtreecommitdiff
path: root/src/looper.hh
blob: 092dfe59d1a45a6731c3d5dde5281d9a626a61d9 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef LOOPER_HH
#define LOOPER_HH

#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>

namespace logger {
class Logger;
}  // namespace logger

namespace looper {

constexpr static uint8_t EVENT_READ = 1;
constexpr static uint8_t EVENT_WRITE = 2;
constexpr static uint8_t EVENT_ERROR = 4;

class Hook;

class Looper {
 public:
  virtual ~Looper() = default;

  virtual void add(int fd, uint8_t events,
                   std::function<void(uint8_t)> callback) = 0;
  virtual void update(int fd, uint8_t events) = 0;
  virtual void remove(int fd) = 0;

  // Returned id is never 0
  virtual uint32_t schedule(double delay,
                            std::function<void(uint32_t)> callback) = 0;
  virtual void cancel(uint32_t id) = 0;

  virtual bool run(logger::Logger& logger) = 0;
  virtual void quit() = 0;

  virtual void add_hook(Hook* hook) = 0;
  virtual void remove_hook(Hook* hook) = 0;

 protected:
  Looper() = default;
  Looper(Looper const&) = delete;
  Looper& operator=(Looper const&) = delete;
};

class HookHelper {
 public:
  virtual ~HookHelper() = default;

  virtual void monitor(int fd, uint8_t events) = 0;
  virtual void timeout(std::chrono::milliseconds timeout) = 0;

 protected:
  HookHelper() = default;
  HookHelper(HookHelper const&) = delete;
  HookHelper& operator=(HookHelper const&) = delete;
};

class Hook {
 public:
  virtual ~Hook() = default;

  virtual void before(HookHelper& helper) = 0;
  virtual void after() = 0;

 protected:
  Hook() = default;
};

[[nodiscard]]
std::unique_ptr<Looper> create();

}  // namespace looper

#endif  // LOOPER_HH