tdlib-obf API
Reference documentation for the public tdlib-obf API, generated from TDLib schemas and public headers
Loading...
Searching...
No Matches
TlObject.h
Go to the documentation of this file.
1//
2// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
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
20namespace td {
21
22class TlStorerCalcLength;
23
24class TlStorerUnsafe;
25
26class TlStorerToString;
27
31class 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
91namespace tl {
92
93template <class T>
94class 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 &) = delete;
101 unique_ptr &operator=(const unique_ptr &) = 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
160template <class T>
161bool operator==(std::nullptr_t, const unique_ptr<T> &p) {
162 return !p;
163}
164template <class T>
165bool operator==(const unique_ptr<T> &p, std::nullptr_t) {
166 return !p;
167}
168template <class T>
169bool operator!=(std::nullptr_t, const unique_ptr<T> &p) {
170 return static_cast<bool>(p);
171}
172template <class T>
173bool operator!=(const unique_ptr<T> &p, std::nullptr_t) {
174 return static_cast<bool>(p);
175}
176
177} // namespace tl
179
183template <class Type>
184using tl_object_ptr = tl::unique_ptr<Type>;
185
201template <class Type, class... Args>
203 return tl_object_ptr<Type>(new Type(std::forward<Args>(args)...));
204}
205
250template <class ToT, class FromT>
252 return tl_object_ptr<ToT>(static_cast<ToT *>(from.release()));
253}
254
258template <class ToT, class FromT>
260 return tl_object_ptr<ToT>(static_cast<ToT *>(from.release()));
261}
262
263} // namespace td
tl_object_ptr< ToT > move_tl_object_as(tl_object_ptr< FromT > &from)
Definition TlObject.h:251
tl_object_ptr< Type > make_tl_object(Args &&...args)
Definition TlObject.h:202
tl::unique_ptr< Type > tl_object_ptr
Definition TlObject.h:184
virtual std::int32_t get_id() const =0
virtual void store(TlStorerUnsafe &s) const
Definition TlObject.h:42
TlObject(const TlObject &)=delete
TlObject & operator=(const TlObject &)=delete
TlObject & operator=(TlObject &&)=default
TlObject(TlObject &&)=default
virtual void store(TlStorerToString &s, const char *field_name) const =0
virtual ~TlObject()=default
TlObject()=default
virtual void store(TlStorerCalcLength &s) const
Definition TlObject.h:49