diff --git a/td/tl/TlObject.h b/td/tl/TlObject.h index 0c0789b2d..bc8d48a30 100644 --- a/td/tl/TlObject.h +++ b/td/tl/TlObject.h @@ -11,9 +11,11 @@ * Contains the declarations of a base class for all TL-objects and some helper methods */ +#include #include #include #include +#include #include namespace td { @@ -90,46 +92,54 @@ 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() 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; + } ~unique_ptr() { reset(); } - unique_ptr(std::nullptr_t) { + + unique_ptr(std::nullptr_t) noexcept { } - explicit unique_ptr(T *ptr) : ptr_(ptr) { + explicit unique_ptr(T *ptr) noexcept : ptr_(ptr) { } template ::value>> - unique_ptr(unique_ptr &&other) : ptr_(static_cast(other.release())) { + unique_ptr(unique_ptr &&other) noexcept : ptr_(static_cast(other.release())) { } template ::value>> - unique_ptr &operator=(unique_ptr &&other) { - auto other_ptr = static_cast(other.release()); - reset(); - ptr_ = other_ptr; + unique_ptr &operator=(unique_ptr &&other) noexcept { + reset(static_cast(other.release())); return *this; } - void reset() { + void reset(T *new_ptr = nullptr) noexcept { delete ptr_; - ptr_ = nullptr; + ptr_ = new_ptr; } - T *release() { + T *release() noexcept { auto res = ptr_; ptr_ = nullptr; return res; } - T *get() const { + T *get() const noexcept { return ptr_; } - T *operator->() const { + T *operator->() const noexcept { return ptr_; } - T &operator*() const { + T &operator*() const noexcept { return *ptr_; } - operator bool() const { + explicit operator bool() const noexcept { return ptr_ != nullptr; }