#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