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
|
|
|
|
|
2018-02-03 13:58:18 +01:00
|
|
|
#include "td/tl/TlObject.h"
|
|
|
|
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-02-03 13:58:18 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
template <class Func, std::int32_t constructor_id>
|
|
|
|
class TlFetchBoxed {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static auto parse(ParserT &parser) -> decltype(Func::parse(parser)) {
|
|
|
|
if (parser.fetch_int() != constructor_id) {
|
|
|
|
parser.set_error("Wrong constructor found");
|
|
|
|
return decltype(Func::parse(parser))();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-21 18:54:20 +01:00
|
|
|
return Func::parse(parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlFetchTrue {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static bool parse(ParserT &parser) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlFetchBool {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static bool parse(ParserT &parser) {
|
2018-12-31 20:04:05 +01:00
|
|
|
constexpr std::int32_t ID_BOOL_FALSE = 0xbc799737;
|
|
|
|
constexpr std::int32_t ID_BOOL_TRUE = 0x997275b5;
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
std::int32_t c = parser.fetch_int();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (c == ID_BOOL_TRUE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (c != ID_BOOL_FALSE) {
|
2019-02-21 18:54:20 +01:00
|
|
|
parser.set_error("Bool expected");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlFetchInt {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static std::int32_t parse(ParserT &parser) {
|
|
|
|
return parser.fetch_int();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlFetchLong {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static std::int64_t parse(ParserT &parser) {
|
|
|
|
return parser.fetch_long();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlFetchDouble {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static double parse(ParserT &parser) {
|
|
|
|
return parser.fetch_double();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlFetchInt128 {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static UInt128 parse(ParserT &parser) {
|
|
|
|
return parser.template fetch_binary<UInt128>();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlFetchInt256 {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static UInt256 parse(ParserT &parser) {
|
|
|
|
return parser.template fetch_binary<UInt256>();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class TlFetchString {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static T parse(ParserT &parser) {
|
|
|
|
return parser.template fetch_string<T>();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class TlFetchBytes {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static T parse(ParserT &parser) {
|
|
|
|
return parser.template fetch_string<T>();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Func>
|
|
|
|
class TlFetchVector {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
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");
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
v.reserve(multiplicity);
|
|
|
|
for (std::uint32_t i = 0; i < multiplicity; i++) {
|
2019-02-21 18:54:20 +01:00
|
|
|
v.push_back(Func::parse(parser));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class TlFetchObject {
|
|
|
|
public:
|
|
|
|
template <class ParserT>
|
2019-02-21 18:54:20 +01:00
|
|
|
static tl_object_ptr<T> parse(ParserT &parser) {
|
|
|
|
return T::fetch(parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|