.. _program_listing_file_include_opentelemetry_nostd_unique_ptr.h: Program Listing for File unique_ptr.h ===================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/opentelemetry/nostd/unique_ptr.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2020, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #ifdef HAVE_CPP_STDLIB # include "opentelemetry/std/unique_ptr.h" #else # include # include # include # include # include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { namespace detail { template struct unique_ptr_element_type { using type = T; }; template struct unique_ptr_element_type { using type = T; }; } // namespace detail template class unique_ptr { public: using element_type = typename detail::unique_ptr_element_type::type; using pointer = element_type *; unique_ptr() noexcept : ptr_{nullptr} {} unique_ptr(std::nullptr_t) noexcept : ptr_{nullptr} {} explicit unique_ptr(pointer ptr) noexcept : ptr_{ptr} {} unique_ptr(unique_ptr &&other) noexcept : ptr_{other.release()} {} template ::value>::type * = nullptr> unique_ptr(unique_ptr &&other) noexcept : ptr_{other.release()} {} template ::value>::type * = nullptr> unique_ptr(std::unique_ptr &&other) noexcept : ptr_{other.release()} {} ~unique_ptr() { reset(); } unique_ptr &operator=(unique_ptr &&other) noexcept { reset(other.release()); return *this; } unique_ptr &operator=(std::nullptr_t) noexcept { reset(); return *this; } template ::value>::type * = nullptr> unique_ptr &operator=(unique_ptr &&other) noexcept { reset(other.release()); return *this; } template ::value>::type * = nullptr> unique_ptr &operator=(std::unique_ptr &&other) noexcept { reset(other.release()); return *this; } operator std::unique_ptr() &&noexcept { return std::unique_ptr{release()}; } operator bool() const noexcept { return ptr_ != nullptr; } element_type &operator*() const noexcept { return *ptr_; } pointer operator->() const noexcept { return get(); } pointer get() const noexcept { return ptr_; } void reset(pointer ptr = nullptr) noexcept { if (ptr_ != nullptr) { this->delete_ptr(); } ptr_ = ptr; } pointer release() noexcept { auto result = ptr_; ptr_ = nullptr; return result; } void swap(unique_ptr &other) noexcept { std::swap(ptr_, other.ptr_); } private: pointer ptr_; void delete_ptr() noexcept { if (std::is_array::value) { delete[] ptr_; } else { delete ptr_; } } }; template bool operator==(const unique_ptr &lhs, const unique_ptr &rhs) noexcept { return lhs.get() == rhs.get(); } template bool operator==(const unique_ptr &lhs, std::nullptr_t) noexcept { return lhs.get() == nullptr; } template bool operator==(std::nullptr_t, const unique_ptr &rhs) noexcept { return nullptr == rhs.get(); } template bool operator!=(const unique_ptr &lhs, const unique_ptr &rhs) noexcept { return lhs.get() != rhs.get(); } template bool operator!=(const unique_ptr &lhs, std::nullptr_t) noexcept { return lhs.get() != nullptr; } template bool operator!=(std::nullptr_t, const unique_ptr &rhs) noexcept { return nullptr != rhs.get(); } } // namespace nostd OPENTELEMETRY_END_NAMESPACE #endif