TlObject.h
Go to the documentation of this file.
1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #pragma once
8 
14 #include <cstddef>
15 #include <cstdint>
16 #include <string>
17 #include <type_traits>
18 #include <utility>
19 
20 namespace td {
21 
22 class TlStorerCalcLength;
23 
24 class TlStorerUnsafe;
25 
26 class TlStorerToString;
27 
31 class TlObject {
32  public:
36  virtual std::int32_t get_id() const = 0;
37 
42  virtual void store(TlStorerUnsafe &s) const {
43  }
44 
49  virtual void store(TlStorerCalcLength &s) const {
50  }
51 
57  virtual void store(TlStorerToString &s, const char *field_name) const = 0;
58 
62  TlObject() = default;
63 
67  TlObject(const TlObject &) = delete;
68 
72  TlObject &operator=(const TlObject &) = delete;
73 
77  TlObject(TlObject &&) = default;
78 
82  TlObject &operator=(TlObject &&) = default;
83 
87  virtual ~TlObject() = default;
88 };
89 
91 namespace tl {
92 
93 template <class T>
94 class unique_ptr {
95  public:
96  using pointer = T *;
97  using element_type = T;
98 
99  unique_ptr() noexcept = default;
100  unique_ptr(const unique_ptr &other) = delete;
101  unique_ptr &operator=(const unique_ptr &other) = delete;
102  unique_ptr(unique_ptr &&other) noexcept : ptr_(other.release()) {
103  }
104  unique_ptr &operator=(unique_ptr &&other) noexcept {
105  reset(other.release());
106  return *this;
107  }
108  ~unique_ptr() {
109  reset();
110  }
111 
112  unique_ptr(std::nullptr_t) noexcept {
113  }
114  explicit unique_ptr(T *ptr) noexcept : ptr_(ptr) {
115  }
116  template <class S, class = typename std::enable_if<std::is_base_of<T, S>::value>::type>
117  unique_ptr(unique_ptr<S> &&other) noexcept : ptr_(static_cast<S *>(other.release())) {
118  }
119  template <class S, class = typename std::enable_if<std::is_base_of<T, S>::value>::type>
120  unique_ptr &operator=(unique_ptr<S> &&other) noexcept {
121  reset(static_cast<T *>(other.release()));
122  return *this;
123  }
124  void reset(T *new_ptr = nullptr) noexcept {
125  static_assert(sizeof(T) > 0, "Can't destroy unique_ptr with incomplete type");
126  delete ptr_;
127  ptr_ = new_ptr;
128  }
129  T *release() noexcept {
130  auto res = ptr_;
131  ptr_ = nullptr;
132  return res;
133  }
134  T *get() noexcept {
135  return ptr_;
136  }
137  const T *get() const noexcept {
138  return ptr_;
139  }
140  T *operator->() noexcept {
141  return ptr_;
142  }
143  const T *operator->() const noexcept {
144  return ptr_;
145  }
146  T &operator*() noexcept {
147  return *ptr_;
148  }
149  const T &operator*() const noexcept {
150  return *ptr_;
151  }
152  explicit operator bool() const noexcept {
153  return ptr_ != nullptr;
154  }
155 
156  private:
157  T *ptr_{nullptr};
158 };
159 
160 template <class T>
161 bool operator==(std::nullptr_t, const unique_ptr<T> &p) {
162  return !p;
163 }
164 template <class T>
165 bool operator==(const unique_ptr<T> &p, std::nullptr_t) {
166  return !p;
167 }
168 template <class T>
169 bool operator!=(std::nullptr_t, const unique_ptr<T> &p) {
170  return static_cast<bool>(p);
171 }
172 template <class T>
173 bool operator!=(const unique_ptr<T> &p, std::nullptr_t) {
174  return static_cast<bool>(p);
175 }
176 
177 } // namespace tl
179 
183 template <class Type>
184 using tl_object_ptr = tl::unique_ptr<Type>;
185 
201 template <class Type, class... Args>
203  return tl_object_ptr<Type>(new Type(std::forward<Args>(args)...));
204 }
205 
250 template <class ToT, class FromT>
252  return tl_object_ptr<ToT>(static_cast<ToT *>(from.release()));
253 }
254 
258 template <class ToT, class FromT>
260  return tl_object_ptr<ToT>(static_cast<ToT *>(from.release()));
261 }
262 
263 } // namespace td
virtual void store(TlStorerCalcLength &s) const
Definition: TlObject.h:49
tl_object_ptr< Type > make_tl_object(Args &&...args)
Definition: TlObject.h:202
TlObject & operator=(const TlObject &)=delete
tl_object_ptr< ToT > move_tl_object_as(tl_object_ptr< FromT > &from)
Definition: TlObject.h:251
virtual std::int32_t get_id() const =0
tl::unique_ptr< Type > tl_object_ptr
Definition: TlObject.h:184
Definition: TlObject.h:31
virtual void store(TlStorerUnsafe &s) const
Definition: TlObject.h:42
TlObject()=default
virtual ~TlObject()=default