ArticyXImporter
ArticyXImporter for Unreal Engine
Loading...
Searching...
No Matches
ArticyReflectable.h
1//
2// Copyright (c) 2023 articy Software GmbH & Co. KG. All rights reserved.
3//
4
5#pragma once
6
7#include "CoreMinimal.h"
8#include "Runtime/CoreUObject/Public/UObject/Interface.h"
9#include "Runtime/Launch/Resources/Version.h"
10#include "ArticyChangedProperty.h"
11#include "ArticyReflectable.generated.h"
12
13DECLARE_MULTICAST_DELEGATE_OneParam(FReportChangedDelegate, FArticyChangedProperty&);
14
15UINTERFACE()
16class UArticyReflectable : public UInterface { GENERATED_BODY() };
17
24{
25 GENERATED_BODY()
26
27public:
29 template<typename TValue>
30 TValue SetProp(FName Property, TValue Value, int32 ArrayIndex = 0);
31
33 template<typename TValue>
34 TValue& GetProp(FName Property, int32 ArrayIndex = 0);
35
37 template<typename TValue>
38 const TValue& GetProp(FName Property, int32 ArrayIndex = 0) const
39 {
40 //the const cast here is only used so we don't have to re-implement the same method again
41 return const_cast<IArticyReflectable*>(this)->GetProp<TValue>(Property, ArrayIndex);
42 }
43
45 template<typename TValue>
46 TValue* GetPropPtr(FName Property, int32 ArrayIndex = 0) const
47 {
48 //look up the (hopefully already cached) property pointers
49 auto propPointers = GetPropertyPointers();
50 auto prop = propPointers.Find(Property);
51 if(prop)
52 return (*prop)->ContainerPtrToValuePtr<TValue>(_getUObject(), ArrayIndex);
53
54 return nullptr;
55 }
56
58 FProperty* GetProperty(FName Property) const
59 {
60 //look up the (hopefully already cached) property pointers
61 auto propPointers = GetPropertyPointers();
62 auto prop = propPointers.Find(Property);
63 if(prop)
64 return *prop;
65
66 return nullptr;
67 }
68
70 static bool HasProperty(const UClass* Class, const FName &Property)
71 {
72 return GetPropertyPointers(Class).Find(Property) != nullptr;
73 }
74
75 virtual UClass* GetObjectClass() const { return _getUObject()->GetClass(); }
76
77 FReportChangedDelegate ReportChanged;
78
79private:
85 TMap<FName, FProperty*>& GetPropertyPointers() const
86 {
87 return GetPropertyPointers(GetObjectClass());
88 }
89
90 static TMap<FName, FProperty*>& GetPropertyPointers(const UClass* Class)
91 {
92 static TMap<const UClass*, TMap<FName, FProperty*>> PropertyPointers;
93 auto& pp = PropertyPointers.FindOrAdd(Class);
94 if(pp.Num() == 0)
95 {
96 //cache property pointers
97 for(TFieldIterator<FProperty> It(Class); It; ++It)
98 pp.Add(*It->GetNameCPP(), *It);
99 }
100
101 return pp;
102 }
103};
104
105//---------------------------------------------------------------------------//
106
107template <typename TValue>
108TValue IArticyReflectable::SetProp(FName Property, TValue Value, int32 ArrayIndex)
109{
110 TValue* valPtr = GetPropPtr<TValue>(Property, ArrayIndex);
111 if(valPtr)
112 {
113 FArticyChangedProperty ChangedProperty;
114 ChangedProperty.Property = Property;
115 ChangedProperty.SetObjectReference(this);
116
117 (*valPtr) = Value;
118
119 ReportChanged.Broadcast(ChangedProperty);
120 return (*valPtr);
121 }
122
123 return Value;
124}
125
126template <typename TValue>
127TValue& IArticyReflectable::GetProp(FName Property, int32 ArrayIndex)
128{
129 TValue* valPtr = GetPropPtr<TValue>(Property, ArrayIndex);
130 if(valPtr)
131 return (*valPtr);
132
133 static TValue Empty;
134 return Empty;
135}
Definition ArticyReflectable.h:24
FProperty * GetProperty(FName Property) const
Definition ArticyReflectable.h:58
const TValue & GetProp(FName Property, int32 ArrayIndex=0) const
Definition ArticyReflectable.h:38
TValue * GetPropPtr(FName Property, int32 ArrayIndex=0) const
Definition ArticyReflectable.h:46
static bool HasProperty(const UClass *Class, const FName &Property)
Definition ArticyReflectable.h:70
TValue & GetProp(FName Property, int32 ArrayIndex=0)
Definition ArticyReflectable.h:127
TValue SetProp(FName Property, TValue Value, int32 ArrayIndex=0)
Definition ArticyReflectable.h:108
Definition ArticyReflectable.h:16
Definition ArticyChangedProperty.h:14