-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMsg.h
64 lines (54 loc) · 1.1 KB
/
Msg.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include <memory>
class BaseMsg;
using BaseMsgPtr = std::shared_ptr<BaseMsg>;
class BaseMsg : public std::enable_shared_from_this<BaseMsg>
{
public:
BaseMsg(int _priority = 0) : priority(_priority), timestamp(0) {}
virtual ~BaseMsg() {}
BaseMsgPtr shared_from_base()
{
return shared_from_this();
}
bool operator<(const BaseMsg &other)
{
if (priority == other.priority)
return timestamp > other.timestamp;
return priority < other.priority;
}
void settimestamp(int64_t _timestamp)
{
timestamp = _timestamp;
}
protected:
int priority;
int64_t timestamp;
};
struct BaseMsgPtrCompareLess
{
bool operator()(BaseMsgPtr a, BaseMsgPtr b)
{
return (*a) < (*b);
}
};
template <typename MSG_CONTENT_TYPE>
class Msg : public BaseMsg
{
public:
explicit Msg(const MSG_CONTENT_TYPE &content_, int _priority = 0) : BaseMsg(_priority),
content(content_)
{
}
Msg(const Msg &msg) : content(msg.content)
{
}
~Msg()
{
}
MSG_CONTENT_TYPE getContent() { return content; }
protected:
MSG_CONTENT_TYPE content;
};
template <typename T>
using MsgPtr = std::shared_ptr<Msg<T>>;