UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TypeCompatibleBytes.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5
6#include "CoreTypes.h"
7#include <string.h>
8#include <new>
9#include <type_traits>
10
15template<int32 Size, uint32 Alignment>
17{
18 alignas(Alignment) uint8 Pad[Size];
19};
20
22template<typename ElementType>
24{
25 using ElementTypeAlias_NatVisHelper = ElementType;
26
27 // Trivially constructible and destuctible - users are responsible for managing the lifetime of the inner element.
30
31 // Noncopyable
36
37 // GetTypedPtr only exists for backwards compatibility - these functions do not exist and cannot be implemented for the reference and void specializations.
38 ElementType* GetTypedPtr()
39 {
40 return (ElementType*)this;
41 }
42 const ElementType* GetTypedPtr() const
43 {
44 return (const ElementType*)this;
45 }
46
47 using MutableGetType = ElementType&; // The type returned by Bytes.Get() where Bytes is a non-const lvalue
48 using ConstGetType = const ElementType&; // The type returned by Bytes.Get() where Bytes is a const lvalue
49 using RvalueGetType = ElementType&&; // The type returned by Bytes.Get() where Bytes is an rvalue (non-const)
50
51 // Gets the inner element - no checks are performed to ensure an element is present.
52 ElementType& GetUnchecked() &
53 {
54 return *(ElementType*)this;
55 }
56 const ElementType& GetUnchecked() const&
57 {
58 return *(const ElementType*)this;
59 }
60 ElementType&& GetUnchecked() &&
61 {
62 return (ElementType&&)*(ElementType*)this;
63 }
64
65 // Emplaces an inner element.
66 // Note: no checks are possible to ensure that an element isn't already present. DestroyUnchecked() must be called to end the element's lifetime.
67 template <typename... ArgTypes>
68 void EmplaceUnchecked(ArgTypes&&... Args)
69 {
70 new ((void*)GetTypedPtr()) ElementType((ArgTypes&&)Args...);
71 }
72
73 // Destroys the inner element.
74 // Note: no checks are possible to ensure that there is an element already present.
76 {
78 Ptr->ElementTypeAlias_NatVisHelper::~ElementTypeAlias_NatVisHelper();
79 }
80
81 alignas(ElementType) uint8 Pad[sizeof(ElementType)];
82};
83
84template <typename T>
86{
88
89 // Trivially constructible and destuctible - users are responsible for managing the lifetime of the inner element.
92
93 // Noncopyable
98
99 using MutableGetType = T&; // The type returned by Bytes.Get() where Bytes is a non-const lvalue
100 using ConstGetType = T&; // The type returned by Bytes.Get() where Bytes is a const lvalue
101 using RvalueGetType = T&; // The type returned by Bytes.Get() where Bytes is an rvalue (non-const)
102
103 // Gets the inner element - no checks are performed to ensure an element is present.
104 T& GetUnchecked() const
105 {
106 return *Ptr;
107 }
108
109 // Emplaces an inner element.
110 // Note: no checks are possible to ensure that an element isn't already present. DestroyUnchecked() must be called to end the element's lifetime.
111 void EmplaceUnchecked(T& Ref)
112 {
113 Ptr = &Ref;
114 }
115
116 // Destroys the inner element.
117 // Note: no checks are possible to ensure that there is an element already present.
119 {
120 }
121
122 T* Ptr;
123};
124
125template <>
127{
129
130 // Trivially constructible and destuctible - users are responsible for managing the lifetime of the inner element.
133
134 // Noncopyable
139
140 using MutableGetType = void; // The type returned by Bytes.Get() where Bytes is a non-const lvalue
141 using ConstGetType = void; // The type returned by Bytes.Get() where Bytes is a const lvalue
142 using RvalueGetType = void; // The type returned by Bytes.Get() where Bytes is an rvalue (non-const)
143
144 // Gets the inner element - no checks are performed to ensure an element is present.
145 void GetUnchecked() const
146 {
147 }
148
149 // Emplaces an inner element.
150 // Note: no checks are possible to ensure that an element isn't already present. DestroyUnchecked() must be called to end the element's lifetime.
152 {
153 }
154
155 // Destroys the inner element.
156 // Note: no checks are possible to ensure that there is an element already present.
158 {
159 }
160};
161
162template <
163 typename ToType,
164 typename FromType,
165 std::enable_if_t<sizeof(ToType) == sizeof(FromType) && std::is_trivially_copyable_v<ToType> && std::is_trivially_copyable_v<FromType>>* = nullptr
166>
167inline ToType BitCast(const FromType& From)
168{
169// Ensure we can use this builtin - seems to be present on Clang 9, GCC 11 and MSVC 19.26,
170// but gives spurious "non-void function 'BitCast' should return a value" errors on some
171// Mac and Android toolchains when building PCHs, so avoid those.
172// However, there is a bug in the Clang static analyzer with this builtin: https://github.com/llvm/llvm-project/issues/69922
173// Don't use it when performing static analysis until the bug is fixed.
174#if !defined(__clang_analyzer__) && PLATFORM_COMPILER_SUPPORTS_BUILTIN_BITCAST // can consider replacing with __has_builtin(__builtin_bit_cast) once there's no special cases
175 return __builtin_bit_cast(ToType, From);
176#else
178 memcpy(&Result, &From, sizeof(ToType));
179 return *Result.GetTypedPtr();
180#endif
181}
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
ToType BitCast(const FromType &From)
Definition TypeCompatibleBytes.h:167
uint32 Size
Definition VulkanMemory.cpp:4034
memcpy(InputBufferBase, BinkBlocksData, BinkBlocksSize)
uint8_t uint8
Definition binka_ue_file_header.h:8
Type ToType(const FString &Affiliation)
Definition XmppMultiUserChat.h:55
Definition TypeCompatibleBytes.h:17
uint8 Pad[Size]
Definition TypeCompatibleBytes.h:18
TTypeCompatibleBytes(const TTypeCompatibleBytes &)=delete
TTypeCompatibleBytes & operator=(TTypeCompatibleBytes &&)=delete
T & ElementTypeAlias_NatVisHelper
Definition TypeCompatibleBytes.h:87
void DestroyUnchecked()
Definition TypeCompatibleBytes.h:118
TTypeCompatibleBytes & operator=(const TTypeCompatibleBytes &)=delete
T * Ptr
Definition TypeCompatibleBytes.h:122
T & RvalueGetType
Definition TypeCompatibleBytes.h:101
TTypeCompatibleBytes(TTypeCompatibleBytes &&)=delete
T & ConstGetType
Definition TypeCompatibleBytes.h:100
void EmplaceUnchecked(T &Ref)
Definition TypeCompatibleBytes.h:111
T & GetUnchecked() const
Definition TypeCompatibleBytes.h:104
T & MutableGetType
Definition TypeCompatibleBytes.h:99
void DestroyUnchecked()
Definition TypeCompatibleBytes.h:157
void EmplaceUnchecked()
Definition TypeCompatibleBytes.h:151
void RvalueGetType
Definition TypeCompatibleBytes.h:142
TTypeCompatibleBytes & operator=(const TTypeCompatibleBytes &)=delete
TTypeCompatibleBytes & operator=(TTypeCompatibleBytes &&)=delete
void ElementTypeAlias_NatVisHelper
Definition TypeCompatibleBytes.h:128
void GetUnchecked() const
Definition TypeCompatibleBytes.h:145
TTypeCompatibleBytes(TTypeCompatibleBytes &&)=delete
TTypeCompatibleBytes(const TTypeCompatibleBytes &)=delete
void ConstGetType
Definition TypeCompatibleBytes.h:141
void MutableGetType
Definition TypeCompatibleBytes.h:140
Definition TypeCompatibleBytes.h:24
TTypeCompatibleBytes & operator=(const TTypeCompatibleBytes &)=delete
ElementType & GetUnchecked() &
Definition TypeCompatibleBytes.h:52
ElementType && GetUnchecked() &&
Definition TypeCompatibleBytes.h:60
ElementType ElementTypeAlias_NatVisHelper
Definition TypeCompatibleBytes.h:25
const ElementType & GetUnchecked() const &
Definition TypeCompatibleBytes.h:56
ElementType & MutableGetType
Definition TypeCompatibleBytes.h:47
TTypeCompatibleBytes(const TTypeCompatibleBytes &)=delete
ElementType * GetTypedPtr()
Definition TypeCompatibleBytes.h:38
TTypeCompatibleBytes(TTypeCompatibleBytes &&)=delete
const ElementType & ConstGetType
Definition TypeCompatibleBytes.h:48
TTypeCompatibleBytes()=default
void DestroyUnchecked()
Definition TypeCompatibleBytes.h:75
~TTypeCompatibleBytes()=default
ElementType && RvalueGetType
Definition TypeCompatibleBytes.h:49
const ElementType * GetTypedPtr() const
Definition TypeCompatibleBytes.h:42
void EmplaceUnchecked(ArgTypes &&... Args)
Definition TypeCompatibleBytes.h:68
uint8 Pad[sizeof(ElementType)]
Definition TypeCompatibleBytes.h:81
TTypeCompatibleBytes & operator=(TTypeCompatibleBytes &&)=delete