Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
CompactBinarySerialization.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/ContainersFwd.h"
6#include "Containers/StringFwd.h"
7#include "CoreTypes.h"
8#include "IO/IoHash.h"
9#include "Memory/MemoryFwd.h"
10#include "Memory/MemoryView.h"
11#include "Memory/SharedBuffer.h"
12#include "Serialization/CompactBinary.h"
13#include "Templates/Function.h"
14
15class FArchive;
16class FName;
17struct FGuid;
18
19///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
22{
23
24/** Utility for logging problems with FCbField. For internal use only */
25void LogFieldTooLargeForArrayWarning(uint64 FieldLength);
26
27} // namespace UE::Serialization::Private
28
29///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
30
31/**
32 * Determine the size in bytes of the compact binary field at the start of the view.
33 *
34 * This may be called on an incomplete or invalid field, in which case the returned size is zero.
35 * A size can always be extracted from a valid field with no name if a view of at least the first
36 * 10 bytes is provided, regardless of field size. For fields with names, the size of view needed
37 * to calculate a size is at most 10 + MaxNameLen + MeasureVarUInt(MaxNameLen).
38 *
39 * This function can be used when streaming a field, for example, to determine the size of buffer
40 * to fill before attempting to construct a field from it.
41 *
42 * @param View A memory view that may contain the start of a field.
43 * @param Type HasFieldType means that View contains the type. Otherwise, use the given type.
44 */
46
47/**
48 * Try to determine the type and size of the compact binary field at the start of the view.
49 *
50 * This may be called on an incomplete or invalid field, in which case it will return false, with
51 * OutSize being 0 for invalid fields, otherwise the minimum view size necessary to make progress
52 * in measuring the field on the next call to this function.
53 *
54 * @note A return of true from this function does not indicate that the entire field is valid.
55 *
56 * @param InView A memory view that may contain the start of a field.
57 * @param OutType The type (with flags) of the field. None is written until a value is available.
58 * @param OutSize The total field size for a return of true, 0 for invalid fields, or the size to
59 * make progress in measuring the field on the next call to this function.
60 * @param InType HasFieldType means that InView contains the type. Otherwise, use the given type.
61 * @return true if the size of the field was determined, otherwise false.
62 */
64 FMemoryView InView,
65 ECbFieldType& OutType,
66 uint64& OutSize,
68
69///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70
71/**
72 * Load a compact binary field from an archive.
73 *
74 * The field may be an array or an object, which the caller can convert to by using AsArray or
75 * AsObject as appropriate. The buffer allocator is called to provide the buffer for the field
76 * to load into once its size has been determined.
77 *
78 * @param Ar Archive to read the field from. An error state is set on failure.
79 * @param Allocator Allocator for the buffer that the field is loaded into.
80 * @return A field with a reference to the allocated buffer, or a default field on failure.
81 */
82FCbField LoadCompactBinary(FArchive& Ar, FCbBufferAllocator Allocator = FUniqueBuffer::Alloc);
83
84/** Save a compact binary value to an archive. */
85void SaveCompactBinary(FArchive& Ar, const FCbFieldView& Field);
86void SaveCompactBinary(FArchive& Ar, const FCbArrayView& Array);
87void SaveCompactBinary(FArchive& Ar, const FCbObjectView& Object);
88
89/** Serialize a compact binary value to/from an archive. */
90FArchive& operator<<(FArchive& Ar, FCbField& Field);
91FArchive& operator<<(FArchive& Ar, FCbArray& Array);
92FArchive& operator<<(FArchive& Ar, FCbObject& Object);
93
94///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
95
96/**
97 * LoadFromCompactBinary attempts to load the output value from compact binary.
98 *
99 * Implementations of LoadCompactBinary are expected to assign the output even on failure.
100 * Implementations may accept an optional default value to assign in case of load failure.
101 *
102 * @return true if required fields had the values with the correct type and range, otherwise false.
103 */
104
105bool LoadFromCompactBinary(FCbFieldView Field, FUtf8StringBuilderBase& OutValue);
106bool LoadFromCompactBinary(FCbFieldView Field, FWideStringBuilderBase& OutValue);
109
110inline bool LoadFromCompactBinary(FCbFieldView Field, int8& OutValue, const int8 Default = 0)
111{
112 OutValue = Field.AsInt8(Default);
113 return !Field.HasError();
114}
115
116inline bool LoadFromCompactBinary(FCbFieldView Field, int16& OutValue, const int16 Default = 0)
117{
118 OutValue = Field.AsInt16(Default);
119 return !Field.HasError();
120}
121
122inline bool LoadFromCompactBinary(FCbFieldView Field, int32& OutValue, const int32 Default = 0)
123{
124 OutValue = Field.AsInt32(Default);
125 return !Field.HasError();
126}
127
128inline bool LoadFromCompactBinary(FCbFieldView Field, int64& OutValue, const int64 Default = 0)
129{
130 OutValue = Field.AsInt64(Default);
131 return !Field.HasError();
132}
133
134inline bool LoadFromCompactBinary(FCbFieldView Field, uint8& OutValue, const uint8 Default = 0)
135{
136 OutValue = Field.AsUInt8(Default);
137 return !Field.HasError();
138}
139
140inline bool LoadFromCompactBinary(FCbFieldView Field, uint16& OutValue, const uint16 Default = 0)
141{
142 OutValue = Field.AsUInt16(Default);
143 return !Field.HasError();
144}
145
146inline bool LoadFromCompactBinary(FCbFieldView Field, uint32& OutValue, const uint32 Default = 0)
147{
148 OutValue = Field.AsUInt32(Default);
149 return !Field.HasError();
150}
151
152inline bool LoadFromCompactBinary(FCbFieldView Field, uint64& OutValue, const uint64 Default = 0)
153{
154 OutValue = Field.AsUInt64(Default);
155 return !Field.HasError();
156}
157
158inline bool LoadFromCompactBinary(FCbFieldView Field, float& OutValue, const float Default = 0.0f)
159{
160 OutValue = Field.AsFloat(Default);
161 return !Field.HasError();
162}
163
164inline bool LoadFromCompactBinary(FCbFieldView Field, double& OutValue, const double Default = 0.0)
165{
166 OutValue = Field.AsDouble(Default);
167 return !Field.HasError();
168}
169
170inline bool LoadFromCompactBinary(FCbFieldView Field, bool& OutValue, const bool Default = false)
171{
172 OutValue = Field.AsBool(Default);
173 return !Field.HasError();
174}
175
176inline bool LoadFromCompactBinary(FCbFieldView Field, FIoHash& OutValue, const FIoHash& Default = FIoHash())
177{
178 OutValue = Field.AsHash(Default);
179 return !Field.HasError();
180}
181
183bool LoadFromCompactBinary(FCbFieldView Field, FGuid& OutValue, const FGuid& Default);
184
185inline bool LoadFromCompactBinary(FCbFieldView Field, FCbObjectId& OutValue, const FCbObjectId& Default = FCbObjectId())
186{
187 OutValue = Field.AsObjectId(Default);
188 return !Field.HasError();
189}
190
191template <typename T, typename Allocator>
192inline bool LoadFromCompactBinary(FCbFieldView Field, TArray<T, Allocator>& OutValue)
193{
194 const uint64 Length = Field.AsArrayView().Num();
195 if (Length <= MAX_int32)
196 {
197 OutValue.Reset((int32)Length);
198 bool bOk = !Field.HasError();
199 for (const FCbFieldView& ElementField : Field)
200 {
201 bOk = LoadFromCompactBinary(ElementField, OutValue.Emplace_GetRef()) & bOk;
202 }
203 return bOk;
204 }
205 else
206 {
207 UE::Serialization::Private::LogFieldTooLargeForArrayWarning(Length);
208 return false;
209 }
210}
211
212///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
213
214/** Convert the compact binary to JSON in a multi-line format with indentation. */
215void CompactBinaryToJson(const FCbFieldView& Field, FUtf8StringBuilderBase& Builder);
216void CompactBinaryToJson(const FCbFieldView& Field, FWideStringBuilderBase& Builder);
217void CompactBinaryToJson(const FCbArrayView& Array, FUtf8StringBuilderBase& Builder);
218void CompactBinaryToJson(const FCbArrayView& Array, FWideStringBuilderBase& Builder);
219void CompactBinaryToJson(const FCbObjectView& Object, FUtf8StringBuilderBase& Builder);
220void CompactBinaryToJson(const FCbObjectView& Object, FWideStringBuilderBase& Builder);
221
222/** Convert the compact binary to JSON in a compact format with no added whitespace. */
223void CompactBinaryToCompactJson(const FCbFieldView& Field, FUtf8StringBuilderBase& Builder);
224void CompactBinaryToCompactJson(const FCbFieldView& Field, FWideStringBuilderBase& Builder);
225void CompactBinaryToCompactJson(const FCbArrayView& Array, FUtf8StringBuilderBase& Builder);
226void CompactBinaryToCompactJson(const FCbArrayView& Array, FWideStringBuilderBase& Builder);
227void CompactBinaryToCompactJson(const FCbObjectView& Object, FUtf8StringBuilderBase& Builder);
228void CompactBinaryToCompactJson(const FCbObjectView& Object, FWideStringBuilderBase& Builder);
229
230///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ECbFieldType
bool LoadFromCompactBinary(FCbFieldView Field, FUtf8StringBuilderBase &OutValue)
uint64 MeasureCompactBinary(FMemoryView View, ECbFieldType Type=ECbFieldType::HasFieldType)
void CompactBinaryToJson(const FCbObjectView &Object, FWideStringBuilderBase &Builder)
void CompactBinaryToJson(const FCbArrayView &Array, FWideStringBuilderBase &Builder)
void CompactBinaryToJson(const FCbFieldView &Field, FWideStringBuilderBase &Builder)
bool LoadFromCompactBinary(FCbFieldView Field, FWideStringBuilderBase &OutValue)
void CompactBinaryToCompactJson(const FCbObjectView &Object, FUtf8StringBuilderBase &Builder)
void CompactBinaryToJson(const FCbArrayView &Array, FUtf8StringBuilderBase &Builder)
void CompactBinaryToCompactJson(const FCbArrayView &Array, FWideStringBuilderBase &Builder)
bool LoadFromCompactBinary(FCbFieldView Field, FString &OutValue)
bool LoadFromCompactBinary(FCbFieldView Field, FCbObjectId &OutValue, const FCbObjectId &Default=FCbObjectId())
bool LoadFromCompactBinary(FCbFieldView Field, FGuid &OutValue, const FGuid &Default)
void CompactBinaryToCompactJson(const FCbFieldView &Field, FUtf8StringBuilderBase &Builder)
void CompactBinaryToCompactJson(const FCbObjectView &Object, FWideStringBuilderBase &Builder)
void CompactBinaryToCompactJson(const FCbArrayView &Array, FUtf8StringBuilderBase &Builder)
void CompactBinaryToCompactJson(const FCbFieldView &Field, FWideStringBuilderBase &Builder)
bool TryMeasureCompactBinary(FMemoryView InView, ECbFieldType &OutType, uint64 &OutSize, ECbFieldType InType=ECbFieldType::HasFieldType)
bool LoadFromCompactBinary(FCbFieldView Field, TArray< T, Allocator > &OutValue)
bool LoadFromCompactBinary(FCbFieldView Field, int8 &OutValue, const int8 Default=0)
bool LoadFromCompactBinary(FCbFieldView Field, uint16 &OutValue, const uint16 Default=0)
bool LoadFromCompactBinary(FCbFieldView Field, int32 &OutValue, const int32 Default=0)
void CompactBinaryToJson(const FCbObjectView &Object, FUtf8StringBuilderBase &Builder)
void SaveCompactBinary(FArchive &Ar, const FCbArrayView &Array)
bool LoadFromCompactBinary(FCbFieldView Field, bool &OutValue, const bool Default=false)
bool LoadFromCompactBinary(FCbFieldView Field, FGuid &OutValue)
void SaveCompactBinary(FArchive &Ar, const FCbFieldView &Field)
bool LoadFromCompactBinary(FCbFieldView Field, int64 &OutValue, const int64 Default=0)
bool LoadFromCompactBinary(FCbFieldView Field, uint64 &OutValue, const uint64 Default=0)
void SaveCompactBinary(FArchive &Ar, const FCbObjectView &Object)
bool LoadFromCompactBinary(FCbFieldView Field, uint8 &OutValue, const uint8 Default=0)
FCbField LoadCompactBinary(FArchive &Ar, FCbBufferAllocator Allocator=FUniqueBuffer::Alloc)
bool LoadFromCompactBinary(FCbFieldView Field, double &OutValue, const double Default=0.0)
bool LoadFromCompactBinary(FCbFieldView Field, uint32 &OutValue, const uint32 Default=0)
bool LoadFromCompactBinary(FCbFieldView Field, float &OutValue, const float Default=0.0f)
bool LoadFromCompactBinary(FCbFieldView Field, FIoHash &OutValue, const FIoHash &Default=FIoHash())
bool LoadFromCompactBinary(FCbFieldView Field, int16 &OutValue, const int16 Default=0)
void CompactBinaryToJson(const FCbFieldView &Field, FUtf8StringBuilderBase &Builder)
bool LoadFromCompactBinary(FCbFieldView Field, FName &OutValue)
#define MAX_int32
uint64 AsUInt64(uint64 Default=0)
constexpr bool HasError() const
int64 AsInt64(int64 Default=0)
uint8 AsUInt8(uint8 Default=0)
uint32 AsUInt32(uint32 Default=0)
int32 AsInt32(int32 Default=0)
uint16 AsUInt16(uint16 Default=0)
float AsFloat(float Default=0.0f)
int16 AsInt16(int16 Default=0)
int8 AsInt8(int8 Default=0)
double AsDouble(double Default=0.0)
bool AsBool(bool bDefault=false)
FCbObjectId AsObjectId(const FCbObjectId &Default=FCbObjectId())
FCbObjectId()=default
void LogFieldTooLargeForArrayWarning(uint64 FieldLength)
Definition Vector.h:40
Definition Guid.h:108