summaryrefslogtreecommitdiff
path: root/src/timer_state.hh
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2021-01-27 22:06:49 +0100
committerJoel Klinghed <the_jk@spawned.biz>2021-01-27 22:06:49 +0100
commit06950aab233de6a2f47293d59575bb42f6131660 (patch)
tree62f6eed4a6d35414f656d22b9ac7420849018a11 /src/timer_state.hh
parent1ef9c463f1efc1adfb62e42ab3dd17e8c6394373 (diff)
Complete rewrite using C++ and with shared state support
Diffstat (limited to 'src/timer_state.hh')
-rw-r--r--src/timer_state.hh64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/timer_state.hh b/src/timer_state.hh
new file mode 100644
index 0000000..0a3c1d1
--- /dev/null
+++ b/src/timer_state.hh
@@ -0,0 +1,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