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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
|
// 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>
#include <assert.h>
#ifdef __APPLE__
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#include <memory.h>
#include <memory>
#include <limits.h> // For INT_MAX on GCC
#include "squirrel.h" // Include to get SQUNICODE setting from here
#ifndef _SC
#error No _SC macro - Usually defined in squirrel.h
#endif
// Provide a _sqT(...) macros also (same as _SC but easier to know its for Squirrel)
#ifndef _sqT
#define _sqT _SC
#endif
// For backward compatibility, define _T if outside of Windows platform.
// (really, _SC() or _sqT() are better, since that leaves us free to run
// Squirrel in ASCII or wchar_t mode, regardless of the app being built).
#if !defined(_WIN32) && !defined(_WIN64)
#ifndef _T
#define _T _SC
#endif
#endif
// A comment about strings. We let squirrel.h determine whether to use
// char or wchar_t strings. So here we follow the define SQUNICODE. This
// opens up for using Unicode system calls on Windows while having the script
// engine in ASCII mode, or vice versa. To enable this, also the macro
// _SC("some string") is used instead of _T("some string").
//
// To handle the case where function parameters are given in the opposite
// character mode (char if SQChar is wchar_t and vice versa), such strings
// can be converted on the fly to the other mode in the function call, if
// the define SQPLUS_AUTOCONVERT_OTHER_CHAR is set below. Buffers are
// allocated and kept around for the duration of the function call. The
// same applies to returned strings of the opposite type.
#if defined(_MSC_VER) || defined(__BORLANDC__)
#include <tchar.h>
#ifndef SQUNICODE
#define SCSNPRINTF _snprintf
#define SCPUTS puts
#else
#define SCSNPRINTF _snwprintf
#define SCPUTS _putws
#endif
#if defined(_MSC_VER)
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE // Disable warnings around various sprintf
#endif
#pragma warning(disable: 4996) // When above does not work
#endif
#else
#ifdef SQUNICODE
#define SCSNPRINTF _snwprintf
#define SCPUTS _putws
#include <stdio.h> // for snprintf
#else
#define SCSNPRINTF snprintf
#include <stdio.h> // for snprintf
#define SCPUTS puts
#endif
#endif
#ifndef _WINDEF_
typedef int BOOL;
typedef int INT;
typedef float FLOAT;
#define TRUE 1
#define FALSE 0
#endif
#if 1
#define SQ_CALL_RAISE_ERROR SQTrue
#else
#define SQ_CALL_RAISE_ERROR SQFalse
#endif
#include "SquirrelObject.h"
#include "SquirrelVM.h"
#include "SquirrelBindingsUtils.h"
// All setup defines have moved to its own file
#include "SqPlusSetup.h"
#ifdef SQPLUS_AUTOCONVERT_OTHER_CHAR
#define SQPLUS_AUTOCONVERT_MAX_INSTANCES 8 // In argument conversion, don't keep more than this alive
#include "SqPlusOCharBuf.h"
#endif
#if defined(SQPLUS_SUPPORT_STD_STRING) && defined(SQUNICODE)
#ifdef _MSC_VER
#pragma message("std::string and SQChar as wchar_t is not compatible!")
#else
#warning std::string and SQChar as wchar_t is not compatible!
#endif
#endif
namespace SqPlus {
template<class T> struct TypeWrapper {};
struct SquirrelNull {};
struct SQNoBaseClass {}; // For scripted classes with no base class (or no scripted base class)
struct SQAnything { void * anything; }; // Needed for binding pointers to variables (cannot dereference void *).
typedef SQAnything * SQAnythingPtr;
typedef SQChar * SQCharPtr;
// Helper struct to (sometimes) store a temporary return value as another type.
// Useful when returning const char* and other types that require temp allocation.
// For primitive types, it just maps onto itself.
template<class T>
struct Temporary {
typedef T type;
};
// References are tricky, but they should just be filtered out usually
template<class T>
struct SqAssignableRef {
SqAssignableRef( ) : m_pt(0) { }
void operator = (T& tr){ m_pt=&tr; }
operator T& () { return *m_pt; }
T *m_pt;
};
template<class T>
struct Temporary<T&> {
typedef SqAssignableRef<T> type;
};
// === 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(int _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,int MaxLength) {
int 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<int 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_UINT,
VAR_TYPE_FLOAT,
VAR_TYPE_BOOL,
VAR_TYPE_CONST_STRING,
VAR_TYPE_STRING,
VAR_TYPE_USER_POINTER,
VAR_TYPE_INSTANCE,
#ifdef SQPLUS_SUPPORT_STD_STRING
VAR_TYPE_STD_STRING,
#endif
};
template <typename T>
struct TypeInfo {
const SQChar * typeName;
enum {TypeID=VAR_TYPE_NONE, Size=0, TypeMask='?', IsInstance=0};
};
// === Common Variable Types ===
template<>
struct TypeInfo<INT> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("int")) {}
enum {TypeID=VAR_TYPE_INT,Size=sizeof(INT),TypeMask='i', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct TypeInfo<unsigned> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("uint")) {}
enum {TypeID=VAR_TYPE_UINT,Size=sizeof(unsigned), IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct TypeInfo<FLOAT> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("float")) {}
enum {TypeID=VAR_TYPE_FLOAT,Size=sizeof(FLOAT),TypeMask='f', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct TypeInfo<bool> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("bool")) {}
enum {TypeID=VAR_TYPE_BOOL,Size=sizeof(bool),TypeMask='b', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct TypeInfo<short> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("short")) {}
enum {TypeID=VAR_TYPE_INT,Size=sizeof(short),TypeMask='i', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct TypeInfo<char> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("char")) {}
enum {TypeID=VAR_TYPE_INT,Size=sizeof(char),TypeMask='i', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct TypeInfo<SQUserPointer> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("SQUserPointer")) {}
enum {TypeID=VAR_TYPE_USER_POINTER,Size=sizeof(SQUserPointer),TypeMask='u', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct TypeInfo<SQAnything> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("SQUserPointer")) {}
enum {TypeID=VAR_TYPE_USER_POINTER,Size=sizeof(SQUserPointer),TypeMask='u', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct TypeInfo<SQNoBaseClass> {
const SQChar * typeName;
TypeInfo() : typeName(0) {}
enum {TypeID=-1,Size=0,TypeMask=' ', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct TypeInfo<const SQChar *> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("const SQChar *")) {}
enum {TypeID=VAR_TYPE_CONST_STRING,Size=sizeof(const SQChar *),TypeMask='s', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
#ifdef SQPLUS_AUTOCONVERT_OTHER_CHAR
template<>
struct TypeInfo<const SQOtherChar *> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("const SQOtherChar *")) {}
enum {TypeID=VAR_TYPE_CONST_STRING,Size=sizeof(const SQOtherChar *),TypeMask='s', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<>
struct Temporary<SQOtherChar*> {
typedef SQOthCharBuf type;
};
template<>
struct Temporary<const SQOtherChar*> {
typedef SQOthCharBuf type;
};
#endif // SQPLUS_AUTOCONVERT_OTHER_CHAR
// base case: raw pointer
template<typename T, int is_inst>
struct TypeInfoPtrBase {
const SQChar * typeName;
TypeInfoPtrBase() : typeName(TypeInfo<T>().typeName) {}
enum {TypeID=VAR_TYPE_USER_POINTER,Size=sizeof(T*),TypeMask='u'};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
template<typename T>
struct TypeInfoPtrBase<T,1> : public TypeInfo<T> { };
// Partial specialization for pointers (to access type without pointer / or instance typeinfo)
template<class T>
struct TypeInfo<T*> : public TypeInfoPtrBase<T,TypeInfo<T>::IsInstance> { };
// Same thing for references
template<class T>
struct TypeInfo<T&> : public TypeInfoPtrBase<T,TypeInfo<T>::IsInstance> { };
#ifdef SQPLUS_SUPPORT_STD_STRING
template<>
struct TypeInfo<std::string> {
const SQChar *typeName;
TypeInfo() : typeName(_SC("std::string")) {}
enum {TypeID=SqPlus::VAR_TYPE_STD_STRING,Size=sizeof(std::string),TypeMask='s'};
operator ScriptVarType() {return ScriptVarType(TypeID);}
};
#endif
template<>
struct TypeInfo<ScriptStringVarBase> {
const SQChar * typeName;
TypeInfo() : typeName(_SC("ScriptStringVarBase")) {}
enum {TypeID=VAR_TYPE_STRING,Size=sizeof(ScriptStringVarBase),TypeMask='s', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
// === Fixed String Variants ===
template<int N>
struct TypeInfo<ScriptStringVar<N> > {
SQChar typeName[24];
TypeInfo() { scsprintf(typeName,_SC("ScriptStringVar<%d>"),N); }
enum {TypeID=VAR_TYPE_STRING,Size=N*sizeof(ScriptStringVar<N>),TypeMask='s', IsInstance=0};
operator ScriptVarType() { return ScriptVarType(TypeID); }
};
#ifdef SQPLUS_SMARTPOINTER_OPT
#define SQPLUS_SMARTPOINTER_ACCESSTYPE
#include "SqPlusSmartPointer.h"
#else
enum VarAccessType {VAR_ACCESS_READ_WRITE=0,VAR_ACCESS_READ_ONLY=1<<0,VAR_ACCESS_CONSTANT=1<<1,VAR_ACCESS_STATIC=1<<2};
#endif // SQPLUS_SMARTPOINTER_OPT
// See VarRef and ClassType<> below: for instance assignment.
typedef void (*CopyVarFunc)(void * dst,void * src);
// === Class Type Helper class: returns an ID for each class type and provides base class pointer offset ===
struct ClassTypeBase {
ClassTypeBase() : m_pbase(0), m_name(0), m_offset(0), m_may_have_offset(-1) { }
// Many types cannot have offset, since "this" is the same for all base classes of
// an instance. Detect this, to avoid sum up base offsets all the time.
int MayHaveOffset( ) {
if( m_may_have_offset<0 ){
if( m_offset ) m_may_have_offset = 1;
else m_may_have_offset = m_pbase ? m_pbase->MayHaveOffset() : 0;
}
return m_may_have_offset;
}
int GetOffsetTo( ClassTypeBase *pbase ){
if( !m_pbase ) { /*printf("ClassTypeBase::getOffsetTo - Warning - Base type pointer is NULL!\n" );*/ return 0; }
return m_pbase==pbase ? m_offset : m_offset+m_pbase->GetOffsetTo(pbase);
}
virtual CopyVarFunc vgetCopyFunc(void) = 0;
virtual const SQChar* GetTypeName() = 0;
ClassTypeBase *m_pbase;
const SQChar *m_name; // Name of type
int m_offset; // Adjustment of this pointer between this type and its base class
int m_may_have_offset; // Set to 0 for types that cannot possibly have offset
};
// This level handles instance copying in different ways
template<typename T,bool copyable>
struct ClassTypeCopyImpl;
// Helper struct to decide if type is copyable or not
template<typename T>
struct IsCopyable { enum { value=true }; };
#define DECLARE_NONCOPY_TYPE_INTERN(TYPE) \
template<> struct IsCopyable<TYPE> { enum { value=false }; };
// Macro to declare a type that should _not_ be copied using ordinary
// c++ copy expresssion: *(T*)pt1 = *(T*)pt2;
#define DECLARE_NONCOPY_TYPE(TYPE) namespace SqPlus { \
template<> struct IsCopyable<TYPE> { enum { value=false }; }; \
}
// Base class to do copying in ordinary C++ way
template<typename T>
struct ClassTypeCopyImpl<T,true> : public ClassTypeBase {
static void copy(T * dst,T * src) {
*dst = *src; // This works types with copy ctor / assign operator
} // copy
};
// Base class to do copying with memcpy
template<typename T>
struct ClassTypeCopyImpl<T,false> : public ClassTypeBase {
static void copy(T * dst,T * src) {
memcpy(dst,src,sizeof(T)); // This works for raw data types
} // copy
};
// Base classes to do avoid copying altogether (void case)
template<>
struct ClassTypeCopyImpl<void,true> : public ClassTypeBase {
static void copy(void * dst,void * src) { } // copy
};
template<>
struct ClassTypeCopyImpl<void,false> : public ClassTypeBase {
static void copy(void * dst,void * src) { } // copy
};
template<typename T>
struct ClassType : public ClassTypeCopyImpl<T,IsCopyable<T>::value> {
typedef ClassTypeCopyImpl<T,IsCopyable<T>::value> ClassTypeBase;
ClassType( ) { this->m_name=stGetName(); }
virtual CopyVarFunc vgetCopyFunc(void) { return (CopyVarFunc)&ClassTypeBase::copy; }
virtual const SQChar* GetTypeName(){ return this->m_name; }
template<class BC>
void SetBase(TypeWrapper<BC>) {
this->m_pbase = ClassType<BC>::Get();
T* pt = reinterpret_cast<T*>(this);
this->m_offset = ((char*)pt)-((char*)static_cast<BC*>(pt));
}
static ClassType* Get(){ static ClassType<T> st_ct; return &st_ct; }
static ClassTypeBase* type() { return Get(); }
static CopyVarFunc getCopyFunc(void) { return (CopyVarFunc)&ClassTypeBase::copy; }
static const SQChar* stGetName(){ return TypeInfo<T>().typeName; }
#ifdef SQPLUS_OVERLOAD_OPT
#define SQPLUS_OVERLOAD_RELEASE_HOOK
#include "SqPlusOverload.h"
#endif
};
// === Variable references for script access ===
#define SQ_PLUS_TYPE_TABLE _SC("__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 m_type; // Variable type (from enum above).
ClassTypeBase* instanceType; // Class type of the containing class instance (for member vars only).
ClassTypeBase* varType; // The class type of the variable itself
short m_size; // ATS: Use for short and char support. For debugging only (size of item when pointer to item is dereferenced). Could be used for variable max string buffer length.
short m_access; // VarAccessType.
VarRef() : offsetOrAddrOrConst(0), m_type(VAR_TYPE_NONE), instanceType(0/*(SQUserPointer)-1*/), /*copyFunc(0),*/ m_size(0), m_access(VAR_ACCESS_READ_WRITE) {}
VarRef(void * _offsetOrAddrOrConst, ScriptVarType _type, ClassTypeBase* _instanceType, ClassTypeBase* _varType, int _size, VarAccessType _access) :
offsetOrAddrOrConst(_offsetOrAddrOrConst), m_type(_type), instanceType(_instanceType), varType(_varType), m_size(_size), m_access(_access) {
#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)varType),varType->GetTypeName());
#endif // SQ_SUPPORT_INSTANCE_TYPE_INFO
}
};
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.
int pos=0;
while (scriptName[pos] && pos < maxSize) {
d[pos] = scriptName[pos];
pos++;
} // while
d[pos] = 0; // null terminate.
#else
SCSNPRINTF(buff,maxSize,_SC("_v%s"),scriptName);
#endif
} // getVarNameTag
// Internal use only.
int setVarFunc(HSQUIRRELVM v);
int getVarFunc(HSQUIRRELVM v);
int setInstanceVarFunc(HSQUIRRELVM v);
int getInstanceVarFunc(HSQUIRRELVM v);
// === BEGIN Helpers ===
inline void createTableSetGetHandlers(SquirrelObject & so) {
SquirrelObject delegate = so.GetDelegate();
if (!delegate.Exists(_SC("_set"))) {
delegate = SquirrelVM::CreateTable();
SquirrelVM::CreateFunction(delegate,setVarFunc,_SC("_set"),_SC("sn|b|s")); // String var name = number(int or float) or bool or string.
SquirrelVM::CreateFunction(delegate,getVarFunc,_SC("_get"),_SC("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(_SC("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(_SC("validateConstantType(): type must be INT, FLOAT, BOOL, or CONST CHAR *."));
} // case
} // validateConstantType
inline void createInstanceSetGetHandlers(SquirrelObject & so) {
if (!so.Exists(_SC("_set"))) {
SquirrelVM::CreateFunction(so,setInstanceVarFunc,_SC("_set"),_SC("sn|b|s|x")); // String var name = number(int or float) or bool or string or instance.
SquirrelVM::CreateFunction(so,getInstanceVarFunc,_SC("_get"),_SC("s")); // String var name.
} // if
} // createInstanceSetGetHandlers
// === END Helpers ===
// Provide an overridable way of copying / deleting objects
template<typename T>
struct ObjectCloner {
static T* Clone(T* src){ return new T(src); }
static void Delete(T* dst){ if(dst) delete dst; }
};
// specialization for void type
//template<> inline void ClassType<void>::copy(void *dst, void *src) {}
DECLARE_NONCOPY_TYPE_INTERN(void)
// === 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>(),NULL,ClassType<T>::type(),sizeof(*var),access);
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>(),NULL,ClassType<T>::type(),sizeof(constant),VAR_ACCESS_CONSTANT);
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,ClassTypeBase* 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>::type(),sizeof(*var),access);
createInstanceSetGetHandlers(so);
} // RegisterInstanceVariable
#ifdef SQPLUS_SMARTPOINTER_OPT
#define SQPLUS_SMARTPOINTER_REGISTER_VARIABLE
#include "SqPlusSmartPointer.h"
#endif
template<typename T>
void RegisterInstanceConstant(SquirrelObject & so,ClassTypeBase *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,ClassType<T>::type(),sizeof(constant),VAR_ACCESS_CONSTANT);
createInstanceSetGetHandlers(so);
} // RegisterInstanceConstant
//////////////////////////////////////////////////////////////////////////
/////////// BEGIN Generalized Class/Struct Instance Support //////////////
//////////////////////////////////////////////////////////////////////////
// Was previously in SqPlus namespace
//BOOL CreateNativeClassInstance(HSQUIRRELVM v,const SQChar * classname,SQUserPointer ud,SQRELEASEHOOK hook); // In SquirrelBindingUtils.cpp.
// Create native class instance and leave on stack.
//BOOL CreateConstructNativeClassInstance(HSQUIRRELVM v,const SQChar * className);
// Create new instance, copy 'classToCopy', and store result on stack.
template<typename T>
inline BOOL CreateCopyInstance(HSQUIRRELVM v, const SQChar * className,const T & classToCopy) {
#ifndef SQPLUS_DISABLE_COPY_INSTANCES
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;
#else
return FALSE;
#endif
} // CreateCopyInstance
// Create a new copy of type 'className' and copy 'classToCopy', return result via SquirrelObject.
template<typename T>
inline SquirrelObject NewClassCopy(HSQUIRRELVM v, const SQChar * className,const T & classToCopy) {
if (CreateCopyInstance(v, className,classToCopy)) {
HSQOBJECT t;
sq_getstackobj(v,-1,&t);
SquirrelObject obj(t);
sq_poptop(v);
return obj;
} else {
throw SquirrelError(_SC("NewClassCopy(): could not create class"));
} // if
return SquirrelObject();
} // NewClassCopy
// Return a new class copy on the stack from a varArgs function call.
template<typename T>
inline int ReturnCopy(HSQUIRRELVM v,const T & classToCopy) {
SquirrelObject so(NewClassCopy(v,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;
if (SQ_FAILED(sq_getinstanceup(v,idx,&up,ClassType<T>::type()))) {
up = 0;
}
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(_SC("GetInstance: Invalid argument type"));
}
} // if
return (T *)up;
} // GetInstance
template<typename T> void Push(HSQUIRRELVM v, T* pt);
template<typename T> void Push(HSQUIRRELVM v, T& t);
template<typename T> bool Match(TypeWrapper<T&>, HSQUIRRELVM v, int ix);
template<typename T> bool Match(TypeWrapper<T*>, HSQUIRRELVM v, int ix);
template<typename T> T &Get(TypeWrapper<T&>, HSQUIRRELVM v, int ix);
template<typename T> T *Get(TypeWrapper<T*>, HSQUIRRELVM v, int ix);
#ifdef SQPLUS_USE_GENERIC_HANDLERS
// With template specialization, we get Push handlers per 'precise type match'
// This adds a fallback level after that, where we can delegate the work to
// wider C-style functions that can do something for a whole class hierarchy.
// (GenPush, GenGet, GenMatch).
// This macro allows for a a last generic cast operation before giving control
// to one of GenPush/GenMatch/GenGet.
#ifndef SQPLUS_GEN_CAST
#define SQPLUS_GEN_CAST(TYPE,value) ((TYPE*)value)
#endif
template<typename T> void Push(HSQUIRRELVM v, T* pt){ GenPush(v,SQPLUS_GEN_CAST(T,pt)); }
template<typename T> void Push(HSQUIRRELVM v, T& t){ GenPush(v,SQPLUS_GEN_CAST(T,&t)); }
template<typename T> bool Match(TypeWrapper<T&>, HSQUIRRELVM v, int ix){
if((ScriptVarType)TypeInfo<T>::TypeID!=VAR_TYPE_NONE)
return GenMatch(SQPLUS_GEN_CAST(T*,0),TypeInfo<T>().typeName,v,ix);
else return false;
}
template<typename T> bool Match(TypeWrapper<T*>, HSQUIRRELVM v, int ix){
if((ScriptVarType)TypeInfo<T>::TypeID!=VAR_TYPE_NONE)
return GenMatch(SQPLUS_GEN_CAST(T*,0),TypeInfo<T>().typeName,v,ix);
else return false;
}
template<typename T> T &Get(TypeWrapper<T&>, HSQUIRRELVM v, int ix){
if((ScriptVarType)TypeInfo<T>::TypeID!=VAR_TYPE_NONE)
return *(T*)GenGet(SQPLUS_GEN_CAST(T*,0),TypeInfo<T>().typeName,v,ix);
else return *SQPLUS_GEN_CAST(T,0);
}
template<typename T> T *Get(TypeWrapper<T*>, HSQUIRRELVM v, int ix){
if((ScriptVarType)TypeInfo<T>::TypeID!=VAR_TYPE_NONE)
return (T*)GenGet(SQPLUS_GEN_CAST(T*,0),TypeInfo<T>().typeName,v,ix);
else return NULL;
}
#endif // SQPLUS_USE_GENERIC_HANDLERS
// === BEGIN Function Call Handler Prototypes ===
void Push(HSQUIRRELVM v, char value);
void Push(HSQUIRRELVM v, unsigned char value);
void Push(HSQUIRRELVM v, short value);
void Push(HSQUIRRELVM v, unsigned short value);
void Push(HSQUIRRELVM v, int value);
void Push(HSQUIRRELVM v, unsigned int value);
void Push(HSQUIRRELVM v, long value);
void Push(HSQUIRRELVM v, unsigned long value);
void Push(HSQUIRRELVM v, double value);
void Push(HSQUIRRELVM v, float value);
void Push(HSQUIRRELVM v, const SQChar *value);
void Push(HSQUIRRELVM v, SQChar *value);
void Push(HSQUIRRELVM v, const SquirrelNull &);
void Push(HSQUIRRELVM v, SQFUNCTION value);
void Push(HSQUIRRELVM v, SQAnythingPtr value); // Cast to SQAnythingPtr instead of void * if USE_ARGUMENT_DEPENDANT_OVERLOADS can't be used by your compiler.
void Push(HSQUIRRELVM v, SquirrelObject &so);
#define USE_ARGUMENT_DEPENDANT_OVERLOADS
#ifdef USE_ARGUMENT_DEPENDANT_OVERLOADS
#ifdef _MSC_VER
#pragma warning(disable:4675) // Disable warning: "resolved overload was found by argument-dependent lookup" when class/struct pointers are used as function arguments.
#endif
// === BEGIN Argument Dependent Overloads ===
void Push(HSQUIRRELVM v, bool value); // Pass bool as int if USE_ARGUMENT_DEPENDANT_OVERLOADS can't be used by your compiler.
void Push(HSQUIRRELVM v, const void *value); // Pass SQAnythingPtr instead of void * " "
void Push(HSQUIRRELVM v, const SQUserPointer &value);
// === END Argument Dependent Overloads ===
#endif
#define SQPLUS_CHECK_GET(res) if (!SQ_SUCCEEDED(res)) throw SquirrelError(_SC("sq_get*() failed (type error)"))
bool Match(TypeWrapper<bool>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<char>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<unsigned char>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<short>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<unsigned short>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<int>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<unsigned int>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<long>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<unsigned long>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<float>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<double>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<const SQChar *>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<SQChar *>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<HSQUIRRELVM>, HSQUIRRELVM v, int idx); // See Get() for HSQUIRRELVM below (v is always present).
bool Match(TypeWrapper<void*>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<SquirrelObject>, HSQUIRRELVM v, int idx); // See sq_getstackobj(): always returns true.
void Get(TypeWrapper<void>, HSQUIRRELVM v, int);
bool Get(TypeWrapper<bool>, HSQUIRRELVM v, int idx);
char Get(TypeWrapper<char>, HSQUIRRELVM v, int idx);
unsigned char Get(TypeWrapper<unsigned char>, HSQUIRRELVM v, int idx);
short Get(TypeWrapper<short>, HSQUIRRELVM v, int idx);
unsigned short Get(TypeWrapper<unsigned short>, HSQUIRRELVM v, int idx);
int Get(TypeWrapper<int>, HSQUIRRELVM v, int idx);
unsigned int Get(TypeWrapper<unsigned int>, HSQUIRRELVM v, int idx);
long Get(TypeWrapper<long>, HSQUIRRELVM v, int idx);
unsigned long Get(TypeWrapper<unsigned long>, HSQUIRRELVM v, int idx);
float Get(TypeWrapper<float>, HSQUIRRELVM v, int idx);
double Get(TypeWrapper<double>, HSQUIRRELVM v, int idx);
const SQChar *Get(TypeWrapper<const SQChar *>, HSQUIRRELVM v, int idx);
SquirrelNull Get(TypeWrapper<SquirrelNull>, HSQUIRRELVM v, int idx);
void *Get(TypeWrapper<void *>, HSQUIRRELVM v, int idx);
HSQUIRRELVM Get(TypeWrapper<HSQUIRRELVM>, HSQUIRRELVM v, int /*idx*/); // sq_poptop(v): remove UserData from stack so GetParamCount() matches normal behavior.
SquirrelObject Get(TypeWrapper<SquirrelObject>, HSQUIRRELVM v, int idx);
#ifdef SQPLUS_AUTOCONVERT_OTHER_CHAR
void Push(HSQUIRRELVM v, const SQOtherChar *value);
void Push(HSQUIRRELVM v, SQOtherChar *value);
bool Match(TypeWrapper<const SQOtherChar *>, HSQUIRRELVM v, int idx);
bool Match(TypeWrapper<SQOtherChar *>, HSQUIRRELVM v, int idx);
SQOthCharBuf Get(TypeWrapper<const SQOtherChar *>, HSQUIRRELVM v, int idx);
#endif // SQPLUS_AUTOCONVERT_OTHER_CHAR
#ifdef SQPLUS_SUPPORT_STD_STRING
void Push(HSQUIRRELVM v, const std::string& value);
bool Match(TypeWrapper<const std::string&>, HSQUIRRELVM v, int idx);
std::string Get(TypeWrapper<const std::string&>, HSQUIRRELVM v, int idx);
#endif
// Added jflanglois suggestion, 8/20/06. jcs
#ifdef SQPLUS_SUPPORT_SQ_STD_STRING
typedef std::basic_string<SQChar> sq_std_string;
void Push(HSQUIRRELVM v,const sq_std_string & value);
bool Match(TypeWrapper<const sq_std_string &>, HSQUIRRELVM v, int idx);
sq_std_string Get(TypeWrapper<const sq_std_string &>, HSQUIRRELVM v, int idx);
#endif
// Specialization to support void return type.
void GetRet(TypeWrapper<void>, HSQUIRRELVM v,int idx);
// GetRet() restores the stack for SquirrelFunction<>() calls.
// Hold on to a reference since return value might be temporary string/instance
template<typename RT>
inline RT GetRet(TypeWrapper<RT>,HSQUIRRELVM v,int idx) {
static SquirrelObject st_sq_ret;
static typename Temporary<RT>::type st_ret;
st_ret = Get(TypeWrapper<RT>(),v,idx);
st_sq_ret.AttachToStackObject(idx);
sq_pop(v,2); // restore stack after function call.
return st_ret; }
#ifndef GCC_INLINE_WORKAROUND
# include "SqPlusFunctionCallImpl.h"
#endif // GCC_INLINE_WORKAROUND
// === END Function Call Handlers ===
// Helper, only implement function bodies
#define IMPLEMENT_ENUM_TYPE(TYPE) namespace SqPlus { \
bool Match(TypeWrapper<TYPE>,HSQUIRRELVM v,int idx) { return Match(TypeWrapper<int>(),v,idx); } \
TYPE Get(TypeWrapper<TYPE>,HSQUIRRELVM v,int idx) { return (TYPE)Get(TypeWrapper<int>(),v,idx); } \
void Push(HSQUIRRELVM v,TYPE value) { sq_pushinteger(v,(int)value); } \
} // nameSpace SqPlus
// To register simple types (like enums) so they can be used as arguments
// (however, this does not handle enums as return values correctly, since
// we C++ gets problems with references to temporaries)
#define DECLARE_ENUM_TYPE(TYPE) IMPLEMENT_ENUM_TYPE(TYPE) \
namespace SqPlus { \
template<> struct TypeInfo<TYPE> : public TypeInfo<int> { }; \
} // nameSpace SqPlus
// As above but use when function bodies should not be generated
// (for a header file).
#define PROTOS_ENUM_TYPE(TYPE) namespace SqPlus { \
bool Match(TypeWrapper<TYPE>,HSQUIRRELVM v,int idx); \
TYPE Get(TypeWrapper<TYPE>,HSQUIRRELVM v,int idx); \
void Push(HSQUIRRELVM v,TYPE value); \
template<> struct TypeInfo<TYPE> : public TypeInfo<int> { }; \
} // nameSpace SqPlus
// NAME and macro changes from Ben's (Project5) forum post. 2/26/06 jcs
// Kamaitati's NULL_INSTANCE support. 5/28/06 jcs
// ATS: Splitting the macros in different parts to support custom Push implementation (covariant return type)
#define DECLARE_INSTANCE_TYPEINFO_(TYPE,NAME) \
inline const SQChar * GetTypeName(const TYPE & n) { return _SC(#NAME); } \
template<> \
struct TypeInfo<TYPE> { \
const SQChar * typeName; \
TypeInfo() : typeName( _SC(#NAME)) {} \
enum {TypeID=VAR_TYPE_INSTANCE,Size=sizeof(TYPE),TypeMask='x', IsInstance=1}; \
operator ScriptVarType() { return ScriptVarType(TypeID); } \
};
#define DECLARE_INSTANCE_TYPEINFO(TYPE) namespace SqPlus { \
DECLARE_INSTANCE_TYPEINFO_(TYPE,TYPE) \
} // namespace SqPlus
#define DECLARE_INSTANCE_TYPEINFO_NAME(TYPE,NAME) namespace SqPlus { \
DECLARE_INSTANCE_TYPEINFO_(TYPE,NAME) \
} // namespace SqPlus
#ifdef SQPLUS_SUPPORT_NULL_INSTANCES
// Macro part shared by 'derived' macros
#define DECLARE_INSTANCE_TYPE_BASE_(TYPE,NAME) \
DECLARE_INSTANCE_TYPEINFO_(TYPE,NAME) \
template<> inline bool Match(TypeWrapper<TYPE &>,HSQUIRRELVM v,int idx) { return GetInstance<TYPE,false>(v,idx) != NULL; } \
template<> inline bool Match(TypeWrapper<TYPE *>,HSQUIRRELVM v,int idx) { \
return (sq_gettype(v,idx)==OT_NULL) || (GetInstance<TYPE,false>(v,idx) != NULL); } \
template<> inline TYPE & Get(TypeWrapper<TYPE &>,HSQUIRRELVM v,int idx) { return *GetInstance<TYPE,true>(v,idx); } \
template<> inline TYPE * Get(TypeWrapper<TYPE *>,HSQUIRRELVM v,int idx) { \
if (sq_gettype(v,idx)==OT_NULL) return NULL; \
return GetInstance<TYPE,true>(v,idx); }
// Ordinary case
#define DECLARE_INSTANCE_TYPE_NAME_(TYPE,NAME) namespace SqPlus { \
DECLARE_INSTANCE_TYPE_BASE_(TYPE,NAME) \
template<> inline void Push(HSQUIRRELVM v,TYPE * value) { \
if (!value) sq_pushnull(v); \
else if (!CreateNativeClassInstance(v,GetTypeName(*value),value,0)) \
throw SquirrelError( _SC( "Push(): could not create INSTANCE (check registration name)")); } \
template<> inline void Push(HSQUIRRELVM v,TYPE & value) { if (!CreateCopyInstance(v,GetTypeName(value),value)) throw SquirrelError( _SC( "Push(): could not create INSTANCE copy (check registration name)")); } \
} // nameSpace SqPlus
// Allows for providing custom Push handlers (protos here, impl must be provided by app)
#define DECLARE_INSTANCE_TYPE_NAME_CUSTOM_(TYPE,NAME) namespace SqPlus { \
DECLARE_INSTANCE_TYPE_BASE_(TYPE,NAME) \
template<> void Push(HSQUIRRELVM v,TYPE * value); \
template<> void Push(HSQUIRRELVM v,TYPE & value); \
} // nameSpace SqPlus
#else
#define DECLARE_INSTANCE_TYPE_NAME_(TYPE,NAME) namespace SqPlus { \
DECLARE_INSTANCE_TYPEINFO_(TYPE,NAME) \
template<> inline void Push(HSQUIRRELVM v,TYPE * value) { if (!CreateNativeClassInstance(v,GetTypeName(*value),value,0)) throw SquirrelError( _SC( "Push(): could not create INSTANCE (check registration name)")); } \
template<> inline void Push(HSQUIRRELVM v,TYPE & value) { if (!CreateCopyInstance(v,GetTypeName(value),value)) throw SquirrelError( _SC( "Push(): could not create INSTANCE copy (check registration name)")); } \
template<> inline bool Match(TypeWrapper<TYPE &>,HSQUIRRELVM v,int idx) { return GetInstance<TYPE,false>(v,idx) != NULL; } \
template<> inline bool Match(TypeWrapper<TYPE *>,HSQUIRRELVM v,int idx) { return GetInstance<TYPE,false>(v,idx) != NULL; } \
template<> inline TYPE & Get(TypeWrapper<TYPE &>,HSQUIRRELVM v,int idx) { return *GetInstance<TYPE,true>(v,idx); } \
template<> inline TYPE * Get(TypeWrapper<TYPE *>,HSQUIRRELVM v,int idx) { return GetInstance<TYPE,true>(v,idx); } \
} // 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)
#define DECLARE_INSTANCE_TYPE_CUSTOM(TYPE) DECLARE_INSTANCE_TYPE_NAME_CUSTOM_(TYPE,TYPE)
#define DECLARE_INSTANCE_TYPE_NAME_CUSTOM(TYPE,NAME) DECLARE_INSTANCE_TYPE_NAME_CUSTOM_(TYPE,NAME)
#else
#define SQPLUS_DECLARE_INSTANCE_TYPE_CONST
#include "SqPlusConst.h"
#endif
#ifdef SQPLUS_OVERLOAD_OPT
#define SQPLUS_OVERLOAD_DECLARATION
#include "SqPlusOverload.h"
#endif
// Versions of above for types that aren't copy constructable
#define DECLARE_INSTANCE_TYPEINFO_NOCOPY(TYPE) \
DECLARE_INSTANCE_TYPEINFO(TYPE) \
DECLARE_NONCOPY_TYPE(TYPE)
#define DECLARE_INSTANCE_TYPEINFO_NOCOPY_NAME(TYPE,NAME) namespace SqPlus { \
DECLARE_INSTANCE_TYPEINFO_(TYPE,NAME) \
DECLARE_NONCOPY_TYPE(TYPE)
#define DECLARE_INSTANCE_TYPE_NOCOPY(TYPE) \
DECLARE_INSTANCE_TYPE(TYPE) \
DECLARE_NONCOPY_TYPE(TYPE)
#define DECLARE_INSTANCE_TYPE_NOCOPY_NAME(TYPE,NAME) namespace SqPlus { \
DECLARE_INSTANCE_TYPEINFO_(TYPE,NAME) \
DECLARE_NONCOPY_TYPE(TYPE)
//////////////////////////////////////////////////////////////////////////
//////////// 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,_SC("Incorrect function argument"))
#else
#define sq_argassert(arg,_index_)
#endif
// === Return value variants ===
template<class RT>
struct ReturnSpecialization {
// === Standard Function calls ===
static int Call(RT (*func)(),HSQUIRRELVM v,int /*index*/) {
RT ret = func();
Push(v,ret);
return 1;
}
template<typename P1>
static int Call(RT (*func)(P1),HSQUIRRELVM v,int 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 int Call(RT (*func)(P1,P2),HSQUIRRELVM v,int 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 int Call(RT (*func)(P1,P2,P3),HSQUIRRELVM v,int 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 int Call(RT (*func)(P1,P2,P3,P4),HSQUIRRELVM v,int 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 int Call(RT (*func)(P1,P2,P3,P4,P5),HSQUIRRELVM v,int 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 int Call(RT (*func)(P1,P2,P3,P4,P5,P6),HSQUIRRELVM v,int 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 int Call(RT (*func)(P1,P2,P3,P4,P5,P6,P7),HSQUIRRELVM v,int 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 ===
#define SQPLUS_CALL_MFUNC_RET0
#include "SqPlusCallTemplates.h"
#ifdef SQPLUS_CONST_OPT
#define SQPLUS_CALL_MFUNC_RET0
#include "SqPlusConst.h"
#endif
};
// === No return value variants ===
template<>
struct ReturnSpecialization<void> {
// === Standard function calls ===
static int Call(void (*func)(),HSQUIRRELVM v,int /*index*/) {
(void)v;
func();
return 0;
}
template<typename P1>
static int Call(void (*func)(P1),HSQUIRRELVM v,int index) {
sq_argassert(1,index + 0);
func(
Get(TypeWrapper<P1>(),v,index + 0)
);
return 0;
}
template<typename P1,typename P2>
static int Call(void (*func)(P1,P2),HSQUIRRELVM v,int 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 int Call(void (*func)(P1,P2,P3),HSQUIRRELVM v,int 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 int Call(void (*func)(P1,P2,P3,P4),HSQUIRRELVM v,int 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 int Call(void (*func)(P1,P2,P3,P4,P5),HSQUIRRELVM v,int 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 int Call(void (*func)(P1,P2,P3,P4,P5,P6),HSQUIRRELVM v,int 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 int Call(void (*func)(P1,P2,P3,P4,P5,P6,P7),HSQUIRRELVM v,int 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 ===
#define SQPLUS_CALL_MFUNC_NORET
#include "SqPlusCallTemplates.h"
#ifdef SQPLUS_CONST_OPT
#define SQPLUS_CALL_MFUNC_NORET
#include "SqPlusConst.h"
#endif
};
// === STANDARD Function return value specialized call handlers ===
template<typename RT>
int Call(RT (*func)(),HSQUIRRELVM v,int index) {
return ReturnSpecialization<RT>::Call(func,v,index);
}
template<typename RT,typename P1>
int Call(RT (*func)(P1),HSQUIRRELVM v,int index) {
return ReturnSpecialization<RT>::Call(func,v,index);
}
template<typename RT,typename P1,typename P2>
int Call(RT (*func)(P1,P2),HSQUIRRELVM v,int index) {
return ReturnSpecialization<RT>::Call(func,v,index);
}
template<typename RT,typename P1,typename P2,typename P3>
int Call(RT (*func)(P1,P2,P3),HSQUIRRELVM v,int index) {
return ReturnSpecialization<RT>::Call(func,v,index);
}
template<typename RT,typename P1,typename P2,typename P3,typename P4>
int Call(RT (*func)(P1,P2,P3,P4),HSQUIRRELVM v,int index) {
return ReturnSpecialization<RT>::Call(func,v,index);
}
template<typename RT,typename P1,typename P2,typename P3,typename P4,typename P5>
int Call(RT (*func)(P1,P2,P3,P4,P5),HSQUIRRELVM v,int index) {
return ReturnSpecialization<RT>::Call(func,v,index);
}
template<typename RT,typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
int Call(RT (*func)(P1,P2,P3,P4,P5,P6),HSQUIRRELVM v,int 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>
int Call(RT (*func)(P1,P2,P3,P4,P5,P6,P7),HSQUIRRELVM v,int index) {
return ReturnSpecialization<RT>::Call(func,v,index);
}
// === MEMBER Function return value specialized call handlers ===
#define SQPLUS_CALL_MFUNC_RET1
#include "SqPlusCallTemplates.h"
#ifdef SQPLUS_CONST_OPT
#define SQPLUS_CALL_MFUNC_RET1
#include "SqPlusConst.h"
#endif
// === Direct Call Standard Function handler ===
template<typename Func>
struct DirectCallFunction {
static inline int Dispatch(HSQUIRRELVM v) {
#ifdef SQPLUS_USE_SANDBOX_VM
if( v==SquirrelVM::GetSandboxVMPtr() ){
return sq_throwerror(v, _SC("SqPlus: Cannot exec function from sandbox VM"));
}
#endif
StackHandler sa(v);
int 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 int Dispatch(HSQUIRRELVM v) {
#ifdef SQPLUS_USE_SANDBOX_VM
if( v==SquirrelVM::GetSandboxVMPtr() ){
return sq_throwerror(v, _SC("SqPlus: Cannot exec function from sandbox VM"));
}
#endif
StackHandler sa(v);
int paramCount = sa.GetParamCount();
unsigned char * ud = (unsigned char *)sa.GetUserData(paramCount);
return Call(**(Callee**)ud,*(Func*)(ud + sizeof(Callee*)),v,2);
} // Dispatch
};
// === Direct Call Function handlers ===
#define SQ_CLASS_OBJECT_TABLE_NAME _SC("__ot")
#define SQ_CLASS_HIER_ARRAY _SC("__ca")
template<typename Callee, typename Func>
struct DirectCallInstanceFuncPicker {
Callee *instance;
Func *func;
DirectCallInstanceFuncPicker(HSQUIRRELVM v) {
#ifdef SQPLUS_USE_SANDBOX_VM
if( v==SquirrelVM::GetSandboxVMPtr() ){
instance = NULL;
func = NULL;
return;
}
#endif
StackHandler sa(v);
instance = static_cast<Callee*>(sa.GetInstanceUp(1, 0));
const int paramCount = sa.GetParamCount();
func = static_cast<Func*>(sa.GetUserData(paramCount));
#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 = static_cast<Callee*>(
// <TODO> 64-bit compatible version.
typeTable.GetUserPointer(INT((size_t)ClassType<Callee>::type()))
);
}
#elif defined(SQ_USE_CLASS_INHERITANCE_SIMPLE)
SquirrelObject so(sa.GetObjectHandle(1)); // 'this'
ClassTypeBase *instType; so.GetTypeTag((SQUserPointer*)&instType);
ClassTypeBase *calleeType = ClassType<Callee>::type();
if (instType!=calleeType && instType->MayHaveOffset() ) {
// instance type is nore derived than callee, adjust pointer
int offset = instType->GetOffsetTo(calleeType);
instance = (Callee*)((char*)instance-offset);
}
#endif
}
};
// === Direct Call Instance Member Function handler ===
template<typename Callee,typename Func>
class DirectCallInstanceMemberFunction {
public:
static inline int Dispatch(HSQUIRRELVM v) {
DirectCallInstanceFuncPicker<Callee, Func> p(v);
return p.instance && p.func ?
Call(*(p.instance), *(p.func), v, 2) :
sq_throwerror(v, _SC("Invalid Instance Type"));
}
};
// === Direct Call Instance Global Function handler ===
template<typename Callee,typename Func>
class DirectCallInstanceGlobalFunction {
public:
static inline int Dispatch(HSQUIRRELVM v) {
DirectCallInstanceFuncPicker<Callee, Func> p(v);
return p.func ?
Call(*(p.func), v, 1) :
sq_throwerror(v, _SC("Invalid Instance Type"));
}
};
// === Direct Call Instance Global Function Var Args handler ===
template<typename Callee,typename Func>
class DirectCallInstanceGlobalFunctionVarArgs {
public:
static inline int Dispatch(HSQUIRRELVM v) {
DirectCallInstanceFuncPicker<Callee, Func> p(v);
return p.func && p.instance ?
(*p.func)(p.instance,v) :
sq_throwerror(v, _SC("Invalid Instance Type"));
}
};
// === Direct Call Instance Member Function Variable Argument handler ===
template<typename Callee,typename Func>
class DirectCallInstanceMemberFunctionVarArgs {
public:
static inline int Dispatch(HSQUIRRELVM v) {
DirectCallInstanceFuncPicker<Callee, Func> p(v);
sq_poptop(v); // Remove UserData from stack: so sa.GetParamCount() returns actual param count.
return p.func && p.instance ?
(p.instance->*(*p.func))(v) :
sq_throwerror(v, _SC("Invalid Instance Type"));
}
};
#ifdef SQPLUS_SMARTPOINTER_OPT
#define SQPLUS_SMARTPOINTER_DISPATCH
#include "SqPlusSmartPointer.h"
#endif
// Code fragment useful for debugging new implementations.
#if 0
HSQOBJECT ho = sa.GetObjectHandle(paramCount);
SquirrelObject so(ho);
SQObjectType sot = so.GetType();
#endif
#ifdef SQPLUS_ENABLE_AUTO_TYPEMASK
#include "SqPlusTypeMask.h"
#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);
#ifdef SQPLUS_ENABLE_AUTO_TYPEMASK
sq_setparamscheck(v,0,sqTypeMask<Func>::Get());
#endif
} // 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);
#ifdef SQPLUS_ENABLE_AUTO_TYPEMASK
sq_setparamscheck(v,0,sqTypeMask<Func>::Get());
#endif
} // sq_pushdirectclosure
#ifdef SQPLUS_SMARTPOINTER_OPT
#define SQPLUS_SMARTPOINTER_DIRECT_CLOSURE
#include "SqPlusSmartPointer.h"
#endif
// === 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);
#ifdef SQPLUS_ENABLE_AUTO_TYPEMASK
sq_setparamscheck(v,0,sqTypeMask<Func>::Get());
#endif
} // sq_pushdirectinstanceclosure
// === Global function using this: class pointer retrieved from script class instance ===
template<typename Callee,typename Func>
inline void sq_pushdirectinstanceclosureglobal(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));
// Could check that 1st arg of Func is a Callee
sq_newclosure(v,DirectCallInstanceGlobalFunction<Callee,Func>::Dispatch,nupvalues+1);
#ifdef SQPLUS_ENABLE_AUTO_TYPEMASK
SQChar *tm = (SQChar*)sqTypeMask<Func>::Get();
if( tm ) {
// Censor out the 1st arg, since SqPlus adds that automatically
tm[1] = _SC('x'); //tm[0];
tm++;
}
sq_setparamscheck(v,0,tm?tm:_SC(""));
#endif
} // sq_pushdirectinstanceclosureglobal
// === Global function using this: class pointer retrieved from script class instance ===
template<typename Callee,typename Func>
inline void sq_pushdirectinstanceclosureglobalvarargs(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));
// Could check that 1st arg of Func is a Callee
sq_newclosure(v,DirectCallInstanceGlobalFunctionVarArgs<Callee,Func>::Dispatch,nupvalues+1);
#ifdef SQPLUS_ENABLE_AUTO_TYPEMASK
sq_setparamscheck(v,-1,_SC("x"));
#endif
} // sq_pushdirectinstanceclosureglobal
// === Class Instance call: class pointer retrieved from script class instance (variable arguments) ===
template<typename Callee>
inline void sq_pushdirectinstanceclosurevarargs(HSQUIRRELVM v,const Callee & callee,int (Callee::*func)(HSQUIRRELVM),SQUnsignedInteger nupvalues) {
unsigned char * up = (unsigned char *)sq_newuserdata(v,sizeof(func)); // Also pushed on stack.
memcpy(up,&func,sizeof(func));
typedef int (Callee::*FuncType)(HSQUIRRELVM);
sq_newclosure(v,DirectCallInstanceMemberFunctionVarArgs<Callee, FuncType>::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 STANDARD function (hso is table or class) ===
template<typename Func>
inline void Register(HSQUIRRELVM v,HSQOBJECT hso,Func func,const SQChar * name) {
sq_pushobject(v,hso);
Register(v,func,name);
sq_poptop(v); // Remove hso.
} // Register
// === Register a MEMBER function (hso is table or class) ===
// === Fixed Class pointer call (always calls with object pointer that was registered) ===
template<typename Callee,typename Func>
inline void Register(HSQUIRRELVM v,HSQOBJECT hso,Callee & callee,Func func,const SQChar * name) {
sq_pushobject(v,hso);
Register(v,callee,func,name);
sq_poptop(v); // Remove hso.
} // Register
// === Register an INSTANCE MEMBER function ===
// === Class Instance call: class pointer retrieved from script class instance ===
template<typename Callee,typename Func>
inline void RegisterInstance(HSQUIRRELVM v,HSQOBJECT hclass,Callee & callee,Func func,const SQChar * name) {
sq_pushobject(v,hclass);
sq_pushstring(v,name,-1);
sq_pushdirectinstanceclosure(v,callee,func,0);
sq_createslot(v,-3);
sq_poptop(v); // Remove hclass.
} // RegisterInstance
// === Register an INSTANCE GLOBAL MEMBER function ===
// === Class Instance call: class pointer retrieved from script class instance ===
// Allows embedding global func that takes Callee as 1st arg as a member func
template<typename Callee,typename Func>
inline void RegisterInstanceGlobalFunc(HSQUIRRELVM v,HSQOBJECT hclass,Callee & callee,Func func,const SQChar * name) {
sq_pushobject(v,hclass);
sq_pushstring(v,name,-1);
sq_pushdirectinstanceclosureglobal(v,callee,func,0);
sq_createslot(v,-3);
sq_poptop(v); // Remove hclass.
} // RegisterInstanceGlobaFunc
// === Register an INSTANCE GLOBAL MEMBER WITH VAR ARGS function ===
// === Class Instance call: class pointer retrieved from script class instance ===
// Allows embedding global func that takes Callee as 1st arg as a member func
template<typename Callee,typename Func>
inline void RegisterInstanceGlobalFuncVarArgs(HSQUIRRELVM v,HSQOBJECT hclass,Callee & callee,Func func,const SQChar * name) {
sq_pushobject(v,hclass);
sq_pushstring(v,name,-1);
sq_pushdirectinstanceclosureglobalvarargs(v,callee,func,0);
sq_createslot(v,-3);
sq_poptop(v); // Remove hclass.
} // RegisterInstanceGlobaFunc
#ifdef SQPLUS_SMARTPOINTER_OPT
#define SQPLUS_SMARTPOINTER_REGISTER_INSTANCE
#include "SqPlusSmartPointer.h"
#endif
#ifdef _MSC_VER
#pragma warning(disable : 4995) // Deprecated _snprintf
#endif
// === Register an INSTANCE MEMBER function Variable Arguments ===
// typeMask: "*" means don't check parameters, typeMask=0 means function takes no arguments (and is type checked for that case).
// All the other Squirrel type-masks are passed normally.
template<typename Callee>
inline void RegisterInstanceVarArgs(HSQUIRRELVM v,HSQOBJECT hclass,Callee & callee,int (Callee::*func)(HSQUIRRELVM),const SQChar * name,const SQChar * typeMask=_SC("*")) {
sq_pushobject(v,hclass);
sq_pushstring(v,name,-1);
sq_pushdirectinstanceclosurevarargs(v,callee,func,0);
SQChar tm[64];
SQChar * ptm = tm;
int numParams = SQ_MATCHTYPEMASKSTRING;
if (typeMask) {
if (typeMask[0] == '*') {
ptm = 0; // Variable args: don't check parameters.
// numParams = 0; // Clear SQ_MATCHTYPEMASKSTRING (does not mean match 0 params. See sq_setparamscheck()).
} else {
if (SCSNPRINTF(tm,sizeof(tm),_SC("x%s"),typeMask) < 0) { // Must be an instance.
throw SquirrelError(_SC("RegisterInstanceVarArgs: typeMask string too long."));
} // if
} // if
} else { // <TODO> Need to check object type on stack: table, class, instance, etc.
// _snprintf(tm,sizeof(tm),"x"); // instance.
tm[0] = 'x';
tm[1] = 0;
} // if
if (ptm) { // If ptm == 0, don't check type.
sq_setparamscheck(v,numParams,ptm); // Determine arg count from type string.
} // if
#ifdef _DEBUG
sq_setnativeclosurename(v,-1,name); // For debugging only.
#endif
sq_createslot(v,-3);
sq_poptop(v); // Remove hclass.
} // RegisterInstanceVarArgs
#ifdef _MSC_VER
#pragma warning(default : 4995)
#endif
// === Call Squirrel Functions from C/C++ ===
// No type checking is performed for Squirrel functions as Squirrel types are dynamic:
// Incoming types are passed unchanged to Squirrel functions. The parameter count is checked: an exception is thrown if mismatched.
// Return values must match the RT template argument type, else an exception can be thrown on return.
template<typename RT>
struct SquirrelFunction {
HSQUIRRELVM v;
SquirrelObject object; // Table or class.
SquirrelObject func;
SquirrelFunction() : v(0) {}
SquirrelFunction(HSQUIRRELVM _v,const SquirrelObject & _object,const SquirrelObject & _func) : v(_v), object(_object), func(_func) {}
SquirrelFunction(const SquirrelObject & _object,const SquirrelObject & _func) : v(SquirrelVM::GetVMPtr()), object(_object), func(_func) {}
SquirrelFunction(const SquirrelObject & _object,const SQChar * name) {
v = SquirrelVM::GetVMPtr();
object = _object;
func = object.GetValue(name);
}
SquirrelFunction(const SQChar * name) {
v = SquirrelVM::GetVMPtr();
object = SquirrelVM::GetRootTable();
func = object.GetValue(name);
}
// Release references and reset internal objects to null.
void reset(void) {
func.Reset();
object.Reset();
} // Reset
#define SQPLUS_CHECK_FNCALL(res) if (!SQ_SUCCEEDED(res)) throw SquirrelError(_SC("SquirrelFunction<> call failed"))
RT operator()(void) {
sq_pushobject(v,func.GetObjectHandle());
sq_pushobject(v,object.GetObjectHandle());
SQPLUS_CHECK_FNCALL(sq_call(v,1,SQTrue,SQ_CALL_RAISE_ERROR));
return GetRet(TypeWrapper<RT>(),v,-1);
}
template<typename P1>
RT operator()(P1 p1) {
sq_pushobject(v,func.GetObjectHandle());
sq_pushobject(v,object.GetObjectHandle());
Push(v,p1);
SQPLUS_CHECK_FNCALL(sq_call(v,2,SQTrue,SQ_CALL_RAISE_ERROR));
return GetRet(TypeWrapper<RT>(),v,-1);
}
template<typename P1,typename P2>
RT operator()(P1 p1,P2 p2) {
sq_pushobject(v,func.GetObjectHandle());
sq_pushobject(v,object.GetObjectHandle());
Push(v,p1);
Push(v,p2);
SQPLUS_CHECK_FNCALL(sq_call(v,3,SQTrue,SQ_CALL_RAISE_ERROR));
return GetRet(TypeWrapper<RT>(),v,-1);
}
template<typename P1,typename P2,typename P3>
RT operator()(P1 p1,P2 p2,P3 p3) {
sq_pushobject(v,func.GetObjectHandle());
sq_pushobject(v,object.GetObjectHandle());
Push(v,p1);
Push(v,p2);
Push(v,p3);
SQPLUS_CHECK_FNCALL(sq_call(v,4,SQTrue,SQ_CALL_RAISE_ERROR));
return GetRet(TypeWrapper<RT>(),v,-1);
}
template<typename P1,typename P2,typename P3,typename P4>
RT operator()(P1 p1,P2 p2,P3 p3,P4 p4) {
sq_pushobject(v,func.GetObjectHandle());
sq_pushobject(v,object.GetObjectHandle());
Push(v,p1);
Push(v,p2);
Push(v,p3);
Push(v,p4);
SQPLUS_CHECK_FNCALL(sq_call(v,5,SQTrue,SQ_CALL_RAISE_ERROR));
return GetRet(TypeWrapper<RT>(),v,-1);
}
template<typename P1,typename P2,typename P3,typename P4,typename P5>
RT operator()(P1 p1,P2 p2,P3 p3,P4 p4,P5 p5) {
sq_pushobject(v,func.GetObjectHandle());
sq_pushobject(v,object.GetObjectHandle());
Push(v,p1);
Push(v,p2);
Push(v,p3);
Push(v,p4);
Push(v,p5);
SQPLUS_CHECK_FNCALL(sq_call(v,6,SQTrue,SQ_CALL_RAISE_ERROR));
return GetRet(TypeWrapper<RT>(),v,-1);
}
template<typename P1,typename P2,typename P3,typename P4,typename P5,typename P6>
RT operator()(P1 p1,P2 p2,P3 p3,P4 p4,P5 p5,P6 p6) {
sq_pushobject(v,func.GetObjectHandle());
sq_pushobject(v,object.GetObjectHandle());
Push(v,p1);
Push(v,p2);
Push(v,p3);
Push(v,p4);
Push(v,p5);
Push(v,p6);
SQPLUS_CHECK_FNCALL(sq_call(v,7,SQTrue,SQ_CALL_RAISE_ERROR));
return GetRet(TypeWrapper<RT>(),v,-1);
}
template<typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7>
RT operator()(P1 p1,P2 p2,P3 p3,P4 p4,P5 p5,P6 p6,P7 p7) {
sq_pushobject(v,func.GetObjectHandle());
sq_pushobject(v,object.GetObjectHandle());
Push(v,p1);
Push(v,p2);
Push(v,p3);
Push(v,p4);
Push(v,p5);
Push(v,p6);
Push(v,p7);
SQPLUS_CHECK_FNCALL(sq_call(v,8,SQTrue,SQ_CALL_RAISE_ERROR));
return GetRet(TypeWrapper<RT>(),v,-1);
}
};
// === Class/Struct registration ===
#define SQ_DELETE_CLASS(CLASSTYPE) if (up) { CLASSTYPE * self = (CLASSTYPE *)up; delete self;} return 0
#define SQ_DECLARE_RELEASE(CLASSTYPE) \
static int release(SQUserPointer up,SQInteger size) { \
SQ_DELETE_CLASS(CLASSTYPE); \
}
template<typename T>
struct ReleaseClassPtrPtr {
static int release(SQUserPointer up,SQInteger size) {
if (up) {
T ** self = (T **)up;
delete *self;
} // if
return 0;
} // release
};
template<typename T>
struct ReleaseClassPtr {
static int release(SQUserPointer up,SQInteger size) {
if (up) {
T * self = (T *)up;
delete self;
} // if
return 0;
} // release
};
BOOL CreateClass(HSQUIRRELVM v,SquirrelObject & newClass,SQUserPointer classType,const SQChar * name,const SQChar * baseName=0);
template<typename T>
inline void PopulateAncestry(HSQUIRRELVM v,
SquirrelObject &instance,
T *newClass)
{
// 11/2/05: Create a new table for this instance.
SquirrelObject newObjectTable = SquirrelVM::CreateTable();
// <TODO> 64-bit compatible version.
newObjectTable.SetUserPointer(INT((size_t)ClassType<T>::type()), newClass);
instance.SetValue(SQ_CLASS_OBJECT_TABLE_NAME, newObjectTable);
SquirrelObject classHierArray = instance.GetValue(SQ_CLASS_HIER_ARRAY);
INT count = classHierArray.Len();
// This will be true when more than one C/C++ class is in the hierarchy.
if (count > 1) {
--count; // Skip the most-derived class.
for (INT i = 0; i < count; i++) {
// Kamaitati's changes for C++ inheritance support. jcs 5/28/06
SquirrelObject so = classHierArray.GetValue(i);
sq_pushobject(v,so.GetObjectHandle());
SQUserPointer typeTag;
sq_gettypetag(v,-1,&typeTag);
newObjectTable.SetUserPointer(INT(size_t(typeTag)),newClass);
sq_poptop(v);
}
}
}
// Call PostConstruct() at the end of custom constructors.
template<typename T>
inline int PostConstruct(HSQUIRRELVM v, T *newClass, SQRELEASEHOOK hook)
{
#ifdef SQ_USE_CLASS_INHERITANCE
StackHandler sa(v);
HSQOBJECT ho = sa.GetObjectHandle(1); // OT_INSTANCE
SquirrelObject instance(ho);
PopulateAncestry(v, instance, newClass);
#endif // SQ_USE_CLASS_INHERITANCE
sq_setinstanceup(v, 1, newClass);
sq_setreleasehook(v, 1, hook);
return TRUE;
} // PostConstruct
inline int PostConstructSimple(HSQUIRRELVM v, void *newClass, SQRELEASEHOOK hook){
sq_setinstanceup(v, 1, newClass);
sq_setreleasehook(v, 1, hook);
return TRUE;
} // PostConstructSimple
template<typename T>
struct ConstructReleaseClass {
static int construct(HSQUIRRELVM v) {
return PostConstruct<T>(v,new T(),release);
} // construct
SQ_DECLARE_RELEASE(T)
};
# ifdef SQPLUS_ENABLE_TYPEOF
template<typename T>
int sq_typeof(HSQUIRRELVM v) {
sq_pushstring(v,TypeInfo<T>().typeName,-1);
return 1;
}
# endif
// === Helper for RegisterClassType*() ===
inline void setupClassHierarchy(SquirrelObject newClass) {
// <NOTE> New member vars cannot be added to instances (OT_INSTANCE): additions must occur on the defining class (OT_CLASS), before any instances are instantiated.
if (!newClass.Exists(SQ_CLASS_OBJECT_TABLE_NAME)) { // Will always get table from most-derived registered class.
SquirrelObject objectTable = SquirrelVM::CreateTable();
newClass.SetValue(SQ_CLASS_OBJECT_TABLE_NAME,objectTable); // Constructors must add their 'this' pointer indexed by type to this table. See PostConstruct() above.
// 11/2/05: This table will behave as a static global for each instance unless overwritten during construction (see PostConstruct() above).
} // if
SquirrelObject classHierArray;
if (!newClass.Exists(SQ_CLASS_HIER_ARRAY)) { // Will always get table from most-derived registered class.
classHierArray = SquirrelVM::CreateArray(0); // The only constructor called will be the most-derived class: this array contains all classes in the hierarchy to be constructed.
newClass.SetValue(SQ_CLASS_HIER_ARRAY,classHierArray);
} else {
classHierArray = newClass.GetValue(SQ_CLASS_HIER_ARRAY);
} // if
classHierArray.ArrayAppend(newClass); // Add the class to the hierarchy array. The array values will be released and replaced with UserData to free created ancestor classes.
} // setupClassHierarchy
template<typename T>
inline SquirrelObject RegisterClassType(HSQUIRRELVM v,const SQChar * scriptClassName,const SQChar * baseScriptClassName=0) {
int top = sq_gettop(v);
SquirrelObject newClass;
if (CreateClass(v,newClass,(SQUserPointer)ClassType<T>::type(),scriptClassName,baseScriptClassName)) {
SquirrelVM::CreateFunction(newClass,&ConstructReleaseClass<T>::construct,_SC("constructor"));
# ifdef SQ_USE_CLASS_INHERITANCE
setupClassHierarchy(newClass);
# endif
# ifdef SQPLUS_ENABLE_TYPEOF
SquirrelVM::CreateFunction(newClass,&sq_typeof<T>,_SC("_typeof"));
# endif
} // if
sq_settop(v,top);
return newClass;
} // RegisterClassType
template<typename T>
inline SquirrelObject RegisterClassTypeNoConstructor(HSQUIRRELVM v,const SQChar * scriptClassName,const SQChar * baseScriptClassName=0) {
int top = sq_gettop(v);
SquirrelObject newClass;
if (CreateClass(v,newClass,(SQUserPointer)ClassType<T>::type(),scriptClassName,baseScriptClassName)) {
# ifdef SQ_USE_CLASS_INHERITANCE
setupClassHierarchy(newClass);
# endif
# ifdef SQPLUS_ENABLE_TYPEOF
SquirrelVM::CreateFunction(newClass,&sq_typeof<T>,_SC("_typeof"));
# endif
} // if
sq_settop(v,top);
return newClass;
} // RegisterClassTypeNoConstructor
// === Define and register a C++ class and its members for use with Squirrel ===
// Constructors+destructors are automatically created. Custom constructors must use the
// standard SQFUNCTION signature if variable argument types are required (overloads).
// See testSqPlus2.cpp for examples.
// <NOTE> Do not use SQClassDefBase<> directly, use SQClassDef<> or SQClassDefNoConstructor<>, below.
template<typename TClassType, typename TClassBase>
struct SQClassDefBase {
HSQUIRRELVM v;
const SQChar * name;
SquirrelObject newClass;
#if defined(SQ_USE_CLASS_INHERITANCE) || defined(SQ_USE_CLASS_INHERITANCE_SIMPLE)
const SQChar * base;
// Optional base arg is the name of a base class to inherit from (must already be defined in the Squirrel VM).
SQClassDefBase(HSQUIRRELVM _v,const SQChar * _name=0,const SQChar * _base=0) : v(_v), name(_name), base(_base) {InitBase();}
// Optional base arg is the name of a base class to inherit from (must already be defined in the Squirrel VM).
SQClassDefBase(const SQChar * _name=0,const SQChar * _base=0) : v(SquirrelVM::GetVMPtr()), name(_name), base(_base) {InitBase(TypeWrapper<TClassBase>());}
template<class Base>
void InitBase(TypeWrapper<Base>){ /*assert(base);*/ ClassType<TClassType>::Get()->SetBase(TypeWrapper<Base>()); CheckInitDefaultNames(); }
void InitBase(TypeWrapper<SQNoBaseClass>){ /*assert(!base);*/ CheckInitDefaultNames(); }
void CheckInitDefaultNames(){ if( !name ) name=TypeInfo<TClassType>().typeName; if( !base ) base=TypeInfo<TClassBase>().typeName; }
#else
SQClassDefBase(HSQUIRRELVM _v,const SQChar * _name=0) : v(_v), name(_name) { CheckInitDefaultName(); }
SQClassDefBase(const SQChar * _name=0) : v(SquirrelVM::GetVMPtr()), name(_name) { CheckInitDefaultName(); }
void CheckInitDefaultName(){ if( !name ) name=TypeInfo<TClassType>().typeName; }
#endif
// Register a member function.
template<typename Func>
SQClassDefBase & func(Func pfunc,const SQChar * name) {
RegisterInstance(v,newClass.GetObjectHandle(),*(TClassType *)0,pfunc,name);
return *this;
} // func
// Register a global function as a member function (the global takes a Callee*/& as first arg).
template<typename Func>
SQClassDefBase & globMembFunc(Func pfunc,const SQChar * name) {
RegisterInstanceGlobalFunc(v,newClass.GetObjectHandle(),*(TClassType *)0,pfunc,name);
return *this;
} // globMemberFunc
// Register a global function as a member function (the global takes a Callee*/& as first arg and SQVM* as 2nd).
template<typename Func>
SQClassDefBase & globMembFuncVarArgs(Func pfunc,const SQChar * name) {
RegisterInstanceGlobalFuncVarArgs(v,newClass.GetObjectHandle(),*(TClassType *)0,pfunc,name);
return *this;
} // globMemberFuncVarArgs
#ifdef SQPLUS_SMARTPOINTER_OPT
#define SQPLUS_SMARTPOINTER_CLASS_DEF_FUNC
#include "SqPlusSmartPointer.h"
#endif
// Register a variable-argument member function (supports variable+multiple return values).
// typeMask: "*" means don't check parameters, typeMask=0 means function takes no arguments (and is type checked for that case).
// All the other Squirrel type-masks are passed normally.
template<typename Func>
SQClassDefBase & funcVarArgs(Func pfunc,const SQChar * name,const SQChar * typeMask=_SC("*")) {
RegisterInstanceVarArgs(v,newClass.GetObjectHandle(),*(TClassType *)0,pfunc,name,typeMask);
return *this;
} // funcVarArgs
// === BEGIN static-member+global function registration ===
// === This version is for static member functions only, such as custom constructors where 'this' is not yet valid ===
// typeMask: "*" means don't check parameters, typeMask=0 means function takes no arguments (and is type checked for that case).
// All the other Squirrel type-masks are passed normally.
template<typename Func>
SQClassDefBase & staticFuncVarArgs(Func pfunc,const SQChar * name,const SQChar * typeMask=_SC("*")) {
SquirrelVM::PushObject(newClass);
SquirrelVM::CreateFunction(pfunc,name,typeMask);
SquirrelVM::Pop(1);
return *this;
} // staticFuncVarArgs
// Register a standard global function (effectively embedding a global function in TClassType's script namespace: does not need or use a 'this' pointer).
template<typename Func>
SQClassDefBase & staticFunc(Func pfunc,const SQChar * name) {
Register(v,newClass.GetObjectHandle(),pfunc,name);
return *this;
} // staticFunc
// Register a function to a pre-allocated class/struct member function: will use callee's 'this' (effectively embedding a global function in TClassType's script namespace).
template<typename Callee,typename Func>
SQClassDefBase & staticFunc(Callee & callee,Func pfunc,const SQChar * name) {
Register(v,newClass.GetObjectHandle(),callee,pfunc,name);
return *this;
} // staticFunc
// === END static+global function registration ===
// Register a member variable.
template<typename VarType>
SQClassDefBase & var(VarType TClassType::* pvar,const SQChar * name,VarAccessType access=VAR_ACCESS_READ_WRITE) {
struct CV {
VarType TClassType::* var;
} cv; // Cast Variable helper.
cv.var = pvar;
RegisterInstanceVariable(newClass,ClassType<TClassType>::type(),*(VarType **)&cv,name,access);
return *this;
} // var
// Register a member variable as a UserPointer (read only).
template<typename VarType>
SQClassDefBase & varAsUserPointer(VarType TClassType::* pvar,const SQChar * name) {
struct CV {
VarType TClassType::* var;
} cv; // Cast Variable helper.
cv.var = pvar;
RegisterInstanceVariable(newClass,ClassType<TClassType>::type(),*(SQAnything **)&cv,name,VAR_ACCESS_READ_ONLY);
return *this;
} // varAsUserPointer
#ifdef SQPLUS_SMARTPOINTER_OPT
#define SQPLUS_SMARTPOINTER_CLASS_DEF_VAR
#include "SqPlusSmartPointer.h"
#endif
template<typename VarType>
SQClassDefBase & staticVar(VarType * pvar,const SQChar * name,VarAccessType access=VAR_ACCESS_READ_WRITE) {
struct CV {
VarType * var;
} cv; // Cast Variable helper.
cv.var = pvar;
RegisterInstanceVariable(newClass,ClassType<TClassType>::type(),*(VarType **)&cv,name,VarAccessType(access|VAR_ACCESS_STATIC));
return *this;
} // staticVar
#ifdef SQPLUS_CONST_OPT
#define SQ_REG_CONST_STATIC_VAR
#include "SqPlusConst.h"
#endif
// Member / static member script vars (ordinary Squirrel vars)
SQClassDefBase & scriptVar( const SQChar* name, int ival, SQBool static_var=SQFalse ) {
HSQUIRRELVM v = SquirrelVM::GetVMPtr();
sq_pushobject(v,newClass.GetObjectHandle());
sq_pushstring(v,name,-1);
sq_pushinteger(v,ival);
sq_newslot(v,-3,static_var);
sq_pop(v,1);
return *this;
}
SQClassDefBase & scriptVar( const SQChar* name, double fval, SQBool static_var=SQFalse ) {
HSQUIRRELVM v = SquirrelVM::GetVMPtr();
sq_pushobject(v,newClass.GetObjectHandle());
sq_pushstring(v,name,-1);
sq_pushfloat(v,fval);
sq_newslot(v,-3,static_var);
sq_pop(v,1);
return *this;
}
SQClassDefBase & scriptVar( const SQChar* name, const SQChar* sval, SQBool static_var=SQFalse ) {
HSQUIRRELVM v = SquirrelVM::GetVMPtr();
sq_pushobject(v,newClass.GetObjectHandle());
sq_pushstring(v,name,-1);
sq_pushstring(v,sval,-1);
sq_newslot(v,-3,static_var);
sq_pop(v,1);
return *this;
}
// Register a constant (read-only in script, passed by value (only INT, FLOAT, or BOOL types)).
template<typename ConstantType>
SQClassDefBase & constant(ConstantType constant,const SQChar * name) {
RegisterInstanceConstant(newClass,ClassType<TClassType>::type(),constant,name);
return *this;
} // constant
// Register an enum as an integer (read-only in script).
SQClassDefBase & enumInt(int constant,const SQChar * name) {
RegisterInstanceConstant(newClass,ClassType<TClassType>::type(),constant,name);
return *this;
} // enumInt
#ifdef SQPLUS_OVERLOAD_OPT
#define SQPLUS_OVERLOAD_IMPLEMENTATION
#include "SqPlusOverload.h"
#endif
};
#ifdef SQPLUS_OVERLOAD_OPT
#define SQPLUS_OVERLOAD_FUNCTIONS
#include "SqPlusOverload.h"
#endif
template<typename TClassType, typename TClassBase=SQNoBaseClass>
struct SQClassDef : public SQClassDefBase<TClassType,TClassBase> {
#if defined(SQ_USE_CLASS_INHERITANCE) || defined(SQ_USE_CLASS_INHERITANCE_SIMPLE)
// Optional base arg is the name of a base class to inherit from (must already be defined in the Squirrel VM).
SQClassDef(HSQUIRRELVM _v,const SQChar * _name=0,const SQChar * _base=0) : SQClassDefBase<TClassType,TClassBase>(_v,_name,_base) {
SQClassDefBase<TClassType,TClassBase>::newClass =
RegisterClassType<TClassType>( SQClassDefBase<TClassType,TClassBase>::v,
SQClassDefBase<TClassType,TClassBase>::name,
SQClassDefBase<TClassType,TClassBase>::base );
}
// Optional base arg is the name of a base class to inherit from (must already be defined in the Squirrel VM).
SQClassDef(const SQChar * _name=0,const SQChar * _base=0) : SQClassDefBase<TClassType,TClassBase>(_name,_base) {
SQClassDefBase<TClassType,TClassBase>::newClass =
RegisterClassType< TClassType>( SQClassDefBase<TClassType,TClassBase>::v,
SQClassDefBase<TClassType,TClassBase>::name,
SQClassDefBase<TClassType,TClassBase>::base );
}
#else
SQClassDef(HSQUIRRELVM _v,const SQChar * _name=0) : SQClassDefBase<TClassType,TClassBase>(_v,_name) {
SQClassDefBase<TClassType,TClassBase>::newClass =
RegisterClassType<TClassType>(SQClassDefBase<TClassType,TClassBase>::v,
SQClassDefBase<TClassType,TClassBase>::name );
}
SQClassDef(const SQChar * _name=0) : SQClassDefBase<TClassType,TClassBase>(_name) {
SQClassDefBase<TClassType,TClassBase>::newClass =
RegisterClassType<TClassType>(SQClassDefBase<TClassType,TClassBase>::v,
SQClassDefBase<TClassType,TClassBase>::name );
}
#endif
};
template<typename TClassType, typename TClassBase=SQNoBaseClass>
struct SQClassDefNoConstructor : public SQClassDefBase<TClassType,TClassBase> {
#if defined(SQ_USE_CLASS_INHERITANCE) || defined(SQ_USE_CLASS_INHERITANCE_SIMPLE)
// Optional base arg is the name of a base class to inherit from (must already be defined in the Squirrel VM).
SQClassDefNoConstructor(HSQUIRRELVM _v,const SQChar * _name=0,const SQChar * _base=0) : SQClassDefBase<TClassType,TClassBase>(_v,_name,_base) {
SQClassDefBase<TClassType,TClassBase>::newClass = RegisterClassTypeNoConstructor<TClassType>(SQClassDefBase<TClassType,TClassBase>::v,SQClassDefBase<TClassType,TClassBase>::name,SQClassDefBase<TClassType,TClassBase>::base);
}
// Optional base arg is the name of a base class to inherit from (must already be defined in the Squirrel VM).
SQClassDefNoConstructor(const SQChar * _name=0,const SQChar * _base=0) : SQClassDefBase<TClassType,TClassBase>(_name,_base) {
SQClassDefBase<TClassType,TClassBase>::newClass = RegisterClassTypeNoConstructor<TClassType>(SQClassDefBase<TClassType,TClassBase>::v,SQClassDefBase<TClassType,TClassBase>::name,SQClassDefBase<TClassType,TClassBase>::base);
}
#else
SQClassDefNoConstructor(HSQUIRRELVM _v,const SQChar * _name=0) : SQClassDefBase<TClassType,TClassBase>(_v,_name) {
SQClassDefBase<TClassType,TClassBase>::newClass = RegisterClassTypeNoConstructor<TClassType>(SQClassDefBase<TClassType,TClassBase>::v,SQClassDefBase<TClassType,TClassBase>::name);
}
SQClassDefNoConstructor(const SQChar * _name=0) : SQClassDefBase<TClassType,TClassBase>(_name) {
SQClassDefBase<TClassType,TClassBase>::newClass = RegisterClassTypeNoConstructor<TClassType>(SQClassDefBase<TClassType,TClassBase>::v,SQClassDefBase<TClassType,TClassBase>::name);
}
#endif
};
// === Macros for old style registration. SQClassDef registration is now easier to use (SQ_DECLARE_CLASS() is not needed) ===
#define SQ_DECLARE_CLASS(CLASSNAME) \
static int _##CLASSNAME##_release(SQUserPointer up,SQInteger size) { \
if (up) { \
CLASSNAME * self = (CLASSNAME *)up; \
delete self; \
} \
return 0; \
} \
static int _##CLASSNAME##_constructor(HSQUIRRELVM v) { \
CLASSNAME * pc = new CLASSNAME(); \
sq_setinstanceup(v,1,pc); \
sq_setreleasehook(v,1,_##CLASSNAME##_release); \
return 1; \
}
#define SQ_REGISTER_CLASS(CLASSNAME) \
RegisterClassType(SquirrelVM::GetVMPtr(),_SC(#CLASSNAME),_##CLASSNAME##_constructor)
#define SQ_REGISTER_INSTANCE(NEWSQCLASS,CCLASS,FUNCNAME) \
RegisterInstance(SquirrelVM::GetVMPtr(),NEWSQCLASS.GetObjectHandle(),*(CCLASS *)0,&CCLASS::FUNCNAME,_SC(#FUNCNAME));
#define SQ_REGISTER_INSTANCE_VARARGS(NEWSQCLASS,CCLASS,FUNCNAME) \
RegisterInstanceVarArgs(SquirrelVM::GetVMPtr(),NEWSQCLASS.GetObjectHandle(),*(CCLASS *)0,&CCLASS::FUNCNAME,_SC(#FUNCNAME));
#define SQ_REGISTER_INSTANCE_VARIABLE(NEWSQCLASS,CCLASS,VARNAME) \
RegisterInstanceVariable(NEWSQCLASS,&((CCLASS *)0)->VARNAME,_SC(#VARNAME));
#if defined(USE_ARGUMENT_DEPENDANT_OVERLOADS) && defined(_MSC_VER)
#pragma warning (default:4675)
#endif
}; // namespace SqPlus
// === BEGIN code suggestion from the Wiki ===
// Get any bound type from this SquirrelObject. Note that Squirrel's
// handling of references and pointers still holds here.
template<typename _ty>
inline _ty SquirrelObject::Get(void) {
sq_pushobject(SquirrelVM::_VM,GetObjectHandle());
_ty val = SqPlus::Get(SqPlus::TypeWrapper<_ty>(),SquirrelVM::_VM,-1);
sq_poptop(SquirrelVM::_VM);
return val;
}
// Set any bound type to this SquirrelObject. Note that Squirrel's
// handling of references and pointers still holds here.
template<typename _ty>
inline SquirrelObject SquirrelObject::SetByValue(_ty val) { // classes/structs should be passed by ref (below) to avoid an extra copy.
SqPlus::Push(SquirrelVM::_VM,val);
AttachToStackObject(-1);
sq_poptop(SquirrelVM::_VM);
return *this;
}
// Set any bound type to this SquirrelObject. Note that Squirrel's
// handling of references and pointers still holds here.
template<typename _ty>
inline SquirrelObject &SquirrelObject::Set(_ty & val) {
SqPlus::Push(SquirrelVM::_VM,val);
AttachToStackObject(-1);
sq_poptop(SquirrelVM::_VM);
return *this;
}
// === END code suggestion from the Wiki ===
#endif //_SQ_PLUS_H_
|