Plasma Engine  2.0
Loading...
Searching...
No Matches
BitflagsProperty.h
1#pragma once
2
4
5#include <Foundation/Reflection/Implementation/EnumProperty.h>
6#include <Foundation/Reflection/Implementation/StaticRTTI.h>
7
9template <typename Class, typename EnumType, typename Type>
11{
12public:
13 using RealType = typename plTypeTraits<Type>::NonConstReferenceType;
14 using GetterFunc = Type (Class::*)() const;
15 using SetterFunc = void (Class::*)(Type value);
16
18 plBitflagsAccessorProperty(const char* szPropertyName, GetterFunc getter, SetterFunc setter)
19 : plTypedEnumProperty<EnumType>(szPropertyName)
20 {
21 PL_ASSERT_DEBUG(getter != nullptr, "The getter of a property cannot be nullptr.");
22 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::Bitflags);
23
24 m_Getter = getter;
25 m_Setter = setter;
26
27 if (m_Setter == nullptr)
28 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
29 }
30
31 virtual void* GetPropertyPointer(const void* pInstance) const override
32 {
33 // No access to sub-properties, if we have accessors for this property
34 return nullptr;
35 }
36
37 virtual plInt64 GetValue(const void* pInstance) const override // [tested]
38 {
39 typename EnumType::StorageType enumTemp = (static_cast<const Class*>(pInstance)->*m_Getter)().GetValue();
40 return (plInt64)enumTemp;
41 }
42
43 virtual void SetValue(void* pInstance, plInt64 value) const override // [tested]
44 {
45 PL_ASSERT_DEV(m_Setter != nullptr, "The property '{0}' has no setter function, thus it is read-only.", plAbstractProperty::GetPropertyName());
46 if (m_Setter)
47 (static_cast<Class*>(pInstance)->*m_Setter)((typename EnumType::Enum)value);
48 }
49
50private:
51 GetterFunc m_Getter;
52 SetterFunc m_Setter;
53};
54
55
57template <typename Class, typename EnumType, typename Type>
59{
60public:
61 using GetterFunc = Type (*)(const Class* pInstance);
62 using SetterFunc = void (*)(Class* pInstance, Type value);
63 using PointerFunc = void* (*)(const Class* pInstance);
64
66 plBitflagsMemberProperty(const char* szPropertyName, GetterFunc getter, SetterFunc setter, PointerFunc pointer)
67 : plTypedEnumProperty<EnumType>(szPropertyName)
68 {
69 PL_ASSERT_DEBUG(getter != nullptr, "The getter of a property cannot be nullptr.");
70 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::Bitflags);
71
72 m_Getter = getter;
73 m_Setter = setter;
74 m_Pointer = pointer;
75
76 if (m_Setter == nullptr)
77 plAbstractMemberProperty::m_Flags.Add(plPropertyFlags::ReadOnly);
78 }
79
80 virtual void* GetPropertyPointer(const void* pInstance) const override { return m_Pointer(static_cast<const Class*>(pInstance)); }
81
82 virtual plInt64 GetValue(const void* pInstance) const override // [tested]
83 {
84 typename EnumType::StorageType enumTemp = m_Getter(static_cast<const Class*>(pInstance)).GetValue();
85 return (plInt64)enumTemp;
86 }
87
88 virtual void SetValue(void* pInstance, plInt64 value) const override // [tested]
89 {
90 PL_ASSERT_DEV(m_Setter != nullptr, "The property '{0}' has no setter function, thus it is read-only.", plAbstractProperty::GetPropertyName());
91
92 if (m_Setter)
93 m_Setter(static_cast<Class*>(pInstance), (typename EnumType::Enum)value);
94 }
95
96private:
97 GetterFunc m_Getter;
98 SetterFunc m_Setter;
99 PointerFunc m_Pointer;
100};
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 BitflagsProperty.h:11
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 BitflagsProperty.h:43
virtual plInt64 GetValue(const void *pInstance) const override
Returns the value of the property. Pass the instance pointer to the surrounding class along.
Definition BitflagsProperty.h:37
virtual void * GetPropertyPointer(const void *pInstance) const override
Returns a pointer to the property data or nullptr. If a valid pointer is returned,...
Definition BitflagsProperty.h:31
plBitflagsAccessorProperty(const char *szPropertyName, GetterFunc getter, SetterFunc setter)
Constructor.
Definition BitflagsProperty.h:18
[internal] An implementation of plTypedEnumProperty that accesses the bitflags property data directly...
Definition BitflagsProperty.h:59
virtual void * GetPropertyPointer(const void *pInstance) const override
Returns a pointer to the property data or nullptr. If a valid pointer is returned,...
Definition BitflagsProperty.h:80
plBitflagsMemberProperty(const char *szPropertyName, GetterFunc getter, SetterFunc setter, PointerFunc pointer)
Constructor.
Definition BitflagsProperty.h:66
virtual plInt64 GetValue(const void *pInstance) const override
Returns the value of the property. Pass the instance pointer to the surrounding class along.
Definition BitflagsProperty.h:82
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 BitflagsProperty.h:88
[internal] Base class for enum / bitflags properties that already defines the type.
Definition EnumProperty.h:43
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
@ Bitflags
Bitflags property, cast to plAbstractEnumerationProperty.
Definition AbstractProperty.h:55
typename std::remove_const< typename std::remove_reference< T >::type >::type NonConstReferenceType
removes reference and const qualifier
Definition TypeTraits.h:218