summaryrefslogtreecommitdiff
path: root/src/timer_state.hh
blob: 0a3c1d1c747e68dbcd5298d159ff775e2015b94b (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
#ifndef TIMER_STATE_HH
#define TIMER_STATE_HH

#include <chrono>
#include <filesystem>
#include <memory>

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

  /**
   * Delegate methods must be thread-safe, they will be called on
   * a thread owned by TimerState.
   */
  class Delegate {
  public:
    virtual ~Delegate() = default;

    virtual void start(
        std::chrono::minutes total,
        std::chrono::time_point<std::chrono::system_clock> epoch) = 0;

    virtual void stop(std::chrono::minutes total) = 0;

    virtual void reset() = 0;

    virtual void restart() = 0;

  protected:
    Delegate() = default;
  };

  /**
   * Start timer. If already started nothing happens.
   * Method must be called on the same thread as create.
   */
  virtual void start() = 0;
  /**
   * Stop timer. If already stopped nothing happens.
   * Method must be called on the same thread as create.
   */
  virtual void stop() = 0;
  /**
   * Clear total if stopped. If started nothing happens.
   * Method must be called on the same thread as create.
   */
  virtual void reset() = 0;

  /**
   * Sets up a shared TimerState. state is stored in state_file.
   * Note that delegate may be called before method returns.
   * Returns nullptr in case of error.
   */
  static std::unique_ptr<TimerState> create(std::filesystem::path state_file,
                                            Delegate* delegate);

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

#endif  // TIMER_STATE_HH