UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RelativePtr.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include <limits>
7#include <type_traits>
8
9template<typename T, typename OffsetType = int16>
11{
12 static constexpr OffsetType NullValue = std::numeric_limits<OffsetType>::max();
13
14 static_assert(std::is_integral_v<OffsetType>, "Offset type must be a signed integer");
15
16 explicit TRelativePtr()
17 : Offset(NullValue)
18 {}
19
21 : Offset(NullValue)
22 {}
23
24 explicit TRelativePtr(const void* BasePtr, const void* ThisPtr)
25 {
27 }
28
29 explicit operator bool() const
30 {
31 return Offset != NullValue;
32 }
33
34 T* Resolve(const void* BasePtr) const
35 {
36 return Offset == NullValue ? nullptr : reinterpret_cast<T*>(static_cast<uint8*>(const_cast<void*>(BasePtr)) + Offset);
37 }
38
39 template<typename U>
40 U* Resolve(const void* BasePtr) const
41 {
42 return static_cast<U*>(Resolve(BasePtr));
43 }
44
46 {
47 Offset = NullValue;
48 }
49
50 void Reset(const void* BasePtr, const void* ThisPtr)
51 {
52 const uintptr_t Base = reinterpret_cast<uintptr_t>(BasePtr);
53 const uintptr_t This = reinterpret_cast<uintptr_t>(ThisPtr);
54 if (This >= Base)
55 {
56 const uintptr_t OffsetPtr = This - Base;
57 checkf(OffsetPtr >= 0 && OffsetPtr < std::numeric_limits<OffsetType>::max(),
58 TEXT("Attempting to create a relative pointer outside the bounds of its capacity."));
59 Offset = static_cast<OffsetType>(OffsetPtr);
60 }
61 else if constexpr (std::is_signed_v<OffsetType>)
62 {
63 const uintptr_t OffsetPtr = Base - This;
64 checkf(OffsetPtr >= 0 && OffsetPtr < std::numeric_limits<OffsetType>::max(),
65 TEXT("Attempting to create a relative pointer outside the bounds of its capacity."));
66 Offset = -static_cast<OffsetType>(OffsetPtr);
67 }
68 else
69 {
70 checkf(false, TEXT("Attempting to assign a negative offset to an unsigned relative pointer!!"));
71 }
72 }
73private:
74
75 OffsetType Offset;
76};
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TYPE_OF_NULLPTR TYPE_OF_NULLPTR
The type of the C++ nullptr keyword.
Definition Platform.h:1157
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
const bool
Definition NetworkReplayStreaming.h:178
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition RelativePtr.h:11
T * Resolve(const void *BasePtr) const
Definition RelativePtr.h:34
U * Resolve(const void *BasePtr) const
Definition RelativePtr.h:40
void Reset(const void *BasePtr, const void *ThisPtr)
Definition RelativePtr.h:50
TRelativePtr()
Definition RelativePtr.h:16
static constexpr OffsetType NullValue
Definition RelativePtr.h:12
void Reset(TYPE_OF_NULLPTR)
Definition RelativePtr.h:45
TRelativePtr(TYPE_OF_NULLPTR)
Definition RelativePtr.h:20
TRelativePtr(const void *BasePtr, const void *ThisPtr)
Definition RelativePtr.h:24