// // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // 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 #include "td/utils/unique_ptr.h" #include #include namespace td { // copyable by value td::unique_ptr template class unique_value_ptr final { public: unique_value_ptr() noexcept = default; unique_value_ptr(const unique_value_ptr &other) { if (other != nullptr) { ptr_ = make_unique(*other); } } unique_value_ptr &operator=(const unique_value_ptr &other) { if (other == nullptr) { ptr_ = nullptr; } else { ptr_ = make_unique(*other); } return *this; } unique_value_ptr(unique_value_ptr &&other) noexcept = default; unique_value_ptr &operator=(unique_value_ptr &&other) = default; unique_value_ptr(std::nullptr_t) noexcept { } unique_value_ptr(unique_ptr &&ptr) noexcept : ptr_(std::move(ptr)) { } T *get() noexcept { return ptr_.get(); } const T *get() const noexcept { return ptr_.get(); } T *operator->() noexcept { return ptr_.get(); } const T *operator->() const noexcept { return ptr_.get(); } T &operator*() noexcept { return *ptr_; } const T &operator*() const noexcept { return *ptr_; } explicit operator bool() const noexcept { return ptr_ != nullptr; } private: unique_ptr ptr_; }; template bool operator==(const unique_value_ptr &p, std::nullptr_t) { return !p; } template bool operator!=(const unique_value_ptr &p, std::nullptr_t) { return static_cast(p); } template bool operator==(const unique_value_ptr &lhs, const unique_value_ptr &rhs) { if (lhs == nullptr) { return rhs == nullptr; } return rhs != nullptr && *lhs == *rhs; } template bool operator!=(const unique_value_ptr &lhs, const unique_value_ptr &rhs) { return !(lhs == rhs); } template unique_value_ptr make_unique_value(Args &&...args) { return unique_value_ptr(make_unique(std::forward(args)...)); } } // namespace td