Plasma Engine  2.0
Loading...
Searching...
No Matches
AbstractObjectGraph.h
1#pragma once
2
4
5#include <Foundation/Basics.h>
6#include <Foundation/Containers/HybridArray.h>
7#include <Foundation/Containers/Set.h>
8#include <Foundation/Reflection/Reflection.h>
9#include <Foundation/Strings/HashedString.h>
10#include <Foundation/Types/Enum.h>
11#include <Foundation/Types/Uuid.h>
12#include <Foundation/Types/Variant.h>
13
15
16class PL_FOUNDATION_DLL plAbstractObjectNode
17{
18public:
19 struct Property
20 {
21 plStringView m_sPropertyName;
22 plVariant m_Value;
23 };
24
25 plAbstractObjectNode() = default;
26
27 const plHybridArray<Property, 16>& GetProperties() const { return m_Properties; }
28
29 void AddProperty(plStringView sName, const plVariant& value);
30
31 void RemoveProperty(plStringView sName);
32
33 void ChangeProperty(plStringView sName, const plVariant& value);
34
35 void RenameProperty(plStringView sOldName, plStringView sNewName);
36
37 void ClearProperties();
38
39 // \brief Inlines a custom variant type. Use to patch properties that have been turned into custom variant type.
40 // \sa PL_DEFINE_CUSTOM_VARIANT_TYPE, PL_DECLARE_CUSTOM_VARIANT_TYPE
41 plResult InlineProperty(plStringView sName);
42
43 const plAbstractObjectGraph* GetOwner() const { return m_pOwner; }
44 const plUuid& GetGuid() const { return m_Guid; }
45 plUInt32 GetTypeVersion() const { return m_uiTypeVersion; }
46 void SetTypeVersion(plUInt32 uiTypeVersion) { m_uiTypeVersion = uiTypeVersion; }
47 plStringView GetType() const { return m_sType; }
48 void SetType(plStringView sType);
49
50 const Property* FindProperty(plStringView sName) const;
51 Property* FindProperty(plStringView sName);
52
53 plStringView GetNodeName() const { return m_sNodeName; }
54
55private:
56 friend class plAbstractObjectGraph;
57
58 plAbstractObjectGraph* m_pOwner = nullptr;
59
60 plUuid m_Guid;
61 plUInt32 m_uiTypeVersion = 0;
62 plStringView m_sType;
63 plStringView m_sNodeName;
64
65 plHybridArray<Property, 16> m_Properties;
66};
67PL_DECLARE_REFLECTABLE_TYPE(PL_FOUNDATION_DLL, plAbstractObjectNode);
68
69struct PL_FOUNDATION_DLL plAbstractGraphDiffOperation
70{
71 enum class Op
72 {
73 NodeAdded,
74 NodeRemoved,
75 PropertyChanged
76 };
77
78 Op m_Operation;
79 plUuid m_Node; // prop parent or added / deleted node
80 plString m_sProperty; // prop name or type
81 plUInt32 m_uiTypeVersion; // only used for NodeAdded
82 plVariant m_Value;
83};
84
85struct PL_FOUNDATION_DLL plObjectChangeType
86{
87 using StorageType = plInt8;
88
89 enum Enum : plInt8
90 {
91 NodeAdded,
92 NodeRemoved,
93 PropertySet,
94 PropertyInserted,
95 PropertyRemoved,
96
97 Default = NodeAdded
98 };
99};
100PL_DECLARE_REFLECTABLE_TYPE(PL_FOUNDATION_DLL, plObjectChangeType);
101
102
103struct PL_FOUNDATION_DLL plDiffOperation
104{
105 plEnum<plObjectChangeType> m_Operation;
106 plUuid m_Node; // owner of m_sProperty
107 plString m_sProperty; // property
108 plVariant m_Index;
109 plVariant m_Value;
110};
111PL_DECLARE_REFLECTABLE_TYPE(PL_FOUNDATION_DLL, plDiffOperation);
112
113
114class PL_FOUNDATION_DLL plAbstractObjectGraph
115{
116public:
117 plAbstractObjectGraph() = default;
119
120 void Clear();
121
123 plAbstractObjectNode* Clone(plAbstractObjectGraph& ref_cloneTarget, const plAbstractObjectNode* pRootNode = nullptr, FilterFunction filter = FilterFunction()) const;
124
125 plStringView RegisterString(plStringView sString);
126
127 const plAbstractObjectNode* GetNode(const plUuid& guid) const;
128 plAbstractObjectNode* GetNode(const plUuid& guid);
129
130 const plAbstractObjectNode* GetNodeByName(plStringView sName) const;
131 plAbstractObjectNode* GetNodeByName(plStringView sName);
132
133 plAbstractObjectNode* AddNode(const plUuid& guid, plStringView sType, plUInt32 uiTypeVersion, plStringView sNodeName = {});
134 void RemoveNode(const plUuid& guid);
135
136 const plMap<plUuid, plAbstractObjectNode*>& GetAllNodes() const { return m_Nodes; }
137 plMap<plUuid, plAbstractObjectNode*>& GetAllNodes() { return m_Nodes; }
138
141 void ReMapNodeGuids(const plUuid& seedGuid, bool bRemapInverse = false);
142
150 void ReMapNodeGuidsToMatchGraph(plAbstractObjectNode* pRoot, const plAbstractObjectGraph& rhsGraph, const plAbstractObjectNode* pRhsRoot);
151
153 void FindTransitiveHull(const plUuid& rootGuid, plSet<plUuid>& out_reachableNodes) const;
155 void PruneGraph(const plUuid& rootGuid);
156
159 void ModifyNodeViaNativeCounterpart(plAbstractObjectNode* pRootNode, plDelegate<void(void*, const plRTTI*)> callback);
160
162 plAbstractObjectNode* CopyNodeIntoGraph(const plAbstractObjectNode* pNode);
163
164 plAbstractObjectNode* CopyNodeIntoGraph(const plAbstractObjectNode* pNode, FilterFunction& ref_filter);
165
166 void CreateDiffWithBaseGraph(const plAbstractObjectGraph& base, plDeque<plAbstractGraphDiffOperation>& out_diffResult) const;
167
168 void ApplyDiff(plDeque<plAbstractGraphDiffOperation>& ref_diff);
169
171
172private:
173 PL_DISALLOW_COPY_AND_ASSIGN(plAbstractObjectGraph);
174
175 void RemapVariant(plVariant& value, const plHashTable<plUuid, plUuid>& guidMap);
176 void MergeArrays(const plVariantArray& baseArray, const plVariantArray& leftArray, const plVariantArray& rightArray, plVariantArray& out) const;
177 void ReMapNodeGuidsToMatchGraphRecursive(plHashTable<plUuid, plUuid>& guidMap, plAbstractObjectNode* lhs, const plAbstractObjectGraph& rhsGraph, const plAbstractObjectNode* rhs);
178
179 plSet<plString> m_Strings;
182};
Definition AbstractObjectGraph.h:115
Definition AbstractObjectGraph.h:17
Definition Deque.h:270
Definition HashTable.h:333
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Definition Map.h:408
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
Definition Set.h:238
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
This data type is the abstraction for 128-bit Uuid (also known as GUID) instances.
Definition Uuid.h:11
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
Definition AbstractObjectGraph.h:70
Definition AbstractObjectGraph.h:20
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
Definition AbstractObjectGraph.h:104
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Definition AbstractObjectGraph.h:86
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54