1 // SqPlusConst.h
2 // SqPlus constant type and constant member function support created by Simon Michelmore.
3 // Modular integration 11/14/05 jcs.
4
5 #ifdef SQPLUS_DECLARE_INSTANCE_TYPE_CONST
6 #undef SQPLUS_DECLARE_INSTANCE_TYPE_CONST
7
8 // Kamaitati's NULL_INSTANCE support. 5/28/06 jcs
9
10 #ifdef SQPLUS_SUPPORT_NULL_INSTANCES
11
12 #define DECLARE_INSTANCE_TYPE_NAME_CONST(TYPE,NAME) \
13 DECLARE_INSTANCE_TYPE_NAME_(TYPE,NAME) \
14 namespace SqPlus { \
15 inline void Push(HSQUIRRELVM v,const TYPE * value) { \
16 if (!value) sq_pushnull(v); \
17 else if (!CreateNativeClassInstance(v,GetTypeName(*value),(TYPE*)value,0)) \
18 throw SquirrelError(sqT("Push(): could not create INSTANCE (check registration name)")); } \
19 inline void Push(HSQUIRRELVM v,const TYPE & value) { if (!CreateCopyInstance(GetTypeName(value),value)) throw SquirrelError(sqT("Push(): could not create INSTANCE copy (check registration name)")); } \
20 inline bool Match(TypeWrapper<const TYPE &>,HSQUIRRELVM v,int idx) { return GetInstance<TYPE,false>(v,idx) != NULL; } \
21 inline const TYPE & Get(TypeWrapper<const TYPE &>,HSQUIRRELVM v,int idx) { return *GetInstance<TYPE,true>(v,idx); } \
22 } // nameSpace SqPlus
23
24 #else
25
26 #define DECLARE_INSTANCE_TYPE_NAME_CONST(TYPE,NAME) \
27 DECLARE_INSTANCE_TYPE_NAME_(TYPE,NAME) \
28 namespace SqPlus { \
29 inline void Push(HSQUIRRELVM v,const TYPE * value) { if (!CreateNativeClassInstance(v,GetTypeName(*value),(TYPE*)value,0)) throw SquirrelError(sqT("Push(): could not create INSTANCE (check registration name)")); } \
30 inline void Push(HSQUIRRELVM /*v*/,const TYPE & value) { if (!CreateCopyInstance(GetTypeName(value),value)) throw SquirrelError(sqT("Push(): could not create INSTANCE copy (check registration name)")); } \
31 inline bool Match(TypeWrapper<const TYPE &>,HSQUIRRELVM v,int idx) { return GetInstance<TYPE,false>(v,idx) != NULL; } \
32 inline const TYPE & Get(TypeWrapper<const TYPE &>,HSQUIRRELVM v,int idx) { return *GetInstance<TYPE,true>(v,idx); } \
33 } // nameSpace SqPlus
34
35 #endif
36
37 #define DECLARE_INSTANCE_TYPE(TYPE) DECLARE_INSTANCE_TYPE_NAME_CONST(TYPE,TYPE)
38 #define DECLARE_INSTANCE_TYPE_NAME(TYPE,NAME) DECLARE_INSTANCE_TYPE_NAME_CONST(TYPE,NAME)
39 #endif
40
41 #ifdef SQPLUS_CALL_CONST_MFUNC_RET0
42 #undef SQPLUS_CALL_CONST_MFUNC_RET0
43 template <typename Callee>
Call(Callee & callee,RT (Callee::* func)()const,HSQUIRRELVM v,int)44 static int Call(Callee & callee,RT (Callee::*func)() const,HSQUIRRELVM v,int /*index*/) {
45 RT ret = (callee.*func)();
46 Push(v,ret);
47 return 1;
48 }
49
50 // C::B patch: so it builds on 64bit
51 #ifdef _WIN64
52 template <typename Callee>
Call(Callee & callee,RT (Callee::* func)()const,HSQUIRRELVM v,SQInteger)53 static int Call(Callee & callee,RT (Callee::*func)() const,HSQUIRRELVM v,SQInteger /*index*/) {
54 RT ret = (callee.*func)();
55 Push(v,ret);
56 return 1;
57 }
58 #endif
59
60 template <typename Callee,typename P1>
Call(Callee & callee,RT (Callee::* func)(P1)const,HSQUIRRELVM v,int index)61 static int Call(Callee & callee,RT (Callee::*func)(P1) const,HSQUIRRELVM v,int index) {
62 sq_argassert(1,index + 0);
63 RT ret = (callee.*func)(
64 Get(TypeWrapper<P1>(),v,index + 0)
65 );
66 Push(v,ret);
67 return 1;
68 }
69
70 template<typename Callee,typename P1,typename P2>
Call(Callee & callee,RT (Callee::* func)(P1,P2)const,HSQUIRRELVM v,int index)71 static int Call(Callee & callee,RT (Callee::*func)(P1,P2) const,HSQUIRRELVM v,int index) {
72 sq_argassert(1,index + 0);
73 sq_argassert(2,index + 1);
74 RT ret = (callee.*func)(
75 Get(TypeWrapper<P1>(),v,index + 0),
76 Get(TypeWrapper<P2>(),v,index + 1)
77 );
78 Push(v,ret);
79 return 1;
80 }
81
82 template<typename Callee,typename P1,typename P2,typename P3>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3)const,HSQUIRRELVM v,int index)83 static int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3) const,HSQUIRRELVM v,int index) {
84 sq_argassert(1,index + 0);
85 sq_argassert(2,index + 1);
86 sq_argassert(3,index + 2);
87 RT ret = (callee.*func)(
88 Get(TypeWrapper<P1>(),v,index + 0),
89 Get(TypeWrapper<P2>(),v,index + 1),
90 Get(TypeWrapper<P3>(),v,index + 2)
91 );
92 Push(v,ret);
93 return 1;
94 }
95
96 template<typename Callee,typename P1,typename P2,typename P3,typename P4>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3,P4)const,HSQUIRRELVM v,int index)97 static int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4) const,HSQUIRRELVM v,int index) {
98 sq_argassert(1,index + 0);
99 sq_argassert(2,index + 1);
100 sq_argassert(3,index + 2);
101 sq_argassert(4,index + 3);
102 RT ret = (callee.*func)(
103 Get(TypeWrapper<P1>(),v,index + 0),
104 Get(TypeWrapper<P2>(),v,index + 1),
105 Get(TypeWrapper<P3>(),v,index + 2),
106 Get(TypeWrapper<P4>(),v,index + 3)
107 );
108 Push(v,ret);
109 return 1;
110 }
111
112 template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3,P4,P5)const,HSQUIRRELVM v,int index)113 static int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5) const,HSQUIRRELVM v,int index) {
114 sq_argassert(1,index + 0);
115 sq_argassert(2,index + 1);
116 sq_argassert(3,index + 2);
117 sq_argassert(4,index + 3);
118 sq_argassert(5,index + 4);
119 RT ret = (callee.*func)(
120 Get(TypeWrapper<P1>(),v,index + 0),
121 Get(TypeWrapper<P2>(),v,index + 1),
122 Get(TypeWrapper<P3>(),v,index + 2),
123 Get(TypeWrapper<P4>(),v,index + 3),
124 Get(TypeWrapper<P5>(),v,index + 4)
125 );
126 Push(v,ret);
127 return 1;
128 }
129
130 template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3,P4,P5,P6)const,HSQUIRRELVM v,int index)131 static int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5,P6) const,HSQUIRRELVM v,int index) {
132 sq_argassert(1,index + 0);
133 sq_argassert(2,index + 1);
134 sq_argassert(3,index + 2);
135 sq_argassert(4,index + 3);
136 sq_argassert(5,index + 4);
137 sq_argassert(6,index + 5);
138 RT ret = (callee.*func)(
139 Get(TypeWrapper<P1>(),v,index + 0),
140 Get(TypeWrapper<P2>(),v,index + 1),
141 Get(TypeWrapper<P3>(),v,index + 2),
142 Get(TypeWrapper<P4>(),v,index + 3),
143 Get(TypeWrapper<P5>(),v,index + 4),
144 Get(TypeWrapper<P6>(),v,index + 5)
145 );
146 Push(v,ret);
147 return 1;
148 }
149
150 template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3,P4,P5,P6,P7)const,HSQUIRRELVM v,int index)151 static int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5,P6,P7) const,HSQUIRRELVM v,int index) {
152 sq_argassert(1,index + 0);
153 sq_argassert(2,index + 1);
154 sq_argassert(3,index + 2);
155 sq_argassert(4,index + 3);
156 sq_argassert(5,index + 4);
157 sq_argassert(6,index + 5);
158 sq_argassert(7,index + 6);
159 RT ret = (callee.*func)(
160 Get(TypeWrapper<P1>(),v,index + 0),
161 Get(TypeWrapper<P2>(),v,index + 1),
162 Get(TypeWrapper<P3>(),v,index + 2),
163 Get(TypeWrapper<P4>(),v,index + 3),
164 Get(TypeWrapper<P5>(),v,index + 4),
165 Get(TypeWrapper<P6>(),v,index + 5),
166 Get(TypeWrapper<P7>(),v,index + 6)
167 );
168 Push(v,ret);
169 return 1;
170 }
171 #endif
172
173 #ifdef SQPLUS_CALL_CONST_MFUNC_NORET
174 #undef SQPLUS_CALL_CONST_MFUNC_NORET
175 template<typename Callee>
Call(Callee & callee,void (Callee::* func)()const,HSQUIRRELVM,int)176 static int Call(Callee & callee,void (Callee::*func)() const,HSQUIRRELVM,int /*index*/) {
177 (callee.*func)();
178 return 0;
179 }
180
181 template<typename Callee,typename P1>
Call(Callee & callee,void (Callee::* func)(P1)const,HSQUIRRELVM v,int index)182 static int Call(Callee & callee,void (Callee::*func)(P1) const,HSQUIRRELVM v,int index) {
183 sq_argassert(1,index + 0);
184 (callee.*func)(
185 Get(TypeWrapper<P1>(),v,index + 0)
186 );
187 return 0;
188 }
189
190 template<typename Callee,typename P1,typename P2>
Call(Callee & callee,void (Callee::* func)(P1,P2)const,HSQUIRRELVM v,int index)191 static int Call(Callee & callee,void (Callee::*func)(P1,P2) const,HSQUIRRELVM v,int index) {
192 sq_argassert(1,index + 0);
193 sq_argassert(2,index + 1);
194 (callee.*func)(
195 Get(TypeWrapper<P1>(),v,index + 0),
196 Get(TypeWrapper<P2>(),v,index + 1)
197 );
198 return 0;
199 }
200
201 template<typename Callee,typename P1,typename P2,typename P3>
Call(Callee & callee,void (Callee::* func)(P1,P2,P3)const,HSQUIRRELVM v,int index)202 static int Call(Callee & callee,void (Callee::*func)(P1,P2,P3) const,HSQUIRRELVM v,int index) {
203 sq_argassert(1,index + 0);
204 sq_argassert(2,index + 1);
205 sq_argassert(3,index + 2);
206 (callee.*func)(
207 Get(TypeWrapper<P1>(),v,index + 0),
208 Get(TypeWrapper<P2>(),v,index + 1),
209 Get(TypeWrapper<P3>(),v,index + 2)
210 );
211 return 0;
212 }
213
214 template<typename Callee,typename P1,typename P2,typename P3,typename P4>
Call(Callee & callee,void (Callee::* func)(P1,P2,P3,P4)const,HSQUIRRELVM v,int index)215 static int Call(Callee & callee,void (Callee::*func)(P1,P2,P3,P4) const,HSQUIRRELVM v,int index) {
216 sq_argassert(1,index + 0);
217 sq_argassert(2,index + 1);
218 sq_argassert(3,index + 2);
219 sq_argassert(4,index + 3);
220 (callee.*func)(
221 Get(TypeWrapper<P1>(),v,index + 0),
222 Get(TypeWrapper<P2>(),v,index + 1),
223 Get(TypeWrapper<P3>(),v,index + 2),
224 Get(TypeWrapper<P4>(),v,index + 3)
225 );
226 return 0;
227 }
228
229 template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5>
Call(Callee & callee,void (Callee::* func)(P1,P2,P3,P4,P5)const,HSQUIRRELVM v,int index)230 static int Call(Callee & callee,void (Callee::*func)(P1,P2,P3,P4,P5) const,HSQUIRRELVM v,int index) {
231 sq_argassert(1,index + 0);
232 sq_argassert(2,index + 1);
233 sq_argassert(3,index + 2);
234 sq_argassert(4,index + 3);
235 sq_argassert(5,index + 4);
236 (callee.*func)(
237 Get(TypeWrapper<P1>(),v,index + 0),
238 Get(TypeWrapper<P2>(),v,index + 1),
239 Get(TypeWrapper<P3>(),v,index + 2),
240 Get(TypeWrapper<P4>(),v,index + 3),
241 Get(TypeWrapper<P5>(),v,index + 4)
242 );
243 return 0;
244 }
245
246 template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
Call(Callee & callee,void (Callee::* func)(P1,P2,P3,P4,P5,P6)const,HSQUIRRELVM v,int index)247 static int Call(Callee & callee,void (Callee::*func)(P1,P2,P3,P4,P5,P6) const,HSQUIRRELVM v,int index) {
248 sq_argassert(1,index + 0);
249 sq_argassert(2,index + 1);
250 sq_argassert(3,index + 2);
251 sq_argassert(4,index + 3);
252 sq_argassert(5,index + 4);
253 sq_argassert(6,index + 5);
254 (callee.*func)(
255 Get(TypeWrapper<P1>(),v,index + 0),
256 Get(TypeWrapper<P2>(),v,index + 1),
257 Get(TypeWrapper<P3>(),v,index + 2),
258 Get(TypeWrapper<P4>(),v,index + 3),
259 Get(TypeWrapper<P5>(),v,index + 4),
260 Get(TypeWrapper<P6>(),v,index + 5)
261 );
262 return 0;
263 }
264
265 template<typename Callee,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
Call(Callee & callee,void (Callee::* func)(P1,P2,P3,P4,P5,P6,P7)const,HSQUIRRELVM v,int index)266 static int Call(Callee & callee,void (Callee::*func)(P1,P2,P3,P4,P5,P6,P7) const,HSQUIRRELVM v,int index) {
267 sq_argassert(1,index + 0);
268 sq_argassert(2,index + 1);
269 sq_argassert(3,index + 2);
270 sq_argassert(4,index + 3);
271 sq_argassert(5,index + 4);
272 sq_argassert(6,index + 5);
273 sq_argassert(7,index + 6);
274 (callee.*func)(
275 Get(TypeWrapper<P1>(),v,index + 0),
276 Get(TypeWrapper<P2>(),v,index + 1),
277 Get(TypeWrapper<P3>(),v,index + 2),
278 Get(TypeWrapper<P4>(),v,index + 3),
279 Get(TypeWrapper<P5>(),v,index + 4),
280 Get(TypeWrapper<P6>(),v,index + 5),
281 Get(TypeWrapper<P7>(),v,index + 6)
282 );
283 return 0;
284 }
285 #endif
286
287 #ifdef SQPLUS_CALL_CONST_MFUNC_RET1
288 #undef SQ_REG_CONST_STATIC_VAR
289 template<typename Callee,typename RT>
Call(Callee & callee,RT (Callee::* func)()const,HSQUIRRELVM v,int index)290 int Call(Callee & callee, RT (Callee::*func)() const,HSQUIRRELVM v,int index) {
291 return ReturnSpecialization<RT>::Call(callee,func,v,index);
292 }
293
294 template<typename Callee,typename RT,typename P1>
Call(Callee & callee,RT (Callee::* func)(P1)const,HSQUIRRELVM v,int index)295 int Call(Callee & callee,RT (Callee::*func)(P1) const,HSQUIRRELVM v,int index) {
296 return ReturnSpecialization<RT>::Call(callee,func,v,index);
297 }
298
299 template<typename Callee,typename RT,typename P1,typename P2>
Call(Callee & callee,RT (Callee::* func)(P1,P2)const,HSQUIRRELVM v,int index)300 int Call(Callee & callee,RT (Callee::*func)(P1,P2) const,HSQUIRRELVM v,int index) {
301 return ReturnSpecialization<RT>::Call(callee,func,v,index);
302 }
303
304 template<typename Callee,typename RT,typename P1,typename P2,typename P3>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3)const,HSQUIRRELVM v,int index)305 int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3) const,HSQUIRRELVM v,int index) {
306 return ReturnSpecialization<RT>::Call(callee,func,v,index);
307 }
308
309 template<typename Callee,typename RT,typename P1,typename P2,typename P3,typename P4>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3,P4)const,HSQUIRRELVM v,int index)310 int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4) const,HSQUIRRELVM v,int index) {
311 return ReturnSpecialization<RT>::Call(callee,func,v,index);
312 }
313
314 template<typename Callee,typename RT,typename P1,typename P2,typename P3,typename P4,typename P5>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3,P4,P5)const,HSQUIRRELVM v,int index)315 int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5) const,HSQUIRRELVM v,int index) {
316 return ReturnSpecialization<RT>::Call(callee,func,v,index);
317 }
318
319 template<typename Callee,typename RT,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3,P4,P5,P6)const,HSQUIRRELVM v,int index)320 int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5,P6) const,HSQUIRRELVM v,int index) {
321 return ReturnSpecialization<RT>::Call(callee,func,v,index);
322 }
323
324 template<typename Callee,typename RT,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
Call(Callee & callee,RT (Callee::* func)(P1,P2,P3,P4,P5,P6,P7)const,HSQUIRRELVM v,int index)325 int Call(Callee & callee,RT (Callee::*func)(P1,P2,P3,P4,P5,P6,P7) const,HSQUIRRELVM v,int index) {
326 return ReturnSpecialization<RT>::Call(callee,func,v,index);
327 }
328 #undef SQPLUS_CALL_CONST_MFUNC_RET1
329 #endif
330
331 #ifdef SQ_REG_CONST_STATIC_VAR
332 template<typename VarType>
333 SQClassDef & staticVar(const VarType * pvar,const SQChar * name_,VarAccessType access=VAR_ACCESS_READ_ONLY) {
334 struct CV {
335 const VarType * var;
336 } cv; // Cast Variable helper.
337 cv.var = pvar;
338 RegisterInstanceVariable(newClass,ClassType<TClassType>::type(),*(VarType **)&cv,name_,VarAccessType(access|VAR_ACCESS_STATIC));
339 return *this;
340 } // staticVar
341 #endif
342
343 // SqPlusConst.h
344