UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ObserverPointer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7
8#ifndef ULANG_CHECK_OBSERVER_POINTERS
9 #define ULANG_CHECK_OBSERVER_POINTERS ULANG_DO_CHECK
10#endif
11
12namespace uLang
13{
14
15#if ULANG_CHECK_OBSERVER_POINTERS
16
17template<class ObjectType> class TOPtr;
18
23class CObservedMix
24{
25public:
26
27 CObservedMix() : _ObserverId(ObserverId_Null) {}
29
30private:
31
32 template<class ObjectType> friend class TOPtr;
33
35
36};
37
41template<class ObjectType>
42class TOPtr
43{
44public:
45
46 // Construction
47
49 ULANG_FORCEINLINE TOPtr(const TOPtr & Other) : _Object(Other._Object), _ObserverId(Other._ObserverId) {}
52 template<class OtherObjectType, bool OtherAllowNull, class AllocatorType, typename... AllocatorArgsType, typename = typename TEnableIf<TPointerIsConvertibleFromTo<OtherObjectType, ObjectType>::Value>::Type>
53 ULANG_FORCEINLINE TOPtr(const TSPtrG<OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType...> & SharedPtr) : _Object(SharedPtr.Get()), _ObserverId(GetId(SharedPtr.Get(), SharedPtr.GetAllocator())) {}
54 template<class OtherObjectType, bool OtherAllowNull, class AllocatorType, typename... AllocatorArgsType, typename = typename TEnableIf<TPointerIsConvertibleFromTo<OtherObjectType, ObjectType>::Value>::Type>
56
57 // Assignment
58
59 ULANG_FORCEINLINE TOPtr & operator=(const TOPtr & Other) { _Object = Other._Object; _ObserverId = Other._ObserverId; return *this; }
62 template<class OtherObjectType, bool OtherAllowNull, class AllocatorType, typename... AllocatorArgsType, typename = typename TEnableIf<TPointerIsConvertibleFromTo<OtherObjectType, ObjectType>::Value>::Type>
63 ULANG_FORCEINLINE TOPtr & operator=(const TSPtrG<OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType...> & SharedPtr) { _Object = SharedPtr.Get(); _ObserverId = GetId(SharedPtr.Get(), SharedPtr.GetAllocator()); return *this; }
64 template<class OtherObjectType, bool OtherAllowNull, class AllocatorType, typename... AllocatorArgsType, typename = typename TEnableIf<TPointerIsConvertibleFromTo<OtherObjectType, ObjectType>::Value>::Type>
66
67 // Conversion methods
68
69 ULANG_FORCEINLINE operator ObjectType*() const { return Get(); }
70 ULANG_FORCEINLINE ObjectType & operator*() const { return *Get(); }
71 ULANG_FORCEINLINE ObjectType * operator->() const { return Get(); }
72 ULANG_FORCEINLINE ObjectType * Get() const { ULANG_ASSERTF(!_Object || _ObserverId == _Object->_ObserverId, "Observed object has been deleted!"); return _Object; }
74
75 // Comparison operators
76
77 ULANG_FORCEINLINE bool operator==(const TOPtr & Other) const { return (_Object == Other._Object); }
78 ULANG_FORCEINLINE bool operator!=(const TOPtr & Other) const { return (_Object != Other._Object); }
79 template<class OtherObjectType>
80 ULANG_FORCEINLINE bool operator==(const TOPtr<OtherObjectType> & Other) const { return (_Object == Other._Object); }
81 template<class OtherObjectType>
82 ULANG_FORCEINLINE bool operator!=(const TOPtr<OtherObjectType> & Other) const { return (_Object != Other._Object); }
83 template<class OtherObjectType>
85 template<class OtherObjectType>
87
88 // Validation methods
89
90 ULANG_FORCEINLINE bool IsValid() const { return (_Object && (_ObserverId == _Object->_ObserverId)); }
91 ULANG_FORCEINLINE bool IsStale() const { return (_Object && (_ObserverId != _Object->_ObserverId)); }
92 ULANG_FORCEINLINE bool IsNull() const { return !_Object; }
93 ULANG_FORCEINLINE bool IsSet() const { return (_Object != nullptr); }
94 ULANG_FORCEINLINE operator bool() const { return IsValid(); }
95 ULANG_FORCEINLINE bool operator !() const { return IsNull(); }
96
97 // Id Accessor
98
100
101protected:
102 template<class OtherObjectType> friend class TOPtr;
103
104 template<class AllocatorType>
105 ULANG_FORCEINLINE static EObserverId GetId(const ObjectType * Object, const AllocatorType & Allocator)
106 {
107 EObserverId ObserverId = Object->_ObserverId;
108 if (ObserverId == ObserverId_Null)
109 {
110 Object->_ObserverId = ObserverId = Allocator.GenerateObserverId();
111 }
112 return ObserverId;
113 }
114
117 ObjectType * _Object;
118
121
122};
123
124#else // !ULANG_CHECK_OBSERVER_POINTERS
125
130{
131};
132
136template<class ObjectType>
137class TOPtr
138{
139public:
140
141 // Construction
142
147 template<class OtherObjectType, bool OtherAllowNull, class AllocatorType, typename... AllocatorArgsType, typename = typename TEnableIf<TPointerIsConvertibleFromTo<OtherObjectType, ObjectType>::Value>::Type>
149 template<class OtherObjectType, bool OtherAllowNull, class AllocatorType, typename... AllocatorArgsType, typename = typename TEnableIf<TPointerIsConvertibleFromTo<OtherObjectType, ObjectType>::Value>::Type>
151
152 // Assignment
153
157 template<class OtherObjectType, bool OtherAllowNull, class AllocatorType, typename... AllocatorArgsType, typename = typename TEnableIf<TPointerIsConvertibleFromTo<OtherObjectType, ObjectType>::Value>::Type>
159 template<class OtherObjectType, bool OtherAllowNull, class AllocatorType, typename... AllocatorArgsType, typename = typename TEnableIf<TPointerIsConvertibleFromTo<OtherObjectType, ObjectType>::Value>::Type>
161
162 // Conversion methods
163
164 ULANG_FORCEINLINE operator ObjectType*() const { return _Object; }
165 ULANG_FORCEINLINE ObjectType & operator*() const { return *_Object; }
166 ULANG_FORCEINLINE ObjectType * operator->() const { return _Object; }
167 ULANG_FORCEINLINE ObjectType * Get() const { return _Object; }
168 ULANG_FORCEINLINE void Reset() { _Object = nullptr; }
169
170 // Comparison operators
171
172 ULANG_FORCEINLINE bool operator==(const TOPtr & Other) const { return (_Object == Other._Object); }
173 ULANG_FORCEINLINE bool operator!=(const TOPtr & Other) const { return (_Object != Other._Object); }
174 template<class OtherObjectType>
175 ULANG_FORCEINLINE bool operator==(const TOPtr<OtherObjectType> & Other) const { return (_Object == Other._Object); }
176 template<class OtherObjectType>
177 ULANG_FORCEINLINE bool operator!=(const TOPtr<OtherObjectType> & Other) const { return (_Object != Other._Object); }
178 ULANG_FORCEINLINE bool operator==(const ObjectType * Object) const { return (_Object == Object); }
179 ULANG_FORCEINLINE bool operator!=(const ObjectType * Object) const { return (_Object != Object); }
180
181 // Validation methods
182
183 ULANG_FORCEINLINE bool IsValid() const { return _Object != nullptr; }
184 ULANG_FORCEINLINE bool IsStale() const { return false; }
185 ULANG_FORCEINLINE bool IsNull() const { return _Object == nullptr; }
186 ULANG_FORCEINLINE bool IsSet() const { return _Object != nullptr; }
187 ULANG_FORCEINLINE operator bool() const { return IsValid(); }
188 ULANG_FORCEINLINE bool operator !() const { return IsNull(); }
189
190protected:
191 template<class OtherObjectType> friend class TOPtr;
192
194 ObjectType * _Object;
195
196};
197
198
199#endif
200
201}
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 EnableIf.h:20
Definition ObserverPointer.h:130
Definition Conditionals.h:95
Definition ObserverPointer.h:138
ULANG_FORCEINLINE bool operator==(const ObjectType *Object) const
Definition ObserverPointer.h:178
ULANG_FORCEINLINE void Reset()
Definition ObserverPointer.h:168
ObjectType * _Object
Direct pointer to actual object.
Definition ObserverPointer.h:194
friend class TOPtr
Definition ObserverPointer.h:191
ULANG_FORCEINLINE bool IsValid() const
Definition ObserverPointer.h:183
ULANG_FORCEINLINE TOPtr & operator=(const TOPtr &Other)
Definition ObserverPointer.h:154
ULANG_FORCEINLINE ObjectType * operator->() const
Definition ObserverPointer.h:166
ULANG_FORCEINLINE bool operator!=(const TOPtr< OtherObjectType > &Other) const
Definition ObserverPointer.h:177
ULANG_FORCEINLINE TOPtr()
Definition ObserverPointer.h:143
ULANG_FORCEINLINE TOPtr & operator=(const TUPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &UniquePtr)
Definition ObserverPointer.h:160
ULANG_FORCEINLINE TOPtr(const TUPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &UniquePtr)
Definition ObserverPointer.h:150
ULANG_FORCEINLINE bool IsNull() const
Definition ObserverPointer.h:185
ULANG_FORCEINLINE bool operator!=(const ObjectType *Object) const
Definition ObserverPointer.h:179
ULANG_FORCEINLINE TOPtr & operator=(const TSPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &SharedPtr)
Definition ObserverPointer.h:158
ULANG_FORCEINLINE ObjectType & operator*() const
Definition ObserverPointer.h:165
ULANG_FORCEINLINE bool IsStale() const
Definition ObserverPointer.h:184
ULANG_FORCEINLINE bool operator==(const TOPtr< OtherObjectType > &Other) const
Definition ObserverPointer.h:175
ULANG_FORCEINLINE bool operator==(const TOPtr &Other) const
Definition ObserverPointer.h:172
ULANG_FORCEINLINE TOPtr(const TOPtr< OtherObjectType > &Other)
Definition ObserverPointer.h:146
ULANG_FORCEINLINE bool operator!() const
Definition ObserverPointer.h:188
ULANG_FORCEINLINE bool operator!=(const TOPtr &Other) const
Definition ObserverPointer.h:173
ULANG_FORCEINLINE bool IsSet() const
Definition ObserverPointer.h:186
ULANG_FORCEINLINE ObjectType * Get() const
Definition ObserverPointer.h:167
ULANG_FORCEINLINE TOPtr(const TSPtrG< OtherObjectType, OtherAllowNull, AllocatorType, AllocatorArgsType... > &SharedPtr)
Definition ObserverPointer.h:148
ULANG_FORCEINLINE TOPtr & operator=(const TOPtr< OtherObjectType > &Other)
Definition ObserverPointer.h:156
ULANG_FORCEINLINE TOPtr(const TOPtr &Other)
Definition ObserverPointer.h:144
Definition SharedPointer.h:77
ULANG_FORCEINLINE ObjectType * Get() const
Definition SharedPointer.h:111
Definition UniquePointer.h:15
Definition VVMEngineEnvironment.h:23
EObserverId
Id type for observer pointers.
Definition Allocator.h:15
@ ObserverId_Null
Definition Allocator.h:16