-
Notifications
You must be signed in to change notification settings - Fork 0
/
tl_object_parse.h
147 lines (129 loc) · 3.23 KB
/
tl_object_parse.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
145
146
147
//
// Copyright Aliaksei Levin ([email protected]), Arseny Smirnov ([email protected]) 2014-2023
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/tl/TlObject.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/UInt.h"
#include <cstdint>
#include <string>
#include <vector>
namespace td {
template <class Func, std::int32_t constructor_id>
class TlFetchBoxed {
public:
template <class ParserT>
static auto parse(ParserT &parser) -> decltype(Func::parse(parser)) {
auto parsed_constructor_id = parser.fetch_int();
if (parsed_constructor_id != constructor_id) {
parser.set_error(PSTRING() << "Wrong constructor " << parsed_constructor_id << " found instead of "
<< constructor_id);
return decltype(Func::parse(parser))();
}
return Func::parse(parser);
}
};
class TlFetchTrue {
public:
template <class ParserT>
static bool parse(ParserT &parser) {
return true;
}
};
class TlFetchBool {
public:
template <class ParserT>
static bool parse(ParserT &parser) {
constexpr std::int32_t ID_BOOL_FALSE = 0xbc799737;
constexpr std::int32_t ID_BOOL_TRUE = 0x997275b5;
std::int32_t c = parser.fetch_int();
if (c == ID_BOOL_TRUE) {
return true;
}
if (c != ID_BOOL_FALSE) {
parser.set_error("Bool expected");
}
return false;
}
};
class TlFetchInt {
public:
template <class ParserT>
static std::int32_t parse(ParserT &parser) {
return parser.fetch_int();
}
};
class TlFetchLong {
public:
template <class ParserT>
static std::int64_t parse(ParserT &parser) {
return parser.fetch_long();
}
};
class TlFetchDouble {
public:
template <class ParserT>
static double parse(ParserT &parser) {
return parser.fetch_double();
}
};
class TlFetchInt128 {
public:
template <class ParserT>
static UInt128 parse(ParserT &parser) {
return parser.template fetch_binary<UInt128>();
}
};
class TlFetchInt256 {
public:
template <class ParserT>
static UInt256 parse(ParserT &parser) {
return parser.template fetch_binary<UInt256>();
}
};
template <class T>
class TlFetchString {
public:
template <class ParserT>
static T parse(ParserT &parser) {
return parser.template fetch_string<T>();
}
};
template <class T>
class TlFetchBytes {
public:
template <class ParserT>
static T parse(ParserT &parser) {
return parser.template fetch_string<T>();
}
};
template <class Func>
class TlFetchVector {
public:
template <class ParserT>
static auto parse(ParserT &parser) -> std::vector<decltype(Func::parse(parser))> {
const std::uint32_t multiplicity = parser.fetch_int();
std::vector<decltype(Func::parse(parser))> v;
if (parser.get_left_len() < multiplicity) {
parser.set_error("Wrong vector length");
} else {
v.reserve(multiplicity);
for (std::uint32_t i = 0; i < multiplicity; i++) {
v.push_back(Func::parse(parser));
}
}
return v;
}
};
template <class T>
class TlFetchObject {
public:
template <class ParserT>
static tl_object_ptr<T> parse(ParserT &parser) {
return T::fetch(parser);
}
};
} // namespace td