diff options
author | crupest <crupest@outlook.com> | 2018-09-25 13:37:33 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2018-09-25 13:37:33 +0800 |
commit | ccbc293c0158d4450c0db344474193f17925403f (patch) | |
tree | 1ecde3132b3299ab2f272f541a76bced67f983b1 /src/timer.cpp | |
parent | 184c3b2b39d3fa34f9349a7d7fbebe49bc62f7fc (diff) | |
parent | ea4b0966d8eb5a8e76dfbe4d833a07a4797a3284 (diff) | |
download | cru-ccbc293c0158d4450c0db344474193f17925403f.tar.gz cru-ccbc293c0158d4450c0db344474193f17925403f.tar.bz2 cru-ccbc293c0158d4450c0db344474193f17925403f.zip |
Merge branch 'master' into issue4-dev
Diffstat (limited to 'src/timer.cpp')
-rw-r--r-- | src/timer.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/timer.cpp b/src/timer.cpp new file mode 100644 index 00000000..ce800eee --- /dev/null +++ b/src/timer.cpp @@ -0,0 +1,79 @@ +#include "timer.h" + +namespace cru +{ + UINT_PTR TimerManager::CreateTimer(const UINT milliseconds, const bool loop, ActionPtr action) + { + const auto id = current_count_++; + ::SetTimer(Application::GetInstance()->GetGodWindow()->GetHandle(), id, milliseconds, nullptr); + if (loop) + map_[id] = std::move(action); + else + map_[id] = CreateActionPtr([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); + // When timer is killed in tick action, we need to retain the action itself until the action finishes, so InvokeLater! + InvokeLater([=] + { + map_.erase(find_result); + }); + } + } + + ActionPtr 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 virtual ICancelable + { + 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_); + } + + TimerTask SetTimeout(std::chrono::milliseconds milliseconds, ActionPtr action) + { + auto id = TimerManager::GetInstance()->CreateTimer(static_cast<UINT>(milliseconds.count()), false, std::move(action)); + return std::make_shared<TimerTaskImpl>(id); + } + + TimerTask SetInterval(std::chrono::milliseconds milliseconds, ActionPtr action) + { + auto id = TimerManager::GetInstance()->CreateTimer(static_cast<UINT>(milliseconds.count()), true, std::move(action)); + return std::make_shared<TimerTaskImpl>(id); + } +} |