From 06950aab233de6a2f47293d59575bb42f6131660 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Wed, 27 Jan 2021 22:06:49 +0100 Subject: Complete rewrite using C++ and with shared state support --- src/timer_state.hh | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/timer_state.hh (limited to 'src/timer_state.hh') 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 +#include +#include + +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 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 create(std::filesystem::path state_file, + Delegate* delegate); + +protected: + TimerState() = default; + TimerState(TimerState const&) = delete; + TimerState& operator=(TimerState const&) = delete; +}; + +#endif // TIMER_STATE_HH -- cgit v1.2.3-70-g09d2