Plasma Engine  2.0
Loading...
Searching...
No Matches
MemberProperty.h
1#pragma once
2
4
5#include <Foundation/Reflection/Implementation/AbstractProperty.h>
6#include <Foundation/Reflection/Implementation/StaticRTTI.h>
7#include <Foundation/Types/Variant.h>
8
9// ***********************************************
10// ***** Base class for accessing properties *****
11
12
18template <typename Type>
20{
21public:
23 plTypedMemberProperty(const char* szPropertyName)
24 : plAbstractMemberProperty(szPropertyName)
25 {
26 m_Flags = plPropertyFlags::GetParameterFlags<Type>();
27 static_assert(
28 !std::is_pointer<Type>::value ||
30 "Pointer to standard types are not supported.");
31 }
32
35 virtual const plRTTI* GetSpecificType() const override // [tested]
36 {
37 return plGetStaticRTTI<typename plTypeTraits<Type>::NonConstReferencePointerType>();
38 }
39
41 virtual Type GetValue(const void* pInstance) const = 0; // [tested]
42
46 virtual void SetValue(void* pInstance, Type value) const = 0; // [tested]
47
48 virtual void GetValuePtr(const void* pInstance, void* pObject) const override { *static_cast<Type*>(pObject) = GetValue(pInstance); };
49 virtual void SetValuePtr(void* pInstance, const void* pObject) const override { SetValue(pInstance, *static_cast<const Type*>(pObject)); };
50};
51
56template <>
58{
59public:
60 plTypedMemberProperty(const char* szPropertyName)
61 : plAbstractMemberProperty(szPropertyName)
62 {
63 // We treat const char* as a basic type and not a pointer.
64 m_Flags = plPropertyFlags::GetParameterFlags<const char*>();
65 }
66
67 virtual const plRTTI* GetSpecificType() const override // [tested]
68 {
69 return plGetStaticRTTI<const char*>();
70 }
71
72 virtual const char* GetValue(const void* pInstance) const = 0;
73 virtual void SetValue(void* pInstance, const char* value) const = 0;
74 virtual void GetValuePtr(const void* pInstance, void* pObject) const override { *static_cast<const char**>(pObject) = GetValue(pInstance); };
75 virtual void SetValuePtr(void* pInstance, const void* pObject) const override { SetValue(pInstance, *static_cast<const char* const*>(pObject)); };
76};
77
78
79// *******************************************************************
80// ***** Class for properties that use custom accessor functions *****
81
83template <typename Class, typename Type>
84class plAccessorProperty : public plTypedMemberProperty<typename plTypeTraits<Type>::NonConstReferenceType>
85{
86public:
87 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
88 using GetterFunc = Type (Class::*)() const;
89 using SetterFunc = void (Class::*)(Type value);
90
92 plAccessorProperty(const char* szPropertyName, GetterFunc getter, SetterFunc setter)
93 : plTypedMemberProperty<RealType>(szPropertyName)
94 {
95 PL_ASSERT_DEBUG(getter != nullptr, "The getter of a property cannot be nullptr.");
96
97 m_Getter = getter;
98 m_Setter = setter;
99
100 if (m_Setter == nullptr)
101 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
102 }
103
106 virtual void* GetPropertyPointer(const void* pInstance) const override
107 {
108 // No access to sub-properties, if we have accessors for this property
109 return nullptr;
110 }
111
113 virtual RealType GetValue(const void* pInstance) const override // [tested]
114 {
115 return (static_cast<const Class*>(pInstance)->*m_Getter)();
116 }
117
121 virtual void SetValue(void* pInstance, RealType value) const override // [tested]
122 {
123 PL_ASSERT_DEV(m_Setter != nullptr, "The property '{0}' has no setter function, thus it is read-only.", plAbstractProperty::GetPropertyName());
124
125 if (m_Setter)
126 (static_cast<Class*>(pInstance)->*m_Setter)(value);
127 }
128
129private:
130 GetterFunc m_Getter;
131 SetterFunc m_Setter;
132};
133
134
135// *************************************************************
136// ***** Classes for properties that are accessed directly *****
137
139template <typename Class, typename Type, Type Class::*Member>
141{
142 static Type GetValue(const Class* pInstance) { return (*pInstance).*Member; }
143
144 static void SetValue(Class* pInstance, Type value) { (*pInstance).*Member = value; }
145
146 static void* GetPropertyPointer(const Class* pInstance) { return (void*)&((*pInstance).*Member); }
147};
148
149
151template <typename Class, typename Type>
153{
154public:
155 using GetterFunc = Type (*)(const Class* pInstance);
156 using SetterFunc = void (*)(Class* pInstance, Type value);
157 using PointerFunc = void* (*)(const Class* pInstance);
158
160 plMemberProperty(const char* szPropertyName, GetterFunc getter, SetterFunc setter, PointerFunc pointer)
161 : plTypedMemberProperty<Type>(szPropertyName)
162 {
163 PL_ASSERT_DEBUG(getter != nullptr, "The getter of a property cannot be nullptr.");
164
165 m_Getter = getter;
166 m_Setter = setter;
167 m_Pointer = pointer;
168
169 if (m_Setter == nullptr)
170 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
171 }
172
174 virtual void* GetPropertyPointer(const void* pInstance) const override { return m_Pointer(static_cast<const Class*>(pInstance)); }
175
177 virtual Type GetValue(const void* pInstance) const override { return m_Getter(static_cast<const Class*>(pInstance)); }
178
182 virtual void SetValue(void* pInstance, Type value) const override
183 {
184 PL_ASSERT_DEV(m_Setter != nullptr, "The property '{0}' has no setter function, thus it is read-only.", plAbstractProperty::GetPropertyName());
185
186 if (m_Setter)
187 m_Setter(static_cast<Class*>(pInstance), value);
188 }
189
190private:
191 GetterFunc m_Getter;
192 SetterFunc m_Setter;
193 PointerFunc m_Pointer;
194};
This is the base class for all properties that are members of a class. It provides more information a...
Definition AbstractProperty.h:237
plAbstractMemberProperty(const char *szPropertyName)
Passes the property name through to plAbstractProperty.
Definition AbstractProperty.h:240
const char * GetPropertyName() const
Returns the name of the property.
Definition AbstractProperty.h:158
[internal] An implementation of plTypedMemberProperty that uses custom getter / setter functions to a...
Definition MemberProperty.h:85
virtual RealType GetValue(const void *pInstance) const override
Returns the value of the property. Pass the instance pointer to the surrounding class along.
Definition MemberProperty.h:113
virtual void * GetPropertyPointer(const void *pInstance) const override
Always returns nullptr; once a property is modified through accessors, there is no point in giving mo...
Definition MemberProperty.h:106
virtual void SetValue(void *pInstance, RealType value) const override
Modifies the value of the property. Pass the instance pointer to the surrounding class along.
Definition MemberProperty.h:121
plAccessorProperty(const char *szPropertyName, GetterFunc getter, SetterFunc setter)
Constructor.
Definition MemberProperty.h:92
[internal] An implementation of plTypedMemberProperty that accesses the property data directly.
Definition MemberProperty.h:153
virtual void * GetPropertyPointer(const void *pInstance) const override
Returns a pointer to the member property.
Definition MemberProperty.h:174
plMemberProperty(const char *szPropertyName, GetterFunc getter, SetterFunc setter, PointerFunc pointer)
Constructor.
Definition MemberProperty.h:160
virtual Type GetValue(const void *pInstance) const override
Returns the value of the property. Pass the instance pointer to the surrounding class along.
Definition MemberProperty.h:177
virtual void SetValue(void *pInstance, Type value) const override
Modifies the value of the property. Pass the instance pointer to the surrounding class along.
Definition MemberProperty.h:182
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
virtual void GetValuePtr(const void *pInstance, void *pObject) const override
Writes the value of this property in pInstance to pObject. pObject needs to point to an instance of t...
Definition MemberProperty.h:74
virtual void SetValuePtr(void *pInstance, const void *pObject) const override
Sets the value of pObject to the property in pInstance. pObject needs to point to an instance of this...
Definition MemberProperty.h:75
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 MemberProperty.h:67
The base class for all typed member properties. I.e. once the type of a property is determined,...
Definition MemberProperty.h:20
virtual Type GetValue(const void *pInstance) const =0
Returns the value of the property. Pass the instance pointer to the surrounding class along.
plTypedMemberProperty(const char *szPropertyName)
Passes the property name through to plAbstractMemberProperty.
Definition MemberProperty.h:23
virtual void SetValuePtr(void *pInstance, const void *pObject) const override
Sets the value of pObject to the property in pInstance. pObject needs to point to an instance of this...
Definition MemberProperty.h:49
virtual void GetValuePtr(const void *pInstance, void *pObject) const override
Writes the value of this property in pInstance to pObject. pObject needs to point to an instance of t...
Definition MemberProperty.h:48
virtual void SetValue(void *pInstance, Type value) const =0
Modifies the value of the property. Pass the instance pointer to the surrounding class along.
virtual const plRTTI * GetSpecificType() const override
Returns the actual type of the property. You can then compare that with known types,...
Definition MemberProperty.h:35
PL_ALWAYS_INLINE void Add(const plBitflags< T > &rhs)
Sets the given flag.
Definition Bitflags.h:151
[internal] Helper class to generate accessor functions for (private) members of another class
Definition MemberProperty.h:141
@ ReadOnly
Can only be read but not modified.
Definition AbstractProperty.h:63
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