UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MemoryWriter.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#include "HAL/UnrealMemory.h"
10#include "UObject/NameTypes.h"
11#include "Logging/LogMacros.h"
12#include "CoreGlobals.h"
14
19template <typename ArrayAllocatorType>
21{
22 using IndexSizeType = typename ArrayAllocatorType::SizeType;
23 static constexpr int32 IndexSize = sizeof(IndexSizeType) * 8;
24
25public:
26 TMemoryWriterBase( TArray<uint8, ArrayAllocatorType>& InBytes, bool bIsPersistent = false, bool bSetOffset = false, const FName InArchiveName = NAME_None )
28 , Bytes(InBytes)
30 {
31 this->SetIsSaving(true);
32 this->SetIsPersistent(bIsPersistent);
33 if (bSetOffset)
34 {
35 Offset = InBytes.Num();
36 }
37 }
38
39 virtual void Serialize(void* Data, int64 Num) override
40 {
41 const int64 NumBytesToAdd = Offset + Num - Bytes.Num();
42 if( NumBytesToAdd > 0 )
43 {
45 if constexpr (IndexSize < 64)
46 {
47 constexpr IndexSizeType MaxValue = TNumericLimits<IndexSizeType>::Max();
48 if (NewArrayCount >= MaxValue)
49 {
50 UE_LOG(LogSerialization, Fatal, TEXT("TMemoryWriter with IndexSize=%d does not support data larger than %" INT64_FMT " bytes. Archive name: %s."), IndexSize, (int64)MaxValue, *ArchiveName.ToString());
51 }
52 }
53
54 Bytes.AddUninitialized((IndexSizeType)NumBytesToAdd);
55 }
56
57 check((Offset + Num) <= Bytes.Num());
58
59 if( Num )
60 {
61 FMemory::Memcpy(&Bytes[(IndexSizeType)Offset], Data, Num);
62 Offset += Num;
63 }
64 }
71 virtual FString GetArchiveName() const override
72 {
74 {
75 return ArchiveName.ToString();
76 }
77
78 return FString::Printf(TEXT("FMemoryWriter%d"), IndexSize);
79 }
80
81 int64 TotalSize() override
82 {
83 return Bytes.Num();
84 }
85
86protected:
87
89
92};
93
94template <int IndexSize>
96
97// FMemoryWriter and FMemoryWriter64 are implemented as derived classes rather than aliases
98// so that forward declarations will work.
99
100class FMemoryWriter : public TMemoryWriter<32>
101{
102 using Super = TMemoryWriter<32>;
103
104public:
105 using Super::Super;
106};
107
109{
110 using Super = TMemoryWriter<64>;
111
112public:
113 using Super::Super;
114};
#define INT64_FMT
Definition AndroidPlatformString.h:59
#define check(expr)
Definition AssertionMacros.h:314
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
@ Num
Definition MetalRHIPrivate.h:234
virtual CORE_API void SetIsSaving(bool bInIsSaving)
Definition Archive.cpp:1523
virtual CORE_API void SetIsPersistent(bool bInIsPersistent)
Definition Archive.cpp:1553
Definition MemoryArchive.h:15
int64 Offset
Definition MemoryArchive.h:68
Definition MemoryWriter.h:109
Definition MemoryWriter.h:101
Definition NameTypes.h:617
CORE_API FString ToString() const
Definition UnrealNames.cpp:3537
Definition Array.h:670
UE_FORCEINLINE_HINT SizeType AddUninitialized()
Definition Array.h:1664
UE_REWRITE SizeType Num() const
Definition Array.h:1144
Definition MemoryWriter.h:21
int64 TotalSize() override
Definition MemoryWriter.h:81
const FName ArchiveName
Definition MemoryWriter.h:91
virtual void Serialize(void *Data, int64 Num) override
Definition MemoryWriter.h:39
TMemoryWriterBase(TArray< uint8, ArrayAllocatorType > &InBytes, bool bIsPersistent=false, bool bSetOffset=false, const FName InArchiveName=NAME_None)
Definition MemoryWriter.h:26
virtual FString GetArchiveName() const override
Definition MemoryWriter.h:71
TArray< uint8, ArrayAllocatorType > & Bytes
Definition MemoryWriter.h:88
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160
Definition NumericLimits.h:41