diff options
Diffstat (limited to 'src/timer_state.hh')
| -rw-r--r-- | src/timer_state.hh | 64 |
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 |
