UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UniqueObj.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
7
8// This is essentially a reference version of TUniquePtr
9// Useful to force heap allocation of a value, e.g. in a TMap to give similar behaviour to TIndirectArray
10
11template <typename T>
13{
14public:
15 using ElementType = T;
16
18 : Obj(MakeUnique<T>(*other))
19 {
20 }
21
22 // As TUniqueObj's internal pointer can never be null, we can't move that
23 // On the other hand we can call the move constructor of "T"
25 : Obj(MakeUnique<T>(MoveTemp(*other)))
26 {
27 }
28
29 template <typename... Args>
30 explicit TUniqueObj(Args&&... args)
31 : Obj(MakeUnique<T>(Forward<Args>(args)...))
32 {
33 }
34
35 // Disallow copy-assignment
36 TUniqueObj& operator=(const TUniqueObj&) = delete;
37
38 // Move-assignment is implemented as swapping the internal pointer
40 {
41 Swap(Obj, other.Obj);
42 return *this;
43 }
44
45 template <typename Arg>
47 {
48 *Obj = Forward<Arg>(other);
49 return *this;
50 }
51
52 T& Get() { return *Obj; }
53 const T& Get() const { return *Obj; }
54
55 T* operator->() { return Obj.Get(); }
56 const T* operator->() const { return Obj.Get(); }
57
58 T& operator*() { return *Obj; }
59 const T& operator*() const { return *Obj; }
60
62 {
63 Ar << *Obj;
64 }
65
66private:
67 TUniquePtr<T> Obj;
68};
69
70template <typename T>
72{
73 P.Serialize(Ar);
74 return Ar;
75}
76
80template <typename T> constexpr bool TIsTUniqueObj_V = false;
81template <typename T> constexpr bool TIsTUniqueObj_V< TUniqueObj<T>> = true;
82template <typename T> constexpr bool TIsTUniqueObj_V<const TUniqueObj<T>> = true;
83template <typename T> constexpr bool TIsTUniqueObj_V< volatile TUniqueObj<T>> = true;
84template <typename T> constexpr bool TIsTUniqueObj_V<const volatile TUniqueObj<T>> = true;
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
constexpr bool TIsTUniqueObj_V
Definition UniqueObj.h:80
FArchive & operator<<(FArchive &Ar, TUniqueObj< T > &P)
Definition UniqueObj.h:71
UE_FORCEINLINE_HINT TUniquePtr< T > MakeUnique(TArgs &&... Args)
Definition UniquePtr.h:918
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition Archive.h:1208
Definition UniqueObj.h:13
TUniqueObj(TUniqueObj &&other)
Definition UniqueObj.h:24
T & Get()
Definition UniqueObj.h:52
TUniqueObj & operator=(Arg &&other)
Definition UniqueObj.h:46
TUniqueObj & operator=(const TUniqueObj &)=delete
const T & Get() const
Definition UniqueObj.h:53
TUniqueObj & operator=(TUniqueObj &&other)
Definition UniqueObj.h:39
void Serialize(FArchive &Ar)
Definition UniqueObj.h:61
T * operator->()
Definition UniqueObj.h:55
T ElementType
Definition UniqueObj.h:15
T & operator*()
Definition UniqueObj.h:58
const T * operator->() const
Definition UniqueObj.h:56
const T & operator*() const
Definition UniqueObj.h:59
TUniqueObj(Args &&... args)
Definition UniqueObj.h:30
TUniqueObj(const TUniqueObj &other)
Definition UniqueObj.h:17
Definition UniquePtr.h:107