Plasma Engine  2.0
Loading...
Searching...
No Matches
ArrayProperty.h
1#pragma once
2
4
5#include <Foundation/Reflection/Implementation/AbstractProperty.h>
6
7class plRTTI;
8
10template <typename Type>
12{
13public:
14 plTypedArrayProperty(const char* szPropertyName)
15 : plAbstractArrayProperty(szPropertyName)
16 {
17 m_Flags = plPropertyFlags::GetParameterFlags<Type>();
18 static_assert(!std::is_pointer<Type>::value ||
21 "Pointer to standard types are not supported.");
22 }
23
24 virtual const plRTTI* GetSpecificType() const override { return plGetStaticRTTI<typename plTypeTraits<Type>::NonConstReferencePointerType>(); }
25};
26
28template <>
30{
31public:
32 plTypedArrayProperty(const char* szPropertyName)
33 : plAbstractArrayProperty(szPropertyName)
34 {
35 m_Flags = plPropertyFlags::GetParameterFlags<const char*>();
36 }
37
38 virtual const plRTTI* GetSpecificType() const override { return plGetStaticRTTI<const char*>(); }
39};
40
41
42template <typename Class, typename Type>
44{
45public:
46 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
47 using GetCountFunc = plUInt32 (Class::*)() const;
48 using GetValueFunc = Type (Class::*)(plUInt32 uiIndex) const;
49 using SetValueFunc = void (Class::*)(plUInt32 uiIndex, Type value);
50 using InsertFunc = void (Class::*)(plUInt32 uiIndex, Type value);
51 using RemoveFunc = void (Class::*)(plUInt32 uiIndex);
52
53
55 const char* szPropertyName, GetCountFunc getCount, GetValueFunc getter, SetValueFunc setter, InsertFunc insert, RemoveFunc remove)
56 : plTypedArrayProperty<Type>(szPropertyName)
57 {
58 PL_ASSERT_DEBUG(getCount != nullptr, "The get count function of an array property cannot be nullptr.");
59 PL_ASSERT_DEBUG(getter != nullptr, "The get value function of an array property cannot be nullptr.");
60
61 m_GetCount = getCount;
62 m_Getter = getter;
63 m_Setter = setter;
64 m_Insert = insert;
65 m_Remove = remove;
66
67 if (m_Setter == nullptr)
68 plAbstractArrayProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
69 }
70
71
72 virtual plUInt32 GetCount(const void* pInstance) const override { return (static_cast<const Class*>(pInstance)->*m_GetCount)(); }
73
74 virtual void GetValue(const void* pInstance, plUInt32 uiIndex, void* pObject) const override
75 {
76 PL_ASSERT_DEBUG(uiIndex < GetCount(pInstance), "GetValue: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
77 *static_cast<RealType*>(pObject) = (static_cast<const Class*>(pInstance)->*m_Getter)(uiIndex);
78 }
79
80 virtual void SetValue(void* pInstance, plUInt32 uiIndex, const void* pObject) const override
81 {
82 PL_ASSERT_DEBUG(uiIndex < GetCount(pInstance), "SetValue: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
83 PL_ASSERT_DEBUG(m_Setter != nullptr, "The property '{0}' has no setter function, thus it is read-only.", plAbstractProperty::GetPropertyName());
84 (static_cast<Class*>(pInstance)->*m_Setter)(uiIndex, *static_cast<const RealType*>(pObject));
85 }
86
87 virtual void Insert(void* pInstance, plUInt32 uiIndex, const void* pObject) const override
88 {
89 PL_ASSERT_DEBUG(uiIndex <= GetCount(pInstance), "Insert: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
90 PL_ASSERT_DEBUG(m_Insert != nullptr, "The property '{0}' has no insert function, thus it is read-only.", plAbstractProperty::GetPropertyName());
91 (static_cast<Class*>(pInstance)->*m_Insert)(uiIndex, *static_cast<const RealType*>(pObject));
92 }
93
94 virtual void Remove(void* pInstance, plUInt32 uiIndex) const override
95 {
96 PL_ASSERT_DEBUG(uiIndex < GetCount(pInstance), "Remove: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
97 PL_ASSERT_DEBUG(m_Remove != nullptr, "The property '{0}' has no setter function, thus it is read-only.", plAbstractProperty::GetPropertyName());
98 (static_cast<Class*>(pInstance)->*m_Remove)(uiIndex);
99 }
100
101 virtual void Clear(void* pInstance) const override { SetCount(pInstance, 0); }
102
103 virtual void SetCount(void* pInstance, plUInt32 uiCount) const override
104 {
105 PL_ASSERT_DEBUG(m_Insert != nullptr && m_Remove != nullptr, "The property '{0}' has no remove and insert function, thus it is fixed-size.",
107 while (uiCount < GetCount(pInstance))
108 {
109 Remove(pInstance, GetCount(pInstance) - 1);
110 }
111 while (uiCount > GetCount(pInstance))
112 {
113 RealType elem = RealType();
114 Insert(pInstance, GetCount(pInstance), &elem);
115 }
116 }
117
118private:
119 GetCountFunc m_GetCount;
120 GetValueFunc m_Getter;
121 SetValueFunc m_Setter;
122 InsertFunc m_Insert;
123 RemoveFunc m_Remove;
124};
125
126
127
128template <typename Class, typename Container, Container Class::*Member>
130{
131 using ContainerType = typename plTypeTraits<Container>::NonConstReferenceType;
132 using Type = typename plTypeTraits<typename plContainerSubTypeResolver<ContainerType>::Type>::NonConstReferenceType;
133
134 static const ContainerType& GetConstContainer(const Class* pInstance) { return (*pInstance).*Member; }
135
136 static ContainerType& GetContainer(Class* pInstance) { return (*pInstance).*Member; }
137};
138
139
140template <typename Class, typename Container, typename Type>
141class plMemberArrayProperty : public plTypedArrayProperty<typename plTypeTraits<Type>::NonConstReferenceType>
142{
143public:
144 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
145 using GetConstContainerFunc = const Container& (*)(const Class* pInstance);
146 using GetContainerFunc = Container& (*)(Class* pInstance);
147
148 plMemberArrayProperty(const char* szPropertyName, GetConstContainerFunc constGetter, GetContainerFunc getter)
149 : plTypedArrayProperty<RealType>(szPropertyName)
150 {
151 PL_ASSERT_DEBUG(constGetter != nullptr, "The const get count function of an array property cannot be nullptr.");
152
153 m_ConstGetter = constGetter;
154 m_Getter = getter;
155
156 if (m_Getter == nullptr)
157 plAbstractArrayProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
158 }
159
160 virtual plUInt32 GetCount(const void* pInstance) const override { return m_ConstGetter(static_cast<const Class*>(pInstance)).GetCount(); }
161
162 virtual void GetValue(const void* pInstance, plUInt32 uiIndex, void* pObject) const override
163 {
164 PL_ASSERT_DEBUG(uiIndex < GetCount(pInstance), "GetValue: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
165 *static_cast<RealType*>(pObject) = m_ConstGetter(static_cast<const Class*>(pInstance))[uiIndex];
166 }
167
168 virtual void SetValue(void* pInstance, plUInt32 uiIndex, const void* pObject) const override
169 {
170 PL_ASSERT_DEBUG(uiIndex < GetCount(pInstance), "SetValue: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
171 PL_ASSERT_DEBUG(m_Getter != nullptr, "The property '{0}' has no non-const array accessor function, thus it is read-only.",
173 m_Getter(static_cast<Class*>(pInstance))[uiIndex] = *static_cast<const RealType*>(pObject);
174 }
175
176 virtual void Insert(void* pInstance, plUInt32 uiIndex, const void* pObject) const override
177 {
178 PL_ASSERT_DEBUG(uiIndex <= GetCount(pInstance), "Insert: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
179 PL_ASSERT_DEBUG(m_Getter != nullptr, "The property '{0}' has no non-const array accessor function, thus it is read-only.",
181 m_Getter(static_cast<Class*>(pInstance)).InsertAt(uiIndex, *static_cast<const RealType*>(pObject));
182 }
183
184 virtual void Remove(void* pInstance, plUInt32 uiIndex) const override
185 {
186 PL_ASSERT_DEBUG(uiIndex < GetCount(pInstance), "Remove: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
187 PL_ASSERT_DEBUG(m_Getter != nullptr, "The property '{0}' has no non-const array accessor function, thus it is read-only.",
189 m_Getter(static_cast<Class*>(pInstance)).RemoveAtAndCopy(uiIndex);
190 }
191
192 virtual void Clear(void* pInstance) const override
193 {
194 PL_ASSERT_DEBUG(m_Getter != nullptr, "The property '{0}' has no non-const array accessor function, thus it is read-only.",
196 m_Getter(static_cast<Class*>(pInstance)).Clear();
197 }
198
199 virtual void SetCount(void* pInstance, plUInt32 uiCount) const override
200 {
201 PL_ASSERT_DEBUG(m_Getter != nullptr, "The property '{0}' has no non-const array accessor function, thus it is read-only.",
203 m_Getter(static_cast<Class*>(pInstance)).SetCount(uiCount);
204 }
205
206 virtual void* GetValuePointer(void* pInstance, plUInt32 uiIndex) const override
207 {
208 PL_ASSERT_DEBUG(uiIndex < GetCount(pInstance), "GetValue: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
209 return &(m_Getter(static_cast<Class*>(pInstance))[uiIndex]);
210 }
211
212private:
213 GetConstContainerFunc m_ConstGetter;
214 GetContainerFunc m_Getter;
215};
216
218template <typename Class, typename Container, typename Type>
219class plMemberArrayReadOnlyProperty : public plTypedArrayProperty<typename plTypeTraits<Type>::NonConstReferenceType>
220{
221public:
222 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
223 using GetConstContainerFunc = const Container& (*)(const Class* pInstance);
224
225 plMemberArrayReadOnlyProperty(const char* szPropertyName, GetConstContainerFunc constGetter)
226 : plTypedArrayProperty<RealType>(szPropertyName)
227 {
228 PL_ASSERT_DEBUG(constGetter != nullptr, "The const get count function of an array property cannot be nullptr.");
229
230 m_ConstGetter = constGetter;
231 plAbstractArrayProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
232 }
233
234 virtual plUInt32 GetCount(const void* pInstance) const override { return m_ConstGetter(static_cast<const Class*>(pInstance)).GetCount(); }
235
236 virtual void GetValue(const void* pInstance, plUInt32 uiIndex, void* pObject) const override
237 {
238 PL_ASSERT_DEBUG(uiIndex < GetCount(pInstance), "GetValue: uiIndex ('{0}') is out of range ('{1}')", uiIndex, GetCount(pInstance));
239 *static_cast<RealType*>(pObject) = m_ConstGetter(static_cast<const Class*>(pInstance))[uiIndex];
240 }
241
242 virtual void SetValue(void* pInstance, plUInt32 uiIndex, const void* pObject) const override
243 {
244 PL_REPORT_FAILURE("The property '{0}' is read-only.", plAbstractProperty::GetPropertyName());
245 }
246
247 virtual void Insert(void* pInstance, plUInt32 uiIndex, const void* pObject) const override
248 {
249 PL_REPORT_FAILURE("The property '{0}' is read-only.", plAbstractProperty::GetPropertyName());
250 }
251
252 virtual void Remove(void* pInstance, plUInt32 uiIndex) const override
253 {
254 PL_REPORT_FAILURE("The property '{0}' is read-only.", plAbstractProperty::GetPropertyName());
255 }
256
257 virtual void Clear(void* pInstance) const override
258 {
259 PL_REPORT_FAILURE("The property '{0}' is read-only.", plAbstractProperty::GetPropertyName());
260 }
261
262 virtual void SetCount(void* pInstance, plUInt32 uiCount) const override
263 {
264 PL_REPORT_FAILURE("The property '{0}' is read-only.", plAbstractProperty::GetPropertyName());
265 }
266
267private:
268 GetConstContainerFunc m_ConstGetter;
269};
The base class for a property that represents an array of values.
Definition AbstractProperty.h:273
plAbstractArrayProperty(const char *szPropertyName)
Passes the property name through to plAbstractProperty.
Definition AbstractProperty.h:276
const char * GetPropertyName() const
Returns the name of the property.
Definition AbstractProperty.h:158
Definition ArrayProperty.h:44
virtual void Insert(void *pInstance, plUInt32 uiIndex, const void *pObject) const override
Inserts the target of pObject into the array at index uiIndex.
Definition ArrayProperty.h:87
virtual void Clear(void *pInstance) const override
Clears the array.
Definition ArrayProperty.h:101
virtual void SetCount(void *pInstance, plUInt32 uiCount) const override
Resizes the array to uiCount.
Definition ArrayProperty.h:103
virtual plUInt32 GetCount(const void *pInstance) const override
Returns number of elements.
Definition ArrayProperty.h:72
virtual void Remove(void *pInstance, plUInt32 uiIndex) const override
Removes the element in the array at index uiIndex.
Definition ArrayProperty.h:94
virtual void SetValue(void *pInstance, plUInt32 uiIndex, const void *pObject) const override
Writes the target of pObject to the element at index uiIndex.
Definition ArrayProperty.h:80
virtual void GetValue(const void *pInstance, plUInt32 uiIndex, void *pObject) const override
Writes element at index uiIndex to the target of pObject.
Definition ArrayProperty.h:74
Definition ArrayProperty.h:142
virtual void SetCount(void *pInstance, plUInt32 uiCount) const override
Resizes the array to uiCount.
Definition ArrayProperty.h:199
virtual void SetValue(void *pInstance, plUInt32 uiIndex, const void *pObject) const override
Writes the target of pObject to the element at index uiIndex.
Definition ArrayProperty.h:168
virtual void GetValue(const void *pInstance, plUInt32 uiIndex, void *pObject) const override
Writes element at index uiIndex to the target of pObject.
Definition ArrayProperty.h:162
virtual void Remove(void *pInstance, plUInt32 uiIndex) const override
Removes the element in the array at index uiIndex.
Definition ArrayProperty.h:184
virtual void Clear(void *pInstance) const override
Clears the array.
Definition ArrayProperty.h:192
virtual plUInt32 GetCount(const void *pInstance) const override
Returns number of elements.
Definition ArrayProperty.h:160
virtual void Insert(void *pInstance, plUInt32 uiIndex, const void *pObject) const override
Inserts the target of pObject into the array at index uiIndex.
Definition ArrayProperty.h:176
Read only version of plMemberArrayProperty that does not call any functions that modify the array....
Definition ArrayProperty.h:220
virtual void SetCount(void *pInstance, plUInt32 uiCount) const override
Resizes the array to uiCount.
Definition ArrayProperty.h:262
virtual void Insert(void *pInstance, plUInt32 uiIndex, const void *pObject) const override
Inserts the target of pObject into the array at index uiIndex.
Definition ArrayProperty.h:247
virtual void Clear(void *pInstance) const override
Clears the array.
Definition ArrayProperty.h:257
virtual void Remove(void *pInstance, plUInt32 uiIndex) const override
Removes the element in the array at index uiIndex.
Definition ArrayProperty.h:252
virtual void SetValue(void *pInstance, plUInt32 uiIndex, const void *pObject) const override
Writes the target of pObject to the element at index uiIndex.
Definition ArrayProperty.h:242
virtual plUInt32 GetCount(const void *pInstance) const override
Returns number of elements.
Definition ArrayProperty.h:234
virtual void GetValue(const void *pInstance, plUInt32 uiIndex, void *pObject) const override
Writes element at index uiIndex to the target of pObject.
Definition ArrayProperty.h:236
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
virtual const plRTTI * GetSpecificType() const override
Returns the type information of the constant property. Use this to cast this property to a specific v...
Definition ArrayProperty.h:38
Do not cast into this class or any of its derived classes, use plTypedArrayProperty instead.
Definition ArrayProperty.h:12
virtual const plRTTI * GetSpecificType() const override
Returns the type information of the constant property. Use this to cast this property to a specific v...
Definition ArrayProperty.h:24
Definition ArrayProperty.h:130
PL_ALWAYS_INLINE void Add(const plBitflags< T > &rhs)
Sets the given flag.
Definition Bitflags.h:151
@ ReadOnly
Can only be read but not modified.
Definition AbstractProperty.h:63
Definition TypeTraits.h:207
typename std::remove_const< typename std::remove_reference< T >::type >::type NonConstReferenceType
removes reference and const qualifier
Definition TypeTraits.h:218
A helper struct to convert the C++ type, which is passed as the template argument,...
Definition VariantType.h:97
@ Invalid
The variant stores no (valid) data at the moment.
Definition VariantType.h:27