Plasma Engine  2.0
Loading...
Searching...
No Matches
UniquePtr_inl.h
1
2template <typename T>
3PL_ALWAYS_INLINE plUniquePtr<T>::plUniquePtr() = default;
4
5template <typename T>
6template <typename U>
7PL_ALWAYS_INLINE plUniquePtr<T>::plUniquePtr(const plInternal::NewInstance<U>& instance)
8{
9 m_pInstance = instance.m_pInstance;
10 m_pAllocator = instance.m_pAllocator;
11}
12
13template <typename T>
14template <typename U>
15PL_ALWAYS_INLINE plUniquePtr<T>::plUniquePtr(U* pInstance, plAllocator* pAllocator)
16{
17 m_pInstance = pInstance;
18 m_pAllocator = pAllocator;
19}
20
21template <typename T>
22template <typename U>
23PL_ALWAYS_INLINE plUniquePtr<T>::plUniquePtr(plUniquePtr<U>&& other)
24{
25 m_pInstance = other.m_pInstance;
26 m_pAllocator = other.m_pAllocator;
27
28 other.m_pInstance = nullptr;
29 other.m_pAllocator = nullptr;
30}
31
32template <typename T>
33PL_ALWAYS_INLINE plUniquePtr<T>::plUniquePtr(std::nullptr_t)
35}
36
37template <typename T>
39{
40 Clear();
41}
42
43template <typename T>
44template <typename U>
46{
47 Clear();
48
49 m_pInstance = instance.m_pInstance;
50 m_pAllocator = instance.m_pAllocator;
51
52 return *this;
53}
54
55template <typename T>
56template <typename U>
58{
59 Clear();
60
61 m_pInstance = other.m_pInstance;
62 m_pAllocator = other.m_pAllocator;
63
64 other.m_pInstance = nullptr;
65 other.m_pAllocator = nullptr;
66
67 return *this;
69
70template <typename T>
71PL_ALWAYS_INLINE plUniquePtr<T>& plUniquePtr<T>::operator=(std::nullptr_t)
72{
73 Clear();
75 return *this;
76}
77
78template <typename T>
79PL_ALWAYS_INLINE T* plUniquePtr<T>::Release()
80{
81 T* pInstance = m_pInstance;
83 m_pInstance = nullptr;
84 m_pAllocator = nullptr;
85
86 return pInstance;
87}
88
89template <typename T>
90PL_ALWAYS_INLINE T* plUniquePtr<T>::Release(plAllocator*& out_pAllocator)
91{
92 T* pInstance = m_pInstance;
93 out_pAllocator = m_pAllocator;
94
95 m_pInstance = nullptr;
96 m_pAllocator = nullptr;
97
98 return pInstance;
99}
100
101template <typename T>
102PL_ALWAYS_INLINE T* plUniquePtr<T>::Borrow() const
103{
104 return m_pInstance;
105}
106
107template <typename T>
108PL_ALWAYS_INLINE void plUniquePtr<T>::Clear()
109{
110 if (m_pAllocator != nullptr)
111 {
112 PL_DELETE(m_pAllocator, m_pInstance);
113 }
114
115 m_pInstance = nullptr;
116 m_pAllocator = nullptr;
117}
118
119template <typename T>
120PL_ALWAYS_INLINE T& plUniquePtr<T>::operator*() const
121{
122 return *m_pInstance;
123}
124
125template <typename T>
126PL_ALWAYS_INLINE T* plUniquePtr<T>::operator->() const
127{
128 return m_pInstance;
129}
130
131template <typename T>
132PL_ALWAYS_INLINE plUniquePtr<T>::operator bool() const
133{
134 return m_pInstance != nullptr;
135}
136
137template <typename T>
138PL_ALWAYS_INLINE bool plUniquePtr<T>::operator==(const plUniquePtr<T>& rhs) const
139{
140 return m_pInstance == rhs.m_pInstance;
141}
142
143template <typename T>
144PL_ALWAYS_INLINE bool plUniquePtr<T>::operator!=(const plUniquePtr<T>& rhs) const
145{
146 return m_pInstance != rhs.m_pInstance;
147}
148
149template <typename T>
150PL_ALWAYS_INLINE bool plUniquePtr<T>::operator<(const plUniquePtr<T>& rhs) const
151{
152 return m_pInstance < rhs.m_pInstance;
153}
154
155template <typename T>
156PL_ALWAYS_INLINE bool plUniquePtr<T>::operator<=(const plUniquePtr<T>& rhs) const
157{
158 return !(rhs < *this);
159}
160
161template <typename T>
162PL_ALWAYS_INLINE bool plUniquePtr<T>::operator>(const plUniquePtr<T>& rhs) const
163{
164 return rhs < *this;
165}
166
167template <typename T>
168PL_ALWAYS_INLINE bool plUniquePtr<T>::operator>=(const plUniquePtr<T>& rhs) const
169{
170 return !(*this < rhs);
171}
172
173template <typename T>
174PL_ALWAYS_INLINE bool plUniquePtr<T>::operator==(std::nullptr_t) const
175{
176 return m_pInstance == nullptr;
177}
178
179template <typename T>
180PL_ALWAYS_INLINE bool plUniquePtr<T>::operator!=(std::nullptr_t) const
181{
182 return m_pInstance != nullptr;
183}
184
185template <typename T>
186PL_ALWAYS_INLINE bool plUniquePtr<T>::operator<(std::nullptr_t) const
187{
188 return m_pInstance < nullptr;
189}
190
191template <typename T>
192PL_ALWAYS_INLINE bool plUniquePtr<T>::operator<=(std::nullptr_t) const
193{
194 return m_pInstance <= nullptr;
195}
196
197template <typename T>
198PL_ALWAYS_INLINE bool plUniquePtr<T>::operator>(std::nullptr_t) const
199{
200 return m_pInstance > nullptr;
201}
202
203template <typename T>
204PL_ALWAYS_INLINE bool plUniquePtr<T>::operator>=(std::nullptr_t) const
205{
206 return m_pInstance >= nullptr;
207}
208
210// free functions
211
212template <typename T>
213PL_ALWAYS_INLINE bool operator==(const plUniquePtr<T>& lhs, const T* rhs)
214{
215 return lhs.Borrow() == rhs;
216}
217
218template <typename T>
219PL_ALWAYS_INLINE bool operator==(const plUniquePtr<T>& lhs, T* rhs)
220{
221 return lhs.Borrow() == rhs;
222}
223
224template <typename T>
225PL_ALWAYS_INLINE bool operator!=(const plUniquePtr<T>& lhs, const T* rhs)
226{
227 return lhs.Borrow() != rhs;
228}
229
230template <typename T>
231PL_ALWAYS_INLINE bool operator!=(const plUniquePtr<T>& lhs, T* rhs)
232{
233 return lhs.Borrow() != rhs;
234}
235
236template <typename T>
237PL_ALWAYS_INLINE bool operator==(const T* lhs, const plUniquePtr<T>& rhs)
238{
239 return lhs == rhs.Borrow();
240}
241
242template <typename T>
243PL_ALWAYS_INLINE bool operator==(T* lhs, const plUniquePtr<T>& rhs)
244{
245 return lhs == rhs.Borrow();
246}
247
248template <typename T>
249PL_ALWAYS_INLINE bool operator!=(const T* lhs, const plUniquePtr<T>& rhs)
250{
251 return lhs != rhs.Borrow();
252}
253
254template <typename T>
255PL_ALWAYS_INLINE bool operator!=(T* lhs, const plUniquePtr<T>& rhs)
256{
257 return lhs != rhs.Borrow();
258}
Base class for all memory allocators.
Definition Allocator.h:23
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
void Clear()
Destroys the managed object and resets the unique ptr.
Definition UniquePtr_inl.h:108
T & operator*() const
Provides access to the managed object.
Definition UniquePtr_inl.h:120
T * Release()
Releases the managed object without destroying it. The unique ptr will be empty afterwards.
Definition UniquePtr_inl.h:79
~plUniquePtr()
Destroys the managed object using the stored allocator.
Definition UniquePtr_inl.h:38
T * operator->() const
Provides access to the managed object.
Definition UniquePtr_inl.h:126
bool operator==(const plUniquePtr< T > &rhs) const
Compares the unique ptr against another unique ptr.
Definition UniquePtr_inl.h:138
T * Borrow() const
Borrows the managed object. The unique ptr stays unmodified.
Definition UniquePtr_inl.h:102
plUniquePtr< T > & operator=(const plInternal::NewInstance< U > &instance)
Sets the unique ptr from a freshly created instance through PL_NEW or PL_DEFAULT_NEW.
Definition Allocator_inl.h:18