7#define WITH_SHARED_POINTER_TESTS 0
&& !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
11enum class ESPMode : uint8;
12template<
typename KeyType,
typename ValueType,
typename SetAllocator ,
typename KeyFuncs >
class TMap;
15
16
17namespace SharedPointerTesting
20
21
22
23 template< ESPMode Mode >
24 static void TestSharedPointer()
28 TSharedPtr<
bool, Mode > MyEmptyBoolPtr;
31 check( !MyEmptyBoolPtr.IsValid() );
34 if( MyEmptyBoolPtr.Get() ==
nullptr )
42 TSharedPtr< int32, Mode > MyIntSharedPtr(
new int32( 123 ) );
45 check( MyIntSharedPtr.IsValid() );
48 check( MyIntSharedPtr.IsUnique() );
51 const int32 DeferenceTest = *MyIntSharedPtr;
54 MyIntSharedPtr.Reset();
58 check( MyIntSharedPtr.GetSharedReferenceCount() == 0 );
66 TSharedPtr<
double, Mode > MyBool = 45.0;
72 TSharedPtr<
bool, Mode > FirstBoolRef(
new bool(
false ) );
73 TSharedPtr<
bool, Mode > SecondBoolRef( FirstBoolRef );
78 TSharedPtr<
bool, Mode > FirstBoolRef(
new bool(
false ) );
79 TSharedPtr<
bool, Mode > SecondBoolRef = FirstBoolRef;
88 TSharedPtr< FSharedTest, Mode > SharedArray(
new FSharedTest() );
89 SharedArray->bFoo =
true;
92 ( *SharedArray ).bFoo =
false;
95 TSharedPtr< FSharedTest, Mode > OtherSharedArrayReference( SharedArray );
115 TSharedPtr< FBase, Mode > DerivedAsBasePtr(
new FDerived() );
116 TSharedPtr< FDerived, Mode > DerivedPtr( StaticCastSharedPtr< FDerived >( DerivedAsBasePtr ) );
121 TSharedPtr< FDerived, Mode > DerivedPtr(
new FDerived() );
122 TSharedPtr< FBase, Mode > BasePtr( DerivedPtr );
127 TSharedPtr< FDerived, Mode > DerivedPtr(
new FDerived() );
128 TSharedPtr< FBase, Mode > BasePtr = DerivedPtr;
136 TSharedPtr<
bool, Mode > NullPtr( Foo );
137 check( !NullPtr.IsValid() );
142 TSharedPtr<
bool, Mode > BoolPtr(
new bool(
true ) );
143 check( BoolPtr.IsValid() );
148 TWeakPtr<
bool, Mode > EmptyBoolWeakPtr;
151 check( !EmptyBoolWeakPtr.Pin().IsValid() );
156 TSharedPtr< int32, Mode > SharedInt(
new int32( 64 ) );
157 TWeakPtr< int32, Mode > WeakInt( SharedInt );
160 check( WeakInt.Pin().IsValid() );
165 TSharedPtr< int32, Mode > SharedInt(
new int32( 64 ) );
166 TWeakPtr< int32, Mode > WeakInt = SharedInt;
169 check( WeakInt.Pin().IsValid() );
173 check( !WeakInt.Pin().IsValid() );
178 TSharedPtr< int32, Mode > SharedInt(
new int32( 64 ) );
179 TWeakPtr< int32, Mode > WeakInt = SharedInt;
181 check( !WeakInt.Pin().IsValid() );
186 TSharedPtr< int32, Mode > SharedA(
new int32( 64 ) );
187 TSharedPtr< int32, Mode > SharedB(
new int32( 21 ) );
188 TSharedPtr< int32, Mode > SharedC( SharedB );
190 check( !( SharedA == SharedB ) );
191 check( SharedA != SharedB );
192 check( SharedB == SharedC );
197 TSharedPtr< int32, Mode > SharedA(
new int32( 64 ) );
198 TSharedPtr< int32, Mode > SharedB(
new int32( 21 ) );
200 TWeakPtr< int32, Mode > WeakA( SharedA );
201 TWeakPtr< int32, Mode > WeakB( SharedB );
202 TWeakPtr< int32, Mode > WeakC( SharedB );
205 check( !( WeakA.Pin() == WeakB.Pin() ) );
206 check( WeakA.Pin() != WeakB.Pin() );
207 check( WeakB.Pin() == WeakC.Pin() );
213 check( !( WeakA == WeakB ) );
214 check( WeakA != WeakB );
215 check( WeakB == WeakC );
222 TSharedPtr<
const int32, Mode > IntPtr(
new int32( 10 ) );
223 TSharedPtr<
const float, Mode > FloatPtrA(
new float( 1.0f ) );
224 TSharedPtr<
const float, Mode > FloatPtrB(
new float( 2.0f ) );
226 if( FloatPtrA == FloatPtrB )
231 if( FloatPtrB == IntPtr )
237 FloatPtrA = FloatPtrB;
240 TSharedPtr<
float, Mode > MutableFloat(
new float( 123.0f ) );
242 MutableFloat = FloatPtrA;
246 FloatPtrA = MutableFloat;
248 if( FloatPtrB.IsValid() )
255 TWeakPtr<
const float, Mode > ConstWeakFloat = FloatPtrA;
257 *ConstWeakFloat.Pin() = 20.0f;
261 TWeakPtr<
float, Mode > WeakFloat;
263 WeakFloat = FloatPtrB;
267 WeakFloat = ConstCastSharedPtr<
float >( FloatPtrB );
269 *WeakFloat.Pin() = 20.0f;
274 TSharedPtr<
struct FBarFoo, Mode > VecPtr;
279 VecPtr = TSharedPtr< FBarFoo, Mode >(
new FBarFoo() );
286 TSharedPtr<
bool, Mode > EmptyPtr(
nullptr );
287 TSharedPtr<
float, Mode > FloatPtr =
nullptr;
290 TWeakPtr<
bool, Mode > EmptyWeakPtr(
nullptr );
291 TWeakPtr<
float, Mode > FloatWeakPtr =
nullptr;
294 FloatPtr = TSharedPtr<
float, Mode >(
new float( 0.1f ) );
298 FloatPtr = MakeShareable(
new float( 30.0f ) );
299 TSharedPtr<
double, Mode >( MakeShareable(
new double( 2.0 ) ) );
304 TSharedPtr<
float, Mode > GetFloatMember()
309 TSharedPtr<
float, Mode > FloatVal;
313 TSharedPtr<
float, Mode > GetFloatValue()
315 return MakeShareable(
new float( 123.0f ) );
325 TSharedRef<
bool, Mode > EmptyRef;
329 TSharedRef<
bool, Mode > NullRef =
nullptr;
334 TSharedRef<
float, Mode > FloatRef(
new float( 123.0f ) );
339 TSharedRef<
float, Mode > FloatRef(
new float( 123.0f ) );
342 const float& MyFloat = *FloatRef;
345 const float& MyFloat2 = FloatRef.Get();
351 TSharedRef<
float, Mode > FloatRef =
new float( 123.0f );
357 TSharedRef<
float, Mode > FloatRef = MakeShareable(
new float( 123.0f ) );
365 int32* NullInt =
nullptr;
366 TSharedRef< int32, Mode > RefWithNullObject( NullInt );
371 TSharedRef< int32, Mode > MySharedRef(
new int32( 1 ) );
372 TSharedPtr< int32, Mode > MySharedPtr( MySharedRef );
377 TSharedPtr< int32, Mode > MySharedPtr(
new int32( 1 ) );
379 TSharedRef< int32, Mode > MySharedRef( MySharedPtr );
385 TSharedPtr< int32, Mode > MySharedPtr(
new int32( 1 ) );
387 TSharedRef< int32, Mode > MySharedRef = MySharedPtr;
393 TSharedPtr< int32, Mode > MySharedPtr(
new int32( 1 ) );
394 TSharedRef< int32, Mode > MySharedRef( MySharedPtr.ToSharedRef() );
400 int32* NullInt =
nullptr;
401 TSharedPtr< int32, Mode > MySharedPtr( NullInt );
402 TSharedRef< int32, Mode > MySharedRef( MySharedPtr.ToSharedRef() );
407 TSharedRef< int32, Mode > IntRef(
new int32( 10 ) );
408 IntRef = TSharedRef< int32, Mode >(
new int32( 20 ) );
414 TSharedRef< int32, Mode > IntRef(
new int32( 10 ) );
415 int32* NullInt =
nullptr;
416 IntRef = TSharedRef< int32, Mode >( NullInt );
421 TSharedRef< int32, Mode > IntRef(
new int32( 99 ) );
422 TWeakPtr< int32, Mode > WeakInt = IntRef;
423 if( WeakInt.IsValid() )
431 TSharedRef< int32, Mode > IntRef1(
new int32( 99 ) );
432 TSharedRef< int32, Mode > IntRef2(
new int32( 21 ) );
433 if( IntRef1 == IntRef2 )
437 if( IntRef1 != IntRef2 )
446 TSharedRef< int32, Mode > IntRef(
new int32( 21 ) );
447 TSharedPtr< int32, Mode > IntPtr( IntRef );
450 check( IntRef == IntPtr && IntPtr == IntRef );
456 check( !( IntRef != IntPtr || IntPtr != IntRef ) );
462 TSharedPtr< int32, Mode > NullPtr;
463 check( !( IntRef == NullPtr ) && ( IntRef != NullPtr ) );
474 :
public TSharedFromThis< FMyClass, Mode >
477 TSharedRef< FMyClass, Mode > GetSelfAsShared()
488 TSharedRef< FMyClass, Mode > TheClassPtr( MyClass.GetSelfAsShared() );
491 TSharedPtr< FMyClass, Mode > TheClassPtr1(
new FMyClass() );
493 FMyClass* MyClass = TheClassPtr1.Get();
494 TSharedRef< FMyClass, Mode > TheClassPtr2( MyClass->GetSelfAsShared() );
500 TSharedRef< int32, Mode > FooRef(
new int32( 1 ) );
501 TSharedPtr< int32, Mode > FooPtr( FooRef );
504 TMap< TSharedPtr< int32, Mode >,
bool > SharedPointerHash;
505 SharedPointerHash.Add( FooPtr,
true );
508 TMap< TSharedRef< int32, Mode >,
bool > SharedRefHash;
509 SharedRefHash.Add( FooRef,
true );
510 const bool Value = SharedRefHash.FindRef( FooRef );
514 TMap< int32, TSharedRef< int32, Mode > > SharedRefValueHash;
515 SharedRefValueHash.Add(10, FooRef);
516 const int32* FoundKey = SharedRefValueHash.FindKey(FooRef);
517 check(FoundKey !=
nullptr && *FoundKey == 10);
522 TSharedRef<
const int32, Mode > ConstFooRef(
new int32( 1 ) );
523 TMap< int32, TSharedRef<
const int32, Mode > > ConstSharedRefValueHash;
524 ConstSharedRefValueHash.Add( 10, ConstFooRef );
525 const int32* FoundKey = ConstSharedRefValueHash.FindKey( ConstFooRef );
526 check( FoundKey !=
nullptr && *FoundKey == 10 );
#define WITH_SHARED_POINTER_TESTS