-
Notifications
You must be signed in to change notification settings - Fork 9
/
Message.h
45 lines (38 loc) · 988 Bytes
/
Message.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
#ifndef MESSAGE_H
#define MESSAGE_H
#include <memory>
#include <vector>
#include <string>
class MsgItemBase{};
template<typename T>
class MsgItem : public MsgItemBase
{
public:
MsgItem(const T& val) : item(val) {}
T item;
};
class Message
{
public:
Message();
Message(const std::string& id) : ID(id) {}
~Message() = default;
template<typename... T>
Message(const std::string& id,T... items) : ID(id) { add(items...); }
template<typename T,typename... Tail>
void add(const T& a,Tail... tail)
{
data.push_back(std::unique_ptr<MsgItemBase>(new MsgItem<T>(a)));
add(tail...);
}
void add(){};
template<typename T>
const T& getItem(std::size_t n) const
{
return static_cast<MsgItem<T>*>(data[n].get())->item;
}
std::string ID;
private:
std::vector<std::unique_ptr<MsgItemBase>> data;
};
#endif // MESSAGE_H