unique_ptr.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #if defined(OPENTELEMETRY_STL_VERSION)
  5. # if OPENTELEMETRY_STL_VERSION >= 2011
  6. # include "opentelemetry/std/unique_ptr.h"
  7. # define OPENTELEMETRY_HAVE_STD_UNIQUE_PTR
  8. # endif
  9. #endif
  10. #if !defined(OPENTELEMETRY_HAVE_STD_UNIQUE_PTR)
  11. # include <cstddef>
  12. # include <memory>
  13. # include <type_traits>
  14. # include <utility>
  15. # include "opentelemetry/version.h"
  16. OPENTELEMETRY_BEGIN_NAMESPACE
  17. namespace nostd
  18. {
  19. namespace detail
  20. {
  21. template <class T>
  22. struct unique_ptr_element_type
  23. {
  24. using type = T;
  25. };
  26. template <class T>
  27. struct unique_ptr_element_type<T[]>
  28. {
  29. using type = T;
  30. };
  31. } // namespace detail
  32. /**
  33. * Provide a simplified port of std::unique_ptr that has ABI stability.
  34. *
  35. * Note: This implementation doesn't allow for a custom deleter.
  36. */
  37. template <class T>
  38. class unique_ptr
  39. {
  40. public:
  41. using element_type = typename detail::unique_ptr_element_type<T>::type;
  42. using pointer = element_type *;
  43. unique_ptr() noexcept : ptr_{nullptr} {}
  44. unique_ptr(std::nullptr_t) noexcept : ptr_{nullptr} {}
  45. explicit unique_ptr(pointer ptr) noexcept : ptr_{ptr} {}
  46. unique_ptr(unique_ptr &&other) noexcept : ptr_{other.release()} {}
  47. template <class U,
  48. typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
  49. unique_ptr(unique_ptr<U> &&other) noexcept : ptr_{other.release()}
  50. {}
  51. template <class U,
  52. typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
  53. unique_ptr(std::unique_ptr<U> &&other) noexcept : ptr_{other.release()}
  54. {}
  55. ~unique_ptr() { reset(); }
  56. unique_ptr &operator=(unique_ptr &&other) noexcept
  57. {
  58. reset(other.release());
  59. return *this;
  60. }
  61. unique_ptr &operator=(std::nullptr_t) noexcept
  62. {
  63. reset();
  64. return *this;
  65. }
  66. template <class U,
  67. typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
  68. unique_ptr &operator=(unique_ptr<U> &&other) noexcept
  69. {
  70. reset(other.release());
  71. return *this;
  72. }
  73. template <class U,
  74. typename std::enable_if<std::is_convertible<U *, pointer>::value>::type * = nullptr>
  75. unique_ptr &operator=(std::unique_ptr<U> &&other) noexcept
  76. {
  77. reset(other.release());
  78. return *this;
  79. }
  80. operator std::unique_ptr<T>() && noexcept { return std::unique_ptr<T>{release()}; }
  81. operator bool() const noexcept { return ptr_ != nullptr; }
  82. element_type &operator*() const noexcept { return *ptr_; }
  83. pointer operator->() const noexcept { return get(); }
  84. pointer get() const noexcept { return ptr_; }
  85. void reset(pointer ptr = nullptr) noexcept
  86. {
  87. if (ptr_ != nullptr)
  88. {
  89. this->delete_ptr();
  90. }
  91. ptr_ = ptr;
  92. }
  93. pointer release() noexcept
  94. {
  95. auto result = ptr_;
  96. ptr_ = nullptr;
  97. return result;
  98. }
  99. void swap(unique_ptr &other) noexcept { std::swap(ptr_, other.ptr_); }
  100. private:
  101. pointer ptr_;
  102. void delete_ptr() noexcept
  103. {
  104. if (std::is_array<T>::value)
  105. {
  106. delete[] ptr_;
  107. }
  108. else
  109. {
  110. delete ptr_;
  111. }
  112. }
  113. };
  114. template <class T1, class T2>
  115. bool operator==(const unique_ptr<T1> &lhs, const unique_ptr<T2> &rhs) noexcept
  116. {
  117. return lhs.get() == rhs.get();
  118. }
  119. template <class T>
  120. bool operator==(const unique_ptr<T> &lhs, std::nullptr_t) noexcept
  121. {
  122. return lhs.get() == nullptr;
  123. }
  124. template <class T>
  125. bool operator==(std::nullptr_t, const unique_ptr<T> &rhs) noexcept
  126. {
  127. return nullptr == rhs.get();
  128. }
  129. template <class T1, class T2>
  130. bool operator!=(const unique_ptr<T1> &lhs, const unique_ptr<T2> &rhs) noexcept
  131. {
  132. return lhs.get() != rhs.get();
  133. }
  134. template <class T>
  135. bool operator!=(const unique_ptr<T> &lhs, std::nullptr_t) noexcept
  136. {
  137. return lhs.get() != nullptr;
  138. }
  139. template <class T>
  140. bool operator!=(std::nullptr_t, const unique_ptr<T> &rhs) noexcept
  141. {
  142. return nullptr != rhs.get();
  143. }
  144. } // namespace nostd
  145. OPENTELEMETRY_END_NAMESPACE
  146. #endif /* OPENTELEMETRY_HAVE_STD_UNIQUE_PTR */