Plasma Engine  2.0
Loading...
Searching...
No Matches
FunctionProperty.h
1#pragma once
2
4
5#include <Foundation/Reflection/Implementation/AbstractProperty.h>
6#include <Foundation/Reflection/Implementation/VariantAdapter.h>
7
8
9template <class R, class... Args>
11{
12public:
13 plTypedFunctionProperty(const char* szPropertyName)
14 : plAbstractFunctionProperty(szPropertyName)
15 {
16 }
17
18 virtual const plRTTI* GetReturnType() const override { return plGetStaticRTTI<typename plCleanType<R>::RttiType>(); }
19 virtual plBitflags<plPropertyFlags> GetReturnFlags() const override { return plPropertyFlags::GetParameterFlags<R>(); }
20
21 virtual plUInt32 GetArgumentCount() const override { return sizeof...(Args); }
22
23 template <std::size_t... I>
24 const plRTTI* GetParameterTypeImpl(plUInt32 uiParamIndex, std::index_sequence<I...>) const
25 {
26 // There is a dummy entry at the end to support zero parameter functions (can't have zero-size arrays).
27 static const plRTTI* params[] = {plGetStaticRTTI<typename plCleanType<typename getArgument<I, Args...>::Type>::RttiType>()..., nullptr};
28 return params[uiParamIndex];
29 }
30
31 virtual const plRTTI* GetArgumentType(plUInt32 uiParamIndex) const override
32 {
33 return GetParameterTypeImpl(uiParamIndex, std::make_index_sequence<sizeof...(Args)>{});
34 }
35
36 template <std::size_t... I>
37 plBitflags<plPropertyFlags> GetParameterFlagsImpl(plUInt32 uiParamIndex, std::index_sequence<I...>) const
38 {
39 // There is a dummy entry at the end to support zero parameter functions (can't have zero-size arrays).
40 static plBitflags<plPropertyFlags> params[] = {
41 plPropertyFlags::GetParameterFlags<typename getArgument<I, Args...>::Type>()..., plPropertyFlags::Void};
42 return params[uiParamIndex];
43 }
44
45 virtual plBitflags<plPropertyFlags> GetArgumentFlags(plUInt32 uiParamIndex) const override
46 {
47 return GetParameterFlagsImpl(uiParamIndex, std::make_index_sequence<sizeof...(Args)>{});
48 }
49};
50
51template <typename FUNC>
53{
54};
55
56template <class CLASS, class R, class... Args>
57class plFunctionProperty<R (CLASS::*)(Args...)> : public plTypedFunctionProperty<R, Args...>
58{
59public:
60 using TargetFunction = R (CLASS::*)(Args...);
61
62 plFunctionProperty(const char* szPropertyName, TargetFunction func)
63 : plTypedFunctionProperty<R, Args...>(szPropertyName)
64 {
65 m_Function = func;
66 }
67
68 virtual plFunctionType::Enum GetFunctionType() const override
69 {
71 }
72
73 template <std::size_t... I>
74 PL_FORCE_INLINE void ExecuteImpl(void* pInstance, plVariant& out_returnValue, plArrayPtr<plVariant> arguments, std::index_sequence<I...>) const
75 {
76 CLASS* pTargetInstance = static_cast<CLASS*>(pInstance);
77 if constexpr (std::is_same<R, void>::value)
78 {
79 (pTargetInstance->*m_Function)(plVariantAdapter<typename getArgument<I, Args...>::Type>(arguments[I])...);
80 out_returnValue = plVariant();
81 }
82 else
83 {
84 plVariantAssignmentAdapter<R> returnWrapper(out_returnValue);
85 returnWrapper = (pTargetInstance->*m_Function)(plVariantAdapter<typename getArgument<I, Args...>::Type>(arguments[I])...);
86 }
87 }
88
89 virtual void Execute(void* pInstance, plArrayPtr<plVariant> arguments, plVariant& out_returnValue) const override
90 {
91 ExecuteImpl(pInstance, out_returnValue, arguments, std::make_index_sequence<sizeof...(Args)>{});
92 }
93
94private:
95 TargetFunction m_Function;
96};
97
98template <class CLASS, class R, class... Args>
99class plFunctionProperty<R (CLASS::*)(Args...) const> : public plTypedFunctionProperty<R, Args...>
100{
101public:
102 using TargetFunction = R (CLASS::*)(Args...) const;
103
104 plFunctionProperty(const char* szPropertyName, TargetFunction func)
105 : plTypedFunctionProperty<R, Args...>(szPropertyName)
106 {
107 m_Function = func;
108 this->AddFlags(plPropertyFlags::Const);
109 }
110
111 virtual plFunctionType::Enum GetFunctionType() const override
112 {
114 }
115
116 template <std::size_t... I>
117 PL_FORCE_INLINE void ExecuteImpl(const void* pInstance, plVariant& out_returnValue, plArrayPtr<plVariant> arguments, std::index_sequence<I...>) const
118 {
119 const CLASS* pTargetInstance = static_cast<const CLASS*>(pInstance);
120 if constexpr (std::is_same<R, void>::value)
121 {
122 (pTargetInstance->*m_Function)(plVariantAdapter<typename getArgument<I, Args...>::Type>(arguments[I])...);
123 out_returnValue = plVariant();
124 }
125 else
126 {
127 plVariantAssignmentAdapter<R> returnWrapper(out_returnValue);
128 returnWrapper = (pTargetInstance->*m_Function)(plVariantAdapter<typename getArgument<I, Args...>::Type>(arguments[I])...);
129 }
130 }
131
132 virtual void Execute(void* pInstance, plArrayPtr<plVariant> arguments, plVariant& out_returnValue) const override
133 {
134 ExecuteImpl(pInstance, out_returnValue, arguments, std::make_index_sequence<sizeof...(Args)>{});
135 }
136
137private:
138 TargetFunction m_Function;
139};
140
141template <class R, class... Args>
142class plFunctionProperty<R (*)(Args...)> : public plTypedFunctionProperty<R, Args...>
143{
144public:
145 using TargetFunction = R (*)(Args...);
146
147 plFunctionProperty(const char* szPropertyName, TargetFunction func)
148 : plTypedFunctionProperty<R, Args...>(szPropertyName)
149 {
150 m_Function = func;
151 }
152
154
155 template <std::size_t... I>
156 void ExecuteImpl(plTraitInt<1>, plVariant& out_returnValue, plArrayPtr<plVariant> arguments, std::index_sequence<I...>) const
157 {
158 (*m_Function)(plVariantAdapter<typename getArgument<I, Args...>::Type>(arguments[I])...);
159 out_returnValue = plVariant();
160 }
161
162 template <std::size_t... I>
163 void ExecuteImpl(plTraitInt<0>, plVariant& out_returnValue, plArrayPtr<plVariant> arguments, std::index_sequence<I...>) const
164 {
165 plVariantAssignmentAdapter<R> returnWrapper(out_returnValue);
166 returnWrapper = (*m_Function)(plVariantAdapter<typename getArgument<I, Args...>::Type>(arguments[I])...);
167 }
168
169 virtual void Execute(void* pInstance, plArrayPtr<plVariant> arguments, plVariant& out_returnValue) const override
170 {
171 ExecuteImpl(plTraitInt<std::is_same<R, void>::value>(), out_returnValue, arguments, std::make_index_sequence<sizeof...(Args)>{});
172 }
173
174private:
175 TargetFunction m_Function;
176};
177
178
179template <class CLASS, class... Args>
181{
182public:
184 : plTypedFunctionProperty<CLASS*, Args...>("Constructor")
185 {
186 }
187
189
190 template <std::size_t... I>
191 void ExecuteImpl(plTraitInt<1>, plVariant& out_returnValue, plArrayPtr<plVariant> arguments, std::index_sequence<I...>) const
192 {
193 out_returnValue = CLASS(plVariantAdapter<typename getArgument<I, Args...>::Type>(arguments[I])...);
194 // returnValue = CLASS(static_cast<typename getArgument<I, Args...>::Type>(plVariantAdapter<typename getArgument<I,
195 // Args...>::Type>(arguments[I]))...);
196 }
197
198 template <std::size_t... I>
199 void ExecuteImpl(plTraitInt<0>, plVariant& out_returnValue, plArrayPtr<plVariant> arguments, std::index_sequence<I...>) const
200 {
201 CLASS* pInstance = PL_DEFAULT_NEW(CLASS, plVariantAdapter<typename getArgument<I, Args...>::Type>(arguments[I])...);
202 // CLASS* pInstance = PL_DEFAULT_NEW(CLASS, static_cast<typename getArgument<I, Args...>::Type>(plVariantAdapter<typename getArgument<I,
203 // Args...>::Type>(arguments[I]))...);
204 out_returnValue = pInstance;
205 }
206
207 virtual void Execute(void* pInstance, plArrayPtr<plVariant> arguments, plVariant& out_returnValue) const override
208 {
209 ExecuteImpl(plTraitInt<plIsStandardType<CLASS>::value>(), out_returnValue, arguments, std::make_index_sequence<sizeof...(Args)>{});
210 }
211};
The base class for a property that represents a function.
Definition AbstractProperty.h:535
plAbstractFunctionProperty(const char *szPropertyName)
Passes the property name through to plAbstractProperty.
Definition AbstractProperty.h:538
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Definition FunctionProperty.h:181
virtual plFunctionType::Enum GetFunctionType() const override
Returns the type of function, see plFunctionPropertyType::Enum.
Definition FunctionProperty.h:188
virtual void Execute(void *pInstance, plArrayPtr< plVariant > arguments, plVariant &out_returnValue) const override
Calls the function. Provide the instance on which the function is supposed to be called.
Definition FunctionProperty.h:207
virtual void Execute(void *pInstance, plArrayPtr< plVariant > arguments, plVariant &out_returnValue) const override
Calls the function. Provide the instance on which the function is supposed to be called.
Definition FunctionProperty.h:169
virtual plFunctionType::Enum GetFunctionType() const override
Returns the type of function, see plFunctionPropertyType::Enum.
Definition FunctionProperty.h:153
virtual void Execute(void *pInstance, plArrayPtr< plVariant > arguments, plVariant &out_returnValue) const override
Calls the function. Provide the instance on which the function is supposed to be called.
Definition FunctionProperty.h:132
virtual plFunctionType::Enum GetFunctionType() const override
Returns the type of function, see plFunctionPropertyType::Enum.
Definition FunctionProperty.h:111
virtual plFunctionType::Enum GetFunctionType() const override
Returns the type of function, see plFunctionPropertyType::Enum.
Definition FunctionProperty.h:68
virtual void Execute(void *pInstance, plArrayPtr< plVariant > arguments, plVariant &out_returnValue) const override
Calls the function. Provide the instance on which the function is supposed to be called.
Definition FunctionProperty.h:89
Definition FunctionProperty.h:53
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
Definition FunctionProperty.h:11
virtual const plRTTI * GetArgumentType(plUInt32 uiParamIndex) const override
Returns the type of the given argument.
Definition FunctionProperty.h:31
virtual plBitflags< plPropertyFlags > GetReturnFlags() const override
Returns property flags of the return value.
Definition FunctionProperty.h:19
virtual plUInt32 GetArgumentCount() const override
Returns the number of arguments.
Definition FunctionProperty.h:21
virtual plBitflags< plPropertyFlags > GetArgumentFlags(plUInt32 uiParamIndex) const override
Returns the property flags of the given argument.
Definition FunctionProperty.h:45
virtual const plRTTI * GetReturnType() const override
Returns the type of the return value.
Definition FunctionProperty.h:18
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
Use getArgument<N, Args...>::Type to get the type of the Nth argument in Args.
Definition AbstractProperty.h:383
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
Definition VariantAdapter.h:28
Enum
Definition AbstractProperty.h:525
@ StaticMember
A static member function, instance pointer will be ignored.
Definition AbstractProperty.h:527
@ Member
A normal member function, a valid instance pointer must be provided to call.
Definition AbstractProperty.h:526
@ Constructor
A constructor. Return value is a void* pointing to the new instance allocated with the default alloca...
Definition AbstractProperty.h:528
Used to determine if the given type is a build-in standard variant type.
Definition VariantAdapter.h:74
@ Const
Property value is const.
Definition AbstractProperty.h:58
Type traits.
Definition TypeTraits.h:12
Used to implicitly retrieve any value from an plVariant to be used as a function argument using the a...
Definition VariantAdapter.h:211
Used to automatically assign any value to an plVariant using the assignment rules outlined in plAbstr...
Definition VariantAdapter.h:118