1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#ifndef SQUIRREL_BINDINGS_UTILS_H
#define SQUIRREL_BINDINGS_UTILS_H
struct ScriptClassMemberDecl {
const SQChar *name;
SQFUNCTION func;
int params;
const SQChar *typemask;
};
struct SquirrelClassDecl {
const SQChar *name;
const SQChar *base;
const ScriptClassMemberDecl *members;
};
struct ScriptConstantDecl {
const SQChar *name;
SQObjectType type;
union value {
value(float v){ f = v; }
value(int v){ i = v; }
value(long int v){ li = v; }
value(const SQChar *v){ s = v; }
float f;
int i;
long int li;
const SQChar *s;
} val;
};
struct ScriptNamespaceDecl {
const SQChar *name;
const ScriptClassMemberDecl *members;
const ScriptConstantDecl *constants;
const ScriptClassMemberDecl *delegate;
};
#define _BEGIN_CLASS(classname) \
int __##classname##__typeof(HSQUIRRELVM v) \
{ \
sq_pushstring(v,_SC(#classname),-1); \
return 1; \
} \
struct ScriptClassMemberDecl __##classname##_members[] = { \
{_SC("_typeof"),__##classname##__typeof,1,NULL},
#define _BEGIN_NAMESPACE(xnamespace) struct ScriptClassMemberDecl __##xnamespace##_members[] = {
#define _BEGIN_NAMESPACE_CONSTANTS(xnamespace) {NULL,NULL,0,NULL}}; \
struct ScriptConstantDecl __##xnamespace##_constants[] = {
#define _BEGIN_DELEGATE(xnamespace) struct ScriptClassMemberDecl __##xnamespace##_delegate[] = {
#define _DELEGATE(xnamespace) __##xnamespace##_delegate
#define _END_DELEGATE(classname) {NULL,NULL,NULL,NULL}};
#define _CONSTANT(name,type,val) {_SC(#name),type,val},
#define _CONSTANT_IMPL(name,type) {_SC(#name),type,name},
#define _MEMBER_FUNCTION(classname,name,nparams,typemask) \
{_SC(#name),__##classname##_##name,nparams,typemask},
#define _END_NAMESPACE(classname,delegate) {NULL,OT_NULL,0}}; \
struct ScriptNamespaceDecl __##classname##_decl = { \
_SC(#classname), __##classname##_members,__##classname##_constants,delegate };
#define _END_CLASS(classname) {NULL,NULL,0,NULL}}; \
struct SquirrelClassDecl __##classname##_decl = { \
_SC(#classname), NULL, __##classname##_members };
#define _END_CLASS_INHERITANCE(classname,base) {NULL,NULL,NULL,NULL}}; \
struct SquirrelClassDecl __##classname##_decl = { \
_SC(#classname), _SC(#base), __##classname##_members };
#define _MEMBER_FUNCTION_IMPL(classname,name) \
int __##classname##_##name(HSQUIRRELVM v)
#define _INIT_STATIC_NAMESPACE(classname) CreateStaticNamespace(SquirrelVM::GetVMPtr(),&__##classname##_decl);
#define _INIT_CLASS(classname)CreateClass(SquirrelVM::GetVMPtr(),&__##classname##_decl);
#define _DECL_STATIC_NAMESPACE(xnamespace) extern struct ScriptNamespaceDecl __##xnamespace##_decl;
#define _DECL_CLASS(classname) extern struct SquirrelClassDecl __##classname##_decl;
#define _CHECK_SELF(cppclass,scriptclass) \
cppclass *self = NULL; \
if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)&__##scriptclass##_decl))) { \
return sq_throwerror(v,_SC("invalid instance type"));\
}
#define _CHECK_INST_PARAM(pname,idx,cppclass,scriptclass) \
cppclass *pname = NULL; \
if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer*)&pname,(SQUserPointer)&__##scriptclass##_decl))) { \
return sq_throwerror(v,_SC("invalid instance type"));\
} \
#define _CHECK_INST_PARAM_BREAK(pname,idx,cppclass,scriptclass) \
cppclass *pname = NULL; \
if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer*)&pname,(SQUserPointer)&__##scriptclass##_decl))) { \
break; \
} \
#define _CLASS_TAG(classname) ((unsigned int)&__##classname##_decl)
#define _DECL_NATIVE_CONSTRUCTION(classname,cppclass) \
BOOL push_##classname(cppclass &quat); \
SquirrelObject new_##classname(cppclass &quat);
#define _IMPL_NATIVE_CONSTRUCTION(classname,cppclass) \
static int classname##_release_hook(SQUserPointer p, int size) \
{ \
if(p) { \
cppclass *pv = (cppclass *)p; \
delete pv; \
} \
return 0; \
} \
BOOL push_##classname(cppclass &quat) \
{ \
cppclass *newquat = new cppclass; \
*newquat = quat; \
if(!CreateNativeClassInstance(SquirrelVM::GetVMPtr(),_SC(#classname),newquat,classname##_release_hook)) { \
delete newquat; \
return FALSE; \
} \
return TRUE; \
} \
SquirrelObject new_##classname(cppclass &quat) \
{ \
SquirrelObject ret; \
if(push_##classname(quat)) { \
ret.AttachToStackObject(-1); \
sq_pop(SquirrelVM::GetVMPtr(),1); \
} \
return ret; \
} \
int construct_##classname(cppclass *p) \
{ \
sq_setinstanceup(SquirrelVM::GetVMPtr(),1,p); \
sq_setreleasehook(SquirrelVM::GetVMPtr(),1,classname##_release_hook); \
return 1; \
}
BOOL CreateStaticClass(HSQUIRRELVM v,SquirrelClassDecl *cd);
BOOL CreateStaticNamespace(HSQUIRRELVM v,ScriptNamespaceDecl *sn);
BOOL CreateClass(HSQUIRRELVM v,SquirrelClassDecl *cd);
BOOL InitScriptClasses(HSQUIRRELVM v);
BOOL CreateNativeClassInstance(HSQUIRRELVM v,const SQChar *classname,SQUserPointer ud,SQRELEASEHOOK hook);
BOOL CreateConstructNativeClassInstance(HSQUIRRELVM v,const SQChar * className);
#endif // SQUIRREL_BINDINGS_UTILS_H
|