timer实现
2019-03-20 07:46:17 # cpp

timer_handler.h

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

#pragma once

#include <memory>
#include <set>

#include "comm_func.h"
#include "singleton.h"
#include "timer_handler.h"

namespace Sky {
struct TimerLess : public std::less<TimerHandler::Ptr> {
public:
bool operator()(TimerHandler::Ptr left, TimerHandler::Ptr right) const {
return left->getExpire() < right->getExpire();
}
};

class TimerReactor final {
using handle_set = std::multiset<TimerHandler::Ptr, TimerLess>;

public:
TimerReactor()
: m_now(CommFunc::getMilliTime()) {
}

~TimerReactor() = default;

void add(TimerHandler::Ptr handler);

void del(TimerHandler::Ptr handler);

void del(uint64_t sn);

void handleEvent(uint64_t time);

private:
handle_set m_handles;
uint64_t m_now;
};
} // namespace Sky

timer_handler.cpp

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
#include "timer_handler.h"

#include "comm_func.h"

namespace Sky {
void TimerHandler::init(uint32_t expire, uint32_t interval, uint32_t count, timer_call_back&& callBack) {
m_expire = expire + CommFunc::getMilliTime();
m_interval = interval;
m_count = count;
m_callBack = std::move(callBack);
m_forever = count == 0;
}

bool TimerHandler::update() {
if (m_callBack) {
uint64_t sn = reinterpret_cast<std::uintptr_t>(this);
m_callBack(sn);
}

if (!m_forever && --m_count == 0) {
return false;
}

m_expire = m_expire + m_interval;
return true;
}
} // namespace Sky

timer_reactor.h

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
#pragma once

#include <memory>
#include <set>

#include "comm_func.h"
#include "singleton.h"
#include "timer_handler.h"

namespace Sky {
struct TimerLess : public std::less<TimerHandler::Ptr> {
public:
bool operator()(TimerHandler::Ptr left, TimerHandler::Ptr right) const {
return left->getExpire() < right->getExpire();
}
};

class TimerReactor final {
using handle_set = std::multiset<TimerHandler::Ptr, TimerLess>;

public:
TimerReactor()
: m_now(CommFunc::getMilliTime()) {
}

~TimerReactor() = default;

void add(TimerHandler::Ptr handler);

void del(TimerHandler::Ptr handler);

void del(uint64_t sn);

void handleEvent(uint64_t time);

private:
handle_set m_handles;
uint64_t m_now;
};
} // namespace Sky

timer_reactor.cpp

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
#include "timer_handler.h"

#include "comm_func.h"

namespace Sky {
void TimerHandler::init(uint32_t expire, uint32_t interval, uint32_t count, timer_call_back&& callBack) {
m_expire = expire + CommFunc::getMilliTime();
m_interval = interval;
m_count = count;
m_callBack = std::move(callBack);
m_forever = count == 0;
}

bool TimerHandler::update() {
if (m_callBack) {
uint64_t sn = reinterpret_cast<std::uintptr_t>(this);
m_callBack(sn);
}

if (!m_forever && --m_count == 0) {
return false;
}

m_expire = m_expire + m_interval;
return true;
}
} // namespace Sky