2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
2019-06-19 01:48:34 +02:00
|
|
|
#include "td/utils/Variant.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
#define BEGIN_STORE_FLAGS() \
|
2018-11-09 13:38:41 +01:00
|
|
|
do { \
|
|
|
|
uint32 flags_store = 0; \
|
2018-12-31 20:04:05 +01:00
|
|
|
uint32 bit_offset_store = 0
|
|
|
|
|
|
|
|
#define STORE_FLAG(flag) \
|
|
|
|
flags_store |= (flag) << bit_offset_store; \
|
|
|
|
bit_offset_store++
|
|
|
|
|
2018-11-09 13:38:41 +01:00
|
|
|
#define END_STORE_FLAGS() \
|
|
|
|
CHECK(bit_offset_store < 31); \
|
|
|
|
td::store(flags_store, storer); \
|
|
|
|
} \
|
|
|
|
while (false)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-11-09 13:38:41 +01:00
|
|
|
#define BEGIN_PARSE_FLAGS() \
|
|
|
|
do { \
|
|
|
|
uint32 flags_parse; \
|
|
|
|
uint32 bit_offset_parse = 0; \
|
2018-12-31 20:04:05 +01:00
|
|
|
td::parse(flags_parse, parser)
|
|
|
|
|
|
|
|
#define PARSE_FLAG(flag) \
|
|
|
|
flag = ((flags_parse >> bit_offset_parse) & 1) != 0; \
|
|
|
|
bit_offset_parse++
|
|
|
|
|
2018-11-09 13:38:41 +01:00
|
|
|
#define END_PARSE_FLAGS() \
|
|
|
|
CHECK(bit_offset_parse < 31); \
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK((flags_parse & ~((1 << bit_offset_parse) - 1)) == 0) \
|
2018-11-09 13:38:41 +01:00
|
|
|
<< flags_parse << " " << bit_offset_parse << " " << parser.version(); \
|
|
|
|
} \
|
|
|
|
while (false)
|
|
|
|
|
2019-02-12 17:17:20 +01:00
|
|
|
#define END_PARSE_FLAGS_GENERIC() \
|
|
|
|
CHECK(bit_offset_parse < 31); \
|
|
|
|
LOG_CHECK((flags_parse & ~((1 << bit_offset_parse) - 1)) == 0) << flags_parse << " " << bit_offset_parse; \
|
|
|
|
} \
|
2018-11-09 13:38:41 +01:00
|
|
|
while (false)
|
2018-04-16 17:02:42 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
2019-01-19 23:59:37 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(bool x, StorerT &storer) {
|
|
|
|
storer.store_binary(static_cast<int32>(x));
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(bool &x, ParserT &parser) {
|
|
|
|
x = parser.fetch_int() != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(int32 x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(int32 &x, ParserT &parser) {
|
|
|
|
x = parser.fetch_int();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(uint32 x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(uint32 &x, ParserT &parser) {
|
|
|
|
x = static_cast<uint32>(parser.fetch_int());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(int64 x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(int64 &x, ParserT &parser) {
|
|
|
|
x = parser.fetch_long();
|
|
|
|
}
|
|
|
|
template <class StorerT>
|
|
|
|
void store(uint64 x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(uint64 &x, ParserT &parser) {
|
|
|
|
x = static_cast<uint64>(parser.fetch_long());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(double x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(double &x, ParserT &parser) {
|
|
|
|
x = parser.fetch_double();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(Slice x, StorerT &storer) {
|
|
|
|
storer.store_string(x);
|
|
|
|
}
|
|
|
|
template <class StorerT>
|
|
|
|
void store(const string &x, StorerT &storer) {
|
|
|
|
storer.store_string(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(string &x, ParserT &parser) {
|
|
|
|
x = parser.template fetch_string<string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class StorerT>
|
|
|
|
void store(const vector<T> &vec, StorerT &storer) {
|
|
|
|
storer.store_binary(narrow_cast<int32>(vec.size()));
|
|
|
|
for (auto &val : vec) {
|
|
|
|
store(val, storer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template <class T, class ParserT>
|
|
|
|
void parse(vector<T> &vec, ParserT &parser) {
|
|
|
|
uint32 size = parser.fetch_int();
|
|
|
|
if (parser.get_left_len() < size) {
|
|
|
|
parser.set_error("Wrong vector length");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vec = vector<T>(size);
|
|
|
|
for (auto &val : vec) {
|
|
|
|
parse(val, parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Key, class Hash, class KeyEqual, class Allocator, class StorerT>
|
|
|
|
void store(const std::unordered_set<Key, Hash, KeyEqual, Allocator> &s, StorerT &storer) {
|
|
|
|
storer.store_binary(narrow_cast<int32>(s.size()));
|
|
|
|
for (auto &val : s) {
|
|
|
|
store(val, storer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template <class Key, class Hash, class KeyEqual, class Allocator, class ParserT>
|
|
|
|
void parse(std::unordered_set<Key, Hash, KeyEqual, Allocator> &s, ParserT &parser) {
|
|
|
|
uint32 size = parser.fetch_int();
|
|
|
|
if (parser.get_left_len() < size) {
|
|
|
|
parser.set_error("Wrong set length");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s.clear();
|
|
|
|
for (uint32 i = 0; i < size; i++) {
|
2018-11-11 14:35:51 +01:00
|
|
|
Key val;
|
2018-12-31 20:04:05 +01:00
|
|
|
parse(val, parser);
|
|
|
|
s.insert(std::move(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class StorerT>
|
|
|
|
std::enable_if_t<std::is_enum<T>::value> store(const T &val, StorerT &storer) {
|
|
|
|
store(static_cast<int32>(val), storer);
|
|
|
|
}
|
|
|
|
template <class T, class ParserT>
|
|
|
|
std::enable_if_t<std::is_enum<T>::value> parse(T &val, ParserT &parser) {
|
|
|
|
int32 result;
|
|
|
|
parse(result, parser);
|
|
|
|
val = static_cast<T>(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class StorerT>
|
|
|
|
std::enable_if_t<!std::is_enum<T>::value> store(const T &val, StorerT &storer) {
|
|
|
|
val.store(storer);
|
|
|
|
}
|
|
|
|
template <class T, class ParserT>
|
|
|
|
std::enable_if_t<!std::is_enum<T>::value> parse(T &val, ParserT &parser) {
|
|
|
|
val.parse(parser);
|
|
|
|
}
|
|
|
|
|
2019-06-19 01:48:34 +02:00
|
|
|
template <class... Types, class StorerT>
|
|
|
|
void store(const Variant<Types...> &variant, StorerT &storer) {
|
|
|
|
store(variant.get_offset(), storer);
|
|
|
|
variant.visit([&storer](auto &&value) {
|
|
|
|
using td::store;
|
|
|
|
store(value, storer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
template <class... Types, class ParserT>
|
|
|
|
void parse(Variant<Types...> &variant, ParserT &parser) {
|
|
|
|
auto type_offset = parser.fetch_int();
|
|
|
|
if (type_offset < 0 || type_offset >= static_cast<int32>(sizeof...(Types))) {
|
|
|
|
return parser.set_error("Invalid type");
|
|
|
|
}
|
|
|
|
variant.for_each([type_offset, &parser, &variant](int offset, auto *ptr) {
|
|
|
|
using T = std::decay_t<decltype(*ptr)>;
|
|
|
|
if (offset == type_offset) {
|
|
|
|
variant = T();
|
2019-06-20 18:01:18 +02:00
|
|
|
parse(variant.template get<T>(), parser);
|
2019-06-19 01:48:34 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
string serialize(const T &object) {
|
|
|
|
TlStorerCalcLength calc_length;
|
|
|
|
store(object, calc_length);
|
|
|
|
size_t length = calc_length.get_length();
|
|
|
|
|
|
|
|
string key(length, '\0');
|
|
|
|
if (!is_aligned_pointer<4>(key.data())) {
|
|
|
|
auto ptr = StackAllocator::alloc(length);
|
|
|
|
MutableSlice data = ptr.as_slice();
|
2018-06-25 01:58:43 +02:00
|
|
|
TlStorerUnsafe storer(data.ubegin());
|
2018-12-31 20:04:05 +01:00
|
|
|
store(object, storer);
|
2018-07-06 22:33:11 +02:00
|
|
|
CHECK(storer.get_buf() == data.uend());
|
2018-12-31 20:04:05 +01:00
|
|
|
key.assign(data.begin(), data.size());
|
|
|
|
} else {
|
|
|
|
MutableSlice data = key;
|
2018-06-25 01:58:43 +02:00
|
|
|
TlStorerUnsafe storer(data.ubegin());
|
2018-12-31 20:04:05 +01:00
|
|
|
store(object, storer);
|
2018-07-06 22:33:11 +02:00
|
|
|
CHECK(storer.get_buf() == data.uend());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
TD_WARN_UNUSED_RESULT Status unserialize(T &object, Slice data) {
|
|
|
|
TlParser parser(data);
|
|
|
|
parse(object, parser);
|
|
|
|
parser.fetch_end();
|
|
|
|
return parser.get_status();
|
|
|
|
}
|
2019-01-19 23:59:37 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|