From 70f2dea0cec1019b7e6779226f86033b99e9735b Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 28 Jun 2019 17:53:37 +0200 Subject: [PATCH] Some tweaks to the userdata templates --- src/UserData/UserDataTemplates.hpp | 32 ++++++++++++++++++++++++++--- tests/integration/UserDataTests.cpp | 15 +++++++------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/UserData/UserDataTemplates.hpp b/src/UserData/UserDataTemplates.hpp index 51717ed..16cfa71 100644 --- a/src/UserData/UserDataTemplates.hpp +++ b/src/UserData/UserDataTemplates.hpp @@ -1,12 +1,29 @@ #ifndef 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() \ static Porygon::UserData::UserData* __createUserData(){ \ return new Porygon::UserData::UserData({ \ +/*! +\brief Ends creation of invokable function to create userdata. +*/ #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_FLOAT_TYPE ((Porygon::ScriptType*)new Porygon::NumericScriptType(true, true)) #define PORYGON_STRING_TYPE ((Porygon::ScriptType*)new Porygon::StringScriptType(false, 0)) @@ -54,18 +71,27 @@ \ [](void* obj) -> Porygon::Evaluation::EvalValue* { \ return new Porygon::UserData::UserDataFunction( \ - [](void* obj, Porygon::Evaluation::EvalValue* parameters[], int parameterCount) \ - -> Porygon::Evaluation::EvalValue*{return ((userDataTypeName*)obj)->invoke__##fieldName(obj, parameters, parameterCount);}, \ + [](void* obj, Porygon::Evaluation::EvalValue* par[], int parameterCount) \ + -> Porygon::Evaluation::EvalValue*{return ((userDataTypeName*)obj)->invoke__##fieldName(obj, par, parameterCount);}, \ obj);}, \ nullptr) \ }, + #define PORYGON_INTEGER_FUNCTION(userDataTypeName, fieldName, ...) \ 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, ...) \ - 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( \ __VA_ARGS__ \ ));} diff --git a/tests/integration/UserDataTests.cpp b/tests/integration/UserDataTests.cpp index ecedccd..8a98a66 100644 --- a/tests/integration/UserDataTests.cpp +++ b/tests/integration/UserDataTests.cpp @@ -26,14 +26,15 @@ public: // Declare script properties private: - PORYGON_PREPARE_FUNCTION(UserDataTestObject, getFoo, IntegerEvalValue) - PORYGON_PREPARE_FUNCTION(UserDataTestObject, Addition, IntegerEvalValue, (parameters[0] -> EvaluateInteger()), (parameters[1] -> EvaluateInteger())) +#define TYPE UserDataTestObject + PORYGON_PREPARE_FUNCTION(TYPE, getFoo, IntegerEvalValue) + PORYGON_PREPARE_FUNCTION(TYPE, Addition, IntegerEvalValue, (par[0] -> EvaluateInteger()), (par[1] -> EvaluateInteger())) public: - PORYGON_USERDATA_START() - PORYGON_INTEGER_FIELD(UserDataTestObject, foo) - PORYGON_INTEGER_FUNCTION(UserDataTestObject, getFoo) - PORYGON_INTEGER_FUNCTION(UserDataTestObject, Addition, PORYGON_INTEGER_TYPE, PORYGON_INTEGER_TYPE) - PORYGON_USERDATA_END() + PORYGON_USERDATA( + PORYGON_INTEGER_FIELD(TYPE, foo) + PORYGON_INTEGER_FUNCTION(TYPE, getFoo) + PORYGON_INTEGER_FUNCTION(TYPE, Addition, PORYGON_INTEGER_TYPE, PORYGON_INTEGER_TYPE) + ) }; TEST_CASE( "Gets UserData value", "[integration]" ) {