aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/tl/TlObject.h
blob: 51d7a3a1ed803ad4304817a2abaee7092212f45c (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// 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
 */

#include <cstddef>
#include <cstdint>
#include <string>
#include <type_traits>
#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;
};

/// @cond UNDOCUMENTED
namespace tl {

template <class T>
class unique_ptr {
 public:
  using pointer = T *;
  using element_type = T;

  unique_ptr() noexcept = default;
  unique_ptr(const unique_ptr &) = delete;
  unique_ptr &operator=(const unique_ptr &) = delete;
  unique_ptr(unique_ptr &&other) noexcept : ptr_(other.release()) {
  }
  unique_ptr &operator=(unique_ptr &&other) noexcept {
    reset(other.release());
    return *this;
  }
  ~unique_ptr() {
    reset();
  }

  unique_ptr(std::nullptr_t) noexcept {
  }
  explicit unique_ptr(T *ptr) noexcept : ptr_(ptr) {
  }
  template <class S, class = typename std::enable_if<std::is_base_of<T, S>::value>::type>
  unique_ptr(unique_ptr<S> &&other) noexcept : ptr_(static_cast<S *>(other.release())) {
  }
  template <class S, class = typename std::enable_if<std::is_base_of<T, S>::value>::type>
  unique_ptr &operator=(unique_ptr<S> &&other) noexcept {
    reset(static_cast<T *>(other.release()));
    return *this;
  }
  void reset(T *new_ptr = nullptr) noexcept {
    static_assert(sizeof(T) > 0, "Can't destroy unique_ptr with incomplete type");
    delete ptr_;
    ptr_ = new_ptr;
  }
  T *release() noexcept {
    auto res = ptr_;
    ptr_ = nullptr;
    return res;
  }
  T *get() noexcept {
    return ptr_;
  }
  const T *get() const noexcept {
    return ptr_;
  }
  T *operator->() noexcept {
    return ptr_;
  }
  const T *operator->() const noexcept {
    return ptr_;
  }
  T &operator*() noexcept {
    return *ptr_;
  }
  const T &operator*() const noexcept {
    return *ptr_;
  }
  explicit operator bool() const noexcept {
    return ptr_ != nullptr;
  }

 private:
  T *ptr_{nullptr};
};

template <class T>
bool operator==(std::nullptr_t, const unique_ptr<T> &p) {
  return !p;
}
template <class T>
bool operator==(const unique_ptr<T> &p, std::nullptr_t) {
  return !p;
}
template <class T>
bool operator!=(std::nullptr_t, const unique_ptr<T> &p) {
  return static_cast<bool>(p);
}
template <class T>
bool operator!=(const unique_ptr<T> &p, std::nullptr_t) {
  return static_cast<bool>(p);
}

}  // namespace tl
/// @endcond

/**
 * A smart wrapper to store a pointer to a TL-object.
 */
template <class Type>
using tl_object_ptr = tl::unique_ptr<Type>;

/**
 * A function to create a dynamically allocated TL-object. Can be treated as an analogue of std::make_unique.
 * Usage example:
 * \code
 * auto get_me_request = td::make_tl_object<td::td_api::getMe>();
 * auto message_text = td::make_tl_object<td::td_api::formattedText>("Hello, world!!!",
 *                     td::td_api::array<td::tl_object_ptr<td::td_api::textEntity>>());
 * auto send_message_request = td::make_tl_object<td::td_api::sendMessage>(chat_id, nullptr, nullptr, nullptr, nullptr,
 *      td::make_tl_object<td::td_api::inputMessageText>(std::move(message_text), nullptr, true));
 * \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>
tl_object_ptr<Type> make_tl_object(Args &&...args) {
  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 TL-object to move to.
 * \tparam FromT Type of 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