-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathidentifiers.h
144 lines (121 loc) · 6 KB
/
identifiers.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef _IDENTIFIERS_H
#define _IDENTIFIERS_H
#include <stdint.h>
#include <string>
#include <limits>
#include <stdlib.h>
#include <td/telegram/td_api.h>
template<typename IntType>
class Identifier {
protected:
using IdType = IntType;
explicit Identifier(IntType value) : m_value(value) {}
static IntType convertFromString(const char *s);
public:
bool valid() const { return (m_value != 0); }
IntType value() const { return m_value; }
private:
IntType m_value;
template<typename Object>
friend void setId(Object &object, Identifier<IntType> id);
};
template <typename IntType>
IntType Identifier<IntType>::convertFromString(const char *s)
{
long long x;
static_assert(sizeof(x) >= sizeof(IntType), "long long must hold identifier type");
errno = 0;
x = strtoll(s, NULL, 10);
if (errno || (x < std::numeric_limits<IntType>::min()) || (x > std::numeric_limits<IntType>::max()))
return 0;
return x;
}
template<typename Object, typename IntType>
void setId(Object &object, Identifier<IntType> id)
{
object.id_ = id.value;
}
#define DEFINE_ID_CLASS(classname, inttype) \
class classname: public Identifier<inttype> { \
private: \
explicit classname(IdType id) : Identifier(id) {} \
public: \
classname() : classname(classname::invalid) {} \
static classname fromString(const char *s) { return classname(convertFromString(s)); } \
bool operator==(const classname &other) const { return (value() == other.value()); } \
bool operator!=(const classname &other) const { return (value() != other.value()); } \
bool operator<(const classname &other) const { return (value() < other.value()); } \
static const classname invalid;
DEFINE_ID_CLASS(UserId, int64_t)
friend UserId getId(const td::td_api::user &user);
friend UserId getUserId(const td::td_api::chatTypePrivate &privType);
friend UserId getUserId(const td::td_api::chatMember &member);
friend UserId getUserId(const td::td_api::call &call);
friend UserId getSenderUserId(const td::td_api::message &message);
friend UserId getSenderUserId(const td::td_api::messageForwardOriginUser &forwardOrigin);
friend UserId getUserId(const td::td_api::secretChat &secretChat);
friend UserId getUserId(const td::td_api::updateUserStatus &update);
friend UserId getUserId(const td::td_api::updateUserChatAction &update);
friend UserId getUserId(const td::td_api::importedContacts &contacts, unsigned index);
friend UserId getUserId(const td::td_api::users &users, unsigned index);
};
DEFINE_ID_CLASS(ChatId, int64_t)
friend ChatId getId(const td::td_api::chat &chat);
friend ChatId getChatId(const td::td_api::updateChatPosition &update);
friend ChatId getChatId(const td::td_api::updateChatTitle &update);
friend ChatId getChatId(const td::td_api::messageForwardOriginChannel &forwardOrigin);
friend ChatId getChatId(const td::td_api::message &message);
friend ChatId getChatId(const td::td_api::updateUserChatAction &update);
friend ChatId getChatId(const td::td_api::updateChatLastMessage &update);
};
DEFINE_ID_CLASS(BasicGroupId, int64_t)
friend BasicGroupId getId(const td::td_api::basicGroup &group);
friend BasicGroupId getBasicGroupId(const td::td_api::updateBasicGroupFullInfo &update);
friend BasicGroupId getBasicGroupId(const td::td_api::chatTypeBasicGroup &update);
};
DEFINE_ID_CLASS(SupergroupId, int64_t)
friend SupergroupId getId(const td::td_api::supergroup &group);
friend SupergroupId getSupergroupId(const td::td_api::updateSupergroupFullInfo &update);
friend SupergroupId getSupergroupId(const td::td_api::chatTypeSupergroup &update);
};
DEFINE_ID_CLASS(SecretChatId, int32_t)
friend SecretChatId getId(const td::td_api::secretChat &secretChat);
friend SecretChatId getSecretChatId(const td::td_api::chatTypeSecret &update);
};
DEFINE_ID_CLASS(MessageId, int64_t)
friend MessageId getId(const td::td_api::message &message);
friend MessageId getReplyMessageId(const td::td_api::message &message);
};
#undef DEFINE_ID_CLASS
UserId getId(const td::td_api::user &user);
ChatId getId(const td::td_api::chat &chat);
BasicGroupId getId(const td::td_api::basicGroup &group);
SupergroupId getId(const td::td_api::supergroup &group);
SecretChatId getId(const td::td_api::secretChat &secretChat);
MessageId getId(const td::td_api::message &message);
UserId getUserId(const td::td_api::chatTypePrivate &privType);
UserId getUserId(const td::td_api::chatMember &member);
UserId getUserId(const td::td_api::call &call);
UserId getSenderUserId(const td::td_api::message &message);
UserId getSenderUserId(const td::td_api::messageForwardOriginUser &forwardOrigin);
UserId getUserId(const td::td_api::secretChat &secretChat);
UserId getUserId(const td::td_api::updateUserStatus &update);
UserId getUserId(const td::td_api::updateUserChatAction &update);
UserId getUserId(const td::td_api::importedContacts &contacts, unsigned index);
UserId getUserId(const td::td_api::users &users, unsigned index);
ChatId getChatId(const td::td_api::updateChatPosition &update);
ChatId getChatId(const td::td_api::updateChatTitle &update);
ChatId getChatId(const td::td_api::messageForwardOriginChannel &forwardOrigin);
ChatId getChatId(const td::td_api::message &message);
ChatId getChatId(const td::td_api::updateUserChatAction &update);
ChatId getChatId(const td::td_api::updateChatLastMessage &update);
BasicGroupId getBasicGroupId(const td::td_api::updateBasicGroupFullInfo &update);
BasicGroupId getBasicGroupId(const td::td_api::chatTypeBasicGroup &chatType);
SupergroupId getSupergroupId(const td::td_api::updateSupergroupFullInfo &update);
SupergroupId getSupergroupId(const td::td_api::chatTypeSupergroup &chatType);
SecretChatId getSecretChatId(const td::td_api::chatTypeSecret &update);
MessageId getReplyMessageId(const td::td_api::message &message);
namespace std {
static inline std::string to_string(UserId id) { return to_string(id.value()); }
}
#endif