/codeblocks-10.05-release/src/include/scripting/sqplus/sqplus.h
C Header | 1992 lines | 1559 code | 256 blank | 177 comment | 77 complexity | 6a309a30c210c36b9ec8305cf33ada45 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
Large files files are truncated, but you can click here to view the full file
- // SqPlus.h
- // Created by John Schultz 9/05/05, major update 10/05/05.
- // Template function call design from LuaPlusCD by Joshua C. Jensen,
- // inspired by luabind which was inspired by boost::python.
- // Const argument, const member functions, and Mac OS-X changes by Simon Michelmore.
- // DECLARE_INSTANCE_TYPE_NAME changes by Ben (Project5) from http://www.squirrel-lang.org/forums/.
- // Added Kamaitati's changes 5/28/06.
- // Free for any use.
- #ifndef _SQ_PLUS_H_
- #define _SQ_PLUS_H_
- #include <stdlib.h>
- #ifdef __APPLE__
- #include <malloc/malloc.h>
- #else
- // C::B patch: support for FreeBSD
- #ifdef __FreeBSD__
- #include <stdlib.h>
- #else
- #include <malloc.h>
- #endif
- #endif
- #include <memory.h>
- #if defined(_MSC_VER) || defined(__BORLANDC__)
- #include <tchar.h>
- #ifndef UNICODE
- #define SCSNPRINTF _snprintf
- #define SCPUTS puts
- #else
- #define SCSNPRINTF _snwprintf
- #define SCPUTS _putws
- #endif
- #else
- // C::B patch: Convert conflicting _T() to sqT() --> applies to nearly *all* files!
- // #define _T(n) n
- #define sqT(n) n
- #define SCSNPRINTF snprintf
- #include <stdio.h> // for snprintf
- #define SCPUTS puts
- #endif
- #if 1
- #define SQ_CALL_RAISE_ERROR SQTrue
- #else
- #define SQ_CALL_RAISE_ERROR SQFalse
- #endif
- #undef _UNICODE
- #include "squirrel.h"
- // C::B patch: so it builds on 64bit, ecapsulate bool/int/float using Squirrel types
- #ifndef _WINDEF_
- typedef SQInteger BOOL;
- typedef SQInteger INT;
- typedef SQFloat FLOAT;
- #define TRUE 1
- #define FALSE 0
- #endif
- #include "SquirrelObject.h"
- #include "SquirrelVM.h"
- #include "SquirrelBindingsUtils.h"
- namespace SqPlus {
- // === Class Inheritance Support ===
- // Inheritance in Squirrel allows one class to inherit a base class's functions and variables.
- // Variables are merged: if Derived has a var name 'val' and Base has a var of the same name,
- // the resulting var 'val' will take Derived's initialization value.
- // Functions are not merged, and can be called via Squirrel scoping rules.
- // Define SQ_USE_CLASS_INHERITANCE to enable class inheritance support
- // (requires slightly more memory and adds some CPU overhead).
- // Can also be useful for debugging, as class type information is checked before
- // dispatching instance function calls and before accessing instance member variables.
- #define SQ_USE_CLASS_INHERITANCE
- // Comment out to turn off instance type info support (to save a small amount of memory).
- #define SQ_SUPPORT_INSTANCE_TYPE_INFO
- // === Constant argument and constant member function support ===
- // Define SQPLUS_CONST_OPT before including SqPlus.h for constant argument + constant member function support.
- // C::B patch: Enable this option
- #define SQPLUS_CONST_OPT
- // === Uncomment to support std::string ===
- //#define SQPLUS_SUPPORT_STD_STRING
- // === Uncomment to support typedef std::basic_string<SQChar> sq_std_string ===
- //#define SQPLUS_SUPPORT_SQ_STD_STRING
- // === Uncomment to support NULL INSTANCE arguments ===
- //#define SQPLUS_SUPPORT_NULL_INSTANCES
- // === Uncomment to support C++ style inheritance
- #define CPP_STYLE_INHERITANCE
- // === Uncomment to skip sq_argassert() ===
- //#define SQ_SKIP_ARG_ASSERT
- template<class T> struct TypeWrapper {};
- struct SquirrelNull {};
- struct SQAnything { void * anything; }; // Needed for binding pointers to variables (cannot dereference void *).
- typedef SQAnything * SQAnythingPtr;
- typedef SQChar * SQCharPtr;
- // === Do not use directly: use one of the predefined sizes below ===
- struct ScriptStringVarBase {
- const unsigned char MaxLength; // Real length is MaxLength+1.
- SQChar s[1];
- ScriptStringVarBase(SQInteger _MaxLength) : MaxLength(_MaxLength) {}
- operator SQChar * () { return &s[0]; }
- operator void * () { return (void *)&s[0]; }
- const SQChar * operator = (const SQChar * _s) {
- return safeStringCopy(s,_s,MaxLength);
- }
- // Special safe string copy where MaxLength is 1 less than true buffer length.
- // strncpy() pads out nulls for the full length of the buffer specified by MaxLength.
- static inline SQChar * safeStringCopy(SQChar * d,const SQChar * s,SQInteger MaxLength) {
- SQInteger i=0;
- while (s[i]) {
- d[i] = s[i];
- i++;
- if (i == MaxLength) break;
- } // while
- d[i] = 0; // Null terminate.
- return d;
- } // safeStringCopy
- };
- // === Do not use directly: use one of the predefined sizes below ===
- template<SQInteger MAXLENGTH> // MAXLENGTH is max printable characters (trailing NULL is accounted for in ScriptStringVarBase::s[1]).
- struct ScriptStringVar : ScriptStringVarBase {
- SQChar ps[MAXLENGTH];
- ScriptStringVar() : ScriptStringVarBase(MAXLENGTH) {
- s[0] = 0;
- }
- ScriptStringVar(const SQChar * _s) : ScriptStringVarBase(MAXLENGTH) {
- *this = _s;
- }
- const SQChar * operator = (const SQChar * _s) {
- return safeStringCopy(s,_s,MaxLength);
- }
- const SQChar * operator = (const ScriptStringVar & _s) {
- return safeStringCopy(s,_s.s,MaxLength);
- }
- bool operator == (const ScriptStringVar & _s) {
- return _strcmp(s,_s.s) == 0;
- }
- bool compareCaseInsensitive(const ScriptStringVar & _s) {
- return _stricmp(s,_s.s) == 0;
- }
- };
- // === Fixed size strings for scripting ===
- typedef ScriptStringVar<8> ScriptStringVar8;
- typedef ScriptStringVar<16> ScriptStringVar16;
- typedef ScriptStringVar<32> ScriptStringVar32;
- typedef ScriptStringVar<64> ScriptStringVar64;
- typedef ScriptStringVar<128> ScriptStringVar128;
- typedef ScriptStringVar<256> ScriptStringVar256;
- // === Script Variable Types ===
- enum ScriptVarType {VAR_TYPE_NONE=-1,VAR_TYPE_INT=0,VAR_TYPE_FLOAT,VAR_TYPE_BOOL,VAR_TYPE_CONST_STRING,VAR_TYPE_STRING,VAR_TYPE_USER_POINTER,VAR_TYPE_INSTANCE};
- template <typename T>
- struct TypeInfo {
- const SQChar * typeName;
- enum {TypeID=VAR_TYPE_NONE,Size=0};
- };
- // === Common Variable Types ===
- template<>
- struct TypeInfo<INT> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("int")) {}
- enum {TypeID=VAR_TYPE_INT,Size=sizeof(INT)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<FLOAT> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("float")) {}
- enum {TypeID=VAR_TYPE_FLOAT,Size=sizeof(FLOAT)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<bool> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("bool")) {}
- enum {TypeID=VAR_TYPE_BOOL,Size=sizeof(bool)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<SQUserPointer> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("SQUserPointer")) {}
- enum {TypeID=VAR_TYPE_USER_POINTER,Size=sizeof(SQUserPointer)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<SQAnything> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("SQUserPointer")) {}
- enum {TypeID=VAR_TYPE_USER_POINTER,Size=sizeof(SQUserPointer)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<const SQChar *> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("const SQChar *")) {}
- enum {TypeID=VAR_TYPE_CONST_STRING,Size=sizeof(const SQChar *)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<ScriptStringVarBase> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("ScriptStringVarBase")) {}
- enum {TypeID=VAR_TYPE_STRING,Size=sizeof(ScriptStringVarBase)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- // === Fixed String Variants ===
- template<>
- struct TypeInfo<ScriptStringVar8> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("ScriptStringVar8")) {}
- enum {TypeID=VAR_TYPE_STRING,Size=sizeof(ScriptStringVar8)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<ScriptStringVar16> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("ScriptStringVar16")) {}
- enum {TypeID=VAR_TYPE_STRING,Size=sizeof(ScriptStringVar16)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<ScriptStringVar32> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("ScriptStringVar32")) {}
- enum {TypeID=VAR_TYPE_STRING,Size=sizeof(ScriptStringVar32)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<ScriptStringVar64> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("ScriptStringVar64")) {}
- enum {TypeID=VAR_TYPE_STRING,Size=sizeof(ScriptStringVar64)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<ScriptStringVar128> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("ScriptStringVar128")) {}
- enum {TypeID=VAR_TYPE_STRING,Size=sizeof(ScriptStringVar128)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- template<>
- struct TypeInfo<ScriptStringVar256> {
- const SQChar * typeName;
- TypeInfo() : typeName(sqT("ScriptStringVar256")) {}
- enum {TypeID=VAR_TYPE_STRING,Size=sizeof(ScriptStringVar256)};
- operator ScriptVarType() { return ScriptVarType(TypeID); }
- };
- enum VarAccessType {VAR_ACCESS_READ_WRITE=0,VAR_ACCESS_READ_ONLY=1<<0,VAR_ACCESS_CONSTANT=1<<1,VAR_ACCESS_STATIC=1<<2};
- // See VarRef and ClassType<> below: for instance assignment.
- typedef void (*CopyVarFunc)(void * dst,void * src);
- // === Variable references for script access ===
- #define SQ_PLUS_TYPE_TABLE sqT("__SqTypes")
- struct VarRef {
- // In this case 'offsetOrAddrOrConst' is simpler than using an anonymous union.
- void * offsetOrAddrOrConst; // Instance member variable offset from 'this' pointer base (as size_t), or address if static variable (void *), or constant value.
- ScriptVarType type; // Variable type (from enum above).
- SQUserPointer instanceType; // Unique ID for the containing class instance (for instance vars only). When the var is an instance, its type is encoded in copyFunc.
- CopyVarFunc copyFunc; // Function pointer to copy variables (for instance variables only).
- short size; // Currently for debugging only (size of item when pointer to item is dereferenced). Could be used for variable max string buffer length.
- short access; // VarAccessType.
- const SQChar * typeName; // Type name string (to create instances by name).
- VarRef() : offsetOrAddrOrConst(0), type(VAR_TYPE_NONE), instanceType((SQUserPointer)-1), copyFunc(0), size(0), access(VAR_ACCESS_READ_WRITE) {}
- VarRef(void * _offsetOrAddrOrConst, ScriptVarType _type, SQUserPointer _instanceType, CopyVarFunc _copyFunc, SQInteger _size,VarAccessType _access,const SQChar * _typeName) :
- offsetOrAddrOrConst(_offsetOrAddrOrConst), type(_type), instanceType(_instanceType), copyFunc(_copyFunc), size(_size), access(_access), typeName(_typeName) {
- #ifdef SQ_SUPPORT_INSTANCE_TYPE_INFO
- SquirrelObject typeTable = SquirrelVM::GetRootTable().GetValue(SQ_PLUS_TYPE_TABLE);
- if (typeTable.IsNull()) {
- typeTable = SquirrelVM::CreateTable();
- SquirrelObject root = SquirrelVM::GetRootTable();
- root.SetValue(SQ_PLUS_TYPE_TABLE,typeTable);
- } // if
- typeTable.SetValue(INT((size_t)copyFunc),typeName);
- #endif
- }
- };
- typedef VarRef * VarRefPtr;
- // Internal use only.
- inline void getVarNameTag(SQChar * buff,INT maxSize,const SQChar * scriptName) {
- // assert(maxSize > 3);
- #if 1
- SQChar * d = buff;
- d[0] = '_';
- d[1] = 'v';
- d = &d[2];
- maxSize -= (2+1); // +1 = space for null.
- SQInteger pos=0;
- while (scriptName[pos] && pos < maxSize) {
- d[pos] = scriptName[pos];
- pos++;
- } // while
- d[pos] = 0; // null terminate.
- #else
- SCSNPRINTF(buff,maxSize,sqT("_v%s"),scriptName);
- #endif
- } // getVarNameTag
- // Internal use only.
- SQInteger setVarFunc(HSQUIRRELVM v);
- SQInteger getVarFunc(HSQUIRRELVM v);
- SQInteger setInstanceVarFunc(HSQUIRRELVM v);
- SQInteger getInstanceVarFunc(HSQUIRRELVM v);
- // === BEGIN Helpers ===
- inline void createTableSetGetHandlers(SquirrelObject & so) {
- SquirrelObject delegate = so.GetDelegate();
- if (!delegate.Exists(sqT("_set"))) {
- delegate = SquirrelVM::CreateTable();
- SquirrelVM::CreateFunction(delegate,setVarFunc,sqT("_set"),sqT("sn|b|s")); // String var name = number(SQInteger or float) or bool or string.
- SquirrelVM::CreateFunction(delegate,getVarFunc,sqT("_get"),sqT("s")); // String var name.
- so.SetDelegate(delegate);
- } // if
- } // createTableSetGetHandlers
- inline VarRefPtr createVarRef(SquirrelObject & so,const SQChar * scriptVarName) {
- VarRefPtr pvr=0;
- ScriptStringVar256 scriptVarTagName; getVarNameTag(scriptVarTagName,sizeof(scriptVarTagName),scriptVarName);
- if (!so.GetUserData(scriptVarTagName,(SQUserPointer *)&pvr)) {
- so.NewUserData(scriptVarTagName,sizeof(*pvr));
- if (!so.GetUserData(scriptVarTagName,(SQUserPointer *)&pvr)) throw SquirrelError(sqT("Could not create UserData."));
- } // if
- return pvr;
- } // createVarRef
- template<typename T>
- void validateConstantType(T constant) {
- switch(TypeInfo<T>()) {
- case VAR_TYPE_INT:
- case VAR_TYPE_FLOAT:
- case VAR_TYPE_BOOL:
- case VAR_TYPE_CONST_STRING:
- break;
- default:
- throw SquirrelError(sqT("validateConstantType(): type must be INT, FLOAT, BOOL, or CONST CHAR *."));
- } // case
- } // validateConstantType
- inline void createInstanceSetGetHandlers(SquirrelObject & so) {
- if (!so.Exists(sqT("_set"))) {
- SquirrelVM::CreateFunction(so,setInstanceVarFunc,sqT("_set"),sqT("sn|b|s|x")); // String var name = number(SQInteger or float) or bool or string or instance.
- SquirrelVM::CreateFunction(so,getInstanceVarFunc,sqT("_get"),sqT("s")); // String var name.
- } // if
- } // createInstanceSetGetHandlers
- // === END Helpers ===
- // === Class Type Helper class: returns a unique number for each class type ===
- template<typename T>
- struct ClassType {
- static SQUserPointer type(void) { return (SQUserPointer)© }
- static CopyVarFunc getCopyFunc(void) { return (CopyVarFunc)© }
- static void copy(T * dst,T * src) {
- *dst = *src;
- } // copy
- };
- // === Bind a global or pre-allocated (not instance) class member variable or constant (for tables only (not classes)) ===
- template<typename T>
- void BindVariable(SquirrelObject & so,T * var,const SQChar * scriptVarName,VarAccessType access=VAR_ACCESS_READ_WRITE) {
- VarRefPtr pvr = createVarRef(so,scriptVarName);
- *pvr = VarRef(var,TypeInfo<T>(),0,ClassType<T>::getCopyFunc(),sizeof(*var),access,TypeInfo<T>().typeName);
- createTableSetGetHandlers(so);
- } // BindVariable
- // === Bind a constant by value: INT, FLOAT, BOOL, or CONST CHAR * (for tables only (not classes)) ===
- template<typename T>
- void BindConstant(SquirrelObject & so,T constant,const SQChar * scriptVarName) {
- validateConstantType(constant);
- VarRefPtr pvr = createVarRef(so,scriptVarName);
- struct CV {
- T var;
- } cv; // Cast Variable helper.
- cv.var = constant;
- *pvr = VarRef(*(void **)&cv,TypeInfo<T>(),0,0,sizeof(constant),VAR_ACCESS_CONSTANT,TypeInfo<T>().typeName);
- createTableSetGetHandlers(so);
- } // BindConstant
- template<typename T>
- void BindVariable(T * var,const SQChar * scriptVarName,VarAccessType access=VAR_ACCESS_READ_WRITE) {
- SquirrelObject so = SquirrelVM::GetRootTable();
- BindVariable(so,var,scriptVarName,access);
- } // BindVariable
- template<typename T>
- void BindConstant(T constant,const SQChar * scriptVarName) {
- SquirrelObject so = SquirrelVM::GetRootTable();
- BindConstant(so,constant,scriptVarName);
- } // BindConstant
- // === Register a class instance member variable or constant. var argument provides type and offset ( effectively &((ClassType *)0)->var ) ===
- // classType is the type of the member variable's containing class.
- template<typename T>
- void RegisterInstanceVariable(SquirrelObject & so,SQUserPointer classType,T * var,const SQChar * scriptVarName,VarAccessType access=VAR_ACCESS_READ_WRITE) {
- VarRef * pvr = createVarRef(so,scriptVarName);
- void * offsetOrAddrOrConst = (void *)var; // var must be passed in as &obj->var, where obj = 0 (the address is the offset), or as static/global address.
- *pvr = VarRef(offsetOrAddrOrConst,TypeInfo<T>(),classType,ClassType<T>::getCopyFunc(),sizeof(*var),access,TypeInfo<T>().typeName);
- createInstanceSetGetHandlers(so);
- } // RegisterInstanceVariable
- template<typename T>
- void RegisterInstanceConstant(SquirrelObject & so,SQUserPointer classType,T constant,const SQChar * scriptVarName) {
- validateConstantType(constant);
- VarRef * pvr = createVarRef(so,scriptVarName);
- struct CV {
- T var;
- size_t pad;
- } cv; // Cast Variable helper.
- cv.var = constant;
- *pvr = VarRef(*(void **)&cv,TypeInfo<T>(),classType,0,sizeof(constant),VAR_ACCESS_CONSTANT,TypeInfo<T>().typeName);
- createInstanceSetGetHandlers(so);
- } // RegisterInstanceConstant
- //////////////////////////////////////////////////////////////////////////
- /////////// BEGIN Generalized Class/Struct Instance Support //////////////
- //////////////////////////////////////////////////////////////////////////
- //BOOL CreateNativeClassInstance(HSQUIRRELVM v,const SQChar * classname,SQUserPointer ud,SQRELEASEHOOK hook); // In SquirrelBindingUtils.cpp.
- // Create native class instance and leave on stack.
- inline BOOL CreateConstructNativeClassInstance(HSQUIRRELVM v,const SQChar * className) {
- SQInteger oldtop = sq_gettop(v);
- sq_pushroottable(v);
- sq_pushstring(v,className,-1);
- if (SQ_FAILED(sq_rawget(v,-2))) { // Get the class (created with sq_newclass()).
- sq_settop(v,oldtop);
- return FALSE;
- } // if
- #if 0
- sq_remove(v,-3); // Remove the root table.
- sq_push(v,1); // Push the 'this'.
- #else // Kamaitati's change. 5/28/06 jcs.
- sq_remove(v,-2); // Remove the root table.
- sq_pushroottable(v); // Push the 'this'.
- #endif
- if (SQ_FAILED(sq_call(v,1,SQTrue,SQ_CALL_RAISE_ERROR))) { // Call ClassName(): creates new instance and calls constructor (instead of sq_createinstance() where constructor is not called).
- sq_settop(v,oldtop);
- return FALSE;
- } // if
- sq_remove(v,-2); // Remove the class.
- // SQInteger newtop = sq_gettop(v);
- return TRUE;
- } // CreateConstructNativeClassInstance
- // Create new instance, copy 'classToCopy', and store result on stack.
- template<typename T>
- inline BOOL CreateCopyInstance(const SQChar * className,const T & classToCopy) {
- HSQUIRRELVM v = SquirrelVM::GetVMPtr();
- if (!CreateConstructNativeClassInstance(v,className)) {
- return FALSE;
- } // if
- SQUserPointer up=0;
- sq_getinstanceup(v,-1,&up,ClassType<T>::type());
- if (!up) return FALSE;
- T * newClass = (T *)up;
- *newClass = classToCopy; // <TODO> Optimized version that uses the copy constructor.
- return TRUE;
- } // CreateCopyInstance
- // Create a new copy of type 'className' and copy 'classToCopy', return result via SquirrelObject.
- template<typename T>
- inline SquirrelObject NewClassCopy(const SQChar * className,const T & classToCopy) {
- SquirrelObject ret;
- if (CreateCopyInstance(className,classToCopy)) {
- ret.AttachToStackObject(-1);
- sq_poptop(SquirrelVM::GetVMPtr());
- } else {
- throw SquirrelError(sqT("NewClassCopy(): could not create class"));
- } // if
- return ret;
- } // NewClassCopy
- // Return a new class copy on the stack from a varArgs function call.
- template<typename T>
- inline SQInteger ReturnCopy(HSQUIRRELVM v,const T & classToCopy) {
- SquirrelObject so(NewClassCopy(GetTypeName(classToCopy),classToCopy));
- return StackHandler(v).Return(so);
- } // ReturnCopy
- // Katsuaki Kawachi's GetInstance<> exception change. 6/27/06 jcs
- // Get an instance of type T from the stack at idx (for function calls).
- template<typename T,bool ExceptionOnError>
- T * GetInstance(HSQUIRRELVM v,SQInteger idx) {
- SQUserPointer up=0;
- sq_getinstanceup(v,idx,&up,ClassType<T>::type());
- if (ExceptionOnError) { // This code block should be compiled out when ExceptionOnError is false. In any case, the compiler should not generate a test condition (include or exclude the enclosed code block).
- if (!up) throw SquirrelError(sqT("GetInstance: Invalid argument type"));
- } // if
- return (T *)up;
- } // GetInstance
- // NAME and macro changes from Ben's (Project5) forum post. 2/26/06 jcs
- // Kamaitati's NULL_INSTANCE support. 5/28/06 jcs
- #ifdef SQPLUS_SUPPORT_NULL_INSTANCES
- #define DECLARE_INSTANCE_TYPE_NAME_(TYPE,NAME) namespace SqPlus { \
- inline const SQChar * GetTypeName(const TYPE & n) { return sqT(#NAME); } \
- inline void Push(HSQUIRRELVM v,TYPE * value) { \
- if (!value) sq_pushnull(v); \
- else if (!CreateNativeClassInstance(v,GetTypeName(*value),value,0)) \
- throw SquirrelError(sqT("Push(): could not create INSTANCE (check registration name)")); } \
- inline void Push(HSQUIRRELVM v,TYPE & value) { if (!CreateCopyInstance(GetTypeName(value),value)) throw SquirrelError(sqT("Push(): could not create INSTANCE copy (check registration name)")); } \
- inline bool Match(TypeWrapper<TYPE &>,HSQUIRRELVM v,SQInteger idx) { return GetInstance<TYPE,false>(v,idx) != NULL; } \
- inline bool Match(TypeWrapper<TYPE *>,HSQUIRRELVM v,SQInteger idx) { \
- return (sq_gettype(v,idx)==OT_NULL) || (GetInstance<TYPE,false>(v,idx) != NULL); } \
- inline TYPE & Get(TypeWrapper<TYPE &>,HSQUIRRELVM v,SQInteger idx) { return *GetInstance<TYPE,true>(v,idx); } \
- inline TYPE * Get(TypeWrapper<TYPE *>,HSQUIRRELVM v,SQInteger idx) { \
- if (sq_gettype(v,idx)==OT_NULL) return NULL; \
- return GetInstance<TYPE,true>(v,idx); } \
- template<> \
- struct TypeInfo<TYPE> { \
- const SQChar * typeName; \
- TypeInfo() : typeName(sqT(#NAME)) {} \
- enum {TypeID=VAR_TYPE_INSTANCE,Size=sizeof(TYPE)}; \
- operator ScriptVarType() { return ScriptVarType(TypeID); } \
- }; \
- } // nameSpace SqPlus
- #else
- #define DECLARE_INSTANCE_TYPE_NAME_(TYPE,NAME) namespace SqPlus { \
- inline const SQChar * GetTypeName(const TYPE & n) { return sqT(#NAME); } \
- inline void Push(HSQUIRRELVM v,TYPE * value) { if (!CreateNativeClassInstance(v,GetTypeName(*value),value,0)) throw SquirrelError(sqT("Push(): could not create INSTANCE (check registration name)")); } \
- inline void Push(HSQUIRRELVM v,TYPE & value) { if (!CreateCopyInstance(GetTypeName(value),value)) throw SquirrelError(sqT("Push(): could not create INSTANCE copy (check registration name)")); } \
- inline bool Match(TypeWrapper<TYPE &>,HSQUIRRELVM v,SQInteger idx) { return GetInstance<TYPE,false>(v,idx) != NULL; } \
- inline bool Match(TypeWrapper<TYPE *>,HSQUIRRELVM v,SQInteger idx) { return GetInstance<TYPE,false>(v,idx) != NULL; } \
- inline TYPE & Get(TypeWrapper<TYPE &>,HSQUIRRELVM v,SQInteger idx) { return *GetInstance<TYPE,true>(v,idx); } \
- inline TYPE * Get(TypeWrapper<TYPE *>,HSQUIRRELVM v,SQInteger idx) { return GetInstance<TYPE,true>(v,idx); } \
- template<> \
- struct TypeInfo<TYPE> { \
- const SQChar * typeName; \
- TypeInfo() : typeName(sqT(#NAME)) {} \
- enum {TypeID=VAR_TYPE_INSTANCE,Size=sizeof(TYPE)}; \
- operator ScriptVarType() { return ScriptVarType(TypeID); } \
- }; \
- } // nameSpace SqPlus
- #endif
- // TYPE or NAME below must match the string name used in SqClassDef<>, otherwise name lookup won't match and Squirrel will throw a "can't create instance" error.
- #ifndef SQPLUS_CONST_OPT
- #define DECLARE_INSTANCE_TYPE(TYPE) DECLARE_INSTANCE_TYPE_NAME_(TYPE,TYPE)
- #define DECLARE_INSTANCE_TYPE_NAME(TYPE,NAME) DECLARE_INSTANCE_TYPE_NAME_(TYPE,NAME)
- #else
- #define SQPLUS_DECLARE_INSTANCE_TYPE_CONST
- #include "SqPlusConst.h"
- #endif
- //////////////////////////////////////////////////////////////////////////
- //////////// END Generalized Class/Struct Instance Support ///////////////
- //////////////////////////////////////////////////////////////////////////
- #ifndef SQ_SKIP_ARG_ASSERT
- #define sq_argassert(arg,_index_) if (!Match(TypeWrapper<P##arg>(),v,_index_)) return sq_throwerror(v,sqT("Incorrect function argument"))
- #else
- #define sq_argassert(arg,_index_)
- #endif
- // === Return value variants ===
- template<class RT>
- struct ReturnSpecialization {
- // === Standard Function calls ===
- static SQInteger Call(RT (*func)(),HSQUIRRELVM v,SQInteger /*index*/) {
- RT ret = func();
- Push(v,ret);
- return 1;
- }
- template<typename P1>
- static SQInteger Call(RT (*func)(P1),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- RT ret = func(
- Get(TypeWrapper<P1>(),v,index + 0)
- );
- Push(v,ret);
- return 1;
- }
- template<typename P1,typename P2>
- static SQInteger Call(RT (*func)(P1,P2),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- RT ret = func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1)
- );
- Push(v,ret);
- return 1;
- }
- template<typename P1,typename P2,typename P3>
- static SQInteger Call(RT (*func)(P1,P2,P3),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- RT ret = func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2)
- );
- Push(v,ret);
- return 1;
- }
- template<typename P1,typename P2,typename P3,typename P4>
- static SQInteger Call(RT (*func)(P1,P2,P3,P4),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- RT ret = func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3)
- );
- Push(v,ret);
- return 1;
- }
- template<typename P1,typename P2,typename P3,typename P4,typename P5>
- static SQInteger Call(RT (*func)(P1,P2,P3,P4,P5),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- RT ret = func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4)
- );
- Push(v,ret);
- return 1;
- }
- template<typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
- static SQInteger Call(RT (*func)(P1,P2,P3,P4,P5,P6),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- sq_argassert(6,index + 5);
- RT ret = func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4),
- Get(TypeWrapper<P6>(),v,index + 5)
- );
- Push(v,ret);
- return 1;
- }
- template<typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
- static SQInteger Call(RT (*func)(P1,P2,P3,P4,P5,P6,P7),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- sq_argassert(6,index + 5);
- sq_argassert(7,index + 6);
- RT ret = func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4),
- Get(TypeWrapper<P6>(),v,index + 5),
- Get(TypeWrapper<P7>(),v,index + 6)
- );
- Push(v,ret);
- return 1;
- }
- // === Member Function calls ===
- template <typename Callee>
- static SQInteger Call(Callee & callee,RT (Callee::*func)(),HSQUIRRELVM v,SQInteger /*index*/) {
- RT ret = (callee.*func)();
- Push(v,ret);
- return 1;
- }
- template <typename Callee,typename P1>
- static SQInteger Call(Callee & callee,RT (Callee::*func)(P1),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- RT ret = (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0)
- );
- Push(v,ret);
- return 1;
- }
- template<typename Callee,typename P1,typename P2>
- static SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- RT ret = (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1)
- );
- Push(v,ret);
- return 1;
- }
- template<typename Callee,typename P1,typename P2,typename P3>
- static SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- RT ret = (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2)
- );
- Push(v,ret);
- return 1;
- }
- template<typename Callee,typename P1,typename P2,typename P3,typename P4>
- static SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- RT ret = (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3)
- );
- Push(v,ret);
- return 1;
- }
- template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5>
- static SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- RT ret = (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4)
- );
- Push(v,ret);
- return 1;
- }
- template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
- static SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5,P6),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- sq_argassert(6,index + 5);
- RT ret = (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4),
- Get(TypeWrapper<P6>(),v,index + 5)
- );
- Push(v,ret);
- return 1;
- }
- template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
- static SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5,P6,P7),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- sq_argassert(6,index + 5);
- sq_argassert(7,index + 6);
- RT ret = (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4),
- Get(TypeWrapper<P6>(),v,index + 5),
- Get(TypeWrapper<P7>(),v,index + 6)
- );
- Push(v,ret);
- return 1;
- }
- #ifdef SQPLUS_CONST_OPT
- #define SQPLUS_CALL_CONST_MFUNC_RET0
- #include "SqPlusConst.h"
- #endif
- };
- // === No return value variants ===
- template<>
- struct ReturnSpecialization<void> {
- // === Standard function calls ===
- static SQInteger Call(void (*func)(),HSQUIRRELVM v,SQInteger /*index*/) {
- (void)v;
- func();
- return 0;
- }
- template<typename P1>
- static SQInteger Call(void (*func)(P1),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- func(
- Get(TypeWrapper<P1>(),v,index + 0)
- );
- return 0;
- }
- template<typename P1,typename P2>
- static SQInteger Call(void (*func)(P1,P2),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1)
- );
- return 0;
- }
- template<typename P1,typename P2,typename P3>
- static SQInteger Call(void (*func)(P1,P2,P3),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2)
- );
- return 0;
- }
- template<typename P1,typename P2,typename P3,typename P4>
- static SQInteger Call(void (*func)(P1,P2,P3,P4),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3)
- );
- return 0;
- }
- template<typename P1,typename P2,typename P3,typename P4,typename P5>
- static SQInteger Call(void (*func)(P1,P2,P3,P4,P5),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4)
- );
- return 0;
- }
- template<typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
- static SQInteger Call(void (*func)(P1,P2,P3,P4,P5,P6),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- sq_argassert(6,index + 5);
- func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4),
- Get(TypeWrapper<P6>(),v,index + 5)
- );
- return 0;
- }
- template<typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
- static SQInteger Call(void (*func)(P1,P2,P3,P4,P5,P6,P7),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- sq_argassert(6,index + 5);
- sq_argassert(7,index + 6);
- func(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4),
- Get(TypeWrapper<P6>(),v,index + 5),
- Get(TypeWrapper<P7>(),v,index + 6)
- );
- return 0;
- }
- // === Member function calls ===
- template<typename Callee>
- static SQInteger Call(Callee & callee,void (Callee::*func)(),HSQUIRRELVM,SQInteger /*index*/) {
- (callee.*func)();
- return 0;
- }
- template<typename Callee,typename P1>
- static SQInteger Call(Callee & callee,void (Callee::*func)(P1),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0)
- );
- return 0;
- }
- template<typename Callee,typename P1,typename P2>
- static SQInteger Call(Callee & callee,void (Callee::*func)(P1,P2),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1)
- );
- return 0;
- }
- template<typename Callee,typename P1,typename P2,typename P3>
- static SQInteger Call(Callee & callee,void (Callee::*func)(P1,P2,P3),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2)
- );
- return 0;
- }
- template<typename Callee,typename P1,typename P2,typename P3,typename P4>
- static SQInteger Call(Callee & callee,void (Callee::*func)(P1,P2,P3,P4),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3)
- );
- return 0;
- }
- template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5>
- static SQInteger Call(Callee & callee,void (Callee::*func)(P1,P2,P3,P4,P5),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4)
- );
- return 0;
- }
- template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
- static SQInteger Call(Callee & callee,void (Callee::*func)(P1,P2,P3,P4,P5,P6),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- sq_argassert(6,index + 5);
- (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4),
- Get(TypeWrapper<P6>(),v,index + 5)
- );
- return 0;
- }
- template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
- static SQInteger Call(Callee & callee,void (Callee::*func)(P1,P2,P3,P4,P5,P6,P7),HSQUIRRELVM v,SQInteger index) {
- sq_argassert(1,index + 0);
- sq_argassert(2,index + 1);
- sq_argassert(3,index + 2);
- sq_argassert(4,index + 3);
- sq_argassert(5,index + 4);
- sq_argassert(6,index + 5);
- sq_argassert(7,index + 6);
- (callee.*func)(
- Get(TypeWrapper<P1>(),v,index + 0),
- Get(TypeWrapper<P2>(),v,index + 1),
- Get(TypeWrapper<P3>(),v,index + 2),
- Get(TypeWrapper<P4>(),v,index + 3),
- Get(TypeWrapper<P5>(),v,index + 4),
- Get(TypeWrapper<P6>(),v,index + 5),
- Get(TypeWrapper<P7>(),v,index + 6)
- );
- return 0;
- }
- #ifdef SQPLUS_CONST_OPT
- #define SQPLUS_CALL_CONST_MFUNC_NORET
- #include "SqPlusConst.h"
- #endif
- };
- // === STANDARD Function return value specialized call handlers ===
- template<typename RT>
- SQInteger Call(RT (*func)(),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(func,v,index);
- }
- template<typename RT,typename P1>
- SQInteger Call(RT (*func)(P1),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(func,v,index);
- }
- template<typename RT,typename P1,typename P2>
- SQInteger Call(RT (*func)(P1,P2),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(func,v,index);
- }
- template<typename RT,typename P1,typename P2,typename P3>
- SQInteger Call(RT (*func)(P1,P2,P3),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(func,v,index);
- }
- template<typename RT,typename P1,typename P2,typename P3,typename P4>
- SQInteger Call(RT (*func)(P1,P2,P3,P4),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(func,v,index);
- }
- template<typename RT,typename P1,typename P2,typename P3,typename P4,typename P5>
- SQInteger Call(RT (*func)(P1,P2,P3,P4,P5),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(func,v,index);
- }
- template<typename RT,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
- SQInteger Call(RT (*func)(P1,P2,P3,P4,P5,P6),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(func,v,index);
- }
- template<typename RT,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
- SQInteger Call(RT (*func)(P1,P2,P3,P4,P5,P6,P7),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(func,v,index);
- }
- // === MEMBER Function return value specialized call handlers ===
- template<typename Callee,typename RT>
- SQInteger Call(Callee & callee, RT (Callee::*func)(),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(callee,func,v,index);
- }
- template<typename Callee,typename RT,typename P1>
- SQInteger Call(Callee & callee,RT (Callee::*func)(P1),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(callee,func,v,index);
- }
- template<typename Callee,typename RT,typename P1,typename P2>
- SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(callee,func,v,index);
- }
- template<typename Callee,typename RT,typename P1,typename P2,typename P3>
- SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(callee,func,v,index);
- }
- template<typename Callee,typename RT,typename P1,typename P2,typename P3,typename P4>
- SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(callee,func,v,index);
- }
- template<typename Callee,typename RT,typename P1,typename P2,typename P3,typename P4,typename P5>
- SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(callee,func,v,index);
- }
- template<typename Callee,typename RT,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
- SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5,P6),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(callee,func,v,index);
- }
- template<typename Callee,typename RT,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
- SQInteger Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5,P6,P7),HSQUIRRELVM v,SQInteger index) {
- return ReturnSpecialization<RT>::Call(callee,func,v,index);
- }
- #ifdef SQPLUS_CONST_OPT
- #define SQPLUS_CALL_CONST_MFUNC_RET1
- #include "SqPlusConst.h"
- #endif
- // === Direct Call Standard Function handler ===
- template<typename Func>
- struct DirectCallFunction {
- static inline SQInteger Dispatch(HSQUIRRELVM v) {
- StackHandler sa(v);
- SQInteger paramCount = sa.GetParamCount();
- Func * func = (Func *)sa.GetUserData(paramCount);
- return Call(*func,v,2);
- } // Dispatch
- };
- // === Direct Call Member Function handler ===
- template<typename Callee,typename Func>
- class DirectCallMemberFunction {
- public:
- static inline SQInteger Dispatch(HSQUIRRELVM v) {
- StackHandler sa(v);
- SQInteger paramCount = sa.GetParamCount();
- unsigned char * ud = (unsigned char *)sa.GetUserData(paramCount);
- // C::B patch: Handle invalid instance type here
- if (!*(Callee**)ud) {
- return sq_throwerror(v,sqT("Invalid Instance Type"));
- } // if
- return Call(**(Callee**)ud,*(Func*)(ud + sizeof(Callee*)),v,2);
- } // Dispatch
- };
- // === Direct Call Instance Member Function handler ===
- #define SQ_CLASS_OBJECT_TABLE_NAME sqT("__ot")
- #define SQ_CLASS_HIER_ARRAY sqT("__ca")
- template<typename Callee,typename Func>
- class DirectCallInstanceMemberFunction {
- public:
- static inline SQInteger Dispatch(HSQUIRRELVM v) {
- StackHandler sa(v);
- Callee * instance = (Callee *)sa.GetInstanceUp(1,0);
- SQInteger paramCount = sa.GetParamCount();
- Func * func = (Func *)sa.GetUserData(paramCount);
- // C::B patch: Let the compiler search (comment out the whole block)
- //#ifdef SQ_USE_CLASS_INHERITANCE
- // SquirrelObject so(sa.GetObjectHandle(1)); // 'this'
- // SQUserPointer typetag; so.GetTypeTag(&typetag);
- // SQUserPointer calleeType = ClassType<Callee>::type();
- // if (typetag != calleeType) {
- // SquirrelObject typeTable = so.GetValue(SQ_CLASS_OBJECT_TABLE_NAME);
- // instance = (Callee *)typeTable.GetUserPointer(INT((size_t)ClassType<Callee>::type())); // <TODO> 64-bit compatible version.
- // if (!instance) {
- // return sq_throwerror(v,sqT("Invalid Instance Type"));
- // } // if
- // } // if
- //#endif
- // C::B patch: If not instance -> return Ok anyways
- if (!instance)
- return SQ_OK;
- return Call(*instance,*func,v,2);
- } // Dispatch
- };
- // === Direct Call Instance Member Function Variable Argument handler ===
- template<typename Callee>
- class DirectCallInstanceMemberFunctionVarArgs {
- public:
- typedef SQInteger (Callee::*FuncType)(HSQUIRRELVM);
- static inline SQInteger Dispatch(HSQUIRRELVM v) {
- StackHandler sa(v);
- Callee * instance = (Callee *)sa.GetInstanceUp(1,0);
- SQInteger paramCount = sa.GetParamCount();
- FuncType func = *(FuncType *)sa.GetUserData(paramCount);
- // C::B patch: Let the compiler search (comment out the whole block)
- //#ifdef SQ_USE_CLASS_INHERITANCE
- // SquirrelObject so(sa.GetObjectHandle(1)); // 'this'
- // SQUserPointer typetag; so.GetTypeTag(&typetag);
- // SQUserPointer calleeType = ClassType<Callee>::type();
- // if (typetag != calleeType) {
- // SquirrelObject typeTable = so.GetValue(SQ_CLASS_OBJECT_TABLE_NAME);
- // instance = (Callee *)typeTable.GetUserPointer(INT((size_t)ClassType<Callee>::type())); // <TODO> 64-bit compatible version.
- // if (!instance) {
- // return sq_throwerror(v,sqT("Invalid Instance Type"));
- // } // if
- // } // if
- //#endif
- sq_poptop(v); // Remove UserData from stack: so sa.GetParamCount() returns actual param count.
- // C::B patch: If not instance -> return Ok anyways
- if (!instance)
- return SQ_OK;
- return (instance->*func)(v);
- } // Dispatch
- };
- // Code fragment useful for debugging new implementations.
- #if 0
- HSQOBJECT ho = sa.GetObjectHandle(paramCount);
- SquirrelObject so(ho);
- SQObjectType sot = so.GetType();
- #endif
- // === Standard function call ===
- template<typename Func>
- inline void sq_pushdirectclosure(HSQUIRRELVM v,Func func,SQUnsignedInteger nupvalues) {
- SQUserPointer up = sq_newuserdata(v,sizeof(func)); // Also pushed on stack.
- memcpy(up,&func,sizeof(func));
- sq_newclosure(v,DirectCallFunction<Func>::Dispatch,nupvalues+1);
- } // sq_pushdirectclosure
- // === Fixed Class pointer call (always calls with object pointer that was registered) ===
- template<typename Callee,typename Func>
- inline void sq_pushdirectclosure(HSQUIRRELVM v,const Callee & callee,Func func,SQUnsignedInteger nupvalues) {
- unsigned char * up = (unsigned char *)sq_newuserdata(v,sizeof(Callee*)+sizeof(func)); // Also pushed on stack.
- const SQUserPointer pCallee = (SQUserPointer)&callee;
- memcpy(up,&pCallee,sizeof(Callee*));
- memcpy(up + sizeof(Callee*),&func,sizeof(func));
- sq_newclosure(v,DirectCallMemberFunction<Callee,Func>::Dispatch,nupvalues+1);
- } // sq_pushdirectclosure
- // === Class Instance call: class pointer retrieved from script class instance ===
- template<typename Callee,typename Func>
- inline void sq_pushdirectinstanceclosure(HSQUIRRELVM v,const Callee & callee,Func func,SQUnsignedInteger nupvalues) {
- unsigned char * up = (unsigned char *)sq_newuserdata(v,sizeof(func)); // Also pushed on stack.
- memcpy(up,&func,sizeof(func));
- sq_newclosure(v,DirectCallInstanceMemberFunction<Callee,Func>::Dispatch,nupvalues+1);
- } // sq_pushdirectinstanceclosure
- // === Class Instance call: class pointer retrieved from script class instance (variable arguments) ===
- template<typename Callee>
- inline void sq_pushdirectinstanceclosurevarargs(HSQUIRRELVM v,const Callee & callee,SQInteger (Callee::*func)(HSQUIRRELVM),SQUnsignedInteger nupvalues) {
- unsigned char * up = (unsigned char *)sq_newuserdata(v,sizeof(func)); // Also pushed on stack.
- memcpy(up,&func,sizeof(func));
- sq_newclosure(v,DirectCallInstanceMemberFunctionVarArgs<Callee>::Dispatch,nupvalues+1);
- } // sq_pushdirectinstanceclosurevarargs
- // === Register a STANDARD function (table or class on stack) ===
- template<typename Func>
- inline void Register(HSQUIRRELVM v,Func func,const SQChar * name) {
- sq_pushstring(v,name,-1);
- sq_pushdirectclosure(v,func,0);
- sq_createslot(v,-3); // Stack is restored after this call (same state as before Register() call).
- } // Register
- // === Register a MEMBER function (table or class on stack) ===
- template<typename Callee,typename Func>
- inline void Register(HSQUIRRELVM v,Callee & callee,Func func,const SQChar * name) {
- sq_pushstring(v,name,-1);
- sq_pushdirectclosure(v,callee,func,0);
- sq_createslot(v,-3); // Stack is restored after this call (same state as before Register() call).
- } // Register
- // === Register a STANDARD global function (root table) ===
- template<typename Func>
- inline void RegisterGlobal(HSQUIRRELVM v,Func func,const SQChar * name) {
- sq_pushroottable(v);
- Register(v,func,name);
- sq_poptop(v); // Remove root table.
- } // RegisterGlobal
- template<typename Func>
- inline void RegisterGlobal(Func func,const SQChar * name) {
- RegisterGlobal(SquirrelVM::GetVMPtr(),func,name);
- } // RegisterGlobal
- // === Register a MEMBER global function (root table) ===
- template<typename Callee,typename Func>
- inline void RegisterGlobal(HSQUIRRELVM v,Callee & callee,Func func,const SQChar * name) {
- sq_pushroottable(v);
- Register(v,callee,func,name);
- sq_poptop(v); // Remove root table.
- } // RegisterGlobal
- template<typename Callee,typename Func>
- inline void RegisterGlobal(Callee & callee,Func func,const SQChar * name) {
- RegisterGlobal(SquirrelVM::GetVMPtr(),callee,func,name);
- } // RegisterGlobal
- // === Register a STAND…
Large files files are truncated, but you can click here to view the full file