Some tweaks to the userdata templates
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-28 17:53:37 +02:00
parent 88ea4ed8cd
commit 70f2dea0ce
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 37 additions and 10 deletions

View File

@ -1,12 +1,29 @@
#ifndef PORYGONLANG_USERDATATEMPLATES_HPP #ifndef PORYGONLANG_USERDATATEMPLATES_HPP
#define PORYGONLANG_USERDATATEMPLATES_HPP #define PORYGONLANG_USERDATATEMPLATES_HPP
/*!
\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.
*/
#define PORYGON_USERDATA_START() \ #define PORYGON_USERDATA_START() \
static Porygon::UserData::UserData* __createUserData(){ \ static Porygon::UserData::UserData* __createUserData(){ \
return new Porygon::UserData::UserData({ \ return new Porygon::UserData::UserData({ \
/*!
\brief Ends creation of invokable function to create userdata.
*/
#define PORYGON_USERDATA_END() });}; #define PORYGON_USERDATA_END() });};
/*!
\brief Creates an invokable static function with the name __createUserData. This can be called to generate a userdata 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.
*/
#define PORYGON_USERDATA(fields) \
PORYGON_USERDATA_START() \
fields \
PORYGON_USERDATA_END()
#define PORYGON_INTEGER_TYPE ((Porygon::ScriptType*)new Porygon::NumericScriptType(true, false)) #define PORYGON_INTEGER_TYPE ((Porygon::ScriptType*)new Porygon::NumericScriptType(true, false))
#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))
@ -54,18 +71,27 @@
\ \
[](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* parameters[], int parameterCount) \ [](void* obj, Porygon::Evaluation::EvalValue* par[], int parameterCount) \
-> Porygon::Evaluation::EvalValue*{return ((userDataTypeName*)obj)->invoke__##fieldName(obj, parameters, parameterCount);}, \ -> Porygon::Evaluation::EvalValue*{return ((userDataTypeName*)obj)->invoke__##fieldName(obj, par, parameterCount);}, \
obj);}, \ obj);}, \
nullptr) \ nullptr) \
}, },
#define PORYGON_INTEGER_FUNCTION(userDataTypeName, fieldName, ...) \ #define PORYGON_INTEGER_FUNCTION(userDataTypeName, fieldName, ...) \
PORYGON_FUNCTION(userDataTypeName, fieldName, new Porygon::NumericScriptType(true, false), __VA_ARGS__ ) PORYGON_FUNCTION(userDataTypeName, fieldName, new Porygon::NumericScriptType(true, false), __VA_ARGS__ )
/*!
\brief Creates an invokable function that resolves EvalValues into actual parameters and vice versa for its return type.
\param userDataTypeName The type name of the UserData its created for.
\param fieldName The field name of the original function.
\param returnType The type for which the constructor gets called.
\param ... How to resolve the calling parameters into actual values.
\returns An invokable function.
*/
#define PORYGON_PREPARE_FUNCTION(userDataTypeName, fieldName, returnType, ...) \ #define PORYGON_PREPARE_FUNCTION(userDataTypeName, fieldName, returnType, ...) \
static Porygon::Evaluation::EvalValue* invoke__##fieldName(void* obj, Porygon::Evaluation::EvalValue* parameters[], int parameterCount){ \ static Porygon::Evaluation::EvalValue* invoke__##fieldName(void* obj, Porygon::Evaluation::EvalValue* par[], int parameterCount){ \
return new returnType(((userDataTypeName*)obj)->fieldName( \ return new returnType(((userDataTypeName*)obj)->fieldName( \
__VA_ARGS__ \ __VA_ARGS__ \
));} ));}

View File

@ -26,14 +26,15 @@ public:
// Declare script properties // Declare script properties
private: private:
PORYGON_PREPARE_FUNCTION(UserDataTestObject, getFoo, IntegerEvalValue) #define TYPE UserDataTestObject
PORYGON_PREPARE_FUNCTION(UserDataTestObject, Addition, IntegerEvalValue, (parameters[0] -> EvaluateInteger()), (parameters[1] -> EvaluateInteger())) PORYGON_PREPARE_FUNCTION(TYPE, getFoo, IntegerEvalValue)
PORYGON_PREPARE_FUNCTION(TYPE, Addition, IntegerEvalValue, (par[0] -> EvaluateInteger()), (par[1] -> EvaluateInteger()))
public: public:
PORYGON_USERDATA_START() PORYGON_USERDATA(
PORYGON_INTEGER_FIELD(UserDataTestObject, foo) PORYGON_INTEGER_FIELD(TYPE, foo)
PORYGON_INTEGER_FUNCTION(UserDataTestObject, getFoo) PORYGON_INTEGER_FUNCTION(TYPE, getFoo)
PORYGON_INTEGER_FUNCTION(UserDataTestObject, Addition, PORYGON_INTEGER_TYPE, PORYGON_INTEGER_TYPE) PORYGON_INTEGER_FUNCTION(TYPE, Addition, PORYGON_INTEGER_TYPE, PORYGON_INTEGER_TYPE)
PORYGON_USERDATA_END() )
}; };
TEST_CASE( "Gets UserData value", "[integration]" ) { TEST_CASE( "Gets UserData value", "[integration]" ) {