UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VarargsHelper.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"
6#include "Misc/CString.h"
7#include "HAL/UnrealMemory.h"
8
9#include <cstdarg>
10
11template <typename SerializeFuncType>
13{
14 // Allocate some stack space to use on the first pass. This should be sufficient for most strings.
15 constexpr int DefaultBufferSize = 512;
16
17 TCHAR StackBuffer[DefaultBufferSize];
18 TCHAR* Buffer = StackBuffer;
19 int32 BufferSize = DefaultBufferSize;
20 TCHAR* AllocatedBuffer = nullptr;
21 int32 Length = -1;
22
23 for (;;)
24 {
26 va_copy(ArgsCopy, Args);
29
30 if (Length >= 0 && Length < BufferSize - 1)
31 {
32 break;
33 }
34
35 // Make increasingly large allocations on the heap until we can hold the formatted string.
36 // We need to use SystemMalloc here, as GMalloc might not be safe.
38 BufferSize *= 2;
39 Buffer = AllocatedBuffer = (TCHAR*) FMemory::SystemMalloc(BufferSize * sizeof(TCHAR));
40 if (Buffer == nullptr)
41 {
42 return false;
43 }
44 }
45 Buffer[Length] = TEXT('\0');
46
49 return true;
50}
51
52// This macro expects to be used in a variadic function where the parameter list ends with
53// a `Fmt` text string, followed by the `...` variadic indicator. The passed-in code snippet
54// will have access to the resulting string via a variable named `Buffer`.
55#define GROWABLE_LOGF(SerializeSnippet) \
56 { \
57 va_list Args; \
58 va_start(Args, Fmt); \
59 GrowableLogfV(Fmt, Args, [&](TCHAR* Buffer) { SerializeSnippet; }); \
60 va_end(Args); \
61 }
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
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
bool GrowableLogfV(const TCHAR *Fmt, va_list Args, const SerializeFuncType &SerializeFunc)
Definition VarargsHelper.h:12
static void SystemFree(void *Ptr)
Definition UnrealMemory.h:202
static UE_FORCEINLINE_HINT int32 GetVarArgs(CharType *Dest, SIZE_T DestSize, const CharType *&Fmt, va_list ArgPtr)
Definition CString.h:1222