summaryrefslogtreecommitdiff
path: root/LibCore/CTimer.cpp
blob: fb55ad7e6322f487aa9beb4fcc34347bc63cf3ee (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <LibCore/CTimer.h>

CTimer::CTimer(CObject* parent)
    : CObject(parent)
{
}

CTimer::CTimer(int interval, Function<void()>&& timeout_handler, CObject* parent)
    : CObject(parent)
    , on_timeout(move(timeout_handler))
{
    start(interval);
}

CTimer::~CTimer()
{
}

void CTimer::start()
{
    start(m_interval);
}

void CTimer::start(int interval)
{
    if (m_active)
        return;
    m_interval = interval;
    start_timer(interval);
    m_active = true;
}

void CTimer::restart(int interval)
{
    if (m_active)
        stop();
    start(interval);
}

void CTimer::stop()
{
    if (!m_active)
        return;
    stop_timer();
    m_active = false;
}

void CTimer::timer_event(CTimerEvent&)
{
    if (m_single_shot)
        stop();
    else {
        if (m_interval_dirty) {
            stop();
            start(m_interval);
        }
    }

    if (on_timeout)
        on_timeout();
}