summaryrefslogtreecommitdiffstats
path: root/ext/luawrapper/include/LuaContext.hpp
blob: e3a87ee9b77688fdaf2c9024cdcc3cb101280fad (plain)
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
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
/*
Copyright (c) 2013, Pierre KRIEGER
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef INCLUDE_LUACONTEXT_HPP
#define INCLUDE_LUACONTEXT_HPP

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <random>
#include <set>
#include <stdexcept>
#include <string>
#include <sstream>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <boost/any.hpp>
#include <boost/format.hpp>
#include <boost/mpl/distance.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <boost/type_traits.hpp>
#include <lua.hpp>

#if defined(_MSC_VER) && _MSC_VER < 1900
#   include "misc/exception.hpp"
#endif

#ifdef __GNUC__
#   define ATTR_UNUSED __attribute__((unused))
#else
#   define ATTR_UNUSED
#endif

#define LUACONTEXT_GLOBAL_EQ "e5ddced079fc405aa4937b386ca387d2"
#define EQ_FUNCTION_NAME "__eq"
#define TOSTRING_FUNCTION_NAME "__tostring"

/**
 * Defines a Lua context
 * A Lua context is used to interpret Lua code. Since everything in Lua is a variable (including functions),
 * we only provide few functions like readVariable and writeVariable.
 *
 * You can also write variables with C++ functions so that they are callable by Lua. Note however that you HAVE TO convert
 * your function to std::function (not directly std::bind or a lambda function) so the class can detect which argument types
 * it wants. These arguments may only be of basic types (int, float, etc.) or std::string.
 */

#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif

class LuaContext {
    struct ValueInRegistry;
    template<typename TFunctionObject, typename TFirstParamType> struct Binder;
    template<typename T> struct IsOptional;
    enum Globals_t { Globals }; // tag for "global variables"
public:
    /**
     * @param openDefaultLibs True if luaL_openlibs should be called
     */
    explicit LuaContext(bool openDefaultLibs = true)
    {
        // luaL_newstate can return null if allocation failed
        mState = luaL_newstate();
        if (mState == nullptr)
            throw std::bad_alloc();

        // setting the panic function
        lua_atpanic(mState, [](lua_State* state) -> int {
            const std::string str = lua_tostring(state, -1);
            lua_pop(state, 1);
            assert(false && "lua_atpanic triggered");
            exit(0);
        });

        // opening default library if required to do so
        if (openDefaultLibs)
            luaL_openlibs(mState);

         writeGlobalEq();
    }

    void writeGlobalEq() {
      const auto eqFunction = [](lua_State* lua) -> int {
        try {
          lua_pushstring(lua, "__eq");
          lua_gettable(lua, -2);
          /* if not found, return false */
          if (lua_isnil(lua, -1)) {
            lua_pop(lua, -2);
            lua_pushboolean(lua, false);
            return 1;
          }
          lua_insert(lua, lua_gettop(lua)-2);
          return callRaw(lua, PushedObject{lua, 3}, 1).release();
        } catch(...) {
          Pusher<std::exception_ptr>::push(lua, std::current_exception()).release();
          luaError(lua);
        }
      };
      lua_pushcfunction(mState, eqFunction);
      lua_setglobal(mState, LUACONTEXT_GLOBAL_EQ);
    };

    /**
     * Move constructor
     */
    LuaContext(LuaContext&& s) :
        mState(s.mState)
    {
        s.mState = luaL_newstate();
    }
    
    /**
     * Move operator
     */
    LuaContext& operator=(LuaContext&& s) noexcept
    {
        std::swap(mState, s.mState);
        return *this;
    }

    /**
     * Copy is forbidden
     */
    LuaContext(const LuaContext&) = delete;
    
    /**
     * Copy is forbidden
     */
    LuaContext& operator=(const LuaContext&) = delete;

    /**
     * Destructor
     */
    ~LuaContext() noexcept
    {
        assert(mState);
        lua_close(mState);
    }
    
    /**
     * Thrown when an error happens during execution of lua code (like not enough parameters for a function)
     */
    class ExecutionErrorException : public std::runtime_error
    {
    public:
        ExecutionErrorException(const std::string& msg) :
            std::runtime_error(msg)
        {
        }
    };

    /**
     * Thrown when a syntax error happens in a lua script
     */
    class SyntaxErrorException : public std::runtime_error
    {
    public:
        SyntaxErrorException(const std::string& msg) :
            std::runtime_error(msg)
        {
        }
    };

    /**
     * Thrown when trying to cast a Lua variable to an unvalid type, eg. trying to read a number when the variable is a string
     */
    class WrongTypeException : public std::runtime_error
    {
    public:
        WrongTypeException(const std::string& luaType_, const std::type_info& destination_) :
            std::runtime_error("Trying to cast a lua variable from \"" + luaType_ + "\" to \"" + destination_.name() + "\""),
            luaType(luaType_),
            destination(destination_)
        {
        }
        
        std::string luaType;
        const std::type_info& destination;
    };

    /**
     * Function object that can call a function stored by Lua
     * This type is copiable and movable, but not constructible. It can only be created through readVariable.
     * @tparam TFunctionType    Function type (eg. "int (int, bool)")
     */
    template<typename TFunctionType>
    class LuaFunctionCaller;

    /**
     * Opaque type that identifies a Lua object
     */
    struct LuaObject {
        LuaObject() = default;
        LuaObject(lua_State* state, int index=-1) {
            this->objectInRegistry = std::make_shared<LuaContext::ValueInRegistry>(state, index);
        }
        std::shared_ptr<LuaContext::ValueInRegistry> objectInRegistry;
    };

    /**
     * Opaque type that identifies a Lua thread
     */
    struct ThreadID {
        ThreadID() = default;
        ThreadID(ThreadID&& o) : state(o.state), threadInRegistry(std::move(o.threadInRegistry)) { }
        ThreadID& operator=(ThreadID&& o) { std::swap(state, o.state); std::swap(threadInRegistry, o.threadInRegistry); return *this; }
    public:
        friend LuaContext;
        lua_State* state;
        std::unique_ptr<ValueInRegistry> threadInRegistry;
    };

    /**
     * Type that is considered as an empty array
     */
    enum EmptyArray_t { EmptyArray };

    /**
     * Type for a metatable
     */
    enum Metatable_t { Metatable };

    /**
     * Executes lua code from the stream
     * @param code      A stream that Lua will read its code from
     */
    void executeCode(std::istream& code)
    {
        auto toCall = load(mState, code);
        call<std::tuple<>>(mState, std::move(toCall));
    }

    /**
     * Executes lua code from the stream and returns a value
     * @param code      A stream that Lua will read its code from
     * @tparam TType    The type that the executing code should return
     */
    template<typename TType>
    auto executeCode(std::istream& code)
        -> TType
    {
        auto toCall = load(mState, code);
        return call<TType>(mState, std::move(toCall));
    }

    /**
     * Executes lua code given as parameter
     * @param code      A string containing code that will be executed by Lua
     */
    void executeCode(const std::string& code)
    {
        executeCode(code.c_str());
    }
    
    /*
     * Executes Lua code from the stream and returns a value
     * @param code      A string containing code that will be executed by Lua
     * @tparam TType    The type that the executing code should return
     */
    template<typename TType>
    auto executeCode(const std::string& code)
        -> TType
    {
        return executeCode<TType>(code.c_str());
    }

    /**
     * Executes Lua code
     * @param code      A string containing code that will be executed by Lua
     */
    void executeCode(const char* code)
    {
        auto toCall = load(mState, code);
        call<std::tuple<>>(mState, std::move(toCall));
    }

    /*
     * Executes Lua code from the stream and returns a value
     * @param code      A string containing code that will be executed by Lua
     * @tparam TType    The type that the executing code should return
     */
    template<typename TType>
    auto executeCode(const char* code)
        -> TType
    {
        auto toCall = load(mState, code);
        return call<TType>(mState, std::move(toCall));
    }

    /**
     * Executes lua code from the stream
     * @param code      A stream that Lua will read its code from
     */
    void executeCode(const ThreadID& thread, std::istream& code)
    {
        auto toCall = load(thread.state, code);
        call<std::tuple<>>(thread.state, std::move(toCall));
    }

    /**
     * Executes lua code from the stream and returns a value
     * @param code      A stream that Lua will read its code from
     * @tparam TType    The type that the executing code should return
     */
    template<typename TType>
    auto executeCode(const ThreadID& thread, std::istream& code)
        -> TType
    {
        auto toCall = load(thread.state, code);
        return call<TType>(thread.state, std::move(toCall));
    }

    /**
     * Executes lua code given as parameter
     * @param code      A string containing code that will be executed by Lua
     */
    void executeCode(const ThreadID& thread, const std::string& code)
    {
        executeCode(thread, code.c_str());
    }
    
    /*
     * Executes Lua code from the stream and returns a value
     * @param code      A string containing code that will be executed by Lua
     * @tparam TType    The type that the executing code should return
     */
    template<typename TType>
    auto executeCode(const ThreadID& thread, const std::string& code)
        -> TType
    {
        return executeCode<TType>(thread, code.c_str());
    }

    /**
     * Executes Lua code
     * @param code      A string containing code that will be executed by Lua
     */
    void executeCode(const ThreadID& thread, const char* code)
    {
        auto toCall = load(thread.state, code);
        call<std::tuple<>>(thread.state, std::move(toCall));
    }

    /*
     * Executes Lua code from the stream and returns a value
     * @param code      A string containing code that will be executed by Lua
     * @tparam TType    The type that the executing code should return
     */
    template<typename TType>
    auto executeCode(const ThreadID& thread, const char* code)
        -> TType
    {
        auto toCall = load(thread.state, code);
        return call<TType>(thread.state, std::move(toCall));
    }
    
    /**
     * Tells that Lua will be allowed to access an object's function
     * This is the version "registerFunction(name, &Foo::function)"
     */
    template<typename TPointerToMemberFunction>
    auto registerFunction(const std::string& name, TPointerToMemberFunction pointer)
        -> typename std::enable_if<std::is_member_function_pointer<TPointerToMemberFunction>::value>::type
    {
        registerFunctionImpl(name, std::mem_fn(pointer), tag<TPointerToMemberFunction>{});
    }

    /**
     * Tells that Lua will be allowed to access an object's function
     * This is the version with an explicit template parameter: "registerFunction<void (Foo::*)()>(name, [](Foo&) { })"
     * @param fn                Function object which takes as first parameter a reference to the object
     * @tparam TFunctionType    Pointer-to-member function type
     */
    template<typename TFunctionType, typename TType>
    void registerFunction(const std::string& functionName, TType fn)
    {
        static_assert(std::is_member_function_pointer<TFunctionType>::value, "registerFunction must take a member function pointer type as template parameter");
        registerFunctionImpl(functionName, std::move(fn), tag<TFunctionType>{});
    }

    /**
     * Tells that Lua will be allowed to access an object's function
     * This is the alternative version with an explicit template parameter: "registerFunction<Foo, void (*)()>(name, [](Foo&) { })"
     * @param fn                Function object which takes as first parameter a reference to the object
     * @tparam TObject          Object to register this function to
     * @tparam TFunctionType    Function type
     */
    template<typename TObject, typename TFunctionType, typename TType>
    void registerFunction(const std::string& functionName, TType fn)
    {
        static_assert(std::is_function<TFunctionType>::value, "registerFunction must take a function type as template parameter");
        registerFunctionImpl(functionName, std::move(fn), tag<TObject>{}, tag<TFunctionType>{});
    }

    /**
     * Wrappers for registering "__eq" function in case we want to change this to something else some day
     */

    template<typename TPointerToMemberFunction>
    auto registerEqFunction(TPointerToMemberFunction pointer)
        -> typename std::enable_if<std::is_member_function_pointer<TPointerToMemberFunction>::value>::type
    {
        registerFunctionImpl(EQ_FUNCTION_NAME, std::mem_fn(pointer), tag<TPointerToMemberFunction>{});
    }

    template<typename TFunctionType, typename TType>
    void registerEqFunction(TType fn)
    {
        static_assert(std::is_member_function_pointer<TFunctionType>::value, "registerFunction must take a member function pointer type as template parameter");
        registerFunctionImpl(EQ_FUNCTION_NAME, std::move(fn), tag<TFunctionType>{});
    }

    template<typename TObject, typename TFunctionType, typename TType>
    void registerEqFunction(TType fn)
       {
        static_assert(std::is_function<TFunctionType>::value, "registerFunction must take a function type as template parameter");
        registerFunctionImpl(EQ_FUNCTION_NAME, std::move(fn), tag<TObject>{}, tag<TFunctionType>{});
    }

    /**
     * Wrappers for registering "__tostring" function in case we want to change this to something else some day
     */

    template<typename TPointerToMemberFunction>
    auto registerToStringFunction(TPointerToMemberFunction pointer)
        -> typename std::enable_if<std::is_member_function_pointer<TPointerToMemberFunction>::value>::type
    {
        registerFunctionImpl(TOSTRING_FUNCTION_NAME, std::mem_fn(pointer), tag<TPointerToMemberFunction>{});
    }

    template<typename TFunctionType, typename TType>
    void registerToStringFunction(TType fn)
    {
        static_assert(std::is_member_function_pointer<TFunctionType>::value, "registerFunction must take a member function pointer type as template parameter");
        registerFunctionImpl(TOSTRING_FUNCTION_NAME, std::move(fn), tag<TFunctionType>{});
    }

    template<typename TObject, typename TFunctionType, typename TType>
    void registerToStringFunction(TType fn)
       {
        static_assert(std::is_function<TFunctionType>::value, "registerFunction must take a function type as template parameter");
        registerFunctionImpl(TOSTRING_FUNCTION_NAME, std::move(fn), tag<TObject>{}, tag<TFunctionType>{});
    }

    /**
     * Inverse operation of registerFunction
     * @tparam TType Type whose function belongs to
     */
    template<typename TType>
    void unregisterFunction(const std::string& /*functionName*/)
    {
        lua_pushlightuserdata(mState, const_cast<std::type_info*>(&typeid(TType)));
        lua_pushnil(mState);
        lua_settable(mState, LUA_REGISTRYINDEX);
        checkTypeRegistration(mState, &typeid(TType));
        
        lua_pushlightuserdata(mState, const_cast<std::type_info*>(&typeid(TType*)));
        lua_pushnil(mState);
        lua_settable(mState, LUA_REGISTRYINDEX);
        checkTypeRegistration(mState, &typeid(TType*));
        
        lua_pushlightuserdata(mState, const_cast<std::type_info*>(&typeid(std::shared_ptr<TType>)));
        lua_pushnil(mState);
        lua_settable(mState, LUA_REGISTRYINDEX);
        checkTypeRegistration(mState, &typeid(std::shared_ptr<TType>));
    }
    
    /**
     * Registers a member variable
     * This is the version "registerMember(name, &Foo::member)"
     */
    template<typename TObject, typename TVarType>
    void registerMember(const std::string& name, TVarType TObject::*member)
    {
        // implementation simply calls the custom member with getter and setter
        const auto getter = [=](const TObject& obj) -> TVarType { return obj.*member; };
        const auto setter = [=](TObject& obj, const TVarType& value) { obj.*member = value; };
        registerMember<TVarType (TObject::*)>(name, getter, setter);
    }

    /**
     * Registers a member variable
     * This is the version "registerMember<Foo, int>(name, getter, setter)"
     * @tparam TObject       Type to register the member to
     * @tparam TVarType      Type of the member
     * @param name           Name of the member to register
     * @param readFunction   Function of type "TVarType (const TObject&)"
     * @param writeFunction_  Function of type "void (TObject&, const TVarType&)"
     */
    template<typename TObject, typename TVarType, typename TReadFunction, typename TWriteFunction>
    void registerMember(const std::string& name, TReadFunction readFunction, TWriteFunction writeFunction_)
    {
        registerMemberImpl<TObject,TVarType>(name, std::move(readFunction), std::move(writeFunction_));
    }

    /**
     * Registers a member variable
     * This is the version "registerMember<int (Foo::*)>(name, getter, setter)"
     * @tparam TMemberType   Pointer to member object representing the type
     * @param name           Name of the member to register
     * @param readFunction   Function of type "TVarType (const TObject&)"
     * @param writeFunction_  Function of type "void (TObject&, const TVarType&)"
     */
    template<typename TMemberType, typename TReadFunction, typename TWriteFunction>
    void registerMember(const std::string& name, TReadFunction readFunction, TWriteFunction writeFunction_)
    {
        static_assert(std::is_member_object_pointer<TMemberType>::value, "registerMember must take a member object pointer type as template parameter");
        registerMemberImpl(tag<TMemberType>{}, name, std::move(readFunction), std::move(writeFunction_));
    }

    /**
     * Registers a non-modifiable member variable
     * This is the version "registerMember<Foo, int>(name, getter)"
     * @tparam TObject       Type to register the member to
     * @tparam TVarType      Type of the member
     * @param name           Name of the member to register
     * @param readFunction   Function of type "TVarType (const TObject&)"
     */
    template<typename TObject, typename TVarType, typename TReadFunction>
    void registerMember(const std::string& name, TReadFunction readFunction)
    {
        registerMemberImpl<TObject,TVarType>(name, std::move(readFunction));
    }

    /**
     * Registers a non-modifiable member variable
     * This is the version "registerMember<int (Foo::*)>(name, getter)"
     * @tparam TMemberType   Pointer to member object representing the type
     * @param name           Name of the member to register
     * @param readFunction   Function of type "TVarType (const TObject&)"
     */
    template<typename TMemberType, typename TReadFunction>
    void registerMember(const std::string& name, TReadFunction readFunction)
    {
        static_assert(std::is_member_object_pointer<TMemberType>::value, "registerMember must take a member object pointer type as template parameter");
        registerMemberImpl(tag<TMemberType>{}, name, std::move(readFunction));
    }

    /**
     * Registers a dynamic member variable
     * This is the version "registerMember<Foo, int>(getter, setter)"
     * @tparam TObject       Type to register the member to
     * @tparam TVarType      Type of the member
     * @param readFunction   Function of type "TVarType (const TObject&, const std::string&)"
     * @param writeFunction_  Function of type "void (TObject&, const std::string&, const TVarType&)"
     */
    template<typename TObject, typename TVarType, typename TReadFunction, typename TWriteFunction>
    void registerMember(TReadFunction readFunction, TWriteFunction writeFunction_)
    {
        registerMemberImpl<TObject,TVarType>(std::move(readFunction), std::move(writeFunction_));
    }

    /**
     * Registers a dynamic member variable
     * This is the version "registerMember<int (Foo::*)>(getter, setter)"
     * @tparam TMemberType   Pointer to member object representing the type
     * @param readFunction   Function of type "TVarType (const TObject&, const std::string&)"
     * @param writeFunction_  Function of type "void (TObject&, const std::string&, const TVarType&)"
     */
    template<typename TMemberType, typename TReadFunction, typename TWriteFunction>
    void registerMember(TReadFunction readFunction, TWriteFunction writeFunction_)
    {
        static_assert(std::is_member_object_pointer<TMemberType>::value, "registerMember must take a member object pointer type as template parameter");
        registerMemberImpl(tag<TMemberType>{}, std::move(readFunction), std::move(writeFunction_));
    }

    /**
     * Registers a dynamic non-modifiable member variable
     * This is the version "registerMember<Foo, int>(getter)"
     * @tparam TObject       Type to register the member to
     * @tparam TVarType      Type of the member
     * @param readFunction   Function of type "TVarType (const TObject&, const std::string&)"
     */
    template<typename TObject, typename TVarType, typename TReadFunction>
    void registerMember(TReadFunction readFunction)
    {
        registerMemberImpl<TObject, TVarType>(std::move(readFunction));
    }

    /**
     * Registers a dynamic non-modifiable member variable
     * This is the version "registerMember<int (Foo::*)>(getter)"
     * @tparam TMemberType   Pointer to member object representing the type
     * @param readFunction   Function of type "TVarType (const TObject&, const std::string&)"
     */
    template<typename TMemberType, typename TReadFunction>
    void registerMember(TReadFunction readFunction)
    {
        static_assert(std::is_member_object_pointer<TMemberType>::value, "registerMember must take a member object pointer type as template parameter");
        registerMemberImpl(tag<TMemberType>{}, std::move(readFunction));
    }

    /**
     * Creates a new thread
     * A Lua thread is not really a thread, but rather an "execution stack".
     * You can destroy the thread by calling destroyThread
     * @sa destroyThread
     */
    auto createThread()
        -> ThreadID
    {
        ThreadID result;

        result.state = lua_newthread(mState);
        result.threadInRegistry = std::unique_ptr<ValueInRegistry>(new ValueInRegistry(mState));
        lua_pop(mState, 1);

        return result;
    }

    /**
     * Destroys a thread created with createThread
     * @sa createThread
     */
    void destroyThread(ThreadID& id)
    {
        id.threadInRegistry.reset();
    }
    
    /**
     * Reads the content of a Lua variable
     * 
     * @tparam TType                Type requested for the read
     * @throw WrongTypeException    When the variable is not convertible to the requested type
     * @sa writeVariable
     *
     * Readable types are all types accepted by writeVariable except nullptr, std::unique_ptr and function pointers
     * Additionally supported:
     *  - LuaFunctionCaller<FunctionType>, which is an alternative to std::function
     *  - references to custom objects, in which case it will return the object in-place
     *
     * After the variable name, you can add other parameters.
     * If the variable is an array, it will instead get the element of that array whose offset is the second parameter.
     * Same applies for third, fourth, etc. parameters.
    */
    template<typename TType, typename... TTypes>
    TType readVariable(const std::string& name, TTypes&&... elements) const
    {
        lua_getglobal(mState, name.c_str());
        lookIntoStackTop(mState, std::forward<TTypes>(elements)...);
        return readTopAndPop<TType>(mState, PushedObject{mState, 1});
    }
    
    /**
     * @sa readVariable
     */
    template<typename TType, typename... TTypes>
    TType readVariable(const char* name, TTypes&&... elements) const
    {
        lua_getglobal(mState, name);
        lookIntoStackTop(mState, std::forward<TTypes>(elements)...);
        return readTopAndPop<TType>(mState, PushedObject{mState, 1});
    }
    
    /**
     * @sa readVariable
     */
    template<typename TType, typename... TTypes>
    TType readVariable(const ThreadID& thread, const std::string& name, TTypes&&... elements) const
    {
        lua_getglobal(thread.state, name.c_str());
        lookIntoStackTop(thread.state, std::forward<TTypes>(elements)...);
        return readTopAndPop<TType>(thread.state, PushedObject{thread.state, 1});
    }
    
    /**
     * @sa readVariable
     */
    template<typename TType, typename... TTypes>
    TType readVariable(const ThreadID& thread, const char* name, TTypes&&... elements) const
    {
        lua_getglobal(thread.state, name);
        lookIntoStackTop(thread.state, std::forward<TTypes>(elements)...);
        return readTopAndPop<TType>(thread.state, PushedObject{thread.state, 1});
    }
    
    /**
     * Changes the content of a Lua variable
     * 
     * Accepted values are:
     * - all base types (char, short, int, float, double, bool)
     * - std::string
     * - enums
     * - std::vector<>
     * - std::vector<std::pair<>>, std::map<> and std::unordered_map<> (the key and value must also be accepted values)
     * - std::function<> (all parameters must be accepted values, and return type must be either an accepted value for readVariable or a tuple)
     * - std::shared_ptr<> (std::unique_ptr<> are converted to std::shared_ptr<>)
     * - nullptr (writes nil)
     * - any object
     *
     * All objects are passed by copy and destroyed by the garbage collector if necessary.
     */
    template<typename... TData>
    void writeVariable(TData&&... data) noexcept {
        static_assert(sizeof...(TData) >= 2, "You must pass at least a variable name and a value to writeVariable");
        typedef typename std::decay<typename std::tuple_element<sizeof...(TData) - 1,std::tuple<TData...>>::type>::type
            RealDataType;
        static_assert(!std::is_same<typename Tupleizer<RealDataType>::type,RealDataType>::value, "Error: you can't use LuaContext::writeVariable with a tuple");
        
        setTable<RealDataType>(mState, Globals, std::forward<TData>(data)...);
    }
    
    /**
     * Equivalent to writeVariable(varName, ..., std::function<TFunctionType>(data));
     * This version is more efficient than writeVariable if you want to write functions
     */
    template<typename TFunctionType, typename... TData>
    void writeFunction(TData&&... data) noexcept {
        static_assert(sizeof...(TData) >= 2, "You must pass at least a variable name and a value to writeFunction");
        
        setTable<TFunctionType>(mState, Globals, std::forward<TData>(data)...);
    }

    /**
     * Same as the other writeFunction, except that the template parameter is automatically detected
     * This only works if the data is either a native function pointer, or contains one operator() (this is the case for lambdas)
     */
    template<typename... TData>
    void writeFunction(TData&&... data) noexcept {
        static_assert(sizeof...(TData) >= 2, "You must pass at least a variable name and a value to writeFunction");
        typedef typename std::decay<typename std::tuple_element<sizeof...(TData) - 1,std::tuple<TData...>>::type>::type
            RealDataType;
        typedef typename FunctionTypeDetector<RealDataType>::type
            DetectedFunctionType;
        
        return writeFunction<DetectedFunctionType>(std::forward<TData>(data)...);
    }
    

private:
    // the state is the most important variable in the class since it is our interface with Lua
    //  - registered members and functions are stored in tables at offset &typeid(type) of the registry
    //    each table has its getter functions at offset 0, getter members at offset 1, default getter at offset 2
    //    offset 3 is unused, setter members at offset 4, default setter at offset 5
    lua_State*                  mState;

    
    /**************************************************/
    /*                 PUSH OBJECT                    */
    /**************************************************/
    struct PushedObject {
        PushedObject(lua_State* state_, int num_ = 1) : state(state_), num(num_) {}
        ~PushedObject() { assert(lua_gettop(state) >= num); if (num >= 1) lua_pop(state, num); }
        
        PushedObject& operator=(const PushedObject&) = delete;
        PushedObject(const PushedObject&) = delete;
        PushedObject& operator=(PushedObject&& other) { std::swap(state, other.state); std::swap(num, other.num); return *this; }
        PushedObject(PushedObject&& other) : state(other.state), num(other.num) { other.num = 0; }

        PushedObject operator+(PushedObject&& other) && { PushedObject obj(state, num + other.num); num = 0; other.num = 0; return obj; }
        void operator+=(PushedObject&& other) { assert(state == other.state); num += other.num; other.num = 0; }
        
        auto getState() const -> lua_State* { return state; }
        auto getNum() const -> int { return num; }

        int release() { const auto n = num; num = 0; return n; }
        void pop() { if (num >= 1) lua_pop(state, num); num = 0; }
        void pop(int n) { assert(num >= n); lua_pop(state, n); num -= n; }

    private:
        lua_State* state;
        int num = 0;
    };
    

    /**************************************************/
    /*                     MISC                       */
    /**************************************************/
    // type used as a tag
    template<typename T>
    struct tag {};

    // tag for "the registry"
    enum RegistryTag { Registry };
    
    // this function takes a value representing the offset to look into
    // it will look into the top element of the stack and replace the element by its content at the given index
    template<typename OffsetType1, typename... OffsetTypeOthers>
    static void lookIntoStackTop(lua_State* state, OffsetType1&& offset1, OffsetTypeOthers&&... offsetOthers) {
        static_assert(Pusher<typename std::decay<OffsetType1>::type>::minSize == 1 && Pusher<typename std::decay<OffsetType1>::type>::maxSize == 1, "Impossible to have a multiple-values index");
        auto p1 = Pusher<typename std::decay<OffsetType1>::type>::push(state, offset1);
        lua_gettable(state, -2);
        lua_remove(state, -2);
        p1.release();

        lookIntoStackTop(state, std::forward<OffsetTypeOthers>(offsetOthers)...);
    }

    template<typename... OffsetTypeOthers>
    static void lookIntoStackTop(lua_State* state, Metatable_t, OffsetTypeOthers&&... offsetOthers) {
        lua_getmetatable(state, -1);
        lua_remove(state, -2);

        lookIntoStackTop(state, std::forward<OffsetTypeOthers>(offsetOthers)...);
    }
    
    static void lookIntoStackTop(lua_State*) {
    }
    
    // equivalent of lua_settable with t[k]=n, where t is the value at the index in the template parameter, k is the second parameter, n is the last parameter, and n is pushed by the function in the first parameter
    // if there are more than 3 parameters, parameters 3 to n-1 are considered as sub-indices into the array
    // the dataPusher MUST push only one thing on the stack
    // TTableIndex must be either LUA_REGISTRYINDEX, LUA_GLOBALSINDEX, LUA_ENVINDEX, or the position of the element on the stack
    template<typename TDataType, typename TIndex, typename TData>
    static void setTable(lua_State* state, const PushedObject&, TIndex&& index, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TIndex>::type>::minSize == 1 && Pusher<typename std::decay<TIndex>::type>::maxSize == 1, "Impossible to have a multiple-values index");
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");
        
        auto p1 = Pusher<typename std::decay<TIndex>::type>::push(state, index);
        auto p2 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));

        lua_settable(state, -3);
        p1.release();
        p2.release();
    }

    template<typename TDataType, typename TData>
    static void setTable(lua_State* state, const PushedObject&, const std::string& index, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");

        auto p1 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));
        lua_setfield(state, -2, index.c_str());
        p1.release();
    }

    template<typename TDataType, typename TData>
    static void setTable(lua_State* state, const PushedObject&, const char* index, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");
        
        auto p1 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));
        lua_setfield(state, -2, index);
        p1.release();
    }

    template<typename TDataType, typename TData>
    static void setTable(lua_State* state, const PushedObject&, Metatable_t, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");
        
        auto p1 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));
        lua_setmetatable(state, -2);
        p1.release();
    }

    template<typename TDataType, typename TIndex1, typename TIndex2, typename TIndex3, typename... TIndices>
    static auto setTable(lua_State* state, PushedObject&, TIndex1&& index1, TIndex2&& index2, TIndex3&& index3, TIndices&&... indices) noexcept
        -> typename std::enable_if<!std::is_same<typename std::decay<TIndex1>::type, Metatable_t>::value>::type
    {
        static_assert(Pusher<typename std::decay<TIndex1>::type>::minSize == 1 && Pusher<typename std::decay<TIndex1>::type>::maxSize == 1, "Impossible to have a multiple-values index");
        
        auto p1 = Pusher<typename std::decay<TIndex1>::type>::push(state, std::forward<TIndex1>(index1));
        lua_gettable(state, -2);

        setTable<TDataType>(state, std::move(p1), std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);
    }

    template<typename TDataType, typename TIndex1, typename TIndex2, typename TIndex3, typename... TIndices>
    static auto setTable(lua_State* state, PushedObject&& pushedTable, TIndex1&& index1, TIndex2&& index2, TIndex3&& index3, TIndices&&... indices) noexcept
        -> typename std::enable_if<!std::is_same<typename std::decay<TIndex1>::type, Metatable_t>::value>::type
    {
        static_assert(Pusher<typename std::decay<TIndex1>::type>::minSize == 1 && Pusher<typename std::decay<TIndex1>::type>::maxSize == 1, "Impossible to have a multiple-values index");
        
        auto p1 = Pusher<typename std::decay<TIndex1>::type>::push(state, std::forward<TIndex1>(index1)) + std::move(pushedTable);
        lua_gettable(state, -2);

        setTable<TDataType>(state, std::move(p1), std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);
    }

    template<typename TDataType, typename TIndex2, typename TIndex3, typename... TIndices>
    static void setTable(lua_State* state, PushedObject& pushedObject, Metatable_t, TIndex2&& index2, TIndex3&& index3, TIndices&&... indices) noexcept
    {
        if (lua_getmetatable(state, -1) == 0)
        {
            lua_newtable(state);
            PushedObject p1{state, 1};

            setTable<TDataType>(state, p1, std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);

            lua_setmetatable(state, -2);
            p1.release();
        }
        else
        {
            setTable<TDataType>(state, pushedObject, std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);
        }
    }

    template<typename TDataType, typename TIndex2, typename TIndex3, typename... TIndices>
    static void setTable(lua_State* state, PushedObject&& pushedObject, Metatable_t, TIndex2&& index2, TIndex3&& index3, TIndices&&... indices) noexcept
    {
        if (lua_getmetatable(state, -1) == 0)
        {
            lua_newtable(state);
            PushedObject p1{state, 1};

            setTable<TDataType>(state, p1, std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);

            lua_setmetatable(state, -2);
            p1.release();
        }
        else
        {
            setTable<TDataType>(state, std::move(pushedObject), std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);
        }
    }

    template<typename TDataType, typename TIndex, typename TData>
    static void setTable(lua_State* state, RegistryTag, TIndex&& index, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TIndex>::type>::minSize == 1 && Pusher<typename std::decay<TIndex>::type>::maxSize == 1, "Impossible to have a multiple-values index");
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");
        
        auto p1 = Pusher<typename std::decay<TIndex>::type>::push(state, index);
        auto p2 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));

        lua_settable(state, LUA_REGISTRYINDEX);
        p1.release();
        p2.release();
    }

    template<typename TDataType, typename TData>
    static void setTable(lua_State* state, RegistryTag, const std::string& index, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");

        auto p1 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));
        lua_setfield(state, LUA_REGISTRYINDEX, index.c_str());
        p1.release();
    }

    template<typename TDataType, typename TData>
    static void setTable(lua_State* state, RegistryTag, const char* index, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");

        auto p1 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));
        lua_setfield(state, LUA_REGISTRYINDEX, index);
        p1.release();
    }

    template<typename TDataType, typename TIndex1, typename TIndex2, typename TIndex3, typename... TIndices>
    static void setTable(lua_State* state, RegistryTag, TIndex1&& index1, TIndex2&& index2, TIndex3&& index3, TIndices&&... indices) noexcept
    {
        static_assert(Pusher<typename std::decay<TIndex1>::type>::minSize == 1 && Pusher<typename std::decay<TIndex1>::type>::maxSize == 1, "Impossible to have a multiple-values index");
        
        auto p1 = Pusher<typename std::decay<TIndex1>::type>::push(state, std::forward<TIndex1>(index1));
        lua_gettable(state, LUA_REGISTRYINDEX);

        setTable<TDataType>(state, std::move(p1), std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);
    }

    template<typename TDataType, typename TIndex, typename TData>
    static void setTable(lua_State* state, Globals_t, TIndex&& index, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TIndex>::type>::minSize == 1 && Pusher<typename std::decay<TIndex>::type>::maxSize == 1, "Impossible to have a multiple-values index");
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");
        
        
#       if LUA_VERSION_NUM >= 502

            lua_pushglobaltable(state);
            PushedObject p3{state, 1};
            auto p1 = Pusher<typename std::decay<TIndex>::type>::push(state, index);
            auto p2 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));
            lua_settable(state, -3);

#       else
            
            auto p1 = Pusher<typename std::decay<TIndex>::type>::push(state, index);
            auto p2 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));
            lua_settable(state, LUA_GLOBALSINDEX);

#       endif

        p1.release();
        p2.release();
    }

    template<typename TDataType, typename TData>
    static void setTable(lua_State* state, Globals_t, const std::string& index, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");

        auto p1 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));
        lua_setglobal(state, index.c_str());
        p1.release();
    }

    template<typename TDataType, typename TData>
    static void setTable(lua_State* state, Globals_t, const char* index, TData&& data) noexcept
    {
        static_assert(Pusher<typename std::decay<TDataType>::type>::minSize == 1 && Pusher<typename std::decay<TDataType>::type>::maxSize == 1, "Impossible to have a multiple-values data");

        auto p1 = Pusher<typename std::decay<TDataType>::type>::push(state, std::forward<TData>(data));
        lua_setglobal(state, index);
        p1.release();
    }

    template<typename TDataType, typename TIndex1, typename TIndex2, typename TIndex3, typename... TIndices>
    static void setTable(lua_State* state, Globals_t, TIndex1&& index1, TIndex2&& index2, TIndex3&& index3, TIndices&&... indices) noexcept
    {
        static_assert(Pusher<typename std::decay<TIndex1>::type>::minSize == 1 && Pusher<typename std::decay<TIndex1>::type>::maxSize == 1, "Impossible to have a multiple-values index");
        
#       if LUA_VERSION_NUM >= 502

            lua_pushglobaltable(state);
            auto p1 = Pusher<typename std::decay<TIndex1>::type>::push(state, std::forward<TIndex1>(index1)) + PushedObject{state, 1};
            lua_gettable(state, -2);

#       else

            auto p1 = Pusher<typename std::decay<TIndex1>::type>::push(state, std::forward<TIndex1>(index1));
            lua_gettable(state, LUA_GLOBALSINDEX);

#       endif

        setTable<TDataType>(state, std::move(p1), std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);
    }

    // TODO: g++ reports "ambiguous overload"
    /*template<typename TDataType, typename TIndex2, typename TIndex3, typename... TIndices>
    static void setTable(lua_State* state, Globals_t, const char* index, TIndex2&& index2, TIndex3&& index3, TIndices&&... indices) noexcept
    {
        lua_getglobal(state, index);
        PushedObject p1{state, 1};

        setTable<TDataType>(state, std::move(p1), std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);
    }

    template<typename TDataType, typename TIndex2, typename TIndex3, typename... TIndices>
    static void setTable(lua_State* state, Globals_t, const std::string& index, TIndex2&& index2, TIndex3&& index3, TIndices&&... indices) noexcept
    {
        lua_getglobal(state, index.c_str());
        PushedObject p1{state, 1};

        setTable<TDataType>(state, std::move(p1), std::forward<TIndex2>(index2), std::forward<TIndex3>(index3), std::forward<TIndices>(indices)...);
    }*/

    // simple function that reads the "nb" first top elements of the stack, pops them, and returns the value
    // warning: first parameter is the number of parameters, not the parameter index
    // if read generates an exception, stack is poped anyway
    template<typename TReturnType>
    static auto readTopAndPop(lua_State* state, PushedObject object)
        -> TReturnType
    {
        auto val = Reader<typename std::decay<TReturnType>::type>::read(state, -object.getNum());
        if (!val.is_initialized())
            throw WrongTypeException{lua_typename(state, lua_type(state, -object.getNum())), typeid(TReturnType)};
        return val.get();
    }

    // checks that the offsets for a type's registrations are set in the registry
    static void checkTypeRegistration(lua_State* state, const std::type_info* type)
    {
        lua_pushlightuserdata(state, const_cast<std::type_info*>(type));
        lua_gettable(state, LUA_REGISTRYINDEX);
        if (!lua_isnil(state, -1)) {
            lua_pop(state, 1);
            return;
        }
        lua_pop(state, 1);

        lua_pushlightuserdata(state, const_cast<std::type_info*>(type));
        lua_newtable(state);

        lua_pushinteger(state, 0);
        lua_newtable(state);
        lua_settable(state, -3);

        lua_pushinteger(state, 1);
        lua_newtable(state);
        lua_settable(state, -3);

        lua_pushinteger(state, 3);
        lua_newtable(state);
        lua_settable(state, -3);

        lua_pushinteger(state, 4);
        lua_newtable(state);
        lua_settable(state, -3);

        lua_settable(state, LUA_REGISTRYINDEX);
    }

    // 
#   ifdef _MSC_VER
        __declspec(noreturn)
#   else
        [[noreturn]]
#   endif
    static void luaError(lua_State* state)
    {
        lua_error(state);
        assert(false);
        std::terminate();   // removes compilation warning
    }
    

    /**************************************************/
    /*            FUNCTIONS REGISTRATION              */
    /**************************************************/
    // the "registerFunction" public functions call this one
    template<typename TFunctionType, typename TRetValue, typename TObject, typename... TOtherParams>
    void registerFunctionImpl(const std::string& functionName, TFunctionType function, tag<TObject>, tag<TRetValue (TOtherParams...)>)
    {
        static_assert(std::is_class<TObject>::value || std::is_pointer<TObject>::value || std::is_union<TObject>::value , "registerFunction can only be used for a class a union or a pointer");

        checkTypeRegistration(mState, &typeid(TObject));
        setTable<TRetValue(TObject&, TOtherParams...)>(mState, Registry, &typeid(TObject), 0, functionName, function);
        
        checkTypeRegistration(mState, &typeid(TObject*));
        setTable<TRetValue(TObject*, TOtherParams...)>(mState, Registry, &typeid(TObject*), 0, functionName, [=](TObject* obj, TOtherParams... rest) { assert(obj); return function(*obj, std::forward<TOtherParams>(rest)...); });
        
        checkTypeRegistration(mState, &typeid(std::shared_ptr<TObject>));
        setTable<TRetValue(std::shared_ptr<TObject>, TOtherParams...)>(mState, Registry, &typeid(std::shared_ptr<TObject>), 0, functionName, [=](const std::shared_ptr<TObject>& obj, TOtherParams... rest) { assert(obj); return function(*obj, std::forward<TOtherParams>(rest)...); });
    }
    
    template<typename TFunctionType, typename TRetValue, typename TObject, typename... TOtherParams>
    void registerFunctionImpl(const std::string& functionName, TFunctionType function, tag<const TObject>, tag<TRetValue (TOtherParams...)> fTypeTag)
    {
        registerFunctionImpl(functionName, function, tag<TObject>{}, fTypeTag);

        checkTypeRegistration(mState, &typeid(TObject const*));
        setTable<TRetValue(TObject const*, TOtherParams...)>(mState, Registry, &typeid(TObject const*), 0, functionName, [=](TObject const* obj, TOtherParams... rest) { assert(obj); return function(*obj, std::forward<TOtherParams>(rest)...); });
        
        checkTypeRegistration(mState, &typeid(std::shared_ptr<TObject const>));
        setTable<TRetValue(std::shared_ptr<TObject const>, TOtherParams...)>(mState, Registry, &typeid(std::shared_ptr<TObject const>), 0, functionName, [=](const std::shared_ptr<TObject const>& obj, TOtherParams... rest) { assert(obj); return function(*obj, std::forward<TOtherParams>(rest)...); });
    }

    template<typename TFunctionType, typename TRetValue, typename TObject, typename... TOtherParams>
    void registerFunctionImpl(const std::string& functionName, TFunctionType function, tag<TRetValue (TObject::*)(TOtherParams...)>)
    {
        registerFunctionImpl(functionName, std::move(function), tag<TObject>{}, tag<TRetValue (TOtherParams...)>{});
    }

    template<typename TFunctionType, typename TRetValue, typename TObject, typename... TOtherParams>
    void registerFunctionImpl(const std::string& functionName, TFunctionType function, tag<TRetValue (TObject::*)(TOtherParams...) const>)
    {
        registerFunctionImpl(functionName, std::move(function), tag<const TObject>{}, tag<TRetValue (TOtherParams...)>{});
    }

    template<typename TFunctionType, typename TRetValue, typename TObject, typename... TOtherParams>
    void registerFunctionImpl(const std::string& functionName, TFunctionType function, tag<TRetValue (TObject::*)(TOtherParams...) volatile>)
    {
        registerFunctionImpl(functionName, std::move(function), tag<TObject>{}, tag<TRetValue (TOtherParams...)>{});
    }

    template<typename TFunctionType, typename TRetValue, typename TObject, typename... TOtherParams>
    void registerFunctionImpl(const std::string& functionName, TFunctionType function, tag<TRetValue (TObject::*)(TOtherParams...) const volatile>)
    {
        registerFunctionImpl(functionName, std::move(function), tag<const TObject>{}, tag<TRetValue (TOtherParams...)>{});
    }

    // the "registerMember" public functions call this one
    template<typename TObject, typename TVarType, typename TReadFunction>
    void registerMemberImpl(const std::string& name, TReadFunction readFunction)
    {
        static_assert(std::is_class<TObject>::value || std::is_pointer<TObject>::value, "registerMember can only be called on a class or a pointer");
        
        checkTypeRegistration(mState, &typeid(TObject));
        setTable<TVarType (TObject&)>(mState, Registry, &typeid(TObject), 1, name, [readFunction](TObject const& object) {
            return readFunction(object);
        });
        
        checkTypeRegistration(mState, &typeid(TObject*));
        setTable<TVarType (TObject*)>(mState, Registry, &typeid(TObject*), 1, name, [readFunction](TObject const* object) {
            assert(object);
            return readFunction(*object);
        });
        
        checkTypeRegistration(mState, &typeid(TObject const*));
        setTable<TVarType (TObject const*)>(mState, Registry, &typeid(TObject const*), 1, name, [readFunction](TObject const* object) {
            assert(object);
            return readFunction(*object);
        });
        
        checkTypeRegistration(mState, &typeid(std::shared_ptr<TObject>));
        setTable<TVarType (std::shared_ptr<TObject>)>(mState, Registry, &typeid(std::shared_ptr<TObject>), 1, name, [readFunction](const std::shared_ptr<TObject>& object) {
            assert(object);
            return readFunction(*object);
        });
        
        checkTypeRegistration(mState, &typeid(std::shared_ptr<TObject const>));
        setTable<TVarType (std::shared_ptr<TObject const>)>(mState, Registry, &typeid(std::shared_ptr<TObject const>), 1, name, [readFunction](const std::shared_ptr<TObject const>& object) {
            assert(object);
            return readFunction(*object);
        });
    }

    template<typename TObject, typename TVarType, typename TReadFunction, typename TWriteFunction>
    void registerMemberImpl(const std::string& name, TReadFunction readFunction, TWriteFunction writeFunction_)
    {
        registerMemberImpl<TObject,TVarType>(name, readFunction);

        setTable<void (TObject&, TVarType)>(mState, Registry, &typeid(TObject), 4, name, [writeFunction_](TObject& object, const TVarType& value) {
            writeFunction_(object, value);
        });
        
        setTable<void (TObject*, TVarType)>(mState, Registry, &typeid(TObject*), 4, name, [writeFunction_](TObject* object, const TVarType& value) {
            assert(object);
            writeFunction_(*object, value);
        });
        
        setTable<void (std::shared_ptr<TObject>, TVarType)>(mState, Registry, &typeid(std::shared_ptr<TObject>), 4, name, [writeFunction_](std::shared_ptr<TObject> object, const TVarType& value) {
            assert(object);
            writeFunction_(*object, value);
        });
    }

    template<typename TObject, typename TVarType, typename TReadFunction, typename TWriteFunction>
    void registerMemberImpl(tag<TVarType (TObject::*)>, const std::string& name, TReadFunction readFunction, TWriteFunction writeFunction_)
    {
        registerMemberImpl<TObject,TVarType>(name, std::move(readFunction), std::move(writeFunction_));
    }

    template<typename TObject, typename TVarType, typename TReadFunction>
    void registerMemberImpl(tag<TVarType(TObject::*)>, const std::string& name, TReadFunction readFunction)
    {
        registerMemberImpl<TObject, TVarType>(name, std::move(readFunction));
    }

    // the "registerMember" public functions call this one
    template<typename TObject, typename TVarType, typename TReadFunction>
    void registerMemberImpl(TReadFunction readFunction)
    {
        checkTypeRegistration(mState, &typeid(TObject));
        setTable<TVarType (TObject const&, std::string)>(mState, Registry, &typeid(TObject), 2, [readFunction](TObject const& object, const std::string& name) {
            return readFunction(object, name);
        });
        
        checkTypeRegistration(mState, &typeid(TObject*));
        setTable<TVarType (TObject*, std::string)>(mState, Registry, &typeid(TObject*), 2, [readFunction](TObject const* object, const std::string& name) {
            assert(object);
            return readFunction(*object, name);
        });
        
        checkTypeRegistration(mState, &typeid(TObject const*));
        setTable<TVarType (TObject const*, std::string)>(mState, Registry, &typeid(TObject const*), 2, [readFunction](TObject const* object, const std::string& name) {
            assert(object);
            return readFunction(*object, name);
        });
        
        checkTypeRegistration(mState, &typeid(std::shared_ptr<TObject>));
        setTable<TVarType (std::shared_ptr<TObject>, std::string)>(mState, Registry, &typeid(std::shared_ptr<TObject>), 2, [readFunction](const std::shared_ptr<TObject>& object, const std::string& name) {
            assert(object);
            return readFunction(*object, name);
        });
        
        checkTypeRegistration(mState, &typeid(std::shared_ptr<TObject const>));
        setTable<TVarType (std::shared_ptr<TObject const>, std::string)>(mState, Registry, &typeid(std::shared_ptr<TObject const>), 2, [readFunction](const std::shared_ptr<TObject const>& object, const std::string& name) {
            assert(object);
            return readFunction(*object, name);
        });
    }

    template<typename TObject, typename TVarType, typename TReadFunction, typename TWriteFunction>
    void registerMemberImpl(TReadFunction readFunction, TWriteFunction writeFunction_)
    {
        registerMemberImpl<TObject,TVarType>(readFunction);

        setTable<void (TObject&, std::string, TVarType)>(mState, Registry, &typeid(TObject), 5, [writeFunction_](TObject& object, const std::string& name, const TVarType& value) {
            writeFunction_(object, name, value);
        });
        
        setTable<void (TObject*, std::string, TVarType)>(mState, Registry, &typeid(TObject*), 2, [writeFunction_](TObject* object, const std::string& name, const TVarType& value) {
            assert(object);
            writeFunction_(*object, name, value);
        });
        
        setTable<void (std::shared_ptr<TObject>, std::string, TVarType)>(mState, Registry, &typeid(std::shared_ptr<TObject>), 2, [writeFunction_](const std::shared_ptr<TObject>& object, const std::string& name, const TVarType& value) {
            assert(object);
            writeFunction_(*object, name, value);
        });
    }

    template<typename TObject, typename TVarType, typename TReadFunction, typename TWriteFunction>
    void registerMemberImpl(tag<TVarType (TObject::*)>, TReadFunction readFunction, TWriteFunction writeFunction_)
    {
        registerMemberImpl<TObject,TVarType>(std::move(readFunction), std::move(writeFunction_));
    }

    template<typename TObject, typename TVarType, typename TReadFunction>
    void registerMemberImpl(tag<TVarType(TObject::*)>, TReadFunction readFunction)
    {
        registerMemberImpl<TObject, TVarType>(std::move(readFunction));
    }
    

    /**************************************************/
    /*              LOADING AND CALLING               */
    /**************************************************/
    // this function loads data from the stream and pushes a function at the top of the stack
    // throws in case of syntax error
    static PushedObject load(lua_State* state, std::istream& code) {
        // since the lua_load function requires a static function, we use this structure
        // the Reader structure is at the same time an object storing an istream and a buffer,
        //   and a static function provider
        struct Reader {
            Reader(std::istream& str) : stream(str) {}
            std::istream&           stream;
            std::array<char,512>    buffer;

            // read function ; "data" must be an instance of Reader
            static const char* read(lua_State* /*l*/, void* data, size_t* size) {
                assert(size != nullptr);
                assert(data != nullptr);
                Reader& me = *static_cast<Reader*>(data);
                if (me.stream.eof())    { *size = 0; return nullptr; }

                me.stream.read(me.buffer.data(), me.buffer.size());
                *size = static_cast<size_t>(me.stream.gcount());    // gcount could return a value larger than a size_t, but its maximum is me.buffer.size() so there's no problem
                return me.buffer.data();
            }
        };

        // we create an instance of Reader, and we call lua_load
        Reader reader{code};
        const auto loadReturnValue = lua_load(state, &Reader::read, &reader, "chunk"
#           if LUA_VERSION_NUM >= 502
                , nullptr
#           endif
        );

        // now we have to check return value
        if (loadReturnValue != 0) {
            // there was an error during loading, an error message was pushed on the stack
            const std::string errorMsg = lua_tostring(state, -1);
            lua_pop(state, 1);
            if (loadReturnValue == LUA_ERRMEM)
                throw std::bad_alloc();
            else if (loadReturnValue == LUA_ERRSYNTAX)
                throw SyntaxErrorException{errorMsg};
            throw std::runtime_error("Error while calling lua_load: " + errorMsg);
        }

        return PushedObject{state, 1};
    }
    
    // this function loads data and pushes a function at the top of the stack
    // throws in case of syntax error
    static PushedObject load(lua_State* state, const char* code) {
        auto loadReturnValue = luaL_loadstring(state, code);

        // now we have to check return value
        if (loadReturnValue != 0) {
            // there was an error during loading, an error message was pushed on the stack
            const std::string errorMsg = lua_tostring(state, -1);
            lua_pop(state, 1);
            if (loadReturnValue == LUA_ERRMEM)
                throw std::bad_alloc();
            else if (loadReturnValue == LUA_ERRSYNTAX)
                throw SyntaxErrorException{errorMsg};
            throw std::runtime_error("Error while calling lua_load: " + errorMsg);
        }

        return PushedObject{state, 1};
    }

    // this function calls what is on the top of the stack and removes it (just like lua_call)
    // if an exception is triggered, the top of the stack will be removed anyway
    template<typename TReturnType, typename... TParameters>
    static auto call(lua_State* state, PushedObject toCall, TParameters&&... input)
        -> TReturnType
    {
        typedef typename Tupleizer<TReturnType>::type
            RealReturnType;
        
        // we push the parameters on the stack
        auto inArguments = Pusher<std::tuple<TParameters&&...>>::push(state, std::forward_as_tuple(std::forward<TParameters>(input)...));

        // 
        const int outArgumentsCount = std::tuple_size<RealReturnType>::value;
        auto outArguments = callRaw(state, std::move(toCall) + std::move(inArguments), outArgumentsCount);

        // pcall succeeded, we pop the returned values and return them
        return readTopAndPop<TReturnType>(state, std::move(outArguments));
    }
    
    static int gettraceback(lua_State* L) {
        lua_getglobal(L, "debug"); // stack: error, debug library
        lua_getfield(L, -1, "traceback"); // stack: error, debug library, debug.traceback function
        lua_remove(L, -2); // stack: error, debug.traceback function
        lua_pushstring(L, ""); // stack: error, debug.traceback, ""
        lua_pushinteger(L, 2); // stack: error, debug.traceback, "", 2
        lua_call(L, 2, 1); // stack: error, traceback
        lua_createtable(L, 2, 0); // stack: error, traceback, {}
        lua_insert(L, 1); // stack: {}, error, traceback
        lua_rawseti(L, 1, 2); // stack: {[2]=traceback}, error
        lua_rawseti(L, 1, 1); // stack: {[1]=error,[2]=traceback}
        return 1; // return the table
    }

    // this function just calls lua_pcall and checks for errors
    static PushedObject callRaw(lua_State* state, PushedObject functionAndArguments, const int outArguments)
    {
        // provide traceback handler
        int tbindex = lua_gettop(state) - (functionAndArguments.getNum() - 1);
        lua_pushcfunction(state, gettraceback);

        // move it back up, before our function and arguments
        lua_insert(state, tbindex);

        // calling pcall automatically pops the parameters and pushes output
        const auto pcallReturnValue = lua_pcall(state, functionAndArguments.getNum() - 1, outArguments, tbindex);
        functionAndArguments.release();

        lua_remove(state, tbindex); // remove traceback function


        // if pcall failed, analyzing the problem and throwing
        if (pcallReturnValue != 0) {

            // stack top: {error, traceback}
            lua_rawgeti(state, -1, 1); // stack top: {error, traceback}, error
            lua_rawgeti(state, -2, 2); // stack top: {error, traceback}, error, traceback
            lua_remove(state, -3); // stack top: error, traceback

            PushedObject traceBackRef{state, 1};
            const auto traceBack = readTopAndPop<std::string>(state, std::move(traceBackRef)); // stack top: error
            PushedObject errorCode{state, 1};

            // an error occurred during execution, either an error message or a std::exception_ptr was pushed on the stack
            if (pcallReturnValue == LUA_ERRMEM) {
                throw std::bad_alloc{};

            } else if (pcallReturnValue == LUA_ERRRUN) {
                if (lua_isstring(state, 1)) {
                    // the error is a string
                    const auto str = readTopAndPop<std::string>(state, std::move(errorCode));
                    throw ExecutionErrorException{str+traceBack};

                } else {
                    // an exception_ptr was pushed on the stack
                    // rethrowing it with an additional ExecutionErrorException
                    try {
                        if (const auto exp = readTopAndPop<std::exception_ptr>(state, std::move(errorCode))) {
                            std::rethrow_exception(exp);
                        }
                    } catch(const std::exception& e) {
                        std::throw_with_nested(ExecutionErrorException{std::string{"Exception thrown by a callback function: "} + e.what()});
                    } catch(...) {
                        std::throw_with_nested(ExecutionErrorException{"Exception thrown by a callback function called by Lua. "+traceBack});
                    }
                    throw ExecutionErrorException{"Unknown Lua error"};
                }
            }
        }

        return PushedObject{state, outArguments};
    }

    
    /**************************************************/
    /*                PUSH FUNCTIONS                  */
    /**************************************************/
    template<typename T>
    static PushedObject push(lua_State* state, T&& value)
    {
        return Pusher<typename std::decay<T>::type>::push(state, std::forward<T>(value));
    }

    // the Pusher structures allow you to push a value on the stack
    //  - static const int minSize : minimum size on the stack that the value can have
    //  - static const int maxSize : maximum size on the stack that the value can have
    //  - static int push(const LuaContext&, ValueType) : pushes the value on the stack and returns the size on the stack

    // implementation for custom objects
    template<typename TType, typename = void>
    struct Pusher {
        static const int minSize = 1;
        static const int maxSize = 1;

        template<typename TType2>
        static PushedObject push(lua_State* state, TType2&& value) noexcept {
            // this function is called when lua's garbage collector wants to destroy our object
            // we simply call its destructor
            const auto garbageCallbackFunction = [](lua_State* lua) -> int {
                assert(lua_gettop(lua) == 1);
                TType* ptr = static_cast<TType*>(lua_touserdata(lua, 1));
                assert(ptr);
                ptr->~TType();
                return 0;
            };

            // this function will be stored in __index in the metatable
            const auto indexFunction = [](lua_State* lua) -> int {
                try {
                    assert(lua_gettop(lua) == 2);
                    assert(lua_isuserdata(lua, 1));

                    // searching for a handler
                    lua_pushlightuserdata(lua, const_cast<std::type_info*>(&typeid(TType)));
                    lua_gettable(lua, LUA_REGISTRYINDEX);
                    assert(!lua_isnil(lua, -1));
                    
                    // looking into getter functions
                    lua_pushinteger(lua, 0);
                    lua_gettable(lua, -2);
                    lua_pushvalue(lua, 2);
                    lua_gettable(lua, -2);
                    if (!lua_isnil(lua, -1))
                        return 1;
                    lua_pop(lua, 2);
                    
                    // looking into getter members
                    lua_pushinteger(lua, 1);
                    lua_gettable(lua, -2);
                    lua_pushvalue(lua, 2);
                    lua_gettable(lua, -2);
                    if (!lua_isnil(lua, -1)) {
                        lua_pushvalue(lua, 1);
                        return callRaw(lua, PushedObject{lua, 2}, 1).release();
                    }
                    lua_pop(lua, 2);

                    // looking into default getter
                    lua_pushinteger(lua, 2);
                    lua_gettable(lua, -2);
                    if (lua_isnil(lua, -1))
                        return 1;
                    lua_pushvalue(lua, 1);
                    lua_pushvalue(lua, 2);
                    return callRaw(lua, PushedObject{lua, 3}, 1).release();

                } catch (...) {
                    Pusher<std::exception_ptr>::push(lua, std::current_exception()).release();
                    luaError(lua);
                }
            };

            // this function will be stored in __newindex in the metatable
            const auto newIndexFunction = [](lua_State* lua) -> int {
                try {
                    assert(lua_gettop(lua) == 3);
                    assert(lua_isuserdata(lua, 1));

                    // searching for a handler
                    lua_pushlightuserdata(lua, const_cast<std::type_info*>(&typeid(TType)));
                    lua_rawget(lua, LUA_REGISTRYINDEX);
                    assert(!lua_isnil(lua, -1));
                    
                    // looking into setter members
                    lua_pushinteger(lua, 4);
                    lua_rawget(lua, -2);
                    lua_pushvalue(lua, 2);
                    lua_rawget(lua, -2);
                    if (!lua_isnil(lua, -1)) {
                        lua_pushvalue(lua, 1);
                        lua_pushvalue(lua, 3);
                        callRaw(lua, PushedObject{lua, 3}, 0);
                        lua_pop(lua, 2);
                        return 0;
                    }
                    lua_pop(lua, 2);

                    // looking into default setter
                    lua_pushinteger(lua, 5);
                    lua_rawget(lua, -2);
                    if (lua_isnil(lua, -1))
                    {
                        lua_pop(lua, 2);
                        lua_pushstring(lua, "No setter found");
                        luaError(lua);
                    }
                    lua_pushvalue(lua, 1);
                    lua_pushvalue(lua, 2);
                    lua_pushvalue(lua, 3);
                    callRaw(lua, PushedObject{lua, 4}, 0);
                    lua_pop(lua, 1);
                    return 0;

                } catch (...) {
                    Pusher<std::exception_ptr>::push(lua, std::current_exception()).release();
                    luaError(lua);
                }
            };

            const auto toStringFunction = [](lua_State* lua) -> int {
               try {
                    assert(lua_gettop(lua) == 1);
                    assert(lua_isuserdata(lua, 1));
                    lua_pushstring(lua, "__tostring");
                    lua_gettable(lua, 1);
                    if (lua_isnil(lua, -1))
                    {
                        const void *ptr = lua_topointer(lua, -2);
                        lua_pop(lua, 1);
                        lua_pushstring(lua, (boost::format("userdata 0x%08x") % reinterpret_cast<intptr_t>(ptr)).str().c_str());
                        return 1;
                    }
                    lua_pushvalue(lua, 1);
		    return callRaw(lua, PushedObject{lua, 2}, 1).release();
                } catch (...) {
                    Pusher<std::exception_ptr>::push(lua, std::current_exception()).release();
                    luaError(lua);
                }
            };


            // writing structure for this type into the registry
            checkTypeRegistration(state, &typeid(TType));

            // creating the object
            // lua_newuserdata allocates memory in the internals of the lua library and returns it so we can fill it
            //   and that's what we do with placement-new
            const auto pointerLocation = static_cast<TType*>(lua_newuserdata(state, sizeof(TType)));
            new (pointerLocation) TType(std::forward<TType2>(value));
            PushedObject obj{state, 1};

            // creating the metatable (over the object on the stack)
            // lua_settable pops the key and value we just pushed, so stack management is easy
            // all that remains on the stack after these function calls is the metatable
            lua_newtable(state);
            PushedObject pushedTable{state, 1};

            // using the garbage collecting function we created above
            if (!boost::has_trivial_destructor<TType>::value)
            {
                lua_pushstring(state, "__gc");
                lua_pushcfunction(state, garbageCallbackFunction);
                lua_settable(state, -3);
            }

            // the _typeid index of the metatable will store the type_info*
            lua_pushstring(state, "_typeid");
            lua_pushlightuserdata(state, const_cast<std::type_info*>(&typeid(TType)));
            lua_settable(state, -3);

            // using the index function we created above
            lua_pushstring(state, "__index");
            lua_pushcfunction(state, indexFunction);
            lua_settable(state, -3);

            // using the newindex function we created above
            lua_pushstring(state, "__newindex");
            lua_pushcfunction(state, newIndexFunction);
            lua_settable(state, -3);

            lua_pushstring(state, "__tostring");
            lua_pushcfunction(state, toStringFunction);
            lua_settable(state, -3);

            lua_pushstring(state, "__eq");
            lua_getglobal(state, LUACONTEXT_GLOBAL_EQ);
            lua_settable(state, -3);


            // at this point, the stack contains the object at offset -2 and the metatable at offset -1
            // lua_setmetatable will bind the two together and pop the metatable
            // our custom type remains on the stack (and that's what we want since this is a push function)
            lua_setmetatable(state, -2);
            pushedTable.release();
            
            return obj;
        }
    };
    
    // this structure has a "size" int static member which is equal to the total of the push min size of all the types
    template<typename... TTypes>
    struct PusherTotalMinSize;

    // this structure has a "size" int static member which is equal to the total of the push max size of all the types
    template<typename... TTypes>
    struct PusherTotalMaxSize;
    
    // this structure has a "size" int static member which is equal to the maximum size of the push of all the types
    template<typename... TTypes>
    struct PusherMinSize;
    
    // this structure has a "size" int static member which is equal to the maximum size of the push of all the types
    template<typename... TTypes>
    struct PusherMaxSize;

    
    /**************************************************/
    /*                READ FUNCTIONS                  */
    /**************************************************/
    // the "Reader" structures allow to read data from the stack
    // - the "ReturnType" type is what is returned by the reader, and can be different than the template parameter (especially with references and constness)
    // - the "read" static function will check and read at the same time, returning an empty optional if it is the wrong type
    
    template<typename TType, typename = void>
    struct Reader {
        typedef typename std::conditional<std::is_pointer<TType>::value, TType, TType&>::type
            ReturnType;
        
        static auto read(lua_State* state, int index)
            -> boost::optional<ReturnType>
        {
            if (!test(state, index))
                return boost::none;
            return boost::optional<ReturnType>(*static_cast<TType*>(lua_touserdata(state, index)));
        }

    private:
        static bool test(lua_State* state, int index)
        {
            if (!lua_isuserdata(state, index))
                return false;
            if (!lua_getmetatable(state, index))
                return false;

            // now we have our metatable on the top of the stack
            // retrieving its _typeid member
            lua_pushstring(state, "_typeid");
            lua_gettable(state, -2);
            const auto storedTypeID = static_cast<const std::type_info*>(lua_touserdata(state, -1));
            const auto typeIDToCompare = &typeid(TType);

            // if wrong typeid, returning false
            lua_pop(state, 2);
            if (storedTypeID != typeIDToCompare)
                return false;

            return true;
        }
    };
    
    /**
     * This functions reads multiple values starting at "index" and passes them to the callback
     */
    template<typename TRetValue, typename TCallback>
    static auto readIntoFunction(lua_State* /*state*/, tag<TRetValue>, TCallback&& callback, int /*index*/)
        -> TRetValue
    {
        return callback();
    }
    template<typename TRetValue, typename TCallback, typename TFirstType, typename... TTypes>
    static auto readIntoFunction(lua_State* state, tag<TRetValue> retValueTag, TCallback&& callback, int index, tag<TFirstType>, tag<TTypes>... othersTags)
        -> typename std::enable_if<IsOptional<TFirstType>::value, TRetValue>::type
    {
        if (index >= 0) {
            Binder<TCallback, const TFirstType&> binder{ callback, {} };
            return readIntoFunction(state, retValueTag, binder, index + 1, othersTags...);
        }

        const auto& firstElem = Reader<typename std::decay<TFirstType>::type>::read(state, index);
        if (!firstElem)
            throw WrongTypeException(lua_typename(state, lua_type(state, index)), typeid(TFirstType));

        Binder<TCallback, const TFirstType&> binder{ callback, *firstElem };
        return readIntoFunction(state, retValueTag, binder, index + 1, othersTags...);
    }
    template<typename TRetValue, typename TCallback, typename TFirstType, typename... TTypes>
    static auto readIntoFunction(lua_State* state, tag<TRetValue> retValueTag, TCallback&& callback, int index, tag<TFirstType>, tag<TTypes>... othersTags)
        -> typename std::enable_if<!IsOptional<TFirstType>::value, TRetValue>::type
    {
        if (index >= 0)
            throw std::logic_error("Wrong number of parameters");

        const auto& firstElem = Reader<typename std::decay<TFirstType>::type>::read(state, index);
        if (!firstElem)
            throw WrongTypeException(lua_typename(state, lua_type(state, index)), typeid(TFirstType));

        Binder<TCallback, const TFirstType&> binder{ callback, *firstElem };
        return readIntoFunction(state, retValueTag, binder, index + 1, othersTags...);
    }


    /**************************************************/
    /*                   UTILITIES                    */
    /**************************************************/
    // structure that will ensure that a certain value is stored somewhere in the registry
    struct ValueInRegistry {
        // this constructor will clone and hold the value at the specified index (or by default at the top of the stack) in the registry
        ValueInRegistry(lua_State* lua_, int index=-1) : lua{lua_}
        {
            lua_pushlightuserdata(lua, this);
            lua_pushvalue(lua, -1 + index);
            lua_settable(lua, LUA_REGISTRYINDEX);
        }
        
        // removing the function from the registry
        ~ValueInRegistry()
        {
            lua_pushlightuserdata(lua, this);
            lua_pushnil(lua);
            lua_settable(lua, LUA_REGISTRYINDEX);
        }

        // loads the value and puts it at the top of the stack
        PushedObject pop()
        {
            lua_pushlightuserdata(lua, this);
            lua_gettable(lua, LUA_REGISTRYINDEX);
            return PushedObject{lua, 1};
        }

        ValueInRegistry(const ValueInRegistry&) = delete;
        ValueInRegistry& operator=(const ValueInRegistry&) = delete;

    private:
        lua_State* lua;
    };
    
    // binds the first parameter of a function object
    template<typename TFunctionObject, typename TFirstParamType>
    struct Binder {
        TFunctionObject function;
        TFirstParamType param;

        template<typename... TParams>
        auto operator()(TParams&&... params)
            -> decltype(function(param, std::forward<TParams>(params)...))
        {
            return function(param, std::forward<TParams>(params)...);
        }
    };
    
    // turns a type into a tuple
    // void is turned into std::tuple<>
    // existing tuples are untouched
    template<typename T>
    struct Tupleizer;

    // this structure takes a pointer to a member function type and returns the base function type
    template<typename TType>
    struct RemoveMemberPointerFunction { typedef void type; };          // required because of a compiler bug

    // this structure takes any object and detects its function type
    template<typename TObjectType>
    struct FunctionTypeDetector { typedef typename RemoveMemberPointerFunction<decltype(&std::decay<TObjectType>::type::operator())>::type type; };

    // this structure takes a function arguments list and has the "min" and the "max" static const member variables, whose value equal to the min and max number of parameters for the function
    // the only case where "min != max" is with boost::optional at the end of the list
    template<typename... TArgumentsList>
    struct FunctionArgumentsCounter {};
    
    // true is the template parameter is a boost::optional
    template<typename T>
    struct IsOptional : public std::false_type {};
};

/// @deprecated
static LuaContext::EmptyArray_t ATTR_UNUSED
    LuaEmptyArray {};
/// @deprecated
static LuaContext::Metatable_t ATTR_UNUSED
    LuaMetatable {};
    
/**************************************************/
/*            PARTIAL IMPLEMENTATIONS             */
/**************************************************/
template<>
inline auto LuaContext::readTopAndPop<void>(lua_State* /*state*/, PushedObject /*obj*/)
    -> void
{
}

// this structure takes a template parameter T
// if T is a tuple, it returns T ; if T is not a tuple, it returns std::tuple<T>
// we have to use this structure because std::tuple<std::tuple<...>> triggers a bug in both MSVC++ and GCC
template<typename T>
struct LuaContext::Tupleizer                        { typedef std::tuple<T>         type; };
template<typename... Args>
struct LuaContext::Tupleizer<std::tuple<Args...>>   { typedef std::tuple<Args...>   type; };
template<>
struct LuaContext::Tupleizer<void>                  { typedef std::tuple<>          type; };

// this structure takes any object and detects its function type
template<typename TRetValue, typename... TParameters>
struct LuaContext::FunctionTypeDetector<TRetValue (TParameters...)>             { typedef TRetValue type(TParameters...); };
template<typename TObjectType>
struct LuaContext::FunctionTypeDetector<TObjectType*>                           { typedef typename FunctionTypeDetector<TObjectType>::type type; };

// this structure takes a pointer to a member function type and returns the base function type
template<typename TType, typename TRetValue, typename... TParameters>
struct LuaContext::RemoveMemberPointerFunction<TRetValue (TType::*)(TParameters...)>                    { typedef TRetValue type(TParameters...); };
template<typename TType, typename TRetValue, typename... TParameters>
struct LuaContext::RemoveMemberPointerFunction<TRetValue (TType::*)(TParameters...) const>              { typedef TRetValue type(TParameters...); };
template<typename TType, typename TRetValue, typename... TParameters>
struct LuaContext::RemoveMemberPointerFunction<TRetValue (TType::*)(TParameters...) volatile>           { typedef TRetValue type(TParameters...); };
template<typename TType, typename TRetValue, typename... TParameters>
struct LuaContext::RemoveMemberPointerFunction<TRetValue (TType::*)(TParameters...) const volatile>     { typedef TRetValue type(TParameters...); };

// implementation of PusherTotalMinSize
template<typename TFirst, typename... TTypes>
struct LuaContext::PusherTotalMinSize<TFirst, TTypes...> { static const int size = Pusher<typename std::decay<TFirst>::type>::minSize + PusherTotalMinSize<TTypes...>::size; };
template<>
struct LuaContext::PusherTotalMinSize<> { static const int size = 0; };

// implementation of PusherTotalMaxSize
template<typename TFirst, typename... TTypes>
struct LuaContext::PusherTotalMaxSize<TFirst, TTypes...> { static const int size = Pusher<typename std::decay<TFirst>::type>::maxSize + PusherTotalMaxSize<TTypes...>::size; };
template<>
struct LuaContext::PusherTotalMaxSize<> { static const int size = 0; };

// implementation of PusherMinSize
template<typename TFirst, typename TSecond, typename... TTypes>
struct LuaContext::PusherMinSize<TFirst, TSecond, TTypes...> 
{ 
    static const int size = Pusher<typename std::decay<TFirst>::type>::minSize < Pusher<typename std::decay<TSecond>::type>::minSize 
                            ? 
                            PusherMinSize<typename std::decay<TFirst>::type, TTypes...>::size 
                            : 
                            PusherMinSize<typename std::decay<TSecond>::type, TTypes...>::size;
};

template<typename TFirst>
struct LuaContext::PusherMinSize<TFirst> { static const int size = Pusher<typename std::decay<TFirst>::type>::minSize; };

// implementation of PusherMaxSize
template<typename TFirst, typename... TTypes>
struct LuaContext::PusherMaxSize<TFirst, TTypes...> { static const int size = Pusher<typename std::decay<TFirst>::type>::maxSize > PusherTotalMaxSize<TTypes...>::size ? Pusher<typename std::decay<TFirst>::type>::maxSize : PusherMaxSize<TTypes...>::size; };
template<>
struct LuaContext::PusherMaxSize<> { static const int size = 0; };

// implementation of FunctionArgumentsCounter
template<typename TFirst, typename... TParams>
struct LuaContext::FunctionArgumentsCounter<TFirst, TParams...> {
    typedef FunctionArgumentsCounter<TParams...>
        SubType;
    static const int min = (IsOptional<TFirst>::value && SubType::min == 0) ? 0 : 1 + SubType::min;
    static const int max = 1 + SubType::max;
};
template<>
struct LuaContext::FunctionArgumentsCounter<> {
    static const int min = 0;
    static const int max = 0;
};

// implementation of IsOptional
template<typename T>
struct LuaContext::IsOptional<boost::optional<T>> : public std::true_type {};

// implementation of LuaFunctionCaller
template<typename TFunctionType>
class LuaContext::LuaFunctionCaller { static_assert(std::is_function<TFunctionType>::value, "Template parameter of LuaFunctionCaller must be a function type"); };
template<typename TRetValue, typename... TParams>
class LuaContext::LuaFunctionCaller<TRetValue (TParams...)>
{
public:
    TRetValue operator()(TParams&&... params) const
    {
        auto obj = valueHolder->pop();
        return call<TRetValue>(state, std::move(obj), std::forward<TParams>(params)...);
    }

private:
    std::shared_ptr<ValueInRegistry>    valueHolder;
    lua_State*                          state;

private:
    friend LuaContext;
    explicit LuaFunctionCaller(lua_State* state_, int index) :
        valueHolder(std::make_shared<ValueInRegistry>(state_, index)),
        state(state_)
    {}
};


/**************************************************/
/*                PUSH FUNCTIONS                  */
/**************************************************/
// specializations of the Pusher structure

// opaque Lua references
template<>
struct LuaContext::Pusher<LuaContext::LuaObject> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const LuaContext::LuaObject& value) noexcept {
        if (value.objectInRegistry.get()) {
            PushedObject obj = value.objectInRegistry->pop();
            return obj;
        } else {
            lua_pushnil(state);
            return PushedObject{state, 1};
        }
    }
};

// boolean
template<>
struct LuaContext::Pusher<bool> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, bool value) noexcept {
        lua_pushboolean(state, value);
        return PushedObject{state, 1};
    }
};

// string
template<>
struct LuaContext::Pusher<std::string> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const std::string& value) noexcept {
        lua_pushlstring(state, value.c_str(), value.length());
        return PushedObject{state, 1};
    }
};

// const char*
template<>
struct LuaContext::Pusher<const char*> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const char* value) noexcept {
        lua_pushstring(state, value);
        return PushedObject{state, 1};
    }
};

// const char[N]
template<int N>
struct LuaContext::Pusher<const char[N]> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const char* value) noexcept {
        lua_pushstring(state, value);
        return PushedObject{state, 1};
    }
};

// floating numbers
template<typename T>
struct LuaContext::Pusher<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, T value) noexcept {
        lua_pushnumber(state, value);
        return PushedObject{state, 1};
    }
};

// integers
template<typename T>
struct LuaContext::Pusher<T, typename std::enable_if<std::is_integral<T>::value>::type> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, T value) noexcept {
        lua_pushinteger(state, value);
        return PushedObject{state, 1};
    }
};

// nil
template<>
struct LuaContext::Pusher<std::nullptr_t> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, std::nullptr_t) noexcept {
        lua_pushnil(state);
        return PushedObject{state, 1};
    }
};

// empty arrays
template<>
struct LuaContext::Pusher<LuaContext::EmptyArray_t> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, EmptyArray_t) noexcept {
        lua_newtable(state);
        return PushedObject{state, 1};
    }
};

// std::type_info* is a lightuserdata
template<>
struct LuaContext::Pusher<const std::type_info*> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const std::type_info* ptr) noexcept {
        lua_pushlightuserdata(state, const_cast<std::type_info*>(ptr));
        return PushedObject{state, 1};
    }
};

// thread
template<>
struct LuaContext::Pusher<LuaContext::ThreadID> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const LuaContext::ThreadID& value) noexcept {
        lua_pushthread(value.state);
        return PushedObject{state, 1};
    }
};

// maps
template<typename TKey, typename TValue>
struct LuaContext::Pusher<std::map<TKey,TValue>> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const std::map<TKey,TValue>& value) noexcept {
        static_assert(Pusher<typename std::decay<TKey>::type>::minSize == 1 && Pusher<typename std::decay<TKey>::type>::maxSize == 1, "Can't push multiple elements for a table key");
        static_assert(Pusher<typename std::decay<TValue>::type>::minSize == 1 && Pusher<typename std::decay<TValue>::type>::maxSize == 1, "Can't push multiple elements for a table value");
        
        auto obj = Pusher<EmptyArray_t>::push(state, EmptyArray);

        for (auto i = value.begin(), e = value.end(); i != e; ++i)
            setTable<TValue>(state, obj, i->first, i->second);
        
        return obj;
    }
};

// unordered_maps
template<typename TKey, typename TValue, typename THash, typename TKeyEqual>
struct LuaContext::Pusher<std::unordered_map<TKey,TValue,THash,TKeyEqual>> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const std::unordered_map<TKey,TValue,THash,TKeyEqual>& value) noexcept {
        static_assert(Pusher<typename std::decay<TKey>::type>::minSize == 1 && Pusher<typename std::decay<TKey>::type>::maxSize == 1, "Can't push multiple elements for a table key");
        static_assert(Pusher<typename std::decay<TValue>::type>::minSize == 1 && Pusher<typename std::decay<TValue>::type>::maxSize == 1, "Can't push multiple elements for a table value");
        
        auto obj = Pusher<EmptyArray_t>::push(state, EmptyArray);

        for (auto i = value.begin(), e = value.end(); i != e; ++i)
            setTable<TValue>(state, obj, i->first, i->second);
        
        return obj;
    }
};

// vectors of pairs
template<typename TType1, typename TType2>
struct LuaContext::Pusher<std::vector<std::pair<TType1,TType2>>> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const std::vector<std::pair<TType1,TType2>>& value) noexcept {
        static_assert(Pusher<typename std::decay<TType1>::type>::minSize == 1 && Pusher<typename std::decay<TType1>::type>::maxSize == 1, "Can't push multiple elements for a table key");
        static_assert(Pusher<typename std::decay<TType2>::type>::minSize == 1 && Pusher<typename std::decay<TType2>::type>::maxSize == 1, "Can't push multiple elements for a table value");

        auto obj = Pusher<EmptyArray_t>::push(state, EmptyArray);

        for (auto i = value.begin(), e = value.end(); i != e; ++i)
            setTable<TType2>(state, obj, i->first, i->second);
        
        return obj;
    }
};

// vectors
template<typename TType>
struct LuaContext::Pusher<std::vector<TType>> {
    static const int minSize = 1;
    static const int maxSize = 1;

    static PushedObject push(lua_State* state, const std::vector<TType>& value) noexcept {
        static_assert(Pusher<typename std::decay<TType>::type>::minSize == 1 && Pusher<typename std::decay<TType>::type>::maxSize == 1, "Can't push multiple elements for a table value");
        
        auto obj = Pusher<EmptyArray_t>::push(state, EmptyArray);

        for (unsigned int i = 0; i < value.size(); ++i)
            setTable<TType>(state, obj, i + 1, value[i]);
        
        return obj;
    }
};

// unique_ptr
template<typename TType>
struct LuaContext::Pusher<std::unique_ptr<TType>> {
    static const int minSize = Pusher<std::shared_ptr<TType>>::minSize;
    static const int maxSize = Pusher<std::shared_ptr<TType>>::maxSize;

    static PushedObject push(lua_State* state, std::unique_ptr<TType> value) noexcept {
        return Pusher<std::shared_ptr<TType>>::push(state, std::move(value));
    }
};

// enum
template<typename TEnum>
struct LuaContext::Pusher<TEnum, typename std::enable_if<std::is_enum<TEnum>::value>::type> {
    #if !defined(__clang__) || __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ > 3)
        typedef typename std::underlying_type<TEnum>::type
            RealType;
    #else
        // implementation when std::underlying_type is not supported
        typedef unsigned long
            RealType;
    #endif

    static const int minSize = Pusher<RealType>::minSize;
    static const int maxSize = Pusher<RealType>::maxSize;

    static PushedObject push(lua_State* state, TEnum value) noexcept {
        return Pusher<RealType>::push(state, static_cast<RealType>(value));
    }
};

// any function
// this specialization is not directly called, but is called by other specializations
template<typename TReturnType, typename... TParameters>
struct LuaContext::Pusher<TReturnType (TParameters...)>
{
    static const int minSize = 1;
    static const int maxSize = 1;

    // counts the number of arguments
    typedef FunctionArgumentsCounter<TParameters...>
        LocalFunctionArgumentsCounter;

    // this is the version of "push" for non-trivially destructible function objects
    template<typename TFunctionObject>
    static auto push(lua_State* state, TFunctionObject fn) noexcept
        -> typename std::enable_if<!boost::has_trivial_destructor<TFunctionObject>::value, PushedObject>::type
    {
        // TODO: is_move_constructible not supported by some compilers
        //static_assert(std::is_move_constructible<TFunctionObject>::value, "The function object must be move-constructible");

        // when the lua script calls the thing we will push on the stack, we want "fn" to be executed
        // if we used lua's cfunctions system, we could not detect when the function is no longer in use, which could cause problems
        // so we use userdata instead
        
        // this function is called when the lua script tries to call our custom data type
        // we transfer execution to the "callback" function below
        const auto callCallback = [](lua_State* lua) -> int {
            assert(lua_gettop(lua) >= 1);
            assert(lua_isuserdata(lua, 1));
            auto function = static_cast<TFunctionObject*>(lua_touserdata(lua, 1));
            assert(function);

            return callback(lua, function, lua_gettop(lua) - 1).release();
        };

        // this one is called when lua's garbage collector no longer needs our custom data type
        // we call the function object's destructor
        const auto garbageCallback = [](lua_State* lua) -> int {
            assert(lua_gettop(lua) == 1);
            auto function = static_cast<TFunctionObject*>(lua_touserdata(lua, 1));
            assert(function);
            function->~TFunctionObject();
            return 0;
        };

        // creating the object
        // lua_newuserdata allocates memory in the internals of the lua library and returns it so we can fill it
        //   and that's what we do with placement-new
        const auto functionLocation = static_cast<TFunctionObject*>(lua_newuserdata(state, sizeof(TFunctionObject)));
        new (functionLocation) TFunctionObject(std::move(fn));

        // creating the metatable (over the object on the stack)
        // lua_settable pops the key and value we just pushed, so stack management is easy
        // all that remains on the stack after these function calls is the metatable
        lua_newtable(state);
        lua_pushstring(state, "__call");
        lua_pushcfunction(state, callCallback);
        lua_settable(state, -3);

        lua_pushstring(state, "__gc");
        lua_pushcfunction(state, garbageCallback);
        lua_settable(state, -3);

        // at this point, the stack contains the object at offset -2 and the metatable at offset -1
        // lua_setmetatable will bind the two together and pop the metatable
        // our custom function remains on the stack (and that's what we want)
        lua_setmetatable(state, -2);

        return PushedObject{state, 1};
    }

    // this is the version of "push" for trivially destructible objects
    template<typename TFunctionObject>
    static auto push(lua_State* state, TFunctionObject fn) noexcept
        -> typename std::enable_if<boost::has_trivial_destructor<TFunctionObject>::value, PushedObject>::type
    {
        // TODO: is_move_constructible not supported by some compilers
        //static_assert(std::is_move_constructible<TFunctionObject>::value, "The function object must be move-constructible");

        // when the lua script calls the thing we will push on the stack, we want "fn" to be executed
        // since "fn" doesn't need to be destroyed, we simply push it on the stack

        // this is the cfunction that is the callback
        const auto function = [](lua_State* state_) -> int
        {
            // the function object is an upvalue
            const auto toCall = static_cast<TFunctionObject*>(lua_touserdata(state_, lua_upvalueindex(1)));
            return callback(state_, toCall, lua_gettop(state_)).release();
        };

        // we copy the function object onto the stack
        const auto functionObjectLocation = static_cast<TFunctionObject*>(lua_newuserdata(state, sizeof(TFunctionObject)));
        new (functionObjectLocation) TFunctionObject(std::move(fn));

        // pushing the function with the function object as upvalue
        lua_pushcclosure(state, function, 1);
        return PushedObject{state, 1};
    }
    
    // this is the version of "push" for pointer to functions
    static auto push(lua_State* state, TReturnType (*fn)(TParameters...)) noexcept
        -> PushedObject
    {
        // when the lua script calls the thing we will push on the stack, we want "fn" to be executed
        // since "fn" doesn't need to be destroyed, we simply push it on the stack

        // this is the cfunction that is the callback
        const auto function = [](lua_State* state_) -> int
        {
            // the function object is an upvalue
            const auto toCall = reinterpret_cast<TReturnType (*)(TParameters...)>(lua_touserdata(state_, lua_upvalueindex(1)));
            return callback(state_, toCall, lua_gettop(state_)).release();
        };

        // we copy the function object onto the stack
        lua_pushlightuserdata(state, reinterpret_cast<void*>(fn));

        // pushing the function with the function object as upvalue
        lua_pushcclosure(state, function, 1);
        return PushedObject{state, 1};
    }
    
    // this is the version of "push" for references to functions
    static auto push(lua_State* state, TReturnType (&fn)(TParameters...)) noexcept
        -> PushedObject
    {
        return push(state, &fn);
    }

private:
    // callback that calls the function object
    // this function is used by the callbacks and handles loading arguments from the stack and pushing the return value back
    template<typename TFunctionObject>
    static auto callback(lua_State* state, TFunctionObject* toCall, int argumentsCount)
        -> PushedObject
    {
        // checking if number of parameters is correct
        if (argumentsCount < LocalFunctionArgumentsCounter::min) {
            // if not, using lua_error to return an error
            luaL_where(state, 1);
            lua_pushstring(state, "This function requires at least ");
            lua_pushnumber(state, LocalFunctionArgumentsCounter::min);
            lua_pushstring(state, " parameter(s)");
            lua_concat(state, 4);
            luaError(state);

        } else if (argumentsCount > LocalFunctionArgumentsCounter::max) {
            // if not, using lua_error to return an error
            luaL_where(state, 1);
            lua_pushstring(state, "This function requires at most ");
            lua_pushnumber(state, LocalFunctionArgumentsCounter::max);
            lua_pushstring(state, " parameter(s)");
            lua_concat(state, 4);
            luaError(state);
        }
                
        // calling the function
        try {
            return callback2(state, *toCall, argumentsCount);

        } catch (const WrongTypeException& ex) {
            // wrong parameter type, using lua_error to return an error
            luaL_where(state, 1);
            lua_pushstring(state, "Unable to convert parameter from ");
            lua_pushstring(state, ex.luaType.c_str());
            lua_pushstring(state, " to ");
            lua_pushstring(state, ex.destination.name());
            lua_concat(state, 5);
            luaError(state);

        } catch (const std::exception& e) {
          luaL_where(state, 1);
          lua_pushstring(state, "Caught exception: ");
          lua_pushstring(state, e.what());
          lua_concat(state, 3);
          luaError(state);
        } catch (...) {
            Pusher<std::exception_ptr>::push(state, std::current_exception()).release();
            luaError(state);
        }
    }
    
    template<typename TFunctionObject>
    static auto callback2(lua_State* state, TFunctionObject&& toCall, int argumentsCount)
        -> typename std::enable_if<!std::is_void<TReturnType>::value && !std::is_void<TFunctionObject>::value, PushedObject>::type
    {
        // pushing the result on the stack and returning number of pushed elements
        typedef Pusher<typename std::decay<TReturnType>::type>
            P;
        return P::push(state, readIntoFunction(state, tag<TReturnType>{}, toCall, -argumentsCount, tag<TParameters>{}...));
    }
    
    template<typename TFunctionObject>
    static auto callback2(lua_State* state, TFunctionObject&& toCall, int argumentsCount)
        -> typename std::enable_if<std::is_void<TReturnType>::value && !std::is_void<TFunctionObject>::value, PushedObject>::type
    {
        readIntoFunction(state, tag<TReturnType>{}, toCall, -argumentsCount, tag<TParameters>{}...);
        return PushedObject{state, 0};
    }
};

// C function pointers
template<typename TReturnType, typename... TParameters>
struct LuaContext::Pusher<TReturnType (*)(TParameters...)>
{
    // using the function-pushing implementation
    typedef Pusher<TReturnType (TParameters...)>
        SubPusher;
    static const int minSize = SubPusher::minSize;
    static const int maxSize = SubPusher::maxSize;

    template<typename TType>
    static PushedObject push(lua_State* state, TType value) noexcept {
        return SubPusher::push(state, value);
    }
};

// C function references
template<typename TReturnType, typename... TParameters>
struct LuaContext::Pusher<TReturnType (&)(TParameters...)>
{
    // using the function-pushing implementation
    typedef Pusher<TReturnType(TParameters...)>
        SubPusher;
    static const int minSize = SubPusher::minSize;
    static const int maxSize = SubPusher::maxSize;

    template<typename TType>
    static PushedObject push(lua_State* state, TType value) noexcept {
        return SubPusher::push(state, value);
    }
};

// std::function
template<typename TReturnType, typename... TParameters>
struct LuaContext::Pusher<std::function<TReturnType (TParameters...)>>
{
    // using the function-pushing implementation
    typedef Pusher<TReturnType (TParameters...)>
        SubPusher;
    static const int minSize = SubPusher::minSize;
    static const int maxSize = SubPusher::maxSize;

    static PushedObject push(lua_State* state, const std::function<TReturnType (TParameters...)>& value) noexcept {
        return SubPusher::push(state, value);
    }
};

// boost::variant
template<typename... TTypes>
struct LuaContext::Pusher<boost::variant<TTypes...>>
{
    static const int minSize = PusherMinSize<TTypes...>::size;
    static const int maxSize = PusherMaxSize<TTypes...>::size;

    static PushedObject push(lua_State* state, const boost::variant<TTypes...>& value) noexcept {
        PushedObject obj{state, 0};
        VariantWriter writer{state, obj};
        value.apply_visitor(writer);
        return obj;
    }

private:
    struct VariantWriter : public boost::static_visitor<> {
        template<typename TType>
        void operator()(TType value) noexcept
        {
            obj = Pusher<typename std::decay<TType>::type>::push(state, std::move(value));
        }

        VariantWriter(lua_State* state_, PushedObject& obj_) : state(state_), obj(obj_) {}
        lua_State* state;
        PushedObject& obj;
    };
};

// boost::optional
template<typename TType>
struct LuaContext::Pusher<boost::optional<TType>> {
    typedef Pusher<typename std::decay<TType>::type>
        UnderlyingPusher;

    static const int minSize = UnderlyingPusher::minSize < 1 ? UnderlyingPusher::minSize : 1;
    static const int maxSize = UnderlyingPusher::maxSize > 1 ? UnderlyingPusher::maxSize : 1;

    static PushedObject push(lua_State* state, const boost::optional<TType>& value) noexcept {
        if (value) {
            return UnderlyingPusher::push(state, value.get());
        } else {
            lua_pushnil(state);
            return PushedObject{state, 1};
        }
    }
};

// tuple
template<typename... TTypes>
struct LuaContext::Pusher<std::tuple<TTypes...>> {
    // TODO: NOT EXCEPTION SAFE /!\ //
    static const int minSize = PusherTotalMinSize<TTypes...>::size;
    static const int maxSize = PusherTotalMaxSize<TTypes...>::size;

    static PushedObject push(lua_State* state, const std::tuple<TTypes...>& value) noexcept {
        return PushedObject{state, push2(state, value, std::integral_constant<int,0>{})};
    }

    static PushedObject push(lua_State* state, std::tuple<TTypes...>&& value) noexcept {
        return PushedObject{state, push2(state, std::move(value), std::integral_constant<int,0>{})};
    }

private:
    template<int N>
    static int push2(lua_State* state, const std::tuple<TTypes...>& value, std::integral_constant<int,N>) noexcept {
        typedef typename std::tuple_element<N,std::tuple<TTypes...>>::type ElemType;

        return Pusher<typename std::decay<ElemType>::type>::push(state, std::get<N>(value)).release() +
            push2(state, value, std::integral_constant<int,N+1>{});
    }

    template<int N>
    static int push2(lua_State* state, std::tuple<TTypes...>&& value, std::integral_constant<int,N>) noexcept {
        typedef typename std::tuple_element<N,std::tuple<TTypes...>>::type ElemType;

        return Pusher<typename std::decay<ElemType>::type>::push(state, std::move(std::get<N>(value))).release() +
            push2(state, std::move(value), std::integral_constant<int,N+1>{});
    }
    
    static int push2(lua_State* /*state*/, const std::tuple<TTypes...>&, std::integral_constant<int,sizeof...(TTypes)>) noexcept {
        return 0;
    }
    
    static int push2(lua_State* /*state*/, std::tuple<TTypes...>&&, std::integral_constant<int,sizeof...(TTypes)>) noexcept {
        return 0;
    }
};

/**************************************************/
/*                READ FUNCTIONS                  */
/**************************************************/
// specializations of the Reader structures

// opaque Lua references
template<>
struct LuaContext::Reader<LuaContext::LuaObject>
{
    static auto read(lua_State* state, int index)
        -> boost::optional<LuaContext::LuaObject>
    {
        LuaContext::LuaObject obj(state, index);
        return obj;
    }
};

// reading null
template<>
struct LuaContext::Reader<std::nullptr_t>
{
    static auto read(lua_State* state, int index)
        -> boost::optional<std::nullptr_t>
    {
        if (!lua_isnil(state, index))
            return boost::none;
        return nullptr;
    }
};

// integrals
template<typename TType>
struct LuaContext::Reader<
            TType,
            typename std::enable_if<std::is_integral<TType>::value>::type
        >
{
    static auto read(lua_State* state, int index)
        -> boost::optional<TType>
    {
#       if LUA_VERSION_NUM >= 502

            int success;
            auto value = lua_tointegerx(state, index, &success);
            if (success == 0)
                return boost::none;
            return static_cast<TType>(value);

#       else

            if (!lua_isnumber(state, index))
                return boost::none;
            return static_cast<TType>(lua_tointeger(state, index));

#       endif
    }
};

// floating points
template<typename TType>
struct LuaContext::Reader<
            TType,
            typename std::enable_if<std::is_floating_point<TType>::value>::type
        >
{
    static auto read(lua_State* state, int index)
        -> boost::optional<TType>
    {
#       if LUA_VERSION_NUM >= 502

            int success;
            auto value = lua_tonumberx(state, index, &success);
            if (success == 0)
                return boost::none;
            return static_cast<TType>(value);

#       else

            if (!lua_isnumber(state, index))
                return boost::none;
            return static_cast<TType>(lua_tonumber(state, index));

#       endif
    }
};

// boolean
template<>
struct LuaContext::Reader<bool>
{
    static auto read(lua_State* state, int index)
        -> boost::optional<bool>
    {
        if (!lua_isboolean(state, index))
            return boost::none;
        return lua_toboolean(state, index) != 0;
    }
};

// string
// lua_tostring returns a temporary pointer, but that's not a problem since we copy
//   the data into a std::string
template<>
struct LuaContext::Reader<std::string>
{
    static auto read(lua_State* state, int index)
        -> boost::optional<std::string>
    {
        std::string result;

        // lua_tolstring might convert the variable that would confuse lua_next, so we
        //   make a copy of the variable.
        lua_pushvalue(state, index);

        size_t len;
        const auto val = lua_tolstring(state, -1, &len);

        if (val != nullptr)
          result.assign(val, len);

        lua_pop(state, 1);

        return val != nullptr ? boost::optional<std::string>{ std::move(result) } : boost::none;
    }
};

// enums
template<typename TType>
struct LuaContext::Reader<
            TType,
            typename std::enable_if<std::is_enum<TType>::value>::type
        >
{
    static auto read(lua_State* state, int index)
        -> boost::optional<TType>
    {
        if (!lua_isnumber(state, index) || fmod(lua_tonumber(state, index), 1.) != 0)
            return boost::none;
        return static_cast<TType>(lua_tointeger(state, index));
    }
};

// LuaFunctionCaller
template<typename TRetValue, typename... TParameters>
struct LuaContext::Reader<LuaContext::LuaFunctionCaller<TRetValue (TParameters...)>>
{
    typedef LuaFunctionCaller<TRetValue (TParameters...)>
        ReturnType;

    static auto read(lua_State* state, int index)
        -> boost::optional<ReturnType>
    {
        if (lua_isfunction(state, index) == 0 && lua_isuserdata(state, index) == 0)
            return boost::none;
        return ReturnType(state, index);
    }
};

// function
template<typename TRetValue, typename... TParameters>
struct LuaContext::Reader<std::function<TRetValue (TParameters...)>>
{
    static auto read(lua_State* state, int index)
        -> boost::optional<std::function<TRetValue (TParameters...)>>
    {
		if (auto val = Reader<LuaContext::LuaFunctionCaller<TRetValue (TParameters...)>>::read(state, index))
		{
			std::function<TRetValue (TParameters...)> f{*val};
			return boost::optional<std::function<TRetValue (TParameters...)>>{std::move(f)};
		}

        return boost::none;
    }
};

// vector of pairs
template<typename TType1, typename TType2>
struct LuaContext::Reader<std::vector<std::pair<TType1,TType2>>>
{
    static auto read(lua_State* state, int index)
        -> boost::optional<std::vector<std::pair<TType1, TType2>>>
    {
        if (!lua_istable(state, index))
            return boost::none;

        std::vector<std::pair<TType1, TType2>> result;

        // we traverse the table at the top of the stack
        lua_pushnil(state);     // first key
        while (lua_next(state, (index > 0) ? index : (index - 1)) != 0) {
            // now a key and its value are pushed on the stack
            try {
                auto val1 = Reader<TType1>::read(state, -2);
                auto val2 = Reader<TType2>::read(state, -1);

                if (!val1.is_initialized() || !val2.is_initialized()) {
                    lua_pop(state, 2);      // we remove the value and the key
                    return {};
                }

                result.push_back({ val1.get(), val2.get() });
                lua_pop(state, 1);      // we remove the value but keep the key for the next iteration

            } catch(...) {
                lua_pop(state, 2);      // we remove the value and the key
                return {};
            }
        }

        return { std::move(result) };
    }
};

// map
template<typename TKey, typename TValue>
struct LuaContext::Reader<std::map<TKey,TValue>>
{
    static auto read(lua_State* state, int index)
        -> boost::optional<std::map<TKey,TValue>>
    {
        if (!lua_istable(state, index))
            return boost::none;

        std::map<TKey,TValue> result;

        // we traverse the table at the top of the stack
        lua_pushnil(state);     // first key
        while (lua_next(state, (index > 0) ? index : (index - 1)) != 0) {
            // now a key and its value are pushed on the stack
            try {
                auto key = Reader<TKey>::read(state, -2);
                auto value = Reader<TValue>::read(state, -1);

                if (!key.is_initialized() || !value.is_initialized()) {
                    lua_pop(state, 2);      // we remove the value and the key
                    return {};
                }

                result.insert({ key.get(), value.get() });
                lua_pop(state, 1);      // we remove the value but keep the key for the next iteration

            } catch(...) {
                lua_pop(state, 2);      // we remove the value and the key
                return {};
            }
        }

        return { std::move(result) };
    }
};

// unordered_map
template<typename TKey, typename TValue, typename THash, typename TKeyEqual>
struct LuaContext::Reader<std::unordered_map<TKey,TValue,THash,TKeyEqual>>
{
    static auto read(lua_State* state, int index)
        -> boost::optional<std::unordered_map<TKey,TValue,THash,TKeyEqual>>
    {
        if (!lua_istable(state, index))
            return boost::none;

        std::unordered_map<TKey,TValue,THash,TKeyEqual> result;

        // we traverse the table at the top of the stack
        lua_pushnil(state);     // first key
        while (lua_next(state, (index > 0) ? index : (index - 1)) != 0) {
            // now a key and its value are pushed on the stack
            try {
                auto key = Reader<TKey>::read(state, -2);
                auto value = Reader<TValue>::read(state, -1);

                if (!key.is_initialized() || !value.is_initialized()) {
                    lua_pop(state, 2);      // we remove the value and the key
                    return {};
                }

                result.insert({ key.get(), value.get() });
                lua_pop(state, 1);      // we remove the value but keep the key for the next iteration

            } catch(...) {
                lua_pop(state, 2);      // we remove the value and the key
                return {};
            }
        }

        return { std::move(result) };
    }
};

// optional
// IMPORTANT: optional means "either nil or the value of the right type"
//  * if the value is nil, then an optional containing an empty optional is returned
//  * if the value is of the right type, then an optional containing an optional containing the value is returned
//  * if the value is of the wrong type, then an empty optional is returned
template<typename TType>
struct LuaContext::Reader<boost::optional<TType>>
{
    static auto read(lua_State* state, int index)
        -> boost::optional<boost::optional<TType>>
    {
        if (lua_isnil(state, index))
            return boost::optional<TType>{boost::none};
        if (auto&& other = Reader<TType>::read(state, index))
            return std::move(other);
        return boost::none;
    }
};

// variant
template<typename... TTypes>
struct LuaContext::Reader<boost::variant<TTypes...>>
{
	typedef boost::variant<TTypes...>
		ReturnType;

private:
    // class doing operations for a range of types from TIterBegin to TIterEnd
    template<typename TIterBegin, typename TIterEnd, typename = void>
    struct VariantReader
    {
        using SubReader = Reader<typename std::decay<typename boost::mpl::deref<TIterBegin>::type>::type>;

        static auto read(lua_State* state, int index)
            -> boost::optional<ReturnType>
        {
            // note: using SubReader::read triggers a compilation error when used with a reference
            if (const auto val = SubReader::read(state, index))
                return boost::variant<TTypes...>{*val};
            return VariantReader<typename boost::mpl::next<TIterBegin>::type, TIterEnd>::read(state, index);
        }
    };

    // specialization of class above being called when list of remaining types is empty
    template<typename TIterBegin, typename TIterEnd>
    struct VariantReader<TIterBegin, TIterEnd, typename std::enable_if<boost::mpl::distance<TIterBegin, TIterEnd>::type::value == 0>::type>
    {
        static auto read(lua_State* /*state*/, int /*index*/)
            -> boost::optional<ReturnType> 
        {
            return boost::none;
        }
    };

    // this is the main type
    typedef VariantReader<typename boost::mpl::begin<typename ReturnType::types>::type, typename boost::mpl::end<typename ReturnType::types>::type>
        MainVariantReader;

public:
    static auto read(lua_State* state, int index)
        -> boost::optional<ReturnType>
    {
        return MainVariantReader::read(state, index);
    }
};

// reading a tuple
// tuple have an additional argument for their functions, that is the maximum size to read
// if maxSize is smaller than the tuple size, then the remaining parameters will be left to default value
template<>
struct LuaContext::Reader<std::tuple<>>
{
    static auto read(lua_State* /*state*/, int /*index*/, int /*maxSize*/ = 0)
        -> boost::optional<std::tuple<>>
    {
        return std::tuple<>{};
    }
};

template<typename TFirst, typename... TOthers>
struct LuaContext::Reader<std::tuple<TFirst, TOthers...>,
        typename std::enable_if<!LuaContext::IsOptional<TFirst>::value>::type       // TODO: replace by std::is_default_constructible when it works on every compiler
    >
{
    // this is the "TFirst is NOT default constructible" version

	typedef std::tuple<TFirst, TOthers...>
		ReturnType;

    static auto read(lua_State* state, int index, int maxSize = std::tuple_size<ReturnType>::value)
        -> boost::optional<ReturnType>
    {
        if (maxSize <= 0)
            return boost::none;

        auto firstVal = Reader<TFirst>::read(state, index);
        auto othersVal = Reader<std::tuple<TOthers...>>::read(state, index + 1, maxSize - 1);
        
        if (!firstVal || !othersVal)
            return boost::none;

        return std::tuple_cat(std::tuple<TFirst>(*firstVal), std::move(*othersVal));
    }
};

template<typename TFirst, typename... TOthers>
struct LuaContext::Reader<std::tuple<TFirst, TOthers...>,
        typename std::enable_if<LuaContext::IsOptional<TFirst>::value>::type        // TODO: replace by std::is_default_constructible when it works on every compiler
    >
{
    // this is the "TFirst is default-constructible" version
	
	typedef std::tuple<TFirst, TOthers...>
		ReturnType;
    
    static auto read(lua_State* state, int index, int maxSize = std::tuple_size<ReturnType>::value)
        -> boost::optional<ReturnType>
    {
        auto othersVal = Reader<std::tuple<TOthers...>>::read(state, index + 1, maxSize - 1);
        if (!othersVal)
            return boost::none;
        
        if (maxSize <= 0)
            return std::tuple_cat(std::tuple<TFirst>(), std::move(*othersVal));
        
        auto firstVal = Reader<TFirst>::read(state, index);
        if (!firstVal)
            return boost::none;

        return std::tuple_cat(std::tuple<TFirst>(*firstVal), std::move(*othersVal));
    }
};

#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif

#endif