2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Contains the declarations of a base class for all TL-objects and some helper methods
|
|
|
|
*/
|
|
|
|
|
2018-09-21 19:13:32 +02:00
|
|
|
#include <cstddef>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
2018-09-21 19:13:32 +02:00
|
|
|
#include <type_traits>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class TlStorerCalcLength;
|
|
|
|
|
|
|
|
class TlStorerUnsafe;
|
|
|
|
|
|
|
|
class TlStorerToString;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is a base class for all TDLib TL-objects.
|
|
|
|
*/
|
|
|
|
class TlObject {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Returns an identifier, uniquely determining the TL-type of the object.
|
|
|
|
*/
|
|
|
|
virtual std::int32_t get_id() const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends the object to the storer serializing object, a buffer of fixed length.
|
|
|
|
* \param[in] s Storer to which the object will be appended.
|
|
|
|
*/
|
|
|
|
virtual void store(TlStorerUnsafe &s) const {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends the object to the storer, calculating the TL-length of the serialized object.
|
|
|
|
* \param[in] s Storer to which the object will be appended.
|
|
|
|
*/
|
|
|
|
virtual void store(TlStorerCalcLength &s) const {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for the to_string method. Appends a string representation of the object to the storer.
|
|
|
|
* \param[in] s Storer to which the object string representation will be appended.
|
|
|
|
* \param[in] field_name Object field_name if applicable.
|
|
|
|
*/
|
|
|
|
virtual void store(TlStorerToString &s, const char *field_name) const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
|
|
|
TlObject() = default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deleted copy constructor.
|
|
|
|
*/
|
|
|
|
TlObject(const TlObject &) = delete;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deleted copy assignment operator.
|
|
|
|
*/
|
|
|
|
TlObject &operator=(const TlObject &) = delete;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default move constructor.
|
|
|
|
*/
|
|
|
|
TlObject(TlObject &&) = default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default move assignment operator.
|
|
|
|
*/
|
|
|
|
TlObject &operator=(TlObject &&) = default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Virtual destructor.
|
|
|
|
*/
|
|
|
|
virtual ~TlObject() = default;
|
|
|
|
};
|
|
|
|
|
2020-11-15 15:38:10 +01:00
|
|
|
/// @cond UNDOCUMENTED
|
2018-09-21 17:34:18 +02:00
|
|
|
namespace tl {
|
2018-09-21 19:13:32 +02:00
|
|
|
|
2018-09-21 17:34:18 +02:00
|
|
|
template <class T>
|
|
|
|
class unique_ptr {
|
|
|
|
public:
|
2018-09-22 13:45:52 +02:00
|
|
|
using pointer = T *;
|
|
|
|
using element_type = T;
|
|
|
|
|
2018-09-21 19:13:32 +02:00
|
|
|
unique_ptr() noexcept = default;
|
|
|
|
unique_ptr(const unique_ptr &other) = delete;
|
|
|
|
unique_ptr &operator=(const unique_ptr &other) = delete;
|
|
|
|
unique_ptr(unique_ptr &&other) noexcept : ptr_(other.release()) {
|
|
|
|
}
|
|
|
|
unique_ptr &operator=(unique_ptr &&other) noexcept {
|
|
|
|
reset(other.release());
|
|
|
|
return *this;
|
|
|
|
}
|
2018-09-21 17:34:18 +02:00
|
|
|
~unique_ptr() {
|
|
|
|
reset();
|
|
|
|
}
|
2018-09-21 19:13:32 +02:00
|
|
|
|
|
|
|
unique_ptr(std::nullptr_t) noexcept {
|
2018-09-21 17:34:18 +02:00
|
|
|
}
|
2018-09-21 19:13:32 +02:00
|
|
|
explicit unique_ptr(T *ptr) noexcept : ptr_(ptr) {
|
2018-09-21 17:34:18 +02:00
|
|
|
}
|
2020-10-13 12:51:13 +02:00
|
|
|
template <class S, class = typename std::enable_if<std::is_base_of<T, S>::value>::type>
|
2018-09-21 19:13:32 +02:00
|
|
|
unique_ptr(unique_ptr<S> &&other) noexcept : ptr_(static_cast<S *>(other.release())) {
|
2018-09-21 17:34:18 +02:00
|
|
|
}
|
2020-10-13 12:51:13 +02:00
|
|
|
template <class S, class = typename std::enable_if<std::is_base_of<T, S>::value>::type>
|
2018-09-21 19:13:32 +02:00
|
|
|
unique_ptr &operator=(unique_ptr<S> &&other) noexcept {
|
|
|
|
reset(static_cast<T *>(other.release()));
|
2018-09-21 17:34:18 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2018-09-21 19:13:32 +02:00
|
|
|
void reset(T *new_ptr = nullptr) noexcept {
|
2020-09-14 17:11:34 +02:00
|
|
|
static_assert(sizeof(T) > 0, "Can't destroy unique_ptr with incomplete type");
|
2018-09-21 17:34:18 +02:00
|
|
|
delete ptr_;
|
2018-09-21 19:13:32 +02:00
|
|
|
ptr_ = new_ptr;
|
2018-09-21 17:34:18 +02:00
|
|
|
}
|
2018-09-21 19:13:32 +02:00
|
|
|
T *release() noexcept {
|
2018-09-21 17:34:18 +02:00
|
|
|
auto res = ptr_;
|
|
|
|
ptr_ = nullptr;
|
|
|
|
return res;
|
|
|
|
}
|
2018-09-21 19:52:22 +02:00
|
|
|
T *get() noexcept {
|
2018-09-21 17:34:18 +02:00
|
|
|
return ptr_;
|
|
|
|
}
|
2018-09-21 19:52:22 +02:00
|
|
|
const T *get() const noexcept {
|
2018-09-21 17:34:18 +02:00
|
|
|
return ptr_;
|
|
|
|
}
|
2018-09-21 19:52:22 +02:00
|
|
|
T *operator->() noexcept {
|
|
|
|
return ptr_;
|
|
|
|
}
|
|
|
|
const T *operator->() const noexcept {
|
|
|
|
return ptr_;
|
|
|
|
}
|
|
|
|
T &operator*() noexcept {
|
|
|
|
return *ptr_;
|
|
|
|
}
|
|
|
|
const T &operator*() const noexcept {
|
2018-09-21 17:34:18 +02:00
|
|
|
return *ptr_;
|
|
|
|
}
|
2018-09-21 19:13:32 +02:00
|
|
|
explicit operator bool() const noexcept {
|
2018-09-21 17:34:18 +02:00
|
|
|
return ptr_ != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *ptr_{nullptr};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool operator==(std::nullptr_t, const unique_ptr<T> &p) {
|
2018-09-25 05:26:36 +02:00
|
|
|
return !p;
|
2018-09-21 17:34:18 +02:00
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
bool operator==(const unique_ptr<T> &p, std::nullptr_t) {
|
2018-09-25 05:26:36 +02:00
|
|
|
return !p;
|
2018-09-21 17:34:18 +02:00
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
bool operator!=(std::nullptr_t, const unique_ptr<T> &p) {
|
2018-09-25 05:26:36 +02:00
|
|
|
return static_cast<bool>(p);
|
2018-09-21 17:34:18 +02:00
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
bool operator!=(const unique_ptr<T> &p, std::nullptr_t) {
|
2018-09-25 05:26:36 +02:00
|
|
|
return static_cast<bool>(p);
|
2018-09-21 17:34:18 +02:00
|
|
|
}
|
2018-09-27 18:51:45 +02:00
|
|
|
|
2018-09-21 17:34:18 +02:00
|
|
|
} // namespace tl
|
2020-11-15 15:38:10 +01:00
|
|
|
/// @endcond
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A smart wrapper to store a pointer to a TL-object.
|
|
|
|
*/
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class Type>
|
2018-09-21 17:34:18 +02:00
|
|
|
using tl_object_ptr = tl::unique_ptr<Type>;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A function to create a dynamically allocated TL-object. Can be treated as an analogue of std::make_unique.
|
|
|
|
* Usage example:
|
|
|
|
* \code
|
|
|
|
* auto get_authorization_state_request = td::make_tl_object<td::td_api::getAuthorizationState>();
|
2018-01-30 18:06:54 +01:00
|
|
|
* auto message_text = td::make_tl_object<td::td_api::formattedText>("Hello, world!!!",
|
2020-11-15 17:57:03 +01:00
|
|
|
* td::td_api::array<td::tl_object_ptr<td::td_api::textEntity>>());
|
2020-09-23 00:52:16 +02:00
|
|
|
* auto send_message_request = td::make_tl_object<td::td_api::sendMessage>(chat_id, 0, 0, nullptr, nullptr,
|
2018-01-30 18:06:54 +01:00
|
|
|
* td::make_tl_object<td::td_api::inputMessageText>(std::move(message_text), false, true));
|
2018-12-31 20:04:05 +01:00
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* \tparam Type Type of the TL-object to construct.
|
|
|
|
* \param[in] args Arguments to pass to the object constructor.
|
|
|
|
* \return Wrapped pointer to the created TL-object.
|
|
|
|
*/
|
|
|
|
template <class Type, class... Args>
|
2021-12-09 22:27:13 +01:00
|
|
|
tl_object_ptr<Type> make_tl_object(Args &&...args) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return tl_object_ptr<Type>(new Type(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A function to downcast a wrapped pointer to a TL-object to a pointer to its subclass.
|
|
|
|
* Casting an object to an incorrect type will lead to undefined behaviour.
|
|
|
|
* Examples of usage:
|
|
|
|
* \code
|
|
|
|
* td::tl_object_ptr<td::td_api::callState> call_state = ...;
|
|
|
|
* switch (call_state->get_id()) {
|
|
|
|
* case td::td_api::callStatePending::ID: {
|
|
|
|
* auto state = td::move_tl_object_as<td::td_api::callStatePending>(call_state);
|
|
|
|
* // use state
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* case td::td_api::callStateExchangingKeys::ID: {
|
|
|
|
* // no additional fields, so cast isn't needed
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* case td::td_api::callStateReady::ID: {
|
|
|
|
* auto state = td::move_tl_object_as<td::td_api::callStateReady>(call_state);
|
|
|
|
* // use state
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* case td::td_api::callStateHangingUp::ID: {
|
|
|
|
* // no additional fields, so cast isn't needed
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* case td::td_api::callStateDiscarded::ID: {
|
|
|
|
* auto state = td::move_tl_object_as<td::td_api::callStateDiscarded>(call_state);
|
|
|
|
* // use state
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* case td::td_api::callStateError::ID: {
|
|
|
|
* auto state = td::move_tl_object_as<td::td_api::callStateError>(call_state);
|
|
|
|
* // use state
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* default:
|
|
|
|
* assert(false);
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* \tparam ToT Type of a TL-object to move to.
|
|
|
|
* \tparam FromT Type of a TL-object to move from, this is auto-deduced.
|
|
|
|
* \param[in] from Wrapped pointer to a TL-object.
|
|
|
|
*/
|
|
|
|
template <class ToT, class FromT>
|
|
|
|
tl_object_ptr<ToT> move_tl_object_as(tl_object_ptr<FromT> &from) {
|
|
|
|
return tl_object_ptr<ToT>(static_cast<ToT *>(from.release()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \overload
|
|
|
|
*/
|
|
|
|
template <class ToT, class FromT>
|
|
|
|
tl_object_ptr<ToT> move_tl_object_as(tl_object_ptr<FromT> &&from) {
|
|
|
|
return tl_object_ptr<ToT>(static_cast<ToT *>(from.release()));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|