Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
FieldAccessor.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 "Templates/Function.h"
7
8/**
9 * Helper to provide backward compatibility when converting a raw pointer into accessors.
10 *
11 * The helper is trying hard to mimic's a raw pointer field functionality without breaking compatibility for existing code.
12 *
13 * The helper's getter are all const and return a non-const pointer. This is because
14 * accessing a pointer field inside a const struct will not make that pointer const like we typically
15 * do for accessors. Since we want to mimic the behavior of a public field as much as possible, we
16 * offer that same functionality.
17 *
18 * The helper supports being captured in a lamba as seen below and will become a const copy of the value at the capture's moment.
19 * ex:
20 * [CapturedValue = Object.OnceARawFieldBecomingAnAccessor]()
21 * {
22 * // ExtractedValue contains a copy that will not change even if the original Object.OnceARawFieldBecomingAnAccessor changes.
23 * const Class* ExtractedValue = CapturedValue;
24 * });
25 *
26 * This helper also supports taking a nullptr in its constructor so it properly supports conditional like such
27 * ex:
28 * ExtractedValue* Val = (Condition) ? Object.OnceARawFieldBecomingAnAccessor : nullptr;
29 *
30 * A comforming compiler is supposed to try both conversions (nullptr -> TFieldPtrAccessor and TFieldPtrAccessor -> nullptr)
31 * but MSVC without the /permissive- flag will only try to cast nullptr into a TFieldPtrAccessor, which has to succeed to
32 * avoid breaking compabitility with existing code.
33 *
34 * For more info, please refer to
35 * https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=vs-2019#ambiguous-conditional-operator-arguments
36 *
37 */
38template <typename T>
40{
41public:
42 // Owned by another class that will control the value. Will not use the internal value.
43 TFieldPtrAccessor(TFunction<T* ()> InGet, TFunction<void(T*)> InSet)
46 {
47 }
48
49 // Self-owned value with initializer
50 TFieldPtrAccessor(T* InValue = nullptr)
52 , Get([this]()-> T* { return CapturedValue; })
53 , Set([this](T* InPtr) { CapturedValue = InPtr; })
54 {
55 }
56
57 // Capture the value of the passed field accessor and becomes self-owned.
60 , Get([this]()-> T* { return CapturedValue; })
61 , Set([this](T* InPtr) { CapturedValue = InPtr; })
62 {
63 }
64
65 TFieldPtrAccessor& operator= (const TFieldPtrAccessor& Other) = delete;
67
68 T* operator ->() const
69 {
70 return Get();
71 }
72
73 operator bool() const
74 {
75 return Get() != nullptr;
76 }
77
78 operator T* () const
79 {
80 return Get();
81 }
82
83 template<typename OtherT>
84 explicit operator OtherT* () const
85 {
86 return (OtherT*)Get();
87 }
88
89 bool operator!() const
90 {
91 return Get() == nullptr;
92 }
93
94 bool operator == (const T* OtherPtr) const
95 {
96 return Get() == OtherPtr;
97 }
98
99 bool operator != (const T* OtherPtr) const
100 {
101 return Get() != OtherPtr;
102 }
103
105 {
106 Set(OtherPtr);
107 return *this;
108 }
109
110private:
111 // This is used only when capturing the value from another TFieldPtrAccessor.
112 T* CapturedValue = nullptr;
113 TFunction< T* ()> Get;
114 TFunction<void(T*)> Set;
115};
TFieldPtrAccessor & operator=(TFieldPtrAccessor &&)=delete
TFieldPtrAccessor(T *InValue=nullptr)
TFieldPtrAccessor(TFunction< T *()> InGet, TFunction< void(T *)> InSet)
TFieldPtrAccessor & operator=(const TFieldPtrAccessor &Other)=delete
TFunction< T *()> Get
TFieldPtrAccessor & operator=(T *OtherPtr)
operator OtherT *() const
T * operator->() const
bool operator!=(const T *OtherPtr) const
TFunction< void(T *) Set)
TFieldPtrAccessor(const TFieldPtrAccessor &Other)
bool operator==(const T *OtherPtr) const
bool operator!() const