ACF $AcfVersion:0$
TSharedNullable.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// STL includes
6#include <algorithm>
7#include <memory>
8
9// ACF include
10#include <istd/TNullable.h>
11
12
13namespace istd
14{
15
16
21template <class T>
23{
24public:
26 TSharedNullable(const T& value);
29 bool IsValid() const;
30 bool IsNull() const;
31 bool HasValue() const;
32 const T* GetPtr() const;
33 T* GetPtr();
34 T& GetValue();
35 const T& GetValue() const;
36 void Reset();
37 T& Emplace();
38 void SetNull();
39
40 bool isValid() const
41 {
42 return false;
43 }
44 bool isNull() const
45 {
46 return true;
47 }
48
49 // operators
52
53 // STL-compatibility methods
54 // access operators
55 const T* operator->() const;
56 T* operator->();
57 const T& operator*() const &;
58 T& operator*() &;
59 const T&& operator*() const &&;
60 T&& operator*() &&;
61
62 // set operators
63 TSharedNullable& operator=( std::nullptr_t );
64 TSharedNullable& operator=(const TSharedNullable& other);
65
66 explicit operator bool() const;
67 bool has_value() const;
68
69 // value access
70 T& value() &;
71 const T& value() const &;
72 T&& value() &&;
73 const T&& value() const &&;
74 T value_or(const T& default_value) const;
75
76 void swap(TSharedNullable& other );
77 void reset();
78 T& emplace();
79 T& emplace(T&& value)&&;
80
81 //Compare two TSharedNullable objects
82 bool operator==(const TSharedNullable& other) const;
83 bool operator!=(const TSharedNullable& other) const;
84
85 //Compare a TSharedNullable object with a nullptr
86 bool operator==(std::nullptr_t) const;
87 bool operator!=(std::nullptr_t) const;
88
89 //Compare a TSharedNullable object with a value
90 bool operator==(const T& value) const;
91 bool operator!=(const T& value) const;
92
93private:
94 std::shared_ptr<TNullable<T>> m_dataPtr;
95};
96
97template <class T>
98bool Less(const istd::TSharedNullable<T> &lhs, const istd::TSharedNullable<T> &rhs)
99{
100 return *lhs < *rhs;
101}
102
103
104// public methods
105
106template <class T>
108 :m_dataPtr(new TNullable<T>())
109{
110}
111
112
113template<class T>
115 :m_dataPtr(new TNullable<T>())
116{
117 *m_dataPtr = value;
118}
119
120
121template<class T>
123 :m_dataPtr(new TNullable<T>())
124{
125 *m_dataPtr = std::move(value);
126}
127
128
129template <class T>
133
134
135template<class T>
137{
138 return m_dataPtr->IsValid();
139}
140
141
142template<class T>
144{
145 return m_dataPtr->IsNull();
146}
147
148
149template<class T>
151{
152 return m_dataPtr->HasValue();
153}
154
155
156template<class T>
158{
159 return m_dataPtr->GetPtr();
160}
161
162
163template<class T>
165{
166 return m_dataPtr->GetPtr();
167}
168
169
170template<class T>
172{
173 return m_dataPtr->GetValue();
174}
175
176
177template<class T>
179{
180 return m_dataPtr->GetValue();
181}
182
183
184template<class T>
186{
187 return m_dataPtr.reset(new TNullable<T>);
188}
189
190
191template<class T>
193{
194 return m_dataPtr->Emplace();
195}
196
197
198template<class T>
200{
201 m_dataPtr->SetNull();
202}
203
204
205// operators
206
207template<class T>
209{
210 m_dataPtr->operator=(value);
211
212 return *this;
213}
214
215
216template<class T>
218{
219 m_dataPtr->operator=(std::move(value));
220
221 return *this;
222}
223
224
225// STL-compatibility methods
226// access operators
227
228template<class T>
230{
231 return m_dataPtr->operator->();
232}
233
234
235template<class T>
237{
238 return m_dataPtr->operator->();
239}
240
241
242template<class T>
244{
245 return m_dataPtr->operator*();
246}
247
248
249template<class T>
251{
252 return m_dataPtr->operator*();
253}
254
255
256template<class T>
258{
259 return m_dataPtr->operator*();
260}
261
262
263template<class T>
265{
266 return m_dataPtr->operator*();
267}
268
269
270template<class T>
272{
273 m_dataPtr->operator=(nullptr);
274
275 return *this;
276}
277
278
279template<class T>
281{
282 m_dataPtr = other.m_dataPtr;
283
284 return *this;
285}
286
287
288template<class T>
290{
291 return m_dataPtr->operator bool();
292}
293
294
295template<class T>
297{
298 return m_dataPtr->has_value();
299}
300
301
302template<class T>
304{
305 return m_dataPtr->value();
306}
307
308
309template<class T>
311{
312 return m_dataPtr->value();
313}
314
315
316template<class T>
318{
319 return m_dataPtr->value();
320}
321
322
323template<class T>
324const T&& TSharedNullable<T>::value() const &&
325{
326 return m_dataPtr->value();
327}
328
329
330template<class T>
331T TSharedNullable<T>::value_or(const T& default_value) const
332{
333 return m_dataPtr->value_or(default_value);
334}
335
336
337template<class T>
339{
340 m_dataPtr.swap(other.m_dataPtr);
341}
342
343
344template<class T>
346{
347 Reset();
348}
349
350
351template<class T>
353{
354 return Emplace();
355}
356
357
358template<class T>
360{
361 return m_dataPtr->emplace();
362}
363
364
365template<class T>
367{
368 return m_dataPtr.get() == other.m_dataPtr.get();
369}
370
371
372template<class T>
374{
375 return (!operator==(other));
376}
377
378
379template<class T>
380bool TSharedNullable<T>::operator==(std::nullptr_t) const
381{
382 return m_dataPtr->operator==(nullptr);
383}
384
385
386template<class T>
387bool TSharedNullable<T>::operator!=(std::nullptr_t) const
388{
389 return m_dataPtr->operator!=(nullptr);
390}
391
392
393template<class T>
394bool TSharedNullable<T>::operator==(const T& value) const
395{
396 return m_dataPtr->operator==(value);
397}
398
399
400template<class T>
401bool TSharedNullable<T>::operator!=(const T& value) const
402{
403 return m_dataPtr->operator!=(value);
404}
405
406
407} // namespace istd
408
409
410template <class T>
411inline unsigned int qHash(const istd::TSharedNullable<T> &key, unsigned int seed){
412 return qHash(*key, seed);
413}
414
unsigned int qHash(const istd::TSharedNullable< T > &key, unsigned int seed)
This is a comfort wrap for TNullable that allows several TSharedNullable objects to own the same obje...
bool operator==(const TSharedNullable &other) const
const T * operator->() const
T value_or(const T &default_value) const
void swap(TSharedNullable &other)
const T * GetPtr() const
const T & operator*() const &
bool operator!=(const TSharedNullable &other) const
TSharedNullable & operator=(const T &value)
Standard library.
Definition IComponent.h:17
bool Less(const istd::TSharedNullable< T > &lhs, const istd::TSharedNullable< T > &rhs)