Plasma Engine  2.0
Loading...
Searching...
No Matches
SetProperty.h
1#pragma once
2
4
5#include <Foundation/Reflection/Implementation/AbstractProperty.h>
6
8template <typename Type>
10{
11public:
12 plTypedSetProperty(const char* szPropertyName)
13 : plAbstractSetProperty(szPropertyName)
14 {
15 m_Flags = plPropertyFlags::GetParameterFlags<Type>();
16 }
17
18 virtual const plRTTI* GetSpecificType() const override { return plGetStaticRTTI<typename plTypeTraits<Type>::NonConstReferencePointerType>(); }
19};
20
22template <>
23class plTypedSetProperty<const char*> : public plAbstractSetProperty
24{
25public:
26 plTypedSetProperty(const char* szPropertyName)
27 : plAbstractSetProperty(szPropertyName)
28 {
29 m_Flags = plPropertyFlags::GetParameterFlags<const char*>();
30 }
31
32 virtual const plRTTI* GetSpecificType() const override { return plGetStaticRTTI<const char*>(); }
33};
34
35
36template <typename Class, typename Type, typename Container>
38{
39public:
40 using ContainerType = typename plTypeTraits<Container>::NonConstReferenceType;
41 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
42
43 using InsertFunc = void (Class::*)(Type value);
44 using RemoveFunc = void (Class::*)(Type value);
45 using GetValuesFunc = Container (Class::*)() const;
46
47 plAccessorSetProperty(const char* szPropertyName, GetValuesFunc getValues, InsertFunc insert, RemoveFunc remove)
48 : plTypedSetProperty<Type>(szPropertyName)
49 {
50 PL_ASSERT_DEBUG(getValues != nullptr, "The get values function of an set property cannot be nullptr.");
51
52 m_GetValues = getValues;
53 m_Insert = insert;
54 m_Remove = remove;
55
56 if (m_Insert == nullptr || m_Remove == nullptr)
57 plAbstractSetProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
58 }
59
60
61 virtual bool IsEmpty(const void* pInstance) const override { return (static_cast<const Class*>(pInstance)->*m_GetValues)().IsEmpty(); }
62
63 virtual void Clear(void* pInstance) const override
64 {
65 PL_ASSERT_DEBUG(m_Insert != nullptr && m_Remove != nullptr, "The property '{0}' has no remove and insert function, thus it is read-only",
67
68 // We must not cache the container c here as the Remove can make it invalid
69 // e.g. plArrayPtr by value.
70 while (!IsEmpty(pInstance))
71 {
72 // this should be decltype(auto) c = ...; but MSVC 16 is too dumb for that (MSVC 15 works fine)
73 decltype((static_cast<const Class*>(pInstance)->*m_GetValues)()) c = (static_cast<const Class*>(pInstance)->*m_GetValues)();
74 auto it = cbegin(c);
75 RealType value = *it;
76 Remove(pInstance, &value);
77 }
78 }
79
80 virtual void Insert(void* pInstance, const void* pObject) const override
81 {
82 PL_ASSERT_DEBUG(m_Insert != nullptr, "The property '{0}' has no insert function, thus it is read-only.", plAbstractProperty::GetPropertyName());
83 (static_cast<Class*>(pInstance)->*m_Insert)(*static_cast<const RealType*>(pObject));
84 }
85
86 virtual void Remove(void* pInstance, const void* pObject) const override
87 {
88 PL_ASSERT_DEBUG(m_Remove != nullptr, "The property '{0}' has no setter function, thus it is read-only.", plAbstractProperty::GetPropertyName());
89 (static_cast<Class*>(pInstance)->*m_Remove)(*static_cast<const RealType*>(pObject));
90 }
91
92 virtual bool Contains(const void* pInstance, const void* pObject) const override
93 {
94 for (const auto& value : (static_cast<const Class*>(pInstance)->*m_GetValues)())
95 {
96 if (value == *static_cast<const RealType*>(pObject))
97 return true;
98 }
99 return false;
100 }
101
102 virtual void GetValues(const void* pInstance, plDynamicArray<plVariant>& out_keys) const override
103 {
104 out_keys.Clear();
105 for (const auto& value : (static_cast<const Class*>(pInstance)->*m_GetValues)())
106 {
107 out_keys.PushBack(plVariant(value));
108 }
109 }
110
111private:
112 GetValuesFunc m_GetValues;
113 InsertFunc m_Insert;
114 RemoveFunc m_Remove;
115};
116
117
118
119template <typename Class, typename Container, Container Class::*Member>
121{
122 using ContainerType = typename plTypeTraits<Container>::NonConstReferenceType;
123 using Type = typename plTypeTraits<typename plContainerSubTypeResolver<ContainerType>::Type>::NonConstReferenceType;
124
125 static const ContainerType& GetConstContainer(const Class* pInstance) { return (*pInstance).*Member; }
126
127 static ContainerType& GetContainer(Class* pInstance) { return (*pInstance).*Member; }
128};
129
130
131template <typename Class, typename Container, typename Type>
132class plMemberSetProperty : public plTypedSetProperty<typename plTypeTraits<Type>::NonConstReferenceType>
133{
134public:
135 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
136 using GetConstContainerFunc = const Container& (*)(const Class* pInstance);
137 using GetContainerFunc = Container& (*)(Class* pInstance);
138
139 plMemberSetProperty(const char* szPropertyName, GetConstContainerFunc constGetter, GetContainerFunc getter)
140 : plTypedSetProperty<RealType>(szPropertyName)
141 {
142 PL_ASSERT_DEBUG(constGetter != nullptr, "The const get count function of an set property cannot be nullptr.");
143
144 m_ConstGetter = constGetter;
145 m_Getter = getter;
146
147 if (m_Getter == nullptr)
148 plAbstractSetProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
149 }
150
151 virtual bool IsEmpty(const void* pInstance) const override { return m_ConstGetter(static_cast<const Class*>(pInstance)).IsEmpty(); }
152
153 virtual void Clear(void* pInstance) const override
154 {
155 PL_ASSERT_DEBUG(
156 m_Getter != nullptr, "The property '{0}' has no non-const set accessor function, thus it is read-only.", plAbstractProperty::GetPropertyName());
157 m_Getter(static_cast<Class*>(pInstance)).Clear();
158 }
159
160 virtual void Insert(void* pInstance, const void* pObject) const override
161 {
162 PL_ASSERT_DEBUG(
163 m_Getter != nullptr, "The property '{0}' has no non-const set accessor function, thus it is read-only.", plAbstractProperty::GetPropertyName());
164 m_Getter(static_cast<Class*>(pInstance)).Insert(*static_cast<const RealType*>(pObject));
165 }
166
167 virtual void Remove(void* pInstance, const void* pObject) const override
168 {
169 PL_ASSERT_DEBUG(
170 m_Getter != nullptr, "The property '{0}' has no non-const set accessor function, thus it is read-only.", plAbstractProperty::GetPropertyName());
171 m_Getter(static_cast<Class*>(pInstance)).Remove(*static_cast<const RealType*>(pObject));
172 }
173
174 virtual bool Contains(const void* pInstance, const void* pObject) const override
175 {
176 return m_ConstGetter(static_cast<const Class*>(pInstance)).Contains(*static_cast<const RealType*>(pObject));
177 }
178
179 virtual void GetValues(const void* pInstance, plDynamicArray<plVariant>& out_keys) const override
180 {
181 out_keys.Clear();
182 for (const auto& value : m_ConstGetter(static_cast<const Class*>(pInstance)))
183 {
184 out_keys.PushBack(plVariant(value));
185 }
186 }
187
188private:
189 GetConstContainerFunc m_ConstGetter;
190 GetContainerFunc m_Getter;
191};
const char * GetPropertyName() const
Returns the name of the property.
Definition AbstractProperty.h:158
The base class for a property that represents a set of values.
Definition AbstractProperty.h:313
plAbstractSetProperty(const char *szPropertyName)
Passes the property name through to plAbstractProperty.
Definition AbstractProperty.h:316
Definition SetProperty.h:38
virtual void Clear(void *pInstance) const override
Clears the set.
Definition SetProperty.h:63
virtual bool Contains(const void *pInstance, const void *pObject) const override
Returns whether the target of pObject is in the set.
Definition SetProperty.h:92
virtual void Remove(void *pInstance, const void *pObject) const override
Removes the target of pObject from the set.
Definition SetProperty.h:86
virtual void GetValues(const void *pInstance, plDynamicArray< plVariant > &out_keys) const override
Writes the content of the set to out_keys.
Definition SetProperty.h:102
virtual bool IsEmpty(const void *pInstance) const override
Returns whether the set is empty.
Definition SetProperty.h:61
virtual void Insert(void *pInstance, const void *pObject) const override
Inserts the target of pObject into the set.
Definition SetProperty.h:80
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
Definition DynamicArray.h:81
Definition SetProperty.h:133
virtual void Insert(void *pInstance, const void *pObject) const override
Inserts the target of pObject into the set.
Definition SetProperty.h:160
virtual void Clear(void *pInstance) const override
Clears the set.
Definition SetProperty.h:153
virtual bool Contains(const void *pInstance, const void *pObject) const override
Returns whether the target of pObject is in the set.
Definition SetProperty.h:174
virtual void Remove(void *pInstance, const void *pObject) const override
Removes the target of pObject from the set.
Definition SetProperty.h:167
virtual bool IsEmpty(const void *pInstance) const override
Returns whether the set is empty.
Definition SetProperty.h:151
virtual void GetValues(const void *pInstance, plDynamicArray< plVariant > &out_keys) const override
Writes the content of the set to out_keys.
Definition SetProperty.h:179
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 SetProperty.h:32
Do not cast into this class or any of its derived classes, use plAbstractSetProperty instead.
Definition SetProperty.h:10
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 SetProperty.h:18
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
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 SetProperty.h:121
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