CTL  0.6.1
Computed Tomography Library
copyableuniqueptr.h
1 #ifndef CTL_COPYABLEUNIQUEPTR_H
2 #define CTL_COPYABLEUNIQUEPTR_H
3 
4 #include <memory>
5 
6 namespace CTL {
7 
13 template <class T>
14 class CopyableUniquePtr : private std::unique_ptr<T>
15 {
16 public:
17  // ctors
18  CopyableUniquePtr() = default;
19  CopyableUniquePtr(std::unique_ptr<T> uniquePtr) noexcept;
20  using std::unique_ptr<T>::unique_ptr;
21  // copy
24  // move
25  CopyableUniquePtr(CopyableUniquePtr<T>&& other) = default;
27  // dtor
28  ~CopyableUniquePtr() = default;
29 
30  // public access to `std::unique_ptr<T>` interface
31  using std::unique_ptr<T>::get;
32  using std::unique_ptr<T>::get_deleter;
33  using std::unique_ptr<T>::release;
34  using std::unique_ptr<T>::reset;
35  using std::unique_ptr<T>::swap;
36  using std::unique_ptr<T>::operator bool;
37  using std::unique_ptr<T>::operator->;
38  using std::unique_ptr<T>::operator*;
39 
40  // convenience methods
41  bool isNull() const noexcept;
42  std::unique_ptr<T>& wrapped() noexcept;
43  const std::unique_ptr<T>& wrapped() const noexcept;
44 };
45 
49 template <class T>
50 CopyableUniquePtr<T>::CopyableUniquePtr(std::unique_ptr<T> uniquePtr) noexcept
51  : std::unique_ptr<T>(std::move(uniquePtr))
52 {
53 }
54 
59 template <class T>
61  : std::unique_ptr<T>(other ? static_cast<T*>(other->clone()) : nullptr)
62 {
63 }
64 
70 template <class T>
72 {
73  this->reset(other ? static_cast<T*>(other->clone()) : nullptr);
74  return *this;
75 }
76 
80 template <class T>
81 bool CopyableUniquePtr<T>::isNull() const noexcept
82 {
83  return *this == nullptr;
84 }
85 
89 template <class T>
90 std::unique_ptr<T>& CopyableUniquePtr<T>::wrapped() noexcept
91 {
92  return *this;
93 }
94 
98 template <class T>
99 const std::unique_ptr<T>& CopyableUniquePtr<T>::wrapped() const noexcept
100 {
101  return *this;
102 }
103 
104 } // namespace CTL
105 
106 #endif // CTL_COPYABLEUNIQUEPTR_H
CopyableUniquePtr & operator=(const CopyableUniquePtr< T > &other)
Definition: copyableuniqueptr.h:71
std::unique_ptr< T > & wrapped() noexcept
Definition: copyableuniqueptr.h:90
Extends std::unique_ptr<T> with copy operations using the clone method of type T.
Definition: copyableuniqueptr.h:14
bool isNull() const noexcept
Definition: copyableuniqueptr.h:81