UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UniquePointer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7
8namespace uLang
9{
10
11template<class ObjectType, bool AllowNull, class AllocatorType, typename... AllocatorArgsType> class TUPtrArrayG;
12
13template <typename ObjectType, bool AllowNull, class AllocatorType, typename... AllocatorArgsType>
14class TUPtrG
15{
16public:
17
18 // Construction
19
20 ULANG_FORCEINLINE TUPtrG(NullPtrType NullPtr = nullptr) : _Object(nullptr), _Allocator(DefaultInit) { static_assert(AllowNull, "Cannot default construct a unique reference, as it is not allowed to be null."); }
21 ULANG_FORCEINLINE TUPtrG(TUPtrG && Other) : _Object(Other._Object), _Allocator(Other._Allocator) { Other._Object = nullptr; } // Note: We null here even when AllowNull==false since 99% likely will be destructed immediately after anyway
23 ULANG_FORCEINLINE TUPtrG(TUPtrG<OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType...> && Other) : _Object(Other._Object), _Allocator(Other._Allocator) { Other._Object = nullptr; } // Note: We null here even when AllowNull==false since 99% likely will be destructed immediately after anyway
24 ULANG_FORCEINLINE ~TUPtrG() { if (_Object) Release(); }
25
26 template<typename... CtorArgsType>
27 ULANG_FORCEINLINE TUPtrG& SetNew(CtorArgsType&&... CtorArgs) { if (_Object) { Release(); } _Object = new(_Allocator) ObjectType(uLang::ForwardArg<CtorArgsType>(CtorArgs)...); return *this; }
28 template<typename... CtorArgsType>
29 ULANG_FORCEINLINE static TUPtrG New(AllocatorArgsType&&... AllocatorArgs, CtorArgsType&&... CtorArgs) { AllocatorType Allocator(AllocatorArgs...); ObjectType * Object = new(Allocator) ObjectType(uLang::ForwardArg<CtorArgsType>(CtorArgs)...); return TUPtrG(Object, uLang::Move(Allocator)); }
30
31 // Non-copyable
32
33 TUPtrG(const TUPtrG &) = delete;
34 template<class OtherObjectType, bool OtherAllowNull> TUPtrG(const TUPtrG<OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType...> & Other) = delete;
35 TUPtrG& operator=(const TUPtrG &) = delete;
36 template<class OtherObjectType, bool OtherAllowNull> TUPtrG & operator=(const TUPtrG<OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType...> & Other) = delete;
37
38 // Assignment
39
44
45 // Conversion methods
46
47 ULANG_FORCEINLINE operator ObjectType*() const { return _Object; }
48 ULANG_FORCEINLINE ObjectType & operator*() const { return *_Object; }
49 ULANG_FORCEINLINE ObjectType * operator->() const { return _Object; }
50 ULANG_FORCEINLINE ObjectType * Get() const { return _Object; }
51 ULANG_FORCEINLINE const AllocatorType & GetAllocator() const { return _Allocator; }
52 ULANG_FORCEINLINE void Reset() { if (_Object) { Release(); _Object = nullptr; } }
53
54 ULANG_FORCEINLINE TUPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...>& AsRef() &
55 {
56 static_assert(AllowNull, "Unnecessary conversion!");
57 ULANG_ASSERTF(_Object, "Converting null pointer to reference!");
58 return reinterpret_cast<TUPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...>&>(*this);
59 }
60 ULANG_FORCEINLINE const TUPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...>&& AsRef() &&
61 {
62 static_assert(AllowNull, "Unnecessary conversion!");
63 ULANG_ASSERTF(_Object, "Converting null pointer to reference!");
64 return reinterpret_cast<const TUPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...>&&>(*this);
65 }
66 ULANG_FORCEINLINE const TUPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...>& AsRef() const&
67 {
68 static_assert(AllowNull, "Unnecessary conversion!");
69 ULANG_ASSERTF(_Object, "Converting null pointer to reference!");
70 return reinterpret_cast<const TUPtrG<ObjectType, false, AllocatorType, AllocatorArgsType...>&>(*this);
71 }
73 ULANG_FORCEINLINE TUPtrG<OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType...>& As() { return *reinterpret_cast<TUPtrG<OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType...> *>(this); }
75 ULANG_FORCEINLINE const TUPtrG<OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType...>& As() const { return *reinterpret_cast<const TUPtrG<OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType...> *>(this); }
76
77 // Comparison operators
78
79 ULANG_FORCEINLINE bool operator==(NullPtrType) const { return !_Object; }
80 ULANG_FORCEINLINE bool operator==(const TUPtrG & Other) const { return _Object == Other._Object; }
81 template<class OtherObjectType, bool OtherAllowNull>
83 template<class OtherObjectType, bool OtherAllowNull>
84 ULANG_FORCEINLINE bool operator==(OtherObjectType * Object) const { return _Object == Object; }
85 ULANG_FORCEINLINE bool operator!=(const TUPtrG & Other) const { return _Object != Other._Object; }
86 template<class OtherObjectType, bool OtherAllowNull>
88 template<class OtherObjectType, bool OtherAllowNull>
89 ULANG_FORCEINLINE bool operator!=(OtherObjectType * Object) const { return _Object != Object; }
90 ULANG_FORCEINLINE bool operator< (const TUPtrG & Other) const { return _Object < Other._Object; }
91 template<class OtherObjectType, bool OtherAllowNull>
93 template<class OtherObjectType, bool OtherAllowNull>
94 ULANG_FORCEINLINE bool operator< (OtherObjectType * Object) const { return _Object < Object; }
95 ULANG_FORCEINLINE bool operator> (const TUPtrG & Other) const { return _Object > Other._Object; }
96 template<class OtherObjectType, bool OtherAllowNull>
98 template<class OtherObjectType, bool OtherAllowNull>
99 ULANG_FORCEINLINE bool operator> (OtherObjectType * Object) const { return _Object > Object; }
100
101 // Validation methods
102
103 ULANG_FORCEINLINE operator bool() { return (_Object != nullptr); }
104 ULANG_FORCEINLINE operator bool() const { return (_Object != nullptr); }
105 ULANG_FORCEINLINE bool operator!() const { return (_Object == nullptr); }
106 ULANG_FORCEINLINE bool IsValid() const { return (_Object != nullptr); }
107
108private:
109 template<class OtherObjectType, bool OtherAllowNull, class OtherAllocatorType, typename... OtherAllocatorArgsType> friend class TUPtrG;
110 template<class OtherObjectType, bool OtherAllowNull, class OtherAllocatorType, typename... OtherAllocatorArgsType> friend class TUPtrArrayG;
111
112 ULANG_FORCEINLINE TUPtrG(ObjectType * Object, const AllocatorType & Allocator)
113 : _Object(Object)
114 , _Allocator(Allocator)
115 {
116 }
117
118 template<class OtherObjectType, bool OtherAllowNull>
120 {
121 if (_Object)
122 {
123 Release();
124 }
125
126 _Object = Other._Object;
127 _Allocator = Other._Allocator;
128 Other._Object = nullptr;
129
130 return *this;
131 }
132
135 {
136 _Object->~ObjectType();
137 _Allocator.Deallocate(_Object);
138 }
139
141 ObjectType* _Object;
142
145 AllocatorType _Allocator;
146};
147
149template<class ObjectType>
151
153template<class ObjectType>
155
157template<class ObjectType>
159
161template<class ObjectType>
163
164
165}
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
const bool
Definition NetworkReplayStreaming.h:178
#define ULANG_FORCEINLINE
Definition Common.h:188
#define ULANG_ASSERTF(expr, format,...)
Definition Common.h:290
Definition UniquePointerArray.h:16
Definition UniquePointer.h:15
ULANG_FORCEINLINE bool operator==(NullPtrType) const
Definition UniquePointer.h:79
ULANG_FORCEINLINE bool IsValid() const
Definition UniquePointer.h:106
ULANG_FORCEINLINE bool operator!() const
Definition UniquePointer.h:105
ULANG_FORCEINLINE TUPtrG(NullPtrType NullPtr=nullptr)
Definition UniquePointer.h:20
ULANG_FORCEINLINE ~TUPtrG()
Definition UniquePointer.h:24
ULANG_FORCEINLINE const TUPtrG< OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType... > & As() const
Definition UniquePointer.h:75
ULANG_FORCEINLINE TUPtrG & operator=(TUPtrG &&Other)
Definition UniquePointer.h:41
ULANG_FORCEINLINE bool operator==(const TUPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &Other) const
Definition UniquePointer.h:82
ULANG_FORCEINLINE TUPtrG & operator=(NullPtrType)
Definition UniquePointer.h:40
TUPtrG(const TUPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &Other)=delete
ULANG_FORCEINLINE const TUPtrG< ObjectType, false, AllocatorType, AllocatorArgsType... > & AsRef() const &
Definition UniquePointer.h:66
ULANG_FORCEINLINE const AllocatorType & GetAllocator() const
Definition UniquePointer.h:51
ULANG_FORCEINLINE bool operator!=(OtherObjectType *Object) const
Definition UniquePointer.h:89
TUPtrG(const TUPtrG &)=delete
ULANG_FORCEINLINE TUPtrG(TUPtrG &&Other)
Definition UniquePointer.h:21
TUPtrG & operator=(const TUPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &Other)=delete
ULANG_FORCEINLINE void Reset()
Definition UniquePointer.h:52
ULANG_FORCEINLINE TUPtrG & SetNew(CtorArgsType &&... CtorArgs)
Definition UniquePointer.h:27
ULANG_FORCEINLINE bool operator<(const TUPtrG &Other) const
Definition UniquePointer.h:90
ULANG_FORCEINLINE bool operator!=(const TUPtrG &Other) const
Definition UniquePointer.h:85
static ULANG_FORCEINLINE TUPtrG New(AllocatorArgsType &&... AllocatorArgs, CtorArgsType &&... CtorArgs)
Definition UniquePointer.h:29
ULANG_FORCEINLINE TUPtrG< OtherObjectType, AllowNull, AllocatorType, AllocatorArgsType... > & As()
Definition UniquePointer.h:73
ULANG_FORCEINLINE TUPtrG< ObjectType, false, AllocatorType, AllocatorArgsType... > & AsRef() &
Definition UniquePointer.h:54
ULANG_FORCEINLINE const TUPtrG< ObjectType, false, AllocatorType, AllocatorArgsType... > && AsRef() &&
Definition UniquePointer.h:60
friend class TUPtrG
Definition UniquePointer.h:109
ULANG_FORCEINLINE ObjectType * operator->() const
Definition UniquePointer.h:49
ULANG_FORCEINLINE bool operator==(OtherObjectType *Object) const
Definition UniquePointer.h:84
ULANG_FORCEINLINE ObjectType & operator*() const
Definition UniquePointer.h:48
ULANG_FORCEINLINE TUPtrG(TUPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &&Other)
Definition UniquePointer.h:23
ULANG_FORCEINLINE bool operator==(const TUPtrG &Other) const
Definition UniquePointer.h:80
ULANG_FORCEINLINE bool operator!=(const TUPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &Other) const
Definition UniquePointer.h:87
ULANG_FORCEINLINE ObjectType * Get() const
Definition UniquePointer.h:50
ULANG_FORCEINLINE bool operator>(const TUPtrG &Other) const
Definition UniquePointer.h:95
TUPtrG & operator=(const TUPtrG &)=delete
Definition VVMEngineEnvironment.h:23
std::nullptr_t NullPtrType
Definition Common.h:325
@ DefaultInit
Definition Common.h:378
ULANG_FORCEINLINE T && ForwardArg(typename TRemoveReference< T >::Type &Obj)
Definition References.h:115
ULANG_FORCEINLINE TRemoveReference< T >::Type && Move(T &&Obj)
Definition References.h:86
@ false
Definition radaudio_common.h:23