CsPointer  2.0.0
cs_unique_pointer.h
1 
19 #ifndef LIB_CS_UNIQUE_POINTER_H
20 #define LIB_CS_UNIQUE_POINTER_H
21 
22 #include <compare>
23 #include <memory>
24 
25 namespace CsPointer {
26 
27 template <typename T, typename Deleter = std::default_delete<T>>
28 class CsUniquePointer
29 {
30  public:
31  using pointer = typename std::unique_ptr<T, Deleter>::pointer;
32  using element_type = typename std::unique_ptr<T, Deleter>::element_type;
33  using deleter_type = typename std::unique_ptr<T, Deleter>::deleter_type;
34 
35  using Pointer = pointer;
38 
39  constexpr CsUniquePointer(std::nullptr_t) noexcept
40  {
41  }
42 
43  explicit CsUniquePointer(Pointer p = nullptr) noexcept
44  : m_ptr(p)
45  {
46  }
47 
48  explicit CsUniquePointer(Pointer p, Deleter d) noexcept
49  : m_ptr(p, std::move(d))
50  {
51  }
52 
53  CsUniquePointer(std::unique_ptr<T, Deleter> &&p) noexcept
54  : m_ptr(std::move(p))
55  {
56  }
57 
58  ~CsUniquePointer() = default;
59 
60  CsUniquePointer(const CsUniquePointer &) = delete;
61  CsUniquePointer &operator=(const CsUniquePointer &) = delete;
62 
63  CsUniquePointer(CsUniquePointer &&other) = default;
64  CsUniquePointer &operator=(CsUniquePointer && other) = default;
65 
66  ElementType &operator*() const noexcept(noexcept(* std::declval<Pointer>())) {
67  return *m_ptr;
68  }
69 
70  Pointer operator->() const noexcept {
71  return m_ptr.get();
72  }
73 
74  bool operator!() const noexcept {
75  return m_ptr == nullptr;
76  }
77 
78  explicit operator bool() const noexcept {
79  return ! is_null();
80  }
81 
82  operator std::unique_ptr<T, Deleter>() && noexcept
83  {
84  return std::move(m_ptr);
85  }
86 
87  Pointer data() const noexcept {
88  return m_ptr.get();
89  }
90 
91  Pointer get() const noexcept {
92  return m_ptr.get();
93  }
94 
95  Deleter &get_deleter() noexcept {
96  return m_ptr.get_deleter();
97  }
98 
99  const Deleter &get_deleter() const noexcept {
100  return m_ptr.get_deleter();
101  }
102 
103  bool is_null() const noexcept {
104  return m_ptr == nullptr;
105  }
106 
107  Pointer release() noexcept {
108  return m_ptr.release();
109  }
110 
111  void reset(Pointer other = nullptr) noexcept {
112  if (m_ptr.get() == other) {
113  return;
114  }
115 
116  m_ptr.reset(other);
117  }
118 
119  void swap(CsUniquePointer &other) noexcept {
120  std::swap(m_ptr, other.m_ptr);
121  }
122 
123  Pointer take() noexcept {
124  return m_ptr.release();
125  }
126 
127  template <typename U, typename Deleter_U>
128  auto operator<=>(const CsUniquePointer<U, Deleter_U> &ptr) const noexcept {
129  return this->m_ptr <=> ptr.m_ptr;
130  }
131 
132  template <typename U, typename Deleter_U>
133  bool operator==(const CsUniquePointer<U, Deleter_U> &ptr) const noexcept {
134  return this->m_ptr == ptr.m_ptr;
135  }
136 
137  template <typename U>
138  auto operator<=>(const U *ptr) const noexcept {
139  return this->get() <=> ptr;
140  }
141 
142  template <typename U>
143  bool operator==(const U *ptr) const noexcept {
144  return this->get() == ptr;
145  }
146 
147  auto operator<=>(std::nullptr_t) const noexcept {
148  return this->m_ptr <=> nullptr;
149  }
150 
151  bool operator==(std::nullptr_t) const noexcept {
152  return this->m_ptr == nullptr;
153  }
154 
155  private:
156  std::unique_ptr<T, Deleter> m_ptr;
157 
158  template <typename U, typename D>
159  friend class CsUniqueArrayPointer;
160 };
161 
162 template <typename T, typename... Args, typename = typename std::enable_if_t<! std::is_array_v<T>>>
164 {
165  return std::make_unique<T>(std::forward<Args>(args)...);
166 }
167 
168 template <typename T, typename Deleter>
170 {
171  ptr1.swap(ptr2);
172 }
173 
174 } // end namespace
175 
176 #endif
CsUniquePointer< T > make_unique(Args &&...args)
Definition: cs_unique_pointer.h:163
void swap(CsUniquePointer &other) noexcept
Definition: cs_unique_pointer.h:119
bool operator==(const CsUniquePointer< T1, Deleter1 > &ptr1, const CsUniquePointer< T2, Deleter2 > &ptr2)
Deleter & get_deleter() noexcept
Definition: cs_unique_pointer.h:95
Pointer data() const noexcept
Definition: cs_unique_pointer.h:87
void reset(Pointer other=nullptr) noexcept
Definition: cs_unique_pointer.h:111
bool is_null() const noexcept
Definition: cs_unique_pointer.h:103
bool operator!() const noexcept
Definition: cs_unique_pointer.h:74
Pointer operator->() const noexcept
Definition: cs_unique_pointer.h:70
Pointer release() noexcept
Definition: cs_unique_pointer.h:107
ElementType & operator*() const noexcept (noexcept (*std::declval< Pointer >()))
Definition: cs_unique_pointer.h:66
CsUniquePointer & operator=(CsUniquePointer &&other) = default
Pointer take() noexcept
Definition: cs_unique_pointer.h:123
Contains a pointer to an object and takes exclusive ownership.
Definition: cs_shared_pointer.h:28
CsUniquePointer(std::unique_ptr< T, Deleter > &&p) noexcept
Definition: cs_unique_pointer.h:53
CsUniquePointer(Pointer p, Deleter d) noexcept
Definition: cs_unique_pointer.h:48
CsUniquePointer(Pointer p=nullptr) noexcept
Definition: cs_unique_pointer.h:43
deleter_type DeleterType
Definition: cs_unique_pointer.h:37
const Deleter & get_deleter() const noexcept
Definition: cs_unique_pointer.h:99
element_type ElementType
Definition: cs_unique_pointer.h:36
Pointer get() const noexcept
Definition: cs_unique_pointer.h:91
typename std::unique_ptr< cs_add_missing_extent_t< T >, Deleter >::deleter_type deleter_type
Definition: cs_unique_pointer.h:33
typename std::unique_ptr< cs_add_missing_extent_t< T >, Deleter >::element_type element_type
Definition: cs_unique_pointer.h:32
typename std::unique_ptr< cs_add_missing_extent_t< T >, Deleter >::pointer pointer
Definition: cs_unique_pointer.h:31
Namespace for the CsPointer library.