PorygonLang/src/UserData/UserDataTemplates.hpp

109 lines
4.7 KiB
C++
Raw Normal View History

#ifndef PORYGONLANG_USERDATATEMPLATES_HPP
#define PORYGONLANG_USERDATATEMPLATES_HPP
2019-06-28 15:53:37 +00:00
/*!
\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(type) \
using T_USERDATA = type; \
static Porygon::UserData::UserData* __createUserData(){ \
return new Porygon::UserData::UserData({ \
2019-06-28 15:53:37 +00:00
/*!
\brief Ends creation of invokable function to create userdata.
*/
#define PORYGON_USERDATA_END() });};
2019-06-28 15:53:37 +00:00
/*!
\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(type, fields) \
PORYGON_USERDATA_START(type) \
2019-06-28 15:53:37 +00:00
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))
#define PORYGON_FIELD(fieldName, fieldType, getterHelper, setterHelper) \
{ \
Porygon::Utilities::HashedString::ConstHash(#fieldName), \
new Porygon::UserData::UserDataField(fieldType, \
2019-07-25 15:23:54 +00:00
[](void* obj) -> const Porygon::Evaluation::EvalValue* { return new getterHelper;}, \
[](void* obj, const Porygon::Evaluation::EvalValue* val) { ((T_USERDATA*)obj)->fieldName = setterHelper;} \
) \
}, \
#define PORYGON_READONLY_FIELD(fieldName, fieldType, getterHelper) \
{ \
Porygon::Utilities::HashedString::ConstHash(#fieldName), \
new Porygon::UserData::UserDataField(fieldType, \
[](void* obj) -> Porygon::Evaluation::EvalValue* { return new getterHelper;}, \
nullptr \
) \
}, \
#define PORYGON_INTEGER_FIELD(fieldName) \
PORYGON_FIELD(fieldName, PORYGON_INTEGER_TYPE, \
2019-07-25 15:23:54 +00:00
const Porygon::Evaluation::IntegerEvalValue(((T_USERDATA*)obj)->fieldName), val->EvaluateInteger())
#define PORYGON_READONLY_INTEGER_FIELD(fieldName) \
PORYGON_READONLY_FIELD(fieldName, PORYGON_INTEGER_TYPE, \
Porygon::Evaluation::IntegerEvalValue(((T_USERDATA*)obj)->fieldName))
#define PORYGON_FLOAT_FIELD(fieldName) \
PORYGON_FIELD(fieldName, PORYGON_FLOAT_TYPE, \
Porygon::EvaluationFloatEvalValue(((T_USERDATA*)obj)->fieldName), val->EvaluateFloat())
#define PORYGON_READONLY_FLOAT_FIELD(fieldName) \
PORYGON_READONLY_FIELD(fieldName, PORYGON_FLOAT_TYPE, \
Porygon::EvaluationFloatEvalValue(((T_USERDATA*)obj)->fieldName))
#define PORYGON_FUNCTION(fieldName, returnType, ...) \
{ \
Porygon::Utilities::HashedString::ConstHash(#fieldName), \
new Porygon::UserData::UserDataField( \
new Porygon::GenericFunctionScriptType( \
Porygon::UserData::UserDataFunctionOption::FromRawPointers(returnType, {__VA_ARGS__} )), \
\
\
2019-07-25 15:23:54 +00:00
[](void* obj) -> const Porygon::Evaluation::EvalValue* { \
auto t = new Porygon::Evaluation::GenericFunctionEvalValue(make_shared<GenericFunctionScriptType>(), rand()); \
t->RegisterOption(new Porygon::UserData::UserDataFunction( \
2019-07-25 15:23:54 +00:00
[](void* obj, const Porygon::Evaluation::EvalValue* par[], int parameterCount) \
-> const Porygon::Evaluation::EvalValue*{return ((const T_USERDATA*)obj)->invoke__##fieldName(obj, par, parameterCount);}, \
obj)); \
return t;}, \
nullptr) \
},
2019-06-28 15:53:37 +00:00
#define PORYGON_INTEGER_FUNCTION(fieldName, ...) \
PORYGON_FUNCTION(fieldName, new Porygon::NumericScriptType(true, false), __VA_ARGS__ )
2019-06-28 15:53:37 +00:00
/*!
\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, ...) \
2019-07-25 15:23:54 +00:00
static const Porygon::Evaluation::EvalValue* invoke__##fieldName(void* obj, const Porygon::Evaluation::EvalValue* par[], int parameterCount){ \
return Porygon::Evaluation::EvalValueHelper::Create(((userDataTypeName*)obj)->fieldName( \
__VA_ARGS__ \
));}
#endif //PORYGONLANG_USERDATATEMPLATES_HPP