Cleaner way to define userdata templates
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-28 23:38:47 +02:00
parent eda15e501d
commit ecfc1ae3b7
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 29 additions and 29 deletions

View File

@ -5,7 +5,8 @@
\brief Begins creating an invokable function. Make sure to call PORYGON_USERDATA_END after this. \brief Begins creating an invokable function. Make sure to call PORYGON_USERDATA_END after this.
\returns The start of an invokable function with the name __createUserData. This can be called to return a userdata object. \returns The start of an invokable function with the name __createUserData. This can be called to return a userdata object.
*/ */
#define PORYGON_USERDATA_START() \ #define PORYGON_USERDATA_START(type) \
using T_USERDATA = type; \
static Porygon::UserData::UserData* __createUserData(){ \ static Porygon::UserData::UserData* __createUserData(){ \
return new Porygon::UserData::UserData({ \ return new Porygon::UserData::UserData({ \
@ -19,8 +20,8 @@
\param fields The fields of the object. \param fields The fields of the object.
\returns an invokable static function with the name __createUserData. This can be called to generate a userdata object. \returns an invokable static function with the name __createUserData. This can be called to generate a userdata object.
*/ */
#define PORYGON_USERDATA(fields) \ #define PORYGON_USERDATA(type, fields) \
PORYGON_USERDATA_START() \ PORYGON_USERDATA_START(type) \
fields \ fields \
PORYGON_USERDATA_END() PORYGON_USERDATA_END()
@ -28,16 +29,16 @@
#define PORYGON_FLOAT_TYPE ((Porygon::ScriptType*)new Porygon::NumericScriptType(true, true)) #define PORYGON_FLOAT_TYPE ((Porygon::ScriptType*)new Porygon::NumericScriptType(true, true))
#define PORYGON_STRING_TYPE ((Porygon::ScriptType*)new Porygon::StringScriptType(false, 0)) #define PORYGON_STRING_TYPE ((Porygon::ScriptType*)new Porygon::StringScriptType(false, 0))
#define PORYGON_FIELD(userDataTypeName, fieldName, fieldType, getterHelper, setterHelper) \ #define PORYGON_FIELD(fieldName, fieldType, getterHelper, setterHelper) \
{ \ { \
Porygon::Utilities::HashedString::ConstHash(#fieldName), \ Porygon::Utilities::HashedString::ConstHash(#fieldName), \
new Porygon::UserData::UserDataField(fieldType, \ new Porygon::UserData::UserDataField(fieldType, \
[](void* obj) -> Porygon::Evaluation::EvalValue* { return new getterHelper;}, \ [](void* obj) -> Porygon::Evaluation::EvalValue* { return new getterHelper;}, \
[](void* obj, Porygon::Evaluation::EvalValue* val) { ((userDataTypeName*)obj)->fieldName = setterHelper;} \ [](void* obj, Porygon::Evaluation::EvalValue* val) { ((T_USERDATA*)obj)->fieldName = setterHelper;} \
) \ ) \
}, \ }, \
#define PORYGON_READONLY_FIELD(userDataTypeName, fieldName, fieldType, getterHelper) \ #define PORYGON_READONLY_FIELD(fieldName, fieldType, getterHelper) \
{ \ { \
Porygon::Utilities::HashedString::ConstHash(#fieldName), \ Porygon::Utilities::HashedString::ConstHash(#fieldName), \
new Porygon::UserData::UserDataField(fieldType, \ new Porygon::UserData::UserDataField(fieldType, \
@ -46,24 +47,24 @@
) \ ) \
}, \ }, \
#define PORYGON_INTEGER_FIELD(userDataTypeName, fieldName) \ #define PORYGON_INTEGER_FIELD(fieldName) \
PORYGON_FIELD(userDataTypeName, fieldName, PORYGON_INTEGER_TYPE, \ PORYGON_FIELD(fieldName, PORYGON_INTEGER_TYPE, \
Porygon::Evaluation::IntegerEvalValue(((userDataTypeName*)obj)->fieldName), val->EvaluateInteger()) Porygon::Evaluation::IntegerEvalValue(((T_USERDATA*)obj)->fieldName), val->EvaluateInteger())
#define PORYGON_READONLY_INTEGER_FIELD(userDataTypeName, fieldName) \ #define PORYGON_READONLY_INTEGER_FIELD(fieldName) \
PORYGON_READONLY_FIELD(userDataTypeName, fieldName, PORYGON_INTEGER_TYPE, \ PORYGON_READONLY_FIELD(fieldName, PORYGON_INTEGER_TYPE, \
Porygon::Evaluation::IntegerEvalValue(((userDataTypeName*)obj)->fieldName)) Porygon::Evaluation::IntegerEvalValue(((T_USERDATA*)obj)->fieldName))
#define PORYGON_FLOAT_FIELD(userDataTypeName, fieldName) \ #define PORYGON_FLOAT_FIELD(fieldName) \
PORYGON_FIELD(userDataTypeName, fieldName, PORYGON_FLOAT_TYPE, \ PORYGON_FIELD(fieldName, PORYGON_FLOAT_TYPE, \
Porygon::EvaluationFloatEvalValue(((userDataTypeName*)obj)->fieldName), val->EvaluateFloat()) Porygon::EvaluationFloatEvalValue(((T_USERDATA*)obj)->fieldName), val->EvaluateFloat())
#define PORYGON_READONLY_FLOAT_FIELD(userDataTypeName, fieldName) \ #define PORYGON_READONLY_FLOAT_FIELD(fieldName) \
PORYGON_READONLY_FIELD(userDataTypeName, fieldName, PORYGON_FLOAT_TYPE, \ PORYGON_READONLY_FIELD(fieldName, PORYGON_FLOAT_TYPE, \
Porygon::EvaluationFloatEvalValue(((userDataTypeName*)obj)->fieldName)) Porygon::EvaluationFloatEvalValue(((T_USERDATA*)obj)->fieldName))
#define PORYGON_FUNCTION(userDataTypeName, fieldName, returnType, ...) \ #define PORYGON_FUNCTION(fieldName, returnType, ...) \
{ \ { \
Porygon::Utilities::HashedString::ConstHash(#fieldName), \ Porygon::Utilities::HashedString::ConstHash(#fieldName), \
new Porygon::UserData::UserDataField(Porygon::UserData::UserDataFunctionType::FromRawPointers(returnType, {__VA_ARGS__} ), \ new Porygon::UserData::UserDataField(Porygon::UserData::UserDataFunctionType::FromRawPointers(returnType, {__VA_ARGS__} ), \
@ -72,14 +73,14 @@
[](void* obj) -> Porygon::Evaluation::EvalValue* { \ [](void* obj) -> Porygon::Evaluation::EvalValue* { \
return new Porygon::UserData::UserDataFunction( \ return new Porygon::UserData::UserDataFunction( \
[](void* obj, Porygon::Evaluation::EvalValue* par[], int parameterCount) \ [](void* obj, Porygon::Evaluation::EvalValue* par[], int parameterCount) \
-> Porygon::Evaluation::EvalValue*{return ((userDataTypeName*)obj)->invoke__##fieldName(obj, par, parameterCount);}, \ -> Porygon::Evaluation::EvalValue*{return ((T_USERDATA*)obj)->invoke__##fieldName(obj, par, parameterCount);}, \
obj);}, \ obj);}, \
nullptr) \ nullptr) \
}, },
#define PORYGON_INTEGER_FUNCTION(userDataTypeName, fieldName, ...) \ #define PORYGON_INTEGER_FUNCTION(fieldName, ...) \
PORYGON_FUNCTION(userDataTypeName, fieldName, new Porygon::NumericScriptType(true, false), __VA_ARGS__ ) PORYGON_FUNCTION(fieldName, new Porygon::NumericScriptType(true, false), __VA_ARGS__ )
/*! /*!

View File

@ -27,14 +27,13 @@ public:
// Declare script properties // Declare script properties
private: private:
#define TYPE UserDataTestObject PORYGON_PREPARE_FUNCTION(UserDataTestObject, getFoo, IntegerEvalValue)
PORYGON_PREPARE_FUNCTION(TYPE, getFoo, IntegerEvalValue) PORYGON_PREPARE_FUNCTION(UserDataTestObject, Addition, IntegerEvalValue, (par[0] -> EvaluateInteger()), (par[1] -> EvaluateInteger()))
PORYGON_PREPARE_FUNCTION(TYPE, Addition, IntegerEvalValue, (par[0] -> EvaluateInteger()), (par[1] -> EvaluateInteger()))
public: public:
PORYGON_USERDATA( PORYGON_USERDATA(UserDataTestObject,
PORYGON_INTEGER_FIELD(TYPE, foo) PORYGON_INTEGER_FIELD(foo)
PORYGON_INTEGER_FUNCTION(TYPE, getFoo) PORYGON_INTEGER_FUNCTION(getFoo)
PORYGON_INTEGER_FUNCTION(TYPE, Addition, PORYGON_INTEGER_TYPE, PORYGON_INTEGER_TYPE) PORYGON_INTEGER_FUNCTION(Addition, PORYGON_INTEGER_TYPE, PORYGON_INTEGER_TYPE)
) )
}; };