Plasma Engine  2.0
Loading...
Searching...
No Matches
MapProperty.h
1#pragma once
2
4
5#include <Foundation/Reflection/Implementation/AbstractProperty.h>
6
7class plRTTI;
8
9template <typename Type>
11{
12public:
13 plTypedMapProperty(const char* szPropertyName)
14 : plAbstractMapProperty(szPropertyName)
15 {
16 m_Flags = plPropertyFlags::GetParameterFlags<Type>();
17 static_assert(
18 !std::is_pointer<Type>::value ||
20 "Pointer to standard types are not supported.");
21 }
22
23 virtual const plRTTI* GetSpecificType() const override { return plGetStaticRTTI<typename plTypeTraits<Type>::NonConstReferencePointerType>(); }
24};
25
26
27template <typename Class, typename Type, typename Container>
29{
30public:
31 using ContainerType = typename plTypeTraits<Container>::NonConstReferenceType;
32 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
33
34 using InsertFunc = void (Class::*)(const char* szKey, Type value);
35 using RemoveFunc = void (Class::*)(const char* szKey);
36 using GetValueFunc = bool (Class::*)(const char* szKey, RealType& value) const;
37 using GetKeyRangeFunc = Container (Class::*)() const;
38
39 plAccessorMapProperty(const char* szPropertyName, GetKeyRangeFunc getKeys, GetValueFunc getValue, InsertFunc insert, RemoveFunc remove)
40 : plTypedMapProperty<Type>(szPropertyName)
41 {
42 PL_ASSERT_DEBUG(getKeys != nullptr, "The getKeys function of a map property cannot be nullptr.");
43 PL_ASSERT_DEBUG(getValue != nullptr, "The GetValueFunc function of a map property cannot be nullptr.");
44
45 m_GetKeyRange = getKeys;
46 m_GetValue = getValue;
47 m_Insert = insert;
48 m_Remove = remove;
49
50 if (m_Insert == nullptr || remove == nullptr)
51 plAbstractMapProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
52 }
53
54 virtual bool IsEmpty(const void* pInstance) const override
55 {
56 // this should be decltype(auto) c = ...; but MSVC 16 is too dumb for that (MSVC 15 works fine)
57 decltype((static_cast<const Class*>(pInstance)->*m_GetKeyRange)()) c = (static_cast<const Class*>(pInstance)->*m_GetKeyRange)();
58
59 return begin(c) == end(c);
60 }
61
62 virtual void Clear(void* pInstance) const override
63 {
64 while (true)
65 {
66 // this should be decltype(auto) c = ...; but MSVC 16 is too dumb for that (MSVC 15 works fine)
67 decltype((static_cast<const Class*>(pInstance)->*m_GetKeyRange)()) c = (static_cast<const Class*>(pInstance)->*m_GetKeyRange)();
68
69 auto it = begin(c);
70 if (it != end(c))
71 Remove(pInstance, *it);
72 else
73 return;
74 }
75 }
76
77 virtual void Insert(void* pInstance, const char* szKey, const void* pObject) const override
78 {
79 PL_ASSERT_DEBUG(m_Insert != nullptr, "The property '{0}' has no insert function, thus it is read-only.", plAbstractProperty::GetPropertyName());
80 (static_cast<Class*>(pInstance)->*m_Insert)(szKey, *static_cast<const RealType*>(pObject));
81 }
82
83 virtual void Remove(void* pInstance, const char* szKey) const override
84 {
85 PL_ASSERT_DEBUG(m_Remove != nullptr, "The property '{0}' has no remove function, thus it is read-only.", plAbstractProperty::GetPropertyName());
86 (static_cast<Class*>(pInstance)->*m_Remove)(szKey);
87 }
88
89 virtual bool Contains(const void* pInstance, const char* szKey) const override
90 {
91 RealType value;
92 return (static_cast<const Class*>(pInstance)->*m_GetValue)(szKey, value);
93 }
94
95 virtual bool GetValue(const void* pInstance, const char* szKey, void* pObject) const override
96 {
97 return (static_cast<const Class*>(pInstance)->*m_GetValue)(szKey, *static_cast<RealType*>(pObject));
98 }
99
100 virtual void GetKeys(const void* pInstance, plHybridArray<plString, 16>& out_keys) const override
101 {
102 out_keys.Clear();
103 decltype(auto) c = (static_cast<const Class*>(pInstance)->*m_GetKeyRange)();
104 for (const auto& key : c)
105 {
106 out_keys.PushBack(key);
107 }
108 }
109
110private:
111 GetKeyRangeFunc m_GetKeyRange;
112 GetValueFunc m_GetValue;
113 InsertFunc m_Insert;
114 RemoveFunc m_Remove;
115};
116
117
118template <typename Class, typename Type, typename Container>
120{
121public:
122 using ContainerType = typename plTypeTraits<Container>::NonConstReferenceType;
123 using ContainerSubType = typename plContainerSubTypeResolver<ContainerType>::Type;
124 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
125
126 using InsertFunc = void (Class::*)(const char* szKey, Type value);
127 using RemoveFunc = void (Class::*)(const char* szKey);
128 using GetContainerFunc = Container (Class::*)() const;
129
130 plWriteAccessorMapProperty(const char* szPropertyName, GetContainerFunc getContainer, InsertFunc insert, RemoveFunc remove)
131 : plTypedMapProperty<Type>(szPropertyName)
132 {
133 PL_ASSERT_DEBUG(getContainer != nullptr, "The get count function of a map property cannot be nullptr.");
134
135 m_GetContainer = getContainer;
136 m_Insert = insert;
137 m_Remove = remove;
138
139 if (m_Insert == nullptr)
140 plAbstractMapProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
141 }
142
143 virtual bool IsEmpty(const void* pInstance) const override { return (static_cast<const Class*>(pInstance)->*m_GetContainer)().IsEmpty(); }
144
145 virtual void Clear(void* pInstance) const override
146 {
147 decltype(auto) c = (static_cast<const Class*>(pInstance)->*m_GetContainer)();
148 while (!IsEmpty(pInstance))
149 {
150 auto it = c.GetIterator();
151 Remove(pInstance, it.Key());
152 }
153 }
154
155 virtual void Insert(void* pInstance, const char* szKey, const void* pObject) const override
156 {
157 PL_ASSERT_DEBUG(m_Insert != nullptr, "The property '{0}' has no insert function, thus it is read-only.", plAbstractProperty::GetPropertyName());
158 (static_cast<Class*>(pInstance)->*m_Insert)(szKey, *static_cast<const RealType*>(pObject));
159 }
160
161 virtual void Remove(void* pInstance, const char* szKey) const override
162 {
163 PL_ASSERT_DEBUG(m_Remove != nullptr, "The property '{0}' has no remove function, thus it is read-only.", plAbstractProperty::GetPropertyName());
164 (static_cast<Class*>(pInstance)->*m_Remove)(szKey);
165 }
166
167 virtual bool Contains(const void* pInstance, const char* szKey) const override
168 {
169 return (static_cast<const Class*>(pInstance)->*m_GetContainer)().Contains(szKey);
170 }
171
172 virtual bool GetValue(const void* pInstance, const char* szKey, void* pObject) const override
173 {
174 decltype(auto) c = (static_cast<const Class*>(pInstance)->*m_GetContainer)();
175 const RealType* value = c.GetValue(szKey);
176 if (value)
177 {
178 *static_cast<RealType*>(pObject) = *value;
179 }
180 return value != nullptr;
181 }
182
183 virtual void GetKeys(const void* pInstance, plHybridArray<plString, 16>& out_keys) const override
184 {
185 decltype(auto) c = (static_cast<const Class*>(pInstance)->*m_GetContainer)();
186 out_keys.Clear();
187 for (auto it = c.GetIterator(); it.IsValid(); ++it)
188 {
189 out_keys.PushBack(it.Key());
190 }
191 }
192
193private:
194 GetContainerFunc m_GetContainer;
195 InsertFunc m_Insert;
196 RemoveFunc m_Remove;
197};
198
199
200
201template <typename Class, typename Container, Container Class::*Member>
203{
204 using ContainerType = typename plTypeTraits<Container>::NonConstReferenceType;
205 using Type = typename plTypeTraits<typename plContainerSubTypeResolver<ContainerType>::Type>::NonConstReferenceType;
206
207 static const ContainerType& GetConstContainer(const Class* pInstance) { return (*pInstance).*Member; }
208
209 static ContainerType& GetContainer(Class* pInstance) { return (*pInstance).*Member; }
210};
211
212
213template <typename Class, typename Container, typename Type>
214class plMemberMapProperty : public plTypedMapProperty<typename plTypeTraits<Type>::NonConstReferenceType>
215{
216public:
217 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
218 using GetConstContainerFunc = const Container& (*)(const Class* pInstance);
219 using GetContainerFunc = Container& (*)(Class* pInstance);
220
221 plMemberMapProperty(const char* szPropertyName, GetConstContainerFunc constGetter, GetContainerFunc getter)
222 : plTypedMapProperty<RealType>(szPropertyName)
223 {
224 PL_ASSERT_DEBUG(constGetter != nullptr, "The const get count function of an array property cannot be nullptr.");
225
226 m_ConstGetter = constGetter;
227 m_Getter = getter;
228
229 if (m_Getter == nullptr)
230 plAbstractMapProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
231 }
232
233 virtual bool IsEmpty(const void* pInstance) const override { return m_ConstGetter(static_cast<const Class*>(pInstance)).IsEmpty(); }
234
235 virtual void Clear(void* pInstance) const override
236 {
237 PL_ASSERT_DEBUG(
238 m_Getter != nullptr, "The property '{0}' has no non-const set accessor function, thus it is read-only.", plAbstractProperty::GetPropertyName());
239 m_Getter(static_cast<Class*>(pInstance)).Clear();
240 }
241
242 virtual void Insert(void* pInstance, const char* szKey, const void* pObject) const override
243 {
244 PL_ASSERT_DEBUG(
245 m_Getter != nullptr, "The property '{0}' has no non-const set accessor function, thus it is read-only.", plAbstractProperty::GetPropertyName());
246 m_Getter(static_cast<Class*>(pInstance)).Insert(szKey, *static_cast<const RealType*>(pObject));
247 }
248
249 virtual void Remove(void* pInstance, const char* szKey) const override
250 {
251 PL_ASSERT_DEBUG(
252 m_Getter != nullptr, "The property '{0}' has no non-const set accessor function, thus it is read-only.", plAbstractProperty::GetPropertyName());
253 m_Getter(static_cast<Class*>(pInstance)).Remove(szKey);
254 }
255
256 virtual bool Contains(const void* pInstance, const char* szKey) const override
257 {
258 return m_ConstGetter(static_cast<const Class*>(pInstance)).Contains(szKey);
259 }
260
261 virtual bool GetValue(const void* pInstance, const char* szKey, void* pObject) const override
262 {
263 const RealType* value = m_ConstGetter(static_cast<const Class*>(pInstance)).GetValue(szKey);
264 if (value)
265 {
266 *static_cast<RealType*>(pObject) = *value;
267 }
268 return value != nullptr;
269 }
270
271 virtual void GetKeys(const void* pInstance, plHybridArray<plString, 16>& out_keys) const override
272 {
273 decltype(auto) c = m_ConstGetter(static_cast<const Class*>(pInstance));
274 out_keys.Clear();
275 for (auto it = c.GetIterator(); it.IsValid(); ++it)
276 {
277 out_keys.PushBack(it.Key());
278 }
279 }
280
281private:
282 GetConstContainerFunc m_ConstGetter;
283 GetContainerFunc m_Getter;
284};
The base class for a property that represents a set of values.
Definition AbstractProperty.h:348
plAbstractMapProperty(const char *szPropertyName)
Passes the property name through to plAbstractProperty.
Definition AbstractProperty.h:351
const char * GetPropertyName() const
Returns the name of the property.
Definition AbstractProperty.h:158
Definition MapProperty.h:29
virtual bool GetValue(const void *pInstance, const char *szKey, void *pObject) const override
Writes element at index uiIndex to the target of pObject.
Definition MapProperty.h:95
virtual void Clear(void *pInstance) const override
Clears the set.
Definition MapProperty.h:62
virtual bool Contains(const void *pInstance, const char *szKey) const override
Returns whether the target of pObject is in the set.
Definition MapProperty.h:89
virtual void Insert(void *pInstance, const char *szKey, const void *pObject) const override
Inserts the target of pObject into the set.
Definition MapProperty.h:77
virtual void GetKeys(const void *pInstance, plHybridArray< plString, 16 > &out_keys) const override
Writes the content of the set to out_keys.
Definition MapProperty.h:100
virtual void Remove(void *pInstance, const char *szKey) const override
Removes the target of pObject from the set.
Definition MapProperty.h:83
virtual bool IsEmpty(const void *pInstance) const override
Returns whether the set is empty.
Definition MapProperty.h:54
void PushBack(const T &value)
Pushes value at the end of the array.
Definition ArrayBase_inl.h:333
void Clear()
Clears the array.
Definition ArrayBase_inl.h:184
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Definition MapProperty.h:215
virtual bool GetValue(const void *pInstance, const char *szKey, void *pObject) const override
Writes element at index uiIndex to the target of pObject.
Definition MapProperty.h:261
virtual bool Contains(const void *pInstance, const char *szKey) const override
Returns whether the target of pObject is in the set.
Definition MapProperty.h:256
virtual void Insert(void *pInstance, const char *szKey, const void *pObject) const override
Inserts the target of pObject into the set.
Definition MapProperty.h:242
virtual bool IsEmpty(const void *pInstance) const override
Returns whether the set is empty.
Definition MapProperty.h:233
virtual void GetKeys(const void *pInstance, plHybridArray< plString, 16 > &out_keys) const override
Writes the content of the set to out_keys.
Definition MapProperty.h:271
virtual void Remove(void *pInstance, const char *szKey) const override
Removes the target of pObject from the set.
Definition MapProperty.h:249
virtual void Clear(void *pInstance) const override
Clears the set.
Definition MapProperty.h:235
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
Definition MapProperty.h:11
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 MapProperty.h:23
Definition MapProperty.h:120
virtual bool IsEmpty(const void *pInstance) const override
Returns whether the set is empty.
Definition MapProperty.h:143
virtual void GetKeys(const void *pInstance, plHybridArray< plString, 16 > &out_keys) const override
Writes the content of the set to out_keys.
Definition MapProperty.h:183
virtual bool Contains(const void *pInstance, const char *szKey) const override
Returns whether the target of pObject is in the set.
Definition MapProperty.h:167
virtual void Remove(void *pInstance, const char *szKey) const override
Removes the target of pObject from the set.
Definition MapProperty.h:161
virtual void Insert(void *pInstance, const char *szKey, const void *pObject) const override
Inserts the target of pObject into the set.
Definition MapProperty.h:155
virtual bool GetValue(const void *pInstance, const char *szKey, void *pObject) const override
Writes element at index uiIndex to the target of pObject.
Definition MapProperty.h:172
virtual void Clear(void *pInstance) const override
Clears the set.
Definition MapProperty.h:145
PL_ALWAYS_INLINE void Add(const plBitflags< T > &rhs)
Sets the given flag.
Definition Bitflags.h:151
Template that allows to probe a container for its element type.
Definition AbstractProperty.h:455
Definition MapProperty.h:203
@ 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