SPH
AutoPtr.h
Go to the documentation of this file.
1 #pragma once
2 
7 
10 
12 
14 template <typename T>
15 class AutoPtr {
16  template <typename>
17  friend class AutoPtr;
18 
19 private:
20  T* ptr;
21 
22 public:
24  : ptr(nullptr) {}
25 
26  INLINE AutoPtr(std::nullptr_t)
27  : ptr(nullptr) {}
28 
29  INLINE explicit AutoPtr(T* ptr)
30  : ptr(ptr) {}
31 
32  INLINE AutoPtr(const AutoPtr& other) = delete;
33 
34  template <typename T2>
36  : ptr(other.ptr) {
37  other.ptr = nullptr;
38  }
39 
41  this->reset();
42  }
43 
44  INLINE AutoPtr& operator=(const AutoPtr& other) = delete;
45 
46  template <typename T2>
48  reset();
49  ptr = other.ptr;
50  other.ptr = nullptr;
51  return *this;
52  }
53 
54  INLINE AutoPtr& operator=(std::nullptr_t) {
55  this->reset();
56  return *this;
57  }
58 
59  INLINE T& operator*() const {
60  SPH_ASSERT(ptr);
61  return *ptr;
62  }
63 
64  INLINE T* operator->() const {
65  SPH_ASSERT(ptr);
66  return ptr;
67  }
68 
69  INLINE RawPtr<T> get() const {
70  return ptr;
71  }
72 
73  INLINE explicit operator bool() const {
74  return ptr != nullptr;
75  }
76 
77  INLINE bool operator!() const {
78  return !ptr;
79  }
80 
81  template <typename... TArgs>
82  INLINE decltype(auto) operator()(TArgs&&... args) const {
83  SPH_ASSERT(ptr);
84  return (*ptr)(std::forward<TArgs>(args)...);
85  }
86 
87  INLINE void reset() {
88  alignedDelete(ptr);
89  ptr = nullptr;
90  }
91 
92  INLINE T* release() {
93  T* resource = ptr;
94  ptr = nullptr;
95  return resource;
96  }
97 
98  INLINE void swap(AutoPtr& other) {
99  std::swap(ptr, other.ptr);
100  }
101 };
102 
103 template <typename T>
104 bool operator==(const AutoPtr<T>& ptr, std::nullptr_t) {
105  return !ptr;
106 }
107 
108 template <typename T>
109 bool operator==(std::nullptr_t, const AutoPtr<T>& ptr) {
110  return !ptr;
111 }
112 
113 template <typename T>
114 bool operator!=(const AutoPtr<T>& ptr, std::nullptr_t) {
115  return bool(ptr);
116 }
117 
118 template <typename T>
119 bool operator!=(std::nullptr_t, const AutoPtr<T>& ptr) {
120  return bool(ptr);
121 }
122 
123 template <typename T, typename... TArgs>
124 INLINE AutoPtr<T> makeAuto(TArgs&&... args) {
125  return AutoPtr<T>(alignedNew<T>(std::forward<TArgs>(args)...));
126 }
127 
132 template <typename T1, typename T2>
134  if (RawPtr<T1> ptr = dynamicCast<T1>(source.get())) {
135  source.release();
136  return AutoPtr<T1>(ptr.get());
137  } else {
138  return nullptr;
139  }
140 }
141 
143 
144 namespace std {
145 template <typename T>
146 void swap(Sph::AutoPtr<T>& p1, Sph::AutoPtr<T>& p2) {
147  p1.swap(p2);
148 }
149 } // namespace std
Base class for utility wrappers (Optional, Variant, ...)
INLINE void alignedDelete(T *ptr)
Deletes an object previously allocated using alignedNew.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
bool operator==(const AutoPtr< T > &ptr, std::nullptr_t)
Definition: AutoPtr.h:104
INLINE AutoPtr< T > makeAuto(TArgs &&... args)
Definition: AutoPtr.h:124
bool operator!=(const AutoPtr< T > &ptr, std::nullptr_t)
Definition: AutoPtr.h:114
INLINE AutoPtr< T1 > dynamicCast(AutoPtr< T2 > &&source)
Performs a dynamic_cast on an AutoPtr, moving the ownership of the resource to the created object.
Definition: AutoPtr.h:133
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Simple non-owning wrapper of pointer.
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
INLINE void swap(AutoPtr &other)
Definition: AutoPtr.h:98
INLINE bool operator!() const
Definition: AutoPtr.h:77
INLINE AutoPtr(const AutoPtr &other)=delete
INLINE AutoPtr & operator=(AutoPtr< T2 > &&other)
Definition: AutoPtr.h:47
INLINE T & operator*() const
Definition: AutoPtr.h:59
INLINE T * operator->() const
Definition: AutoPtr.h:64
INLINE AutoPtr & operator=(const AutoPtr &other)=delete
INLINE AutoPtr & operator=(std::nullptr_t)
Definition: AutoPtr.h:54
~AutoPtr()
Definition: AutoPtr.h:40
INLINE AutoPtr(AutoPtr< T2 > &&other)
Definition: AutoPtr.h:35
INLINE RawPtr< T > get() const
Definition: AutoPtr.h:69
INLINE AutoPtr(T *ptr)
Definition: AutoPtr.h:29
INLINE void reset()
Definition: AutoPtr.h:87
INLINE AutoPtr(std::nullptr_t)
Definition: AutoPtr.h:26
INLINE AutoPtr()
Definition: AutoPtr.h:23
INLINE T * release()
Definition: AutoPtr.h:92
Non-owning wrapper of pointer.
Definition: RawPtr.h:19
Overload of std::swap for Sph::Array.
Definition: Array.h:578
void swap(Sph::Array< T, TCounter > &ar1, Sph::Array< T, TCounter > &ar2)
Definition: Array.h:580