aboutsummaryrefslogtreecommitdiff
path: root/CruUI/timer.h
blob: 7091377597c4d58037df49dd374cae349717c188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once


#include "system_headers.h"
#include <functional>
#include <memory>
#include <map>
#include <optional>

#include "base.h"
#include "application.h"

namespace cru
{
    class TimerManager : public Object
    {
    public:
        static TimerManager* GetInstance()
        {
            return Application::GetInstance()->GetTimerManager();
        }

    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, std::shared_ptr<Action<>> action);
        void KillTimer(UINT_PTR id);
        std::shared_ptr<Action<>> GetAction(UINT_PTR id);

    private:
        std::map<UINT_PTR, std::shared_ptr<Action<>>> map_{};
        UINT_PTR current_count_ = 0;
    };

    struct ITimerTask : virtual Interface
    {
        virtual void Cancel() = 0;
    };

    std::shared_ptr<ITimerTask> SetTimeout(double seconds, std::shared_ptr<Action<>> action);
    std::shared_ptr<ITimerTask> SetInterval(double seconds, std::shared_ptr<Action<>> action);
}