Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Variant.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/AssertionMacros.h"
7#include "Containers/Array.h"
8#include "Containers/UnrealString.h"
9#include "Math/Color.h"
10#include "Math/IntPoint.h"
11#include "Misc/Timespan.h"
12#include "UObject/NameTypes.h"
13#include "Math/Vector2D.h"
14#include "Math/IntRect.h"
15#include "Containers/EnumAsByte.h"
16#include "Math/IntVector.h"
17#include "Math/Vector.h"
18#include "Misc/DateTime.h"
19#include "Math/Box.h"
20#include "Math/BoxSphereBounds.h"
21#include "Math/Vector4.h"
22#include "Math/Plane.h"
23#include "Math/Rotator.h"
24#include "Math/Matrix.h"
25#include "Math/Quat.h"
26#include "Math/TwoVectors.h"
27#include "Math/Transform.h"
28#include "Misc/Guid.h"
29#include "Math/RandomStream.h"
30#include "Misc/NetworkGuid.h"
31#include "Serialization/MemoryWriter.h"
32#include "Serialization/MemoryReader.h"
33
34/**
35 * Enumerates the built-in types that can be stored in instances of FVariant.
36 */
37enum class EVariantTypes : int32
38{
39 Empty = 0,
40 Ansichar = 1,
41 Bool = 2,
42 Box = 3,
44 ByteArray = 5,
45 Color = 6,
46 DateTime = 7,
47 Double = 8,
48 Enum = 9,
49 Float = 10,
50 Guid = 11,
51 Int8 = 12,
52 Int16 = 13,
53 Int32 = 14,
54 Int64 = 15,
55 IntRect = 16,
56 LinearColor = 17,
57 Matrix = 18,
58 Name = 19,
59 Plane = 20,
60 Quat = 21,
61 RandomStream = 22,
62 Rotator = 23,
63 String = 24,
64 Widechar = 25,
65 Timespan = 26,
66 Transform = 27,
67 TwoVectors = 28,
68 UInt8 = 29,
69 UInt16 = 30,
70 UInt32 = 31,
71 UInt64 = 32,
72 Vector = 33,
73 Vector2d = 34,
74 Vector4 = 35,
75 IntPoint = 36,
76 IntVector = 37,
77 NetworkGUID = 38,
78
79 Custom = 0x40
80};
81
82
83/**
84 * Stub for variant type traits.
85 *
86 * Actual type traits need to be declared through template specialization for custom
87 * data types that are to be used in FVariant. Traits for the most commonly used built-in
88 * types are declared below.
89 *
90 * Complex types, such as structures and classes can be serialized into a byte array
91 * and then assigned to a variant. Note that you will be responsible for ensuring
92 * correct byte ordering when serializing those types.
93 *
94 * @param T The type to be used in FVariant.
95 */
96template<typename T> struct TVariantTraits
97{
99 {
100 static_assert(!sizeof(T), "Variant trait must be specialized for this type.");
102 }
103};
104
105
106/**
107 * Implements an extensible union of multiple types.
108 *
109 * Variant types can be used to store a range of different built-in types, as well as user
110 * defined types. The values are internally serialized into a byte array, which means that
111 * only FArchive serializable types are supported at this time.
112 */
114{
115public:
116
117 /** Default constructor. */
120 { }
121
122 FVariant(const FVariant&) = default;
123 FVariant& operator=(const FVariant&) = default;
124 ~FVariant() = default;
125
127 : Type(Other.Type)
129 {
131 }
132
134 {
135 if (&Other != this)
136 {
137 Type = Other.Type;
138 Value = MoveTemp(Other.Value);
140 }
141 return *this;
142 }
143
144 /**
145 * Creates and initializes a new instance with the specified value.
146 *
147 * @param InValue The initial value.
148 */
149 template<typename T>
150 FVariant( T InValue )
151 {
153 writer << InValue;
154
156 }
157
158 /**
159 * Creates and initializes a new instance from a byte array.
160 *
161 * Array values are passed straight through as an optimization. Please note that, if you
162 * serialize any complex types into arrays and then store them in FVariant, you will be
163 * responsible for ensuring byte ordering if the FVariant gets sent over the network.
164 *
165 * @param InValue- The initial value.
166 */
167 FVariant( TArray<uint8>&& InArray )
170 { }
171 FVariant( const TArray<uint8>& InArray )
173 , Value(InArray)
174 { }
175
176 /**
177 * Creates and initializes a new instance from a TCHAR string.
178 *
179 * @param InString The initial value.
180 */
181 FVariant( const TCHAR* InString )
182 {
183 *this = FString(InString);
184 }
185
186public:
187
188 /**
189 * Assignment operator.
190 *
191 * @param T The type of the value to assign.
192 * @param InValue The value to assign.
193 * @return This instance.
194 */
195 template<typename T>
196 FVariant& operator=( T InValue )
197 {
199 Writer << InValue;
200
202
203 return *this;
204 }
205
206 /**
207 * Assignment operator for byte arrays.
208 *
209 * Array values are passed straight through as an optimization. Please note that, if you
210 * serialize any complex types into arrays and then store them in FVariant, you will be
211 * responsible for ensuring byte ordering if the FVariant gets sent over the network.
212 *
213 * @param InArray The byte array to assign.
214 * @return This instance.
215 */
216 FVariant& operator=( TArray<uint8>&& InArray )
217 {
219 Value = MoveTemp(InArray);
220
221 return *this;
222 }
223 FVariant& operator=( const TArray<uint8>& InArray )
224 {
226 Value = InArray;
227
228 return *this;
229 }
230
231 /**
232 * Assignment operator for TCHAR strings.
233 *
234 * @param InString The value to assign.
235 * @return This instance.
236 */
237 FVariant& operator=( const TCHAR* InString )
238 {
239 *this = FString(InString);
240
241 return *this;
242 }
243
244
245 /**
246 * Implicit conversion operator.
247 *
248 * @param T The type to convert the value to.
249 * @return The value converted to the specified type.
250 */
251 template<typename T>
252 operator T() const
253 {
254 return GetValue<T>();
255 }
256
257public:
258
259 /**
260 * Comparison operator for equality.
261 *
262 * @param Other The variant to compare with.
263 * @return true if the values are equal, false otherwise.
264 */
265 bool operator==( const FVariant& Other ) const
266 {
267 return ((Type == Other.Type) && (Value == Other.Value));
268 }
269
270 /**
271 * Comparison operator for inequality.
272 *
273 * @param Other The variant to compare with.
274 * @return true if the values are not equal, false otherwise.
275 */
276 bool operator!=( const FVariant& Other ) const
277 {
278 return ((Type != Other.Type) || (Value != Other.Value));
279 }
280
281public:
282
283 /**
284 * Empties the value.
285 *
286 * @see IsEmpty
287 */
288 void Empty()
289 {
291
292 Value.Empty();
293 }
294
295 /**
296 * Checks whether the value is empty.
297 *
298 * @return true if the value is empty, false otherwise.
299 *
300 * @see Empty
301 */
302 bool IsEmpty() const
303 {
304 return (Type == EVariantTypes::Empty);
305 }
306
307 /**
308 * Gets the stored value as a byte array.
309 *
310 * This method returns the internal representation of any value as an
311 * array of raw bytes. To retrieve values of type TArray<uint8> use
312 * GetValue<TArray<uint8>>() instead.
313 *
314 * @return Byte array.
315 * @see GetValue
316 */
317 const TArray<uint8>& GetBytes() const
318 {
319 return Value;
320 }
321
322 /**
323 * Gets the stored value's size (in bytes).
324 *
325 * @return Size of the value.
326 * @see GetType, GetValue
327 */
328 int32 GetSize() const
329 {
330 return Value.Num();
331 }
332
333 /**
334 * Gets the stored value's type.
335 *
336 * @return Type of the value.
337 * @see GetSize, GetValue
338 */
340 {
341 return Type;
342 }
343
344 /**
345 * Gets the stored value.
346 *
347 * This template function does not provide any automatic conversion between
348 * convertible types. The exact type of the value to be extracted must be known.
349 *
350 * @return The value.
351 * @see GetSize, GetType
352 */
353 template<typename T>
354 T GetValue() const
355 {
357
358 T Result;
359
361 Reader << Result;
362
363 return Result;
364 }
365
366public:
367
368 /**
369 * Serializes the given variant type from or into the specified archive.
370 *
371 * @param Ar The archive to serialize from or into.
372 * @param Variant The value to serialize.
373 * @return The archive.
374 */
375 friend FArchive& operator<<( FArchive& Ar, FVariant& Variant )
376 {
377 return Ar << Variant.Type << Variant.Value;
378 }
379
380private:
381
382 /** Holds the type of the variant. */
384
385 /** Holds the serialized value. */
387};
388
389
390/**
391 * Gets the stored value for byte arrays.
392 *
393 * Array values are passed straight through as an optimization. Please note that, if you serialize
394 * any complex types into arrays and then store them in FVariant, you will be responsible for
395 * ensuring byte ordering if the FVariant gets send over the network.
396 *
397 * To retrieve any value as an array of serialized bytes, use GetBytes() instead.
398 *
399 * @return The byte array.
400 * @see GetBytes
401 */
402template<>
403FORCEINLINE TArray<uint8> FVariant::GetValue<TArray<uint8> >() const
404{
405 check(Type == EVariantTypes::ByteArray);
406
407 return Value;
408}
409
410
411/* Default FVariant traits for built-in types
412 *****************************************************************************/
413
414/** Implements variant type traits for the built-in ANSICHAR type. */
415template<> struct TVariantTraits<ANSICHAR>
416{
417 static CONSTEXPR EVariantTypes GetType() { return EVariantTypes::Ansichar; }
418};
419
420
421/** Implements variant type traits for the built-in bool type. */
422template<> struct TVariantTraits<bool>
423{
425};
426
427
428/** Implements variant type traits for the built-in FBox type. */
429template<> struct TVariantTraits<FBox>
430{
432};
433
434
435/** Implements variant type traits for the built-in FBoxSphereBounds type. */
436template<> struct TVariantTraits<FBoxSphereBounds>
437{
439};
440
441
442/** Implements variant type traits for byte arrays. */
443template<> struct TVariantTraits<TArray<uint8> >
444{
445 static CONSTEXPR EVariantTypes GetType() { return EVariantTypes::ByteArray; }
446};
447
448
449/** Implements variant type traits for the built-in FColor type. */
450template<> struct TVariantTraits<FColor>
451{
453};
454
455
456/** Implements variant type traits for the built-in FDateTime type. */
457template<> struct TVariantTraits<FDateTime>
458{
460};
461
462
463/** Implements variant type traits for the built-in double type. */
464template<> struct TVariantTraits<double>
465{
467};
468
469
470/** Implements variant type traits for enumeration types. */
471template<typename EnumType> struct TVariantTraits<TEnumAsByte<EnumType> >
472{
474};
475
476
477/** Implements variant type traits for the built-in float type. */
478template<> struct TVariantTraits<float>
479{
481};
482
483
484/** Implements variant type traits for the built-in FGuid type. */
485template<> struct TVariantTraits<FGuid>
486{
488};
489
490
491/** Implements variant type traits for the built-in int8 type. */
492template<> struct TVariantTraits<int8>
493{
495};
496
497
498/** Implements variant type traits for the built-in int16 type. */
499template<> struct TVariantTraits<int16>
500{
502};
503
504
505/** Implements variant type traits for the built-in int32 type. */
506template<> struct TVariantTraits<int32>
507{
509};
510
511
512/** Implements variant type traits for the built-in int64 type. */
513template<> struct TVariantTraits<int64>
514{
516};
517
518
519/** Implements variant type traits for the built-in FIntPoint type. */
520template<> struct TVariantTraits<FIntPoint>
521{
523};
524
525
526/** Implements variant type traits for the built-in FIntVector type. */
527template<> struct TVariantTraits<FIntVector>
528{
530};
531
532
533/** Implements variant type traits for the built-in FIntRect type. */
534template<> struct TVariantTraits<FIntRect>
535{
537};
538
539
540/** Implements variant type traits for the built-in FLinearColor type. */
541template<> struct TVariantTraits<FLinearColor>
542{
544};
545
546
547/** Implements variant type traits for the built-in FMatrix type. */
548template<> struct TVariantTraits<FMatrix>
549{
551};
552
553
554/** Implements variant type traits for the built-in FPlane type. */
555template<> struct TVariantTraits<FPlane>
556{
558};
559
560
561/** Implements variant type traits for the built-in FQuat type. */
562template<> struct TVariantTraits<FQuat>
563{
565};
566
567
568/** Implements variant type traits for the built-in FName type. */
569template<> struct TVariantTraits<FName>
570{
572};
573
574
575/** Implements variant type traits for the built-in FRandomStream type. */
577{
579};
580
581
582/** Implements variant type traits for the built-in FRotator type. */
583template<> struct TVariantTraits<FRotator>
584{
586};
587
588
589/** Implements variant type traits for the built-in FString type. */
590template<> struct TVariantTraits<FString>
591{
593};
594
595
596/** Implements variant type traits for the built-in WIDECHAR type. */
597template<> struct TVariantTraits<WIDECHAR>
598{
600};
601
602
603/** Implements variant type traits for the built-in FTimespan type. */
604template<> struct TVariantTraits<FTimespan>
605{
607};
608
609
610/** Implements variant type traits for the built-in FTransform type. */
611template<> struct TVariantTraits<FTransform>
612{
614};
615
616
617/** Implements variant type traits for the built-in FTwoVectors type. */
618template<> struct TVariantTraits<FTwoVectors>
619{
621};
622
623
624/** Implements variant type traits for the built-in uint8 type. */
625template<> struct TVariantTraits<uint8>
626{
628};
629
630
631/** Implements variant type traits for the built-in uint16 type. */
632template<> struct TVariantTraits<uint16>
633{
635};
636
637
638/** Implements variant type traits for the built-in uint32 type. */
639template<> struct TVariantTraits<uint32>
640{
642};
643
644
645/** Implements variant type traits for the built-in uint64 type. */
646template<> struct TVariantTraits<uint64>
647{
649};
650
651
652/** Implements variant type traits for the built-in FVector type. */
653template<> struct TVariantTraits<FVector>
654{
656};
657
658
659/** Implements variant type traits for the built-in FVector2D type. */
660template<> struct TVariantTraits<FVector2D>
661{
663};
664
665
666/** Implements variant type traits for the built-in FVector4 type. */
667template<> struct TVariantTraits<FVector4>
668{
670};
671
672
673/** Implements variant type traits for the built-in NetworkGUID type. */
674template<> struct TVariantTraits<FNetworkGUID>
675{
677};
#define check(expr)
#define FORCEINLINE
Definition Platform.h:644
#define CONSTEXPR
Definition Platform.h:771
EVariantTypes
Definition Variant.h:38
ARK_API FString(const WIDECHAR *Str)
Definition String.cpp:250
FVariant & operator=(T InValue)
Definition Variant.h:196
FVariant(T InValue)
Definition Variant.h:150
bool IsEmpty() const
Definition Variant.h:302
FVariant()
Definition Variant.h:118
EVariantTypes GetType() const
Definition Variant.h:339
FVariant(FVariant &&Other)
Definition Variant.h:126
const TArray< uint8 > & GetBytes() const
Definition Variant.h:317
FVariant & operator=(const TArray< uint8 > &InArray)
Definition Variant.h:223
FVariant & operator=(TArray< uint8 > &&InArray)
Definition Variant.h:216
void Empty()
Definition Variant.h:288
FVariant(const TArray< uint8 > &InArray)
Definition Variant.h:171
TArray< uint8 > Value
Definition Variant.h:386
FVariant & operator=(FVariant &&Other)
Definition Variant.h:133
int32 GetSize() const
Definition Variant.h:328
bool operator==(const FVariant &Other) const
Definition Variant.h:265
FVariant(TArray< uint8 > &&InArray)
Definition Variant.h:167
bool operator!=(const FVariant &Other) const
Definition Variant.h:276
FVariant & operator=(const FVariant &)=default
~FVariant()=default
EVariantTypes Type
Definition Variant.h:383
FVariant(const FVariant &)=default
operator T() const
Definition Variant.h:252
FVariant & operator=(const TCHAR *InString)
Definition Variant.h:237
T GetValue() const
Definition Variant.h:354
FVariant(const TCHAR *InString)
Definition Variant.h:181
Definition Guid.h:108
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:431
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:438
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:452
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:459
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:487
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:522
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:536
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:529
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:543
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:550
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:571
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:676
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:557
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:564
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:578
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:585
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:592
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:606
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:613
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:620
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:662
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:669
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:655
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:473
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:599
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:424
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:466
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:480
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:501
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:508
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:515
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:494
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:634
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:641
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:648
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:627
static CONSTEXPR EVariantTypes GetType()
Definition Variant.h:98