aboutsummaryrefslogtreecommitdiff
path: root/CruUI/timer.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-09-18 01:38:02 +0800
committercrupest <crupest@outlook.com>2018-09-18 01:38:02 +0800
commit4710715102df3806479985679bd8048631ccaab5 (patch)
tree0f18f3c1d278aace11521a93abad97c97eb4ae54 /CruUI/timer.cpp
parent94c066a34900845297c41c134a9a910124a5833d (diff)
downloadcru-4710715102df3806479985679bd8048631ccaab5.tar.gz
cru-4710715102df3806479985679bd8048631ccaab5.tar.bz2
cru-4710715102df3806479985679bd8048631ccaab5.zip
I think I can't sleep well after this commit.
Still a lot of bugs!!!
Diffstat (limited to 'CruUI/timer.cpp')
-rw-r--r--CruUI/timer.cpp163
1 files changed, 73 insertions, 90 deletions
diff --git a/CruUI/timer.cpp b/CruUI/timer.cpp
index eabc7865..7435adde 100644
--- a/CruUI/timer.cpp
+++ b/CruUI/timer.cpp
@@ -2,96 +2,79 @@
namespace cru
{
- TimerManager* TimerManager::instance_ = nullptr;
-
- TimerManager* TimerManager::GetInstance()
- {
- return instance_;
- }
-
- TimerManager::TimerManager()
- {
- instance_ = this;
- }
-
- TimerManager::~TimerManager()
- {
- instance_ = nullptr;
- }
-
- UINT_PTR TimerManager::CreateTimer(const UINT microseconds, const bool loop, const TimerAction & action)
- {
- auto id = ::SetTimer(Application::GetInstance()->GetGodWindow()->GetHandle(), 0, microseconds, nullptr);
- if (loop)
- map_[id] = action;
- else
- map_[id] = [this, action, id]() {
- action();
- this->KillTimer(id);
- };
-
- return id;
- }
-
- void TimerManager::KillTimer(const UINT_PTR id)
- {
- const auto find_result = map_.find(id);
- if (find_result != map_.cend())
- {
- ::KillTimer(nullptr, id);
- map_.erase(find_result);
- }
- }
-
- std::optional<TimerAction> TimerManager::GetAction(const UINT_PTR id)
- {
- auto find_result = map_.find(id);
- if (find_result == map_.cend())
- return std::nullopt;
- return find_result->second;
- }
-
- class TimerTaskImpl : public ITimerTask
- {
- public:
- explicit TimerTaskImpl(UINT_PTR id);
- TimerTaskImpl(const TimerTaskImpl& other) = delete;
- TimerTaskImpl(TimerTaskImpl&& other) = delete;
- TimerTaskImpl& operator=(const TimerTaskImpl& other) = delete;
- TimerTaskImpl& operator=(TimerTaskImpl&& other) = delete;
- ~TimerTaskImpl() override = default;
-
- void Cancel() override;
-
- private:
- UINT_PTR id_;
- };
-
- TimerTaskImpl::TimerTaskImpl(const UINT_PTR id)
- : id_(id)
- {
-
- }
+ UINT_PTR TimerManager::CreateTimer(const UINT milliseconds, const bool loop, std::shared_ptr<Action<>> action)
+ {
+ const auto id = current_count_++;
+ ::SetTimer(Application::GetInstance()->GetGodWindow()->GetHandle(), 0, milliseconds, nullptr);
+ if (loop)
+ map_[id] = std::move(action);
+ else
+ map_[id] = std::make_shared<Action<>>([this, action, id]() mutable {
+ (*action)();
+ this->KillTimer(id);
+ });
+ return id;
+ }
+
+ void TimerManager::KillTimer(const UINT_PTR id)
+ {
+ const auto find_result = map_.find(id);
+ if (find_result != map_.cend())
+ {
+ ::KillTimer(Application::GetInstance()->GetGodWindow()->GetHandle(), id);
+ map_.erase(find_result);
+ }
+ }
+
+ std::shared_ptr<Action<>> TimerManager::GetAction(const UINT_PTR id)
+ {
+ const auto find_result = map_.find(id);
+ if (find_result == map_.cend())
+ return nullptr;
+ return find_result->second;
+ }
+
+ class TimerTaskImpl : public ITimerTask
+ {
+ public:
+ explicit TimerTaskImpl(UINT_PTR id);
+ TimerTaskImpl(const TimerTaskImpl& other) = delete;
+ TimerTaskImpl(TimerTaskImpl&& other) = delete;
+ TimerTaskImpl& operator=(const TimerTaskImpl& other) = delete;
+ TimerTaskImpl& operator=(TimerTaskImpl&& other) = delete;
+ ~TimerTaskImpl() override = default;
+
+ void Cancel() override;
+
+ private:
+ UINT_PTR id_;
+ };
+
+ TimerTaskImpl::TimerTaskImpl(const UINT_PTR id)
+ : id_(id)
+ {
+
+ }
void TimerTaskImpl::Cancel()
- {
- TimerManager::GetInstance()->KillTimer(id_);
- }
-
- inline UINT SecondToMicroSecond(const double seconds)
- {
- return static_cast<UINT>(seconds * 1000);
- }
-
- std::shared_ptr<ITimerTask> SetTimeout(const double seconds, const TimerAction & action)
- {
- auto id = TimerManager::GetInstance()->CreateTimer(SecondToMicroSecond(seconds), false, action);
- return std::make_shared<TimerTaskImpl>(id);
- }
-
- std::shared_ptr<ITimerTask> SetInterval(const double seconds, const TimerAction & action)
- {
- auto id = TimerManager::GetInstance()->CreateTimer(SecondToMicroSecond(seconds), true, action);
- return std::make_shared<TimerTaskImpl>(id);
- }
+ {
+ TimerManager::GetInstance()->KillTimer(id_);
+ }
+
+ inline UINT SecondToMillisecond(const double seconds)
+ {
+ return static_cast<UINT>(seconds * 1000);
+ }
+
+ std::shared_ptr<ITimerTask> SetTimeout(double seconds, std::shared_ptr<Action<>> action)
+ {
+ auto id = TimerManager::GetInstance()->CreateTimer(SecondToMillisecond(seconds), false, std::move(action));
+ return std::make_shared<TimerTaskImpl>(id);
+ }
+
+ std::shared_ptr<ITimerTask> SetInterval(double seconds, std::shared_ptr<Action<>> action)
+ {
+ auto id = TimerManager::GetInstance()->CreateTimer(SecondToMillisecond(seconds), true, std::move(action));
+ return std::make_shared<TimerTaskImpl>(id);
+ }
}