diff --git a/td/tl/TlObject.h b/td/tl/TlObject.h index 4fcbf6bc..0c0789b2 100644 --- a/td/tl/TlObject.h +++ b/td/tl/TlObject.h @@ -89,8 +89,73 @@ class TlObject { /** * A smart wrapper to store a pointer to a TL-object. */ +namespace tl { +template +class unique_ptr { + public: + unique_ptr() = default; + ~unique_ptr() { + reset(); + } + unique_ptr(std::nullptr_t) { + } + explicit unique_ptr(T *ptr) : ptr_(ptr) { + } + template ::value>> + unique_ptr(unique_ptr &&other) : ptr_(static_cast(other.release())) { + } + template ::value>> + unique_ptr &operator=(unique_ptr &&other) { + auto other_ptr = static_cast(other.release()); + reset(); + ptr_ = other_ptr; + return *this; + } + void reset() { + delete ptr_; + ptr_ = nullptr; + } + T *release() { + auto res = ptr_; + ptr_ = nullptr; + return res; + } + T *get() const { + return ptr_; + } + T *operator->() const { + return ptr_; + } + T &operator*() const { + return *ptr_; + } + operator bool() const { + return ptr_ != nullptr; + } + + private: + T *ptr_{nullptr}; +}; + +template +bool operator==(std::nullptr_t, const unique_ptr &p) { + return !bool(p); +} +template +bool operator==(const unique_ptr &p, std::nullptr_t) { + return !bool(p); +} +template +bool operator!=(std::nullptr_t, const unique_ptr &p) { + return bool(p); +} +template +bool operator!=(const unique_ptr &p, std::nullptr_t) { + return bool(p); +} +} // namespace tl template -using tl_object_ptr = std::unique_ptr; +using tl_object_ptr = tl::unique_ptr; /** * A function to create a dynamically allocated TL-object. Can be treated as an analogue of std::make_unique.