Euclid
Geometry Processing and Shape Analysis in C++
Memory.h
1 
7 #pragma once
8 
9 #include <memory>
10 #include <utility>
11 
12 namespace Euclid
13 {
26 template<typename T>
27 class ProPtr
28 {
29 public:
30  using pointer = T*;
31  using element_type = T;
32 
33 public:
37  constexpr ProPtr() noexcept : _data(nullptr), _own(false) {}
38 
42  constexpr ProPtr(std::nullptr_t) noexcept : _data(nullptr), _own(false) {}
43 
51  explicit ProPtr(pointer p, bool own = false) noexcept : _data(p), _own(own)
52  {}
53 
57  ProPtr(const ProPtr& pro) = delete;
58 
62  ProPtr(ProPtr&& pro) noexcept
63  {
64  pro.swap(*this);
65  }
66 
70  template<typename U>
71  ProPtr(ProPtr<U>&& pro) noexcept
72  {
73  auto owning = pro.owns();
74  auto data = pro.release();
75  ProPtr<T> tmp(data, owning);
76  tmp.swap(*this);
77  }
78 
84  {
85  if (_own) {
86  delete _data;
87  }
88  }
89 
93  ProPtr& operator=(const ProPtr& pro) = delete;
94 
99  ProPtr& operator=(std::nullptr_t) noexcept
100  {
101  reset();
102  return *this;
103  }
104 
108  ProPtr& operator=(ProPtr&& pro) noexcept
109  {
110  pro.swap(*this);
111  return *this;
112  }
113 
117  template<typename U>
118  ProPtr& operator=(ProPtr<U>&& pro) noexcept
119  {
120  auto owning = pro.owns();
121  auto data = pro.release();
122  ProPtr<T> tmp(data, owning);
123  tmp.swap(*this);
124  return *this;
125  }
126 
130  pointer release() noexcept
131  {
132  pointer result = nullptr;
133  std::swap(result, _data);
134  _own = false;
135  return result;
136  }
137 
145  void reset(pointer p = pointer(), bool own = false) noexcept
146  {
147  if (_own) {
148  delete _data;
149  }
150  _data = p;
151  _own = own;
152  }
153 
157  void swap(ProPtr& pro) noexcept
158  {
159  using std::swap;
160  swap(pro._data, _data);
161  swap(pro._own, _own);
162  }
163 
167  pointer get() const noexcept
168  {
169  return _data;
170  }
171 
175  explicit operator bool() const noexcept
176  {
177  return _data;
178  }
179 
183  typename std::add_lvalue_reference<T>::type operator*() const
184  {
185  return *_data;
186  }
187 
191  pointer operator->() const noexcept
192  {
193  return _data;
194  }
195 
199  bool owns() const noexcept
200  {
201  return _own;
202  }
203 
204 private:
205  pointer _data;
206  bool _own;
207 };
208 
212 template<typename T>
213 void swap(ProPtr<T>& lhs, ProPtr<T>& rhs)
214 {
215  lhs.swap(rhs);
216 }
217 
219 } // namespace Euclid
~ProPtr()
Desctructor.
Definition: Memory.h:83
void reset(pointer p=pointer(), bool own=false) noexcept
Reset the ProPtr.
Definition: Memory.h:145
A potentially resource owning pointer.
Definition: Memory.h:27
std::add_lvalue_reference< T >::type operator*() const
Return the data being pointed to.
Definition: Memory.h:183
constexpr ProPtr(std::nullptr_t) noexcept
Construct from nullptr_t.
Definition: Memory.h:42
ProPtr & operator=(const ProPtr &pro)=delete
Non-copyable.
ProPtr & operator=(ProPtr &&pro) noexcept
Move assignment operator.
Definition: Memory.h:108
constexpr ProPtr() noexcept
Default constructor.
Definition: Memory.h:37
pointer operator->() const noexcept
Return the raw pointer.
Definition: Memory.h:191
Definition: AABB.h:6
ProPtr(ProPtr< U > &&pro) noexcept
Move construct from a derived type.
Definition: Memory.h:71
ProPtr(pointer p, bool own=false) noexcept
Construct from pointer.
Definition: Memory.h:51
void swap(ProPtr &pro) noexcept
Swap this ProPtr with another, both resource and ownership.
Definition: Memory.h:157
ProPtr(ProPtr &&pro) noexcept
Move constructor.
Definition: Memory.h:62
pointer release() noexcept
Release the ownership(if owns) and return the data.
Definition: Memory.h:130
bool owns() const noexcept
Return whether this pointer owns the resource.
Definition: Memory.h:199
ProPtr & operator=(ProPtr< U > &&pro) noexcept
Move assignment from a derived type.
Definition: Memory.h:118
void swap(ProPtr< T > &lhs, ProPtr< T > &rhs)
Swap contents of two ProPtr<T>s.
Definition: Memory.h:213
ProPtr & operator=(std::nullptr_t) noexcept
Copy assignment from nullptr.
Definition: Memory.h:99