aboutsummaryrefslogtreecommitdiff
path: root/CruUI/timer.h
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-09-01 23:28:28 +0800
committercrupest <crupest@outlook.com>2018-09-01 23:28:28 +0800
commit956a401f9c955f26b7e661dc80f76bfc43fc4124 (patch)
tree8af088933c7bc08942478daddd55c92de8668359 /CruUI/timer.h
downloadcru-956a401f9c955f26b7e661dc80f76bfc43fc4124.tar.gz
cru-956a401f9c955f26b7e661dc80f76bfc43fc4124.tar.bz2
cru-956a401f9c955f26b7e661dc80f76bfc43fc4124.zip
Initial commit
Diffstat (limited to 'CruUI/timer.h')
-rw-r--r--CruUI/timer.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/CruUI/timer.h b/CruUI/timer.h
new file mode 100644
index 00000000..1a512a44
--- /dev/null
+++ b/CruUI/timer.h
@@ -0,0 +1,49 @@
+#pragma once
+
+
+#include "system_headers.h"
+#include <functional>
+#include <memory>
+#include <map>
+#include <optional>
+
+#include "base.h"
+#include "application.h"
+
+namespace cru
+{
+ using TimerAction = std::function<void()>;
+
+ class TimerManager : public Object
+ {
+ friend class cru::Application;
+ private:
+ static TimerManager* instance_;
+
+ public:
+ static TimerManager* GetInstance();
+
+ public:
+ TimerManager();
+ TimerManager(const TimerManager& other) = delete;
+ TimerManager(TimerManager&& other) = delete;
+ TimerManager& operator=(const TimerManager& other) = delete;
+ TimerManager& operator=(TimerManager&& other) = delete;
+ ~TimerManager() override;
+
+ UINT_PTR CreateTimer(UINT microseconds, bool loop, const TimerAction& action);
+ void KillTimer(UINT_PTR id);
+ std::optional<TimerAction> GetAction(UINT_PTR id);
+
+ private:
+ std::map<UINT_PTR, TimerAction> map_{};
+ };
+
+ struct ITimerTask : virtual Interface
+ {
+ virtual void Cancel() = 0;
+ };
+
+ std::shared_ptr<ITimerTask> SetTimeout(double seconds, const TimerAction& action);
+ std::shared_ptr<ITimerTask> SetInterval(double seconds, const TimerAction& action);
+}