Plasma Engine  2.0
Loading...
Searching...
No Matches
EnumProperty.h
1#pragma once
2
4
5#include <Foundation/Reflection/Implementation/MemberProperty.h>
6#include <Foundation/Reflection/Implementation/StaticRTTI.h>
7
12{
13public:
15 plAbstractEnumerationProperty(const char* szPropertyName)
16 : plAbstractMemberProperty(szPropertyName)
17 {
18 }
19
21 virtual plInt64 GetValue(const void* pInstance) const = 0;
22
26 virtual void SetValue(void* pInstance, plInt64 value) const = 0;
27
28 virtual void GetValuePtr(const void* pInstance, void* pObject) const override
29 {
30 *static_cast<plInt64*>(pObject) = GetValue(pInstance);
31 }
32
33 virtual void SetValuePtr(void* pInstance, const void* pObject) const override
34 {
35 SetValue(pInstance, *static_cast<const plInt64*>(pObject));
36 }
37};
38
39
41template <typename EnumType>
43{
44public:
46 plTypedEnumProperty(const char* szPropertyName)
47 : plAbstractEnumerationProperty(szPropertyName)
48 {
49 }
50
53 virtual const plRTTI* GetSpecificType() const override // [tested]
54 {
55 return plGetStaticRTTI<typename plTypeTraits<EnumType>::NonConstReferenceType>();
56 }
57};
58
59
61template <typename Class, typename EnumType, typename Type>
63{
64public:
65 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
66 using GetterFunc = Type (Class::*)() const;
67 using SetterFunc = void (Class::*)(Type value);
68
70 plEnumAccessorProperty(const char* szPropertyName, GetterFunc getter, SetterFunc setter)
71 : plTypedEnumProperty<EnumType>(szPropertyName)
72 {
73 PL_ASSERT_DEBUG(getter != nullptr, "The getter of a property cannot be nullptr.");
74 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::IsEnum);
75
76 m_Getter = getter;
77 m_Setter = setter;
78
79 if (m_Setter == nullptr)
80 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
81 }
82
83 virtual void* GetPropertyPointer(const void* pInstance) const override
84 {
85 // No access to sub-properties, if we have accessors for this property
86 return nullptr;
87 }
88
89 virtual plInt64 GetValue(const void* pInstance) const override // [tested]
90 {
91 plEnum<EnumType> enumTemp = (static_cast<const Class*>(pInstance)->*m_Getter)();
92 return enumTemp.GetValue();
93 }
94
95 virtual void SetValue(void* pInstance, plInt64 value) const override // [tested]
96 {
97 PL_ASSERT_DEV(m_Setter != nullptr, "The property '{0}' has no setter function, thus it is read-only.", plAbstractProperty::GetPropertyName());
98 if (m_Setter)
99 (static_cast<Class*>(pInstance)->*m_Setter)((typename EnumType::Enum)value);
100 }
101
102private:
103 GetterFunc m_Getter;
104 SetterFunc m_Setter;
105};
106
107
109template <typename Class, typename EnumType, typename Type>
111{
112public:
113 using GetterFunc = Type (*)(const Class* pInstance);
114 using SetterFunc = void (*)(Class* pInstance, Type value);
115 using PointerFunc = void* (*)(const Class* pInstance);
116
118 plEnumMemberProperty(const char* szPropertyName, GetterFunc getter, SetterFunc setter, PointerFunc pointer)
119 : plTypedEnumProperty<EnumType>(szPropertyName)
120 {
121 PL_ASSERT_DEBUG(getter != nullptr, "The getter of a property cannot be nullptr.");
122 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::IsEnum);
123
124 m_Getter = getter;
125 m_Setter = setter;
126 m_Pointer = pointer;
127
128 if (m_Setter == nullptr)
129 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
130 }
131
132 virtual void* GetPropertyPointer(const void* pInstance) const override { return m_Pointer(static_cast<const Class*>(pInstance)); }
133
134 virtual plInt64 GetValue(const void* pInstance) const override // [tested]
135 {
136 plEnum<EnumType> enumTemp = m_Getter(static_cast<const Class*>(pInstance));
137 return enumTemp.GetValue();
138 }
139
140 virtual void SetValue(void* pInstance, plInt64 value) const override // [tested]
141 {
142 PL_ASSERT_DEV(m_Setter != nullptr, "The property '{0}' has no setter function, thus it is read-only.", plAbstractProperty::GetPropertyName());
143
144 if (m_Setter)
145 m_Setter(static_cast<Class*>(pInstance), (typename EnumType::Enum)value);
146 }
147
148private:
149 GetterFunc m_Getter;
150 SetterFunc m_Setter;
151 PointerFunc m_Pointer;
152};
The base class for enum and bitflags member properties.
Definition EnumProperty.h:12
virtual plInt64 GetValue(const void *pInstance) const =0
Returns the value of the property. Pass the instance pointer to the surrounding class along.
plAbstractEnumerationProperty(const char *szPropertyName)
Passes the property name through to plAbstractMemberProperty.
Definition EnumProperty.h:15
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 EnumProperty.h:28
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 EnumProperty.h:33
virtual void SetValue(void *pInstance, plInt64 value) const =0
Modifies the value of the property. Pass the instance pointer to the surrounding class along.
This is the base class for all properties that are members of a class. It provides more information a...
Definition AbstractProperty.h:237
const char * GetPropertyName() const
Returns the name of the property.
Definition AbstractProperty.h:158
[internal] An implementation of plTypedEnumProperty that uses custom getter / setter functions to acc...
Definition EnumProperty.h:63
virtual plInt64 GetValue(const void *pInstance) const override
Returns the value of the property. Pass the instance pointer to the surrounding class along.
Definition EnumProperty.h:89
virtual void SetValue(void *pInstance, plInt64 value) const override
Modifies the value of the property. Pass the instance pointer to the surrounding class along.
Definition EnumProperty.h:95
plEnumAccessorProperty(const char *szPropertyName, GetterFunc getter, SetterFunc setter)
Constructor.
Definition EnumProperty.h:70
virtual void * GetPropertyPointer(const void *pInstance) const override
Returns a pointer to the property data or nullptr. If a valid pointer is returned,...
Definition EnumProperty.h:83
[internal] An implementation of plTypedEnumProperty that accesses the enum property data directly.
Definition EnumProperty.h:111
virtual void SetValue(void *pInstance, plInt64 value) const override
Modifies the value of the property. Pass the instance pointer to the surrounding class along.
Definition EnumProperty.h:140
plEnumMemberProperty(const char *szPropertyName, GetterFunc getter, SetterFunc setter, PointerFunc pointer)
Constructor.
Definition EnumProperty.h:118
virtual void * GetPropertyPointer(const void *pInstance) const override
Returns a pointer to the property data or nullptr. If a valid pointer is returned,...
Definition EnumProperty.h:132
virtual plInt64 GetValue(const void *pInstance) const override
Returns the value of the property. Pass the instance pointer to the surrounding class along.
Definition EnumProperty.h:134
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
[internal] Base class for enum / bitflags properties that already defines the type.
Definition EnumProperty.h:43
virtual const plRTTI * GetSpecificType() const override
Returns the actual type of the property. You can then test whether it derives from plEnumBase or plBi...
Definition EnumProperty.h:53
plTypedEnumProperty(const char *szPropertyName)
Passes the property name through to plAbstractEnumerationProperty.
Definition EnumProperty.h:46
PL_ALWAYS_INLINE void Add(const plBitflags< T > &rhs)
Sets the given flag.
Definition Bitflags.h:151
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
PL_ALWAYS_INLINE StorageType GetValue() const
Returns the enum value as an integer.
Definition Enum.h:98
@ ReadOnly
Can only be read but not modified.
Definition AbstractProperty.h:63
@ IsEnum
enum property, cast to plAbstractEnumerationProperty.
Definition AbstractProperty.h:54
typename std::remove_const< typename std::remove_reference< T >::type >::type NonConstReferenceType
removes reference and const qualifier
Definition TypeTraits.h:218