Plasma Engine  2.0
Loading...
Searching...
No Matches
DocumentObjectManager.h
1#pragma once
2
3#include <Foundation/Types/RefCounted.h>
4#include <Foundation/Types/SharedPtr.h>
5#include <Foundation/Types/Status.h>
6#include <ToolsFoundation/Object/DocumentObjectBase.h>
7#include <ToolsFoundation/Reflection/ReflectedType.h>
8#include <ToolsFoundation/ToolsFoundationDLL.h>
9
11class plDocument;
12
13// Prevent conflicts with windows.h
14#ifdef GetObject
15# undef GetObject
16#endif
17
20class PL_TOOLSFOUNDATION_DLL plDocumentRoot : public plReflectedClass
21{
22 PL_ADD_DYNAMIC_REFLECTION(plDocumentRoot, plReflectedClass);
23
26};
27
30{
31public:
32 plDocumentRootObject(const plRTTI* pRootType)
33 : plDocumentStorageObject(pRootType)
34 {
35 m_Guid = plUuid::MakeStableUuidFromString("DocumentRoot");
36 }
37
38public:
39 virtual void InsertSubObject(plDocumentObject* pObject, plStringView sProperty, const plVariant& index) override;
40 virtual void RemoveSubObject(plDocumentObject* pObject) override;
41};
42
45{
47
48 = default;
49
50 const plAbstractProperty* GetProperty() const;
51 plVariant getInsertIndex() const;
52 enum class Type
53 {
54 BeforeReset,
55 AfterReset,
56 BeforeObjectAdded,
57 AfterObjectAdded,
58 BeforeObjectRemoved,
59 AfterObjectRemoved,
60 BeforeObjectMoved,
61 AfterObjectMoved,
62 AfterObjectMoved2,
63 };
64
65 Type m_EventType;
66 const plDocument* m_pDocument = nullptr;
67 const plDocumentObject* m_pObject = nullptr;
68 const plDocumentObject* m_pPreviousParent = nullptr;
69 const plDocumentObject* m_pNewParent = nullptr;
70 plString m_sParentProperty;
71 plVariant m_OldPropertyIndex;
72 plVariant m_NewPropertyIndex;
73};
74
77{
78 plDocumentObjectPropertyEvent() { m_pObject = nullptr; }
79 plVariant getInsertIndex() const;
80
81 enum class Type
82 {
83 PropertySet,
84 PropertyInserted,
85 PropertyRemoved,
86 PropertyMoved,
87 };
88
89 Type m_EventType;
90 const plDocumentObject* m_pObject;
91 plVariant m_OldValue;
92 plVariant m_NewValue;
93 plString m_sProperty;
94 plVariant m_OldIndex;
95 plVariant m_NewIndex;
96};
97
100{
101 plDocumentObjectEvent() { m_pObject = nullptr; }
102
103 enum class Type
104 {
105 BeforeObjectDestroyed,
106 AfterObjectCreated,
107 Invalid
108 };
109
110 Type m_EventType = Type::Invalid;
111 const plDocumentObject* m_pObject;
112};
113
115class PL_TOOLSFOUNDATION_DLL plDocumentObjectManager
116{
117public:
118 // \brief Storage for the object manager so it can be swapped when using multiple sub documents.
119 class Storage : public plRefCounted
120 {
121 public:
122 Storage(const plRTTI* pRootType);
123
124 plDocument* m_pDocument = nullptr;
125 plDocumentRootObject m_RootObject;
126
128
132 };
133
134public:
138
139 plDocumentObjectManager(const plRTTI* pRootType = plDocumentRoot::GetStaticRTTI());
140 virtual ~plDocumentObjectManager();
141 void SetDocument(plDocument* pDocument) { m_pObjectStorage->m_pDocument = pDocument; }
142
143 // Object Construction / Destruction
144 // holds object data
145 plDocumentObject* CreateObject(const plRTTI* pRtti, plUuid guid = plUuid());
146
147 void DestroyObject(plDocumentObject* pObject);
148 virtual void DestroyAllObjects();
149 virtual void GetCreateableTypes(plHybridArray<const plRTTI*, 32>& ref_types) const {};
150
151 void PatchEmbeddedClassObjects(const plDocumentObject* pObject) const;
152
153 const plDocumentObject* GetRootObject() const { return &m_pObjectStorage->m_RootObject; }
154 plDocumentObject* GetRootObject() { return &m_pObjectStorage->m_RootObject; }
155 const plDocumentObject* GetObject(const plUuid& guid) const;
156 plDocumentObject* GetObject(const plUuid& guid);
157 const plDocument* GetDocument() const { return m_pObjectStorage->m_pDocument; }
158 plDocument* GetDocument() { return m_pObjectStorage->m_pDocument; }
159
160 // Property Change
161 plStatus SetValue(plDocumentObject* pObject, plStringView sProperty, const plVariant& newValue, plVariant index = plVariant());
162 plStatus InsertValue(plDocumentObject* pObject, plStringView sProperty, const plVariant& newValue, plVariant index = plVariant());
163 plStatus RemoveValue(plDocumentObject* pObject, plStringView sProperty, plVariant index = plVariant());
164 plStatus MoveValue(plDocumentObject* pObject, plStringView sProperty, const plVariant& oldIndex, const plVariant& newIndex);
165
166 // Structure Change
167 void AddObject(plDocumentObject* pObject, plDocumentObject* pParent, plStringView sParentProperty, plVariant index);
168 void RemoveObject(plDocumentObject* pObject);
169 void MoveObject(plDocumentObject* pObject, plDocumentObject* pNewParent, plStringView sParentProperty, plVariant index);
170
171 // Structure Change Test
172 plStatus CanAdd(const plRTTI* pRtti, const plDocumentObject* pParent, plStringView sParentProperty, const plVariant& index) const;
173 plStatus CanRemove(const plDocumentObject* pObject) const;
174 plStatus CanMove(const plDocumentObject* pObject, const plDocumentObject* pNewParent, plStringView sParentProperty, const plVariant& index) const;
175 plStatus CanSelect(const plDocumentObject* pObject) const;
176
177 bool IsUnderRootProperty(plStringView sRootProperty, const plDocumentObject* pObject) const;
178 bool IsUnderRootProperty(plStringView sRootProperty, const plDocumentObject* pParent, plStringView sParentProperty) const;
179 bool IsTemporary(const plDocumentObject* pObject) const;
180 bool IsTemporary(const plDocumentObject* pParent, plStringView sParentProperty) const;
181
183 plSharedPtr<plDocumentObjectManager::Storage> GetStorage() { return m_pObjectStorage; }
184
185private:
186 virtual plDocumentObject* InternalCreateObject(const plRTTI* pRtti) { return PL_DEFAULT_NEW(plDocumentStorageObject, pRtti); }
187 virtual void InternalDestroyObject(plDocumentObject* pObject) { PL_DEFAULT_DELETE(pObject); }
188
189 void InternalAddObject(plDocumentObject* pObject, plDocumentObject* pParent, plStringView sParentProperty, plVariant index);
190 void InternalRemoveObject(plDocumentObject* pObject);
191 void InternalMoveObject(plDocumentObject* pNewParent, plDocumentObject* pObject, plStringView sParentProperty, plVariant index);
192
193 virtual plStatus InternalCanAdd(const plRTTI* pRtti, const plDocumentObject* pParent, plStringView sParentProperty, const plVariant& index) const
194 {
195 return plStatus(PL_SUCCESS);
196 };
197 virtual plStatus InternalCanRemove(const plDocumentObject* pObject) const { return plStatus(PL_SUCCESS); };
198 virtual plStatus InternalCanMove(
199 const plDocumentObject* pObject, const plDocumentObject* pNewParent, plStringView sParentProperty, const plVariant& index) const
200 {
201 return plStatus(PL_SUCCESS);
202 };
203 virtual plStatus InternalCanSelect(const plDocumentObject* pObject) const { return plStatus(PL_SUCCESS); };
204
205 void RecursiveAddGuids(plDocumentObject* pObject);
206 void RecursiveRemoveGuids(plDocumentObject* pObject);
207 void PatchEmbeddedClassObjectsInternal(plDocumentObject* pObject, const plRTTI* pType, bool addToDoc);
208
209private:
210 friend class plObjectAccessorBase;
211
213
214 plCopyOnBroadcastEvent<const plDocumentObjectStructureEvent&>::Unsubscriber m_StructureEventsUnsubscriber;
215 plCopyOnBroadcastEvent<const plDocumentObjectPropertyEvent&>::Unsubscriber m_PropertyEventsUnsubscriber;
216 plEvent<const plDocumentObjectEvent&>::Unsubscriber m_ObjectEventsUnsubscriber;
217};
This is the base interface for all properties in the reflection system. It provides enough informatio...
Definition AbstractProperty.h:150
Definition Document.h:57
Definition DocumentObjectBase.h:11
Definition DocumentObjectManager.h:120
Represents to content of a document. Every document has exactly one root object under which all objec...
Definition DocumentObjectManager.h:116
Standard root object for most documents. m_RootObjects stores what is in the document and m_TempObjec...
Definition DocumentObjectManager.h:21
Implementation detail of plDocumentObjectManager.
Definition DocumentObjectManager.h:30
Definition DocumentObjectBase.h:61
Definition Event.h:177
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 ObjectAccessorBase.h:8
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
Base class for reference counted objects.
Definition RefCounted.h:52
All classes that should be dynamically reflectable, need to be derived from this base class.
Definition DynamicRTTI.h:86
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
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
static plUuid MakeStableUuidFromString(plStringView sString)
Creates a uuid from a string. The result is always the same for the same string.
Definition UuidGenerator.cpp:5
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
Used by plDocumentObjectManager::m_ObjectEvents.
Definition DocumentObjectManager.h:100
Used by plDocumentObjectManager::m_PropertyEvents.
Definition DocumentObjectManager.h:77
Used by plDocumentObjectManager::m_StructureEvents.
Definition DocumentObjectManager.h:45
An plResult with an additional message for the reason of failure.
Definition Status.h:12