summaryrefslogtreecommitdiffstats
path: root/squirrel_3_0_1_stable/_OLD_sqplus/SqPlusOverload.h
blob: 9668469ad3f7556ad23920c394895ed09b1bb0b7 (plain) (blame)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
// SqPlusOverload.h
// SqPlus function overloading support created by Katsuaki Kawachi.
//
// Const member function fixed Tegan
//   from http://squirrel-lang.org/forums/thread/2160.aspx

#ifdef SQPLUS_OVERLOAD_RELEASE_HOOK
#undef SQPLUS_OVERLOAD_RELEASE_HOOK

    // These end up int ClassType<T> now
    static inline int destruct(SQUserPointer up, SQInteger size) {
        if (up) {
            static_cast<T*>(up)->~T();
            sq_free(up, size);
        }
        return 0;
    }

    static inline SQRELEASEHOOK &release(void) {
        static SQRELEASEHOOK hook = ClassType<T>::destruct;
        return hook;
    }

    void releaseHook(SQRELEASEHOOK releaseHook) {
        release() = releaseHook;
        //return *this;
    }
    
    static inline SQRELEASEHOOK &getReleaseHook(void) {
        return release();
    }

#endif // SQPLUS_OVERLOAD_RELEASE_HOOK


#ifdef SQPLUS_OVERLOAD_DECLARATION
#undef SQPLUS_OVERLOAD_DECLARATION

template<typename Func> struct Arg;

#endif // SQPLUS_OVERLOAD_DECLARATION


#ifdef SQPLUS_OVERLOAD_IMPLEMENTATION
#undef SQPLUS_OVERLOAD_IMPLEMENTATION
private:
    class SQFuncHolder {
    private:
        template<typename T>
        class FlexArray {
        protected:
            SquirrelObject array;
        public:
            FlexArray(int size = 0) {
                this->resize(size);
            }
            int size(void) const {
                return array.Len();
            }
            void resize(int newSize) {
                if (this->size() == 0) {
                    array = SquirrelVM::CreateArray(newSize);
                } else {
                    array.ArrayResize(newSize);
                }
            }
            void push_back(const T &t) {
                this->set(this->size(), t);
            }
            void set(int index, const T &t) {
                get(index) = t;
            }
            T &get(int index) {
                if (index >= array.Len()) {
                    resize(index + 1);
                }
                SQUserPointer up = array.GetUserPointer(index);
                if (!up) {
                    up = sq_newuserdata(SquirrelVM::GetVMPtr(), sizeof(T));
                    new(static_cast<T*>(up)) T;
                    array.SetUserPointer(index, up);
                }
                return *static_cast<T*>(up);
            }
        };
        /*
          storage of wrapped C++ functions
         */
        typedef SQInteger(*WrappedFunction)(HSQUIRRELVM, bool, int);
        typedef FlexArray<WrappedFunction> SQWrappedFuncArray;
        typedef FlexArray<SQWrappedFuncArray> SQWrappedFuncArray2 ;
        typedef FlexArray<SQWrappedFuncArray2> SQWrappedFuncArray3 ;

        struct MemberHolder {
            static SQWrappedFuncArray &funcs(int functionIndex,
                                             int paramCount) {
                static SQWrappedFuncArray3 funcs;
                return funcs.get(paramCount).get(functionIndex);
            }
        };
        struct StaticHolder {
            static SQWrappedFuncArray &funcs(int functionIndex,
                                             int paramCount) {
                static SQWrappedFuncArray3 funcs;
                return funcs.get(paramCount).get(functionIndex);
            }
        };
        struct ConstructorHolder {
            static SQWrappedFuncArray &funcs(int paramCount) {
                static SQWrappedFuncArray2 funcs;
                return funcs.get(paramCount);
            }
        };
            
        /*
          wrapper for C++ functions
         */
        template<typename Mfunc> struct MemberDispatcher {
            static inline FlexArray<Mfunc> &mfunc(void) {
                static FlexArray<Mfunc> mfunc;
                return mfunc;
            }
            static inline SQInteger
            dispatch(HSQUIRRELVM v, bool execute, int functionIndex) {
                return execute ?
                    Call(*GetInstance<TClassType, true>(v, 1),
                         mfunc().get(functionIndex), v, 2) :
                    Arg<Mfunc>::argTypeDistance(v);
            }
        };
        template<typename Sfunc> struct StaticDispatcher {
            static inline FlexArray<Sfunc> &sfunc(void) {
                static FlexArray<Sfunc> sfunc;
                return sfunc;
            }
            static inline SQInteger
            dispatch(HSQUIRRELVM v, bool execute, int functionIndex) {
                return execute ?
                    Call(sfunc().get(functionIndex), v, 2) :
                    Arg<Sfunc>::argTypeDistance(v);
            }
        };
        template<typename Cfunc> struct Constructor {
            static inline Cfunc &cfunc(void) {
                static Cfunc cfunc = 0;
                return cfunc;
            }
            static inline SQInteger
            construct(HSQUIRRELVM v, bool execute, int) {
                return execute ?
                    Call(cfunc(), v, 2) :
                    Arg<Cfunc>::argTypeDistance(v);
            }
        };

        // search and call an overloaded function on runtime
        static inline SQInteger
        call(SQWrappedFuncArray &funcs, HSQUIRRELVM v, int functionIndex = 0) {
            bool ambiguous = false;
            int imin = -1;
            int dmin = INT_MAX;
            for (int i = 0, size = funcs.size(); i < size; ++i) {
                // FIXME: to be refactored
                const int d = (**funcs.get(i))(v, false, functionIndex);
                if (d == 0) { // complete match
                    imin = i;
                    ambiguous = false;
                    goto SQPLUS_OVERLOAD_CALL_IMMEDIATE_EXECUTION;
                } else if (0 < d && d < dmin) {
                    dmin = d;
                    imin = i;
                    ambiguous = false;
                } else if (d == dmin) {
                    ambiguous = true;
                }
            }

            if (ambiguous) {
                return sq_throwerror(
                    v, _SC("Call of overloaded function is ambiguous")
                    );
            } else if (imin == -1) {
                return sq_throwerror(
                    v, _SC("No match for given arguments")
                    );
            }

          SQPLUS_OVERLOAD_CALL_IMMEDIATE_EXECUTION:
            // FIXME: to be refactored
            return (**funcs.get(imin))(v, true, functionIndex);
        }
        
    public:
        template<typename Mfunc> static inline void
        addMemberFunc(int functionIndex, Mfunc mfunc) {
            MemberHolder::funcs(functionIndex, Arg<Mfunc>::num()).push_back(
                &MemberDispatcher<Mfunc>::dispatch
                );
            MemberDispatcher<Mfunc>::mfunc().set(functionIndex, mfunc);
        }
        template<typename Sfunc> static inline void
        addStaticFunc(int functionIndex, Sfunc sfunc) {
            StaticHolder::funcs(functionIndex, Arg<Sfunc>::num()).push_back(
                &StaticDispatcher<Sfunc>::dispatch
                );
            StaticDispatcher<Sfunc>::sfunc().set(functionIndex, sfunc);
        }
        template<typename Cfunc> static inline void
        addConstructor(Cfunc cfunc) {
            ConstructorHolder::funcs(Arg<Cfunc>::num()).push_back(
                &Constructor<Cfunc>::construct
                );
            Constructor<Cfunc>::cfunc() = cfunc;
        }
            
        static inline SQInteger
        memberCall(int paramCount, HSQUIRRELVM v, int functionIndex) {
            return call(MemberHolder::funcs(functionIndex, paramCount),
                        v, functionIndex);
        }
        static inline SQInteger
        staticCall(int paramCount, HSQUIRRELVM v, int functionIndex) {
            return call(StaticHolder::funcs(functionIndex, paramCount),
                        v, functionIndex);
        }
        static inline SQInteger
        constructorCall(int paramCount, HSQUIRRELVM v, int) {
            return call(ConstructorHolder::funcs(paramCount), v);
        }
    }; // class SQFuncHolder


    struct FunctionNameEnumerator {
        SquirrelObject names;
        FunctionNameEnumerator(void) : names(SquirrelVM::CreateTable()) {}
        int index(const SQChar *n) {
            int i;
            SquirrelObject v = names.GetValue(n);
            if (v.IsNull()) {
                i = names.Len();
                names.SetValue(n, i);
            } else {
                i = v.ToInteger();
            }
            return i;
        }
    };

    FunctionNameEnumerator overloadedMemberNames;
    FunctionNameEnumerator overloadedStaticMemberNames;

    static SquirrelObject &
    functionIndexHolders(HSQUIRRELVM v) {
        static SquirrelObject indexHolders;
        if (indexHolders.IsNull()) {
            sq_newtable(v);
            indexHolders.AttachToStackObject(-1);
            sq_pop(v, 1);
        }    
        return indexHolders;
    }

    template<int N>
    static SquirrelObject &
    indexHolder(HSQUIRRELVM v) {
        static SquirrelObject holder;
        if (holder.IsNull()) {
            sq_pushobject(v, functionIndexHolders(v).GetObjectHandle());
            sq_pushinteger(v, N);
            if (SQ_OK == sq_rawget(v, -2)) {
                holder.AttachToStackObject(-1);
                sq_pop(v, 3);
            } else {
                sq_pushinteger(v, N);
                sq_newtable(v);
                holder.AttachToStackObject(-1);
                sq_rawset(v, -3);
                sq_pop(v, 1);
            }
        }
        return holder;
    }

    template<typename Func>
    class SQOverloader {
    private:
        static inline SQInteger switcher(HSQUIRRELVM v,
                                         int(*caller)(int, HSQUIRRELVM, int),
                                         int fidx) {
            return (*caller)(StackHandler(v).GetParamCount() - 1,
                             v,
                             fidx);
        }
        
        static inline SQInteger memberSwitcher(HSQUIRRELVM v) {
            SQInteger fidx;
            sq_pushobject(v, indexHolder<0>(v).GetObjectHandle());
            sq_push(v, 0);      // native closure
            sq_rawget(v, -2);
            sq_getinteger(v, -1, &fidx);
            sq_pop(v, 2);
            return switcher(v, SQFuncHolder::memberCall, fidx);
        }
        static inline SQInteger staticSwitcher(HSQUIRRELVM v) {
            SQInteger fidx;
            sq_pushobject(v, indexHolder<1>(v).GetObjectHandle());
            sq_push(v, 0);      // native closure
            sq_rawget(v, -2);
            sq_getinteger(v, -1, &fidx);
            sq_pop(v, 2);
            return switcher(v, SQFuncHolder::staticCall, fidx);
        }
        static inline SQInteger constructorSwitcher(HSQUIRRELVM v) {
            return switcher(v, SQFuncHolder::constructorCall, 0);
        }
        
    public:
        static inline void addMemberFunc(SQClassDefBase<TClassType,TClassBase> *def,
                                         Func mfunc,
                                         const SQChar *name) {
            const int fidx = def->overloadedMemberNames.index(name);
            SQFuncHolder::addMemberFunc(fidx, mfunc);
            def->staticFuncVarArgs(memberSwitcher, name);

            HSQUIRRELVM v = def->v;
            // get closure
            sq_pushobject(v, def->newClass.GetObjectHandle());
            sq_pushstring(v, name, -1);
            sq_rawget(v, -2);
            // holder[closure] = fidx
            sq_pushobject(v, indexHolder<0>(v).GetObjectHandle());
            sq_push(v, -2);
            sq_pushinteger(v, fidx);
            sq_rawset(v, -3);
            //
            sq_pop(v, 3);
        }

        static inline void addOperatorFunc(SQClassDefBase<TClassType,TClassBase> *def,
                                           Func ofunc,
                                           const SQChar *name) {
            if (Arg<Func>::num() != 1) {
                //assert(false &&
                //       "Cannot add this function as operator (argc != 1)");
				abort();
			}
            SQChar proxy[256];
            scsprintf(proxy, _SC("overloaded%s"), name);
            
            addMemberFunc(def, ofunc, proxy);

            SQChar script[512];
            scsprintf(script, _SC("%s.%s<-function(o){return %s.%s(o);}"),
                      def->name, name, def->name, proxy);
            SquirrelVM::RunScript(SquirrelVM::CompileBuffer(script));
        }
            
        static inline void addStaticFunc(SQClassDefBase<TClassType,TClassBase> *def,
                                         Func sfunc,
                                         const SQChar *name) {
            const int fidx = def->overloadedStaticMemberNames.index(name);
            SQFuncHolder::addStaticFunc(fidx, sfunc);
            def->staticFuncVarArgs(staticSwitcher, name);

            HSQUIRRELVM v = def->v;
            // get closure
            sq_pushobject(v, def->newClass.GetObjectHandle());
            sq_pushstring(v, name, -1);
            sq_rawget(v, -2);

            // holder[closure] = fidx
            sq_pushobject(v, indexHolder<1>(v).GetObjectHandle());
            sq_push(v, -2);
            sq_pushinteger(v, fidx);
            sq_rawset(v, -3);
            //
            sq_pop(v, 3);
        }
        template<typename Cfunc>
        static inline void addConstructor(SQClassDefBase<TClassType,TClassBase> *def,
                                          Cfunc cfunc) {
            SQFuncHolder::addConstructor(cfunc);
            def->staticFuncVarArgs(constructorSwitcher, _SC("constructor"));
        }
        static inline void addGlobalFunc(SQClassDefBase<TClassType,TClassBase> *def,
                                         Func gfunc,
                                         const SQChar *name) {
            const int fidx = def->overloadedStaticMemberNames.index(name);
            SQFuncHolder::addStaticFunc(fidx, gfunc);
            SquirrelVM::CreateFunctionGlobal(staticSwitcher, name, _SC("*"));

            HSQUIRRELVM v = def->v;
            // get closure
            sq_pushroottable(v);
            sq_pushstring(v, name, -1);
            sq_rawget(v, -2);

            // holder[closure] = fidx
            sq_pushobject(v, indexHolder<1>(v).GetObjectHandle());
            sq_push(v, -2);
            sq_pushinteger(v, fidx);
            sq_rawset(v, -3);
            //
            sq_pop(v, 3);
        }
    };

public:
    template<typename Mfunc>
    SQClassDefBase<TClassType,TClassBase> &overloadFunc(Mfunc mfunc, const SQChar *n) {
        SQOverloader<Mfunc>::addMemberFunc(this, mfunc, n);
        return *this;
    }
    template<typename Ofunc>
    SQClassDefBase<TClassType,TClassBase> &overloadOperator(Ofunc ofunc, const SQChar *n){
        SQOverloader<Ofunc>::addOperatorFunc(this, ofunc, n);
        return *this;
    }
    template<typename Sfunc>
    SQClassDefBase<TClassType,TClassBase> &overloadStaticFunc(Sfunc sfunc,
                                                   const SQChar *n) {
        SQOverloader<Sfunc>::addStaticFunc(this, sfunc, n);
        return *this;
    }
    template<typename Cmetafunc>
    SQClassDefBase<TClassType,TClassBase> &overloadConstructor(void) {
        SQOverloader<Cmetafunc>::addConstructor(this, &Arg<Cmetafunc>::create);
        return *this;
    }
    template<typename Cfunc>
    SQClassDefBase<TClassType,TClassBase> &overloadConstructor(Cfunc cfunc) {
        SQOverloader<Cfunc>::addConstructor(this, cfunc);
        return *this;
    }
    template<typename Gfunc>
    SQClassDefBase<TClassType,TClassBase> &overloadGlobalFunc(Gfunc gfunc,
                                                   const SQChar *n) {
        SQOverloader<Gfunc>::addGlobalFunc(this, gfunc, n);
        return *this;
    }


#endif // SQPLUS_OVERLOAD_IMPLEMENTATION



#ifdef SQPLUS_OVERLOAD_FUNCTIONS
#undef SQPLUS_OVERLOAD_FUNCTIONS

struct GlobalFuncOverloader {};
static inline SQClassDefBase<GlobalFuncOverloader,SQNoBaseClass> &globalFuncOverloader(void)
{
    static SQClassDefBase<GlobalFuncOverloader,SQNoBaseClass> fo(_SC("GlobalFuncOverloader"));
    return fo;
}

template<typename Gfunc> void
OverloadGlobal(Gfunc gfunc, const SQChar *n)
{
    globalFuncOverloader().overloadGlobalFunc(gfunc, n);
}

template<typename TYPE> struct CheckInstance {
    template<typename T> struct unref {typedef T type;};
    template<typename T> struct unref<T&> {typedef T type;};
    template<typename T> struct unref<T*> {typedef T type;};
    template<typename T> struct unref<const T&> {typedef T type;};
    template<typename T> struct unref<const T*> {typedef T type;};
    
    /*
      d = -1 : not in hierarchy
      d = 0  : same
      d > 0  : similar (o is d-th subclass of TYPE)
    */
    static inline int distance(HSQUIRRELVM v, int index) {
        HSQOBJECT o;
        sq_resetobject(&o);
        sq_getstackobj(v, index, &o);
    
        const int top = sq_gettop(v);
        sq_pushroottable(v);

        // Check plain object type
        int d = -1;
        if (Match(TypeWrapper<TYPE>(), v, index)) {
            d = 0;
            
            // Check instance type hierarchy
            if (sq_type(o) == OT_INSTANCE) {
                SQUserPointer dsttype =
                    ClassType<typename unref<TYPE>::type>::type();
            
                SQUserPointer argtype;
                for (sq_getclass(v, index);
                     sq_gettypetag(v, -1, &argtype) == SQ_OK;
                     sq_getbase(v, -1)) {
                    if (argtype == dsttype) {
                        goto SQPLUS_OVERLOAD_DISTANCE_IMMEDIATE_RETURN;
                    }
                    ++d;
                }
                d = -1;         // no matching type found
            }
        }
      SQPLUS_OVERLOAD_DISTANCE_IMMEDIATE_RETURN:
        sq_settop(v, top);
        return d;
    }
};


template<typename T, typename R>
struct Arg<R(T::*)(void)> {
    static inline int num(void) {return 0;}
    static inline int argTypeDistance(HSQUIRRELVM) {
        return 0;
    }
};

template<typename T, typename R, typename A1>
struct Arg<R(T::*)(A1)> {
    static inline int num(void) {return 1;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        return Arg<R(*)(A1)>::argTypeDistance(v);
    }
};

template<typename T, typename R, typename A1, typename A2>
struct Arg<R(T::*)(A1, A2)> {
    static inline int num(void) {return 2;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        return Arg<R(*)(A1, A2)>::argTypeDistance(v);
    }
};

template<typename T, typename R, typename A1, typename A2, typename A3>
struct Arg<R(T::*)(A1, A2, A3)> {
    static inline int num(void) {return 3;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        return Arg<R(*)(A1, A2, A3)>::argTypeDistance(v);
    }
};

template<typename T, typename R, typename A1, typename A2, typename A3, typename A4>
struct Arg<R(T::*)(A1, A2, A3, A4)> {
    static inline int num(void) {return 4;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        return Arg<R(*)(A1, A2, A3, A4)>::argTypeDistance(v);
    }
};

template<typename T, typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
struct Arg<R(T::*)(A1, A2, A3, A4, A5)> {
    static inline int num(void) {return 5;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        return Arg<R(*)(A1, A2, A3, A4, A5)>::argTypeDistance(v);
    }
};

template<typename T, typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
struct Arg<R(T::*)(A1, A2, A3, A4, A5, A6)> {
    static inline int num(void) {return 6;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        return Arg<R(*)(A1, A2, A3, A4, A5, A6)>::argTypeDistance(v);
    }
};

template<typename T, typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
struct Arg<R(T::*)(A1, A2, A3, A4, A5, A6, A7)> {
    static inline int num(void) {return 7;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        return Arg<R(*)(A1, A2, A3, A4, A5, A6, A7)>::argTypeDistance(v);
    }
};

#ifdef SQPLUS_CONST_OPT
template<typename T, typename R>
struct Arg<R(T::*)(void) const> {
	static inline int num(void) {return 0;}
	static inline int argTypeDistance(HSQUIRRELVM) {
		return 0;
	}
};

template<typename T, typename R, typename A1>
struct Arg<R(T::*)(A1) const> {
	static inline int num(void) {return 1;}
	static inline int argTypeDistance(HSQUIRRELVM v) {
		return Arg<R(*)(A1)>::argTypeDistance(v);
	}
};

template<typename T, typename R, typename A1, typename A2>
struct Arg<R(T::*)(A1, A2) const> {
	static inline int num(void) {return 2;}
	static inline int argTypeDistance(HSQUIRRELVM v) {
		return Arg<R(*)(A1, A2)>::argTypeDistance(v);
	}
};

template<typename T, typename R, typename A1, typename A2, typename A3>
struct Arg<R(T::*)(A1, A2, A3) const> {
	static inline int num(void) {return 3;}
	static inline int argTypeDistance(HSQUIRRELVM v) {
		return Arg<R(*)(A1, A2, A3)>::argTypeDistance(v);
	}
};

template<typename T, typename R, typename A1, typename A2, typename A3, typename A4>
struct Arg<R(T::*)(A1, A2, A3, A4) const> {
	static inline int num(void) {return 4;}
	static inline int argTypeDistance(HSQUIRRELVM v) {
		return Arg<R(*)(A1, A2, A3, A4)>::argTypeDistance(v);
	}
};

template<typename T, typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
struct Arg<R(T::*)(A1, A2, A3, A4, A5) const> {
	static inline int num(void) {return 5;}
	static inline int argTypeDistance(HSQUIRRELVM v) {
		return Arg<R(*)(A1, A2, A3, A4, A5)>::argTypeDistance(v);
	}
};

template<typename T, typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
struct Arg<R(T::*)(A1, A2, A3, A4, A5, A6) const> {
	static inline int num(void) {return 6;}
	static inline int argTypeDistance(HSQUIRRELVM v) {
		return Arg<R(*)(A1, A2, A3, A4, A5, A6)>::argTypeDistance(v);
	}
};

template<typename T, typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
struct Arg<R(T::*)(A1, A2, A3, A4, A5, A6, A7) const> {
	static inline int num(void) {return 7;}
	static inline int argTypeDistance(HSQUIRRELVM v) {
		return Arg<R(*)(A1, A2, A3, A4, A5, A6, A7)>::argTypeDistance(v);
	}
};
#endif

static inline int classAllocationError(HSQUIRRELVM v) {
    return sq_throwerror(v, _SC("Failed to allocate memory"));
}

template<typename R>
struct Arg<R(*)(void)> {
    static inline int num(void) {return 0;}
    static inline int argTypeDistance(HSQUIRRELVM) {return 0;}
    static inline int create(void) {
        HSQUIRRELVM v = SquirrelVM::GetVMPtr();
        R *r = static_cast<R*>(sq_malloc(sizeof(R)));
        return r ?
            PostConstruct<R>(v, new(r) R,
                             ClassType<R>::getReleaseHook()) :
            classAllocationError(v);
    }
};

template<typename R, typename A1>
struct Arg<R(*)(A1)> {
    static inline int num(void) {return 1;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        int s, r;
        r = 0;
        s = CheckInstance<A1>::distance(v, 2); if (s < 0) {return -1;} r += s;
        return r;
    }
    static inline int create(A1 a1) {
        HSQUIRRELVM v = SquirrelVM::GetVMPtr();
        R *r = static_cast<R*>(sq_malloc(sizeof(R)));
        return r ?
            PostConstruct<R>(v, new(r) R(a1),
                             ClassType<R>::getReleaseHook()) :
            classAllocationError(v);
    }
};

template<typename R, typename A1, typename A2>
struct Arg<R(*)(A1, A2)> {
    static inline int num(void) {return 2;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        int s, r;
        r = 0;
        s = CheckInstance<A1>::distance(v, 2); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A2>::distance(v, 3); if (s < 0) {return -1;} r += s;
        return r;
    }
    static inline int create(A1 a1, A2 a2) {
        HSQUIRRELVM v = SquirrelVM::GetVMPtr();
        R *r = static_cast<R*>(sq_malloc(sizeof(R)));
        return r ?
            PostConstruct<R>(v, new(r) R(a1, a2),
                             ClassType<R>::getReleaseHook()) :
            classAllocationError(v);
    }
};

template<typename R, typename A1, typename A2, typename A3>
struct Arg<R(*)(A1, A2, A3)> {
    static inline int num(void) {return 3;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        int s, r;
        r = 0;
        s = CheckInstance<A1>::distance(v, 2); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A2>::distance(v, 3); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A3>::distance(v, 4); if (s < 0) {return -1;} r += s;
        return r;
    }
    static inline int create(A1 a1, A2 a2, A3 a3) {
        HSQUIRRELVM v = SquirrelVM::GetVMPtr();
        R *r = static_cast<R*>(sq_malloc(sizeof(R)));
        return r ?
            PostConstruct<R>(v, new(r) R(a1, a2, a3),
                             ClassType<R>::getReleaseHook()) :
            classAllocationError(v);
    }
};

template<typename R, typename A1, typename A2, typename A3, typename A4>
struct Arg<R(*)(A1, A2, A3, A4)> {
    static inline int num(void) {return 4;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        int s, r;
        r = 0;
        s = CheckInstance<A1>::distance(v, 2); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A2>::distance(v, 3); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A3>::distance(v, 4); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A4>::distance(v, 5); if (s < 0) {return -1;} r += s;
        return r;
    }
    static inline int create(A1 a1, A2 a2, A3 a3, A4 a4) {
        HSQUIRRELVM v = SquirrelVM::GetVMPtr();
        R *r = static_cast<R*>(sq_malloc(sizeof(R)));
        return r ?
            PostConstruct<R>(v, new(r) R(a1, a2, a3, a4),
                             ClassType<R>::getReleaseHook()) :
            classAllocationError(v);
    }
};

template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
struct Arg<R(*)(A1, A2, A3, A4, A5)> {
    static inline int num(void) {return 5;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        int s, r;
        r = 0;
        s = CheckInstance<A1>::distance(v, 2); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A2>::distance(v, 3); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A3>::distance(v, 4); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A4>::distance(v, 5); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A5>::distance(v, 6); if (s < 0) {return -1;} r += s;
        return r;
    }
    static inline int create(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) {
        HSQUIRRELVM v = SquirrelVM::GetVMPtr();
        R *r = static_cast<R*>(sq_malloc(sizeof(R)));
        return r ?
            PostConstruct<R>(v, new(r) R(a1, a2, a3, a4, a5),
                             ClassType<R>::getReleaseHook()) :
            classAllocationError(v);
    }
};

template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
struct Arg<R(*)(A1, A2, A3, A4, A5, A6)> {
    static inline int num(void) {return 6;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        int s, r;
        r = 0;
        s = CheckInstance<A1>::distance(v, 2); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A2>::distance(v, 3); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A3>::distance(v, 4); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A4>::distance(v, 5); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A5>::distance(v, 6); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A6>::distance(v, 7); if (s < 0) {return -1;} r += s;
        return r;
    }
    static inline int create(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) {
        HSQUIRRELVM v = SquirrelVM::GetVMPtr();
        R *r = static_cast<R*>(sq_malloc(sizeof(R)));
        return r ?
            PostConstruct<R>(v, new(r) R(a1, a2, a3, a4, a5, a6),
                             ClassType<R>::getReleaseHook()) :
            classAllocationError(v);
    }
};

template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
struct Arg<R(*)(A1, A2, A3, A4, A5, A6, A7)> {
    static inline int num(void) {return 7;}
    static inline int argTypeDistance(HSQUIRRELVM v) {
        int s, r;
        r = 0;
        s = CheckInstance<A1>::distance(v, 2); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A2>::distance(v, 3); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A3>::distance(v, 4); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A4>::distance(v, 5); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A5>::distance(v, 6); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A6>::distance(v, 7); if (s < 0) {return -1;} r += s;
        s = CheckInstance<A7>::distance(v, 8); if (s < 0) {return -1;} r += s;
        return r;
    }
    static inline int create(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) {
        HSQUIRRELVM v = SquirrelVM::GetVMPtr();
        R *r = static_cast<R*>(sq_malloc(sizeof(R)));
        return r ?
            PostConstruct<R>(v, new(r) R(a1, a2, a3, a4, a5, a6, a7),
                             ClassType<R>::getReleaseHook()) :
            classAllocationError(v);
    }
};
#endif // SQPLUS_OVERLOAD_FUNCTIONS

// SqPlusOverload.h

// Local Variables: 
// mode: c++
// End: