From efdce672123284847bd7fb6f12ac1ec96f28f3ef Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 7 Nov 2018 21:40:04 +0800 Subject: Make all header *.hpp . --- src/timer.hpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/timer.hpp (limited to 'src/timer.hpp') diff --git a/src/timer.hpp b/src/timer.hpp new file mode 100644 index 00000000..5d6e8b71 --- /dev/null +++ b/src/timer.hpp @@ -0,0 +1,58 @@ +#pragma once + + +#include "system_headers.hpp" +#include +#include +#include +#include + +#include "base.hpp" + +namespace cru +{ + using TimerAction = std::function; + + class TimerManager : public Object + { + public: + TimerManager() = default; + TimerManager(const TimerManager& other) = delete; + TimerManager(TimerManager&& other) = delete; + TimerManager& operator=(const TimerManager& other) = delete; + TimerManager& operator=(TimerManager&& other) = delete; + ~TimerManager() override = default; + + UINT_PTR CreateTimer(UINT milliseconds, bool loop, const TimerAction& action); + void KillTimer(UINT_PTR id); + std::optional> GetAction(UINT_PTR id); + + private: + std::map> map_{}; + UINT_PTR current_count_ = 0; + }; + + class TimerTask + { + friend TimerTask SetTimeout(std::chrono::milliseconds milliseconds, const TimerAction& action); + friend TimerTask SetInterval(std::chrono::milliseconds milliseconds, const TimerAction& action); + + private: + explicit TimerTask(UINT_PTR id); + + public: + TimerTask(const TimerTask& other) = default; + TimerTask(TimerTask&& other) = default; + TimerTask& operator=(const TimerTask& other) = default; + TimerTask& operator=(TimerTask&& other) = default; + ~TimerTask() = default; + + void Cancel(); + + private: + UINT_PTR id_; + }; + + TimerTask SetTimeout(std::chrono::milliseconds milliseconds, const TimerAction& action); + TimerTask SetInterval(std::chrono::milliseconds milliseconds, const TimerAction& action); +} -- cgit v1.2.3