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
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SWIG and C++</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="#ffffff">
<H1><a name="SWIGPlus"></a>6 SWIG and C++</H1>
<!-- INDEX -->
<div class="sectiontoc">
<ul>
<li><a href="#SWIGPlus_nn2">Comments on C++ Wrapping</a>
<li><a href="#SWIGPlus_nn3">Approach</a>
<li><a href="#SWIGPlus_nn4">Supported C++ features</a>
<li><a href="#SWIGPlus_nn5">Command line options and compilation</a>
<li><a href="#SWIGPlus_nn38">Proxy classes</a>
<ul>
<li><a href="#SWIGPlus_nn39">Construction of proxy classes</a>
<li><a href="#SWIGPlus_nn40">Resource management in proxies</a>
<li><a href="#SWIGPlus_nn41">Language specific details</a>
</ul>
<li><a href="#SWIGPlus_nn6">Simple C++ wrapping</a>
<ul>
<li><a href="#SWIGPlus_nn7">Constructors and destructors</a>
<li><a href="#SWIGPlus_nn8">Default constructors, copy constructors and implicit destructors</a>
<li><a href="#SWIGPlus_nn9">When constructor wrappers aren't created</a>
<li><a href="#SWIGPlus_nn10">Copy constructors</a>
<li><a href="#SWIGPlus_nn11">Member functions</a>
<li><a href="#SWIGPlus_nn12">Static members</a>
<li><a href="#SWIGPlus_member_data">Member data</a>
</ul>
<li><a href="#SWIGPlus_default_args">Default arguments</a>
<li><a href="#SWIGPlus_nn15">Protection</a>
<li><a href="#SWIGPlus_nn16">Enums and constants</a>
<li><a href="#SWIGPlus_nn17">Friends</a>
<li><a href="#SWIGPlus_nn18">References and pointers</a>
<li><a href="#SWIGPlus_nn19">Pass and return by value</a>
<li><a href="#SWIGPlus_nn20">Inheritance</a>
<li><a href="#SWIGPlus_nn21">A brief discussion of multiple inheritance, pointers, and type checking</a>
<li><a href="#SWIGPlus_overloaded_methods">Wrapping Overloaded Functions and Methods</a>
<ul>
<li><a href="#SWIGPlus_nn24">Dispatch function generation</a>
<li><a href="#SWIGPlus_nn25">Ambiguity in Overloading</a>
<li><a href="#SWIGPlus_ambiguity_resolution_renaming">Ambiguity resolution and renaming</a>
<li><a href="#SWIGPlus_nn27">Comments on overloading</a>
</ul>
<li><a href="#SWIGPlus_nn28">Wrapping overloaded operators</a>
<li><a href="#SWIGPlus_class_extension">Class extension</a>
<li><a href="#SWIGPlus_nn30">Templates</a>
<li><a href="#SWIGPlus_namespaces">Namespaces</a>
<ul>
<li><a href="#SWIGPlus_nspace">The nspace feature for namespaces</a>
</ul>
<li><a href="#SWIGPlus_renaming_templated_types_namespaces">Renaming templated types in namespaces</a>
<li><a href="#SWIGPlus_exception_specifications">Exception specifications</a>
<li><a href="#SWIGPlus_catches">Exception handling with %catches</a>
<li><a href="#SWIGPlus_nn33">Pointers to Members</a>
<li><a href="#SWIGPlus_smart_pointers">Smart pointers and operator->()</a>
<li><a href="#SWIGPlus_ref_unref">C++ reference counted objects - ref/unref feature</a>
<li><a href="#SWIGPlus_nn35">Using declarations and inheritance</a>
<li><a href="#SWIGPlus_nested_classes">Nested classes</a>
<li><a href="#SWIGPlus_const">A brief rant about const-correctness</a>
<li><a href="#SWIGPlus_nn42">Where to go for more information</a>
</ul>
</div>
<!-- INDEX -->
<p>
This chapter describes SWIG's support for wrapping C++. As a prerequisite,
you should first read the chapter <a href="SWIG.html#SWIG">SWIG Basics</a> to see
how SWIG wraps ANSI C. Support for C++ builds upon ANSI C
wrapping and that material will be useful in understanding this chapter.
</p>
<H2><a name="SWIGPlus_nn2"></a>6.1 Comments on C++ Wrapping</H2>
<p>
Because of its complexity and the fact that C++ can be
difficult to integrate with itself let alone other languages, SWIG
only provides support for a subset of C++ features. Fortunately,
this is now a rather large subset.
</p>
<p>
In part, the problem with C++ wrapping is that there is no
semantically obvious (or automatic ) way to map many of its advanced
features into other languages. As a simple example, consider the
problem of wrapping C++ multiple inheritance to a target language with
no such support. Similarly, the use of overloaded operators and
overloaded functions can be problematic when no such capability exists
in a target language.
</p>
<p>
A more subtle issue with C++ has to do with the way that some C++
programmers think about programming libraries. In the world of SWIG,
you are really trying to create binary-level software components for
use in other languages. In order for this to work, a "component" has
to contain real executable instructions and there has to be some kind
of binary linking mechanism for accessing its functionality. In
contrast, C++ has increasingly relied upon generic programming and
templates for much of its functionality.
Although templates are a powerful feature, they are largely orthogonal
to the whole notion of binary components and libraries. For example,
an STL <tt>vector</tt> does not define any kind of binary object for
which SWIG can just create a wrapper. To further complicate matters,
these libraries often utilize a lot of behind the scenes magic in
which the semantics of seemingly basic operations (e.g., pointer
dereferencing, procedure call, etc.) can be changed in dramatic and
sometimes non-obvious ways. Although this "magic" may present few
problems in a C++-only universe, it greatly complicates the problem of
crossing language boundaries and provides many opportunities to shoot
yourself in the foot. You will just have to be careful.
</p>
<H2><a name="SWIGPlus_nn3"></a>6.2 Approach</H2>
<p>
To wrap C++, SWIG uses a layered approach to code generation.
At the lowest level, SWIG generates a collection of procedural ANSI-C style
wrappers. These wrappers take care of basic type conversion,
type checking, error handling, and other low-level details of the C++ binding.
These wrappers are also sufficient to bind C++ into any target language
that supports built-in procedures. In some sense, you might view this
layer of wrapping as providing a C library interface to C++.
On top of the low-level procedural (flattened) interface, SWIG generates proxy classes
that provide a natural object-oriented (OO) interface to the underlying code. The proxy classes are typically
written in the target language itself. For instance, in Python, a real
Python class is used to provide a wrapper around the underlying C++ object.
</p>
<p>
It is important to emphasize that SWIG takes a deliberately
conservative and non-intrusive approach to C++ wrapping. SWIG does not
encapsulate C++ classes inside a special C++ adaptor, it does not rely
upon templates, nor does it add in additional C++ inheritance when
generating wrappers. The last thing that most C++ programs need is
even more compiler magic. Therefore, SWIG tries to maintain a very
strict and clean separation between the implementation of your C++
application and the resulting wrapper code. You might say that SWIG
has been written to follow the principle of least surprise--it does
not play sneaky tricks with the C++ type system, it doesn't mess with
your class hierarchies, and it doesn't introduce new semantics.
Although this approach might not provide the most seamless integration
with C++, it is safe, simple, portable, and debuggable.
</p>
<p>
Some of this chapter focuses on the low-level procedural interface to
C++ that is used as the foundation for all language modules. Keep in
mind that the target languages also provide the high-level OO interface via
proxy classes. More detailed coverage can be found in the documentation
for each target language.
</p>
<H2><a name="SWIGPlus_nn4"></a>6.3 Supported C++ features</H2>
<p>
SWIG currently supports most C++ features including the following:</p>
<ul>
<li>Classes
<li>Constructors and destructors
<li>Virtual functions
<li>Public inheritance (including multiple inheritance)
<li>Static functions
<li>Function and method overloading
<li>Operator overloading for many standard operators
<li>References
<li>Templates (including specialization and member templates)
<li>Pointers to members
<li>Namespaces
<li>Default parameters
<li>Smart pointers
</ul>
<p>
The following C++ features are not currently supported:</p>
<ul>
<li>Overloaded versions of certain operators (new, delete, etc.)
</ul>
<p>
As a rule of thumb, SWIG should not be used on raw C++ source files, use header files only.
</p>
<p>
SWIG's C++ support is an ongoing project so some of these limitations may be lifted
in future releases. However, we make no promises. Also, submitting a bug report is a very
good way to get problems fixed (wink).
</p>
<H2><a name="SWIGPlus_nn5"></a>6.4 Command line options and compilation</H2>
<p>
When wrapping C++ code, it is critical that SWIG be called with the
`<tt>-c++</tt>' option. This changes the way a number of critical
features such as memory management are handled. It
also enables the recognition of C++ keywords. Without the <tt>-c++</tt>
flag, SWIG will either issue a warning or a large number of syntax
errors if it encounters C++ code in an interface file.</p>
<p>
When compiling and linking the resulting wrapper file, it is normal
to use the C++ compiler. For example:
</p>
<div class="shell">
<pre>
$ swig -c++ -tcl example.i
$ c++ -c example_wrap.cxx
$ c++ example_wrap.o $(OBJS) -o example.so
</pre>
</div>
<p>
Unfortunately, the process varies slightly on each platform. Make sure
you refer to the documentation on each target language for further
details. The SWIG Wiki also has further details.
</p>
<b>Compatibility Note:</b> Early versions of SWIG generated just a flattened low-level C style API to C++ classes by default.
The <tt>-noproxy</tt> commandline option is recognised by many target languages and will generate just this
interface as in earlier versions.
<H2><a name="SWIGPlus_nn38"></a>6.5 Proxy classes</H2>
<p>
In order to provide a natural mapping from C++ classes to the target language classes, SWIG's target
languages mostly wrap C++ classes with special proxy classes. These
proxy classes are typically implemented in the target language itself.
For example, if you're building a Python module, each C++ class is
wrapped by a Python proxy class. Or if you're building a Java module, each
C++ class is wrapped by a Java proxy class.
</p>
<H3><a name="SWIGPlus_nn39"></a>6.5.1 Construction of proxy classes</H3>
<p>
Proxy classes are always constructed as an extra layer of wrapping that uses low-level
accessor functions. To illustrate, suppose you had a
C++ class like this:
</p>
<div class="code">
<pre>
class Foo {
public:
Foo();
~Foo();
int bar(int x);
int x;
};
</pre>
</div>
<p>
Using C++ as pseudocode, a proxy class looks something like this:
</p>
<div class="code">
<pre>
class FooProxy {
private:
Foo *self;
public:
FooProxy() {
self = new_Foo();
}
~FooProxy() {
delete_Foo(self);
}
int bar(int x) {
return Foo_bar(self,x);
}
int x_get() {
return Foo_x_get(self);
}
void x_set(int x) {
Foo_x_set(self,x);
}
};
</pre>
</div>
<p>
Of course, always keep in mind that the real proxy class is written in the target language.
For example, in Python, the proxy might look roughly like this:
</p>
<div class="targetlang">
<pre>
class Foo:
def __init__(self):
self.this = new_Foo()
def __del__(self):
delete_Foo(self.this)
def bar(self,x):
return Foo_bar(self.this,x)
def __getattr__(self,name):
if name == 'x':
return Foo_x_get(self.this)
...
def __setattr__(self,name,value):
if name == 'x':
Foo_x_set(self.this,value)
...
</pre>
</div>
<p>
Again, it's important to emphasize that the low-level accessor functions are always used by the
proxy classes.
Whenever possible, proxies try to take advantage of language features that are similar to C++. This
might include operator overloading, exception handling, and other features.
</p>
<H3><a name="SWIGPlus_nn40"></a>6.5.2 Resource management in proxies</H3>
<p>
A major issue with proxies concerns the memory management of wrapped objects. Consider the following
C++ code:
</p>
<div class="code">
<pre>
class Foo {
public:
Foo();
~Foo();
int bar(int x);
int x;
};
class Spam {
public:
Foo *value;
...
};
</pre>
</div>
<p>
Consider some script code that uses these classes:
</p>
<div class="targetlang">
<pre>
f = Foo() # Creates a new Foo
s = Spam() # Creates a new Spam
s.value = f # Stores a reference to f inside s
g = s.value # Returns stored reference
g = 4 # Reassign g to some other value
del f # Destroy f
</pre>
</div>
<p>
Now, ponder the resulting memory management issues. When objects are
created in the script, the objects are wrapped by newly created proxy
classes. That is, there is both a new proxy class instance and a new
instance of the underlying C++ class. In this example, both
<tt>f</tt> and <tt>s</tt> are created in this way. However, the
statement <tt>s.value</tt> is rather curious---when executed, a
pointer to <tt>f</tt> is stored inside another object. This means
that the scripting proxy class <em>AND</em> another C++ class share a
reference to the same object. To make matters even more interesting,
consider the statement <tt>g = s.value</tt>. When executed, this
creates a new proxy class <tt>g</tt> that provides a wrapper around the
C++ object stored in <tt>s.value</tt>. In general, there is no way to
know where this object came from---it could have been created by the
script, but it could also have been generated internally. In this
particular example, the assignment of <tt>g</tt> results in a second
proxy class for <tt>f</tt>. In other words, a reference to <tt>f</tt>
is now shared by two proxy classes <em>and</em> a C++ class.
</p>
<p>
Finally, consider what happens when objects are destroyed. In the
statement, <tt>g=4</tt>, the variable <tt>g</tt> is reassigned. In
many languages, this makes the old value of <tt>g</tt> available for
garbage collection. Therefore, this causes one of the proxy classes
to be destroyed. Later on, the statement <tt>del f</tt> destroys the
other proxy class. Of course, there is still a reference to the
original object stored inside another C++ object. What happens to it?
Is the object still valid?
</p>
<p>
To deal with memory management problems, proxy classes provide an API
for controlling ownership. In C++ pseudocode, ownership control might look
roughly like this:
</p>
<div class="code">
<pre>
class FooProxy {
public:
Foo *self;
int thisown;
FooProxy() {
self = new_Foo();
thisown = 1; // Newly created object
}
~FooProxy() {
if (thisown) delete_Foo(self);
}
...
// Ownership control API
void disown() {
thisown = 0;
}
void acquire() {
thisown = 1;
}
};
class FooPtrProxy: public FooProxy {
public:
FooPtrProxy(Foo *s) {
self = s;
thisown = 0;
}
};
class SpamProxy {
...
FooProxy *value_get() {
return FooPtrProxy(Spam_value_get(self));
}
void value_set(FooProxy *v) {
Spam_value_set(self,v->self);
v->disown();
}
...
};
</pre>
</div>
<p>
Looking at this code, there are a few central features:
</p>
<ul>
<li>Each proxy class keeps an extra flag to indicate ownership. C++ objects are only destroyed
if the ownership flag is set.
</li>
<li>When new objects are created in the target language, the ownership flag is set.
</li>
<li>When a reference to an internal C++ object is returned, it is wrapped by a proxy
class, but the proxy class does not have ownership.
</li>
<li>In certain cases, ownership is adjusted. For instance, when a value is assigned to the member of
a class, ownership is lost.
</li>
<li>Manual ownership control is provided by special <tt>disown()</tt> and <tt>acquire()</tt> methods.
</li>
</ul>
<p>
Given the tricky nature of C++ memory management, it is impossible for proxy classes to automatically handle
every possible memory management problem. However, proxies do provide a mechanism for manual control that
can be used (if necessary) to address some of the more tricky memory management problems.
</p>
<H3><a name="SWIGPlus_nn41"></a>6.5.3 Language specific details</H3>
<p>
Language specific details on proxy classes are contained in the chapters describing each target language. This
chapter has merely introduced the topic in a very general way.
</p>
<H2><a name="SWIGPlus_nn6"></a>6.6 Simple C++ wrapping</H2>
<p>
The following code shows a SWIG interface file for a simple C++
class.</p>
<div class="code"><pre>
%module list
%{
#include "list.h"
%}
// Very simple C++ example for linked list
class List {
public:
List();
~List();
int search(char *value);
void insert(char *);
void remove(char *);
char *get(int n);
int length;
static void print(List *l);
};
</pre></div>
<p>
To generate wrappers for this class, SWIG first reduces the class to a collection of low-level C-style
accessor functions which are then used by the proxy classes.
</p>
<H3><a name="SWIGPlus_nn7"></a>6.6.1 Constructors and destructors</H3>
<p>
C++ constructors and destructors are translated into accessor
functions such as the following :</p>
<div class="code"><pre>
List * new_List(void) {
return new List;
}
void delete_List(List *l) {
delete l;
}
</pre></div>
<H3><a name="SWIGPlus_nn8"></a>6.6.2 Default constructors, copy constructors and implicit destructors</H3>
<p>
Following the C++ rules for implicit constructor and destructors, SWIG
will automatically assume there is one even when they are not
explicitly declared in the class interface.
</p>
<p>
In general then:
</p>
<ul>
<li>
If a C++ class does not declare any explicit constructor, SWIG will
automatically generate a wrapper for one.
</li>
<li>
If a C++ class does not declare an explicit copy constructor, SWIG will
automatically generate a wrapper for one if the <tt>%copyctor</tt> is used.
</li>
<li>
If a C++ class does not declare an explicit destructor, SWIG will
automatically generate a wrapper for one.
</li>
</ul>
<p>
And as in C++, a few rules that alters the previous behavior:
</p>
<ul>
<li>A default constructor is not created if a class already defines a constructor with arguments.
</li>
<li>Default constructors are not generated for classes with pure virtual methods or for classes that
inherit from an abstract class, but don't provide definitions for all of the pure methods.
</li>
<li>A default constructor is not created unless all base classes support a
default constructor.
</li>
<li>Default constructors and implicit destructors are not created if a class
defines them in a <tt>private</tt> or <tt>protected</tt> section.
</li>
<li>Default constructors and implicit destructors are not created if any base
class defines a non-public default constructor or destructor.
</li>
</ul>
<p>
SWIG should never generate a default constructor, copy constructor or
default destructor wrapper for a class in which it is illegal to do so. In
some cases, however, it could be necessary (if the complete class
declaration is not visible from SWIG, and one of the above rules is
violated) or desired (to reduce the size of the final interface) by
manually disabling the implicit constructor/destructor generation.
</p>
<p>
To manually disable these, the <tt>%nodefaultctor</tt> and <tt>%nodefaultdtor</tt>
<a href="Customization.html#Customization_feature_flags">feature flag</a> directives
can be used. Note that these directives only affects the
implicit generation, and they have no effect if the default/copy
constructors or destructor are explicitly declared in the class
interface.
</p>
<p>
For example:
</p>
<div class="code">
<pre>
%nodefaultctor Foo; // Disable the default constructor for class Foo.
class Foo { // No default constructor is generated, unless one is declared
...
};
class Bar { // A default constructor is generated, if possible
...
};
</pre>
</div>
<p>
The directive <tt>%nodefaultctor</tt> can also be applied "globally", as in:
</p>
<div class="code">
<pre>
%nodefaultctor; // Disable creation of default constructors
class Foo { // No default constructor is generated, unless one is declared
...
};
class Bar {
public:
Bar(); // The default constructor is generated, since one is declared
};
%clearnodefaultctor; // Enable the creation of default constructors again
</pre>
</div>
<p>
The corresponding <tt>%nodefaultdtor</tt> directive can be used
to disable the generation of the default or implicit destructor, if
needed. Be aware, however, that this could lead to memory leaks in the
target language. Hence, it is recommended to use this directive only
in well known cases. For example:
</p>
<div class="code">
<pre>
%nodefaultdtor Foo; // Disable the implicit/default destructor for class Foo.
class Foo { // No destructor is generated, unless one is declared
...
};
</pre>
</div>
<p>
<b>Compatibility Note:</b> The generation of default
constructors/implicit destructors was made the default behavior in SWIG
1.3.7. This may break certain older modules, but the old behavior can
be easily restored using <tt>%nodefault</tt> or the
<tt>-nodefault</tt> command line option. Furthermore, in order for
SWIG to properly generate (or not generate) default constructors, it
must be able to gather information from both the <tt>private</tt> and
<tt>protected</tt> sections (specifically, it needs to know if a private or
protected constructor/destructor is defined). In older versions of
SWIG, it was fairly common to simply remove or comment out
the private and protected sections of a class due to parser limitations.
However, this removal may now cause SWIG to erroneously generate constructors
for classes that define a constructor in those sections. Consider restoring
those sections in the interface or using <tt>%nodefault</tt> to fix the problem.
</p>
<p>
<b>Note:</b> The <tt>%nodefault</tt>
directive/<tt>-nodefault</tt> options described above, which disable both the default
constructor and the implicit destructors, could lead to memory
leaks, and so it is strongly recommended to not use them.
</p>
<H3><a name="SWIGPlus_nn9"></a>6.6.3 When constructor wrappers aren't created</H3>
<p>
If a class defines a constructor, SWIG normally tries to generate a wrapper for it. However, SWIG will
not generate a constructor wrapper if it thinks that it will result in illegal wrapper code. There are really
two cases where this might show up.
</p>
<p>
First, SWIG won't generate wrappers for protected or private constructors. For example:
</p>
<div class="code">
<pre>
class Foo {
protected:
Foo(); // Not wrapped.
public:
...
};
</pre>
</div>
<p>
Next, SWIG won't generate wrappers for a class if it appears to be abstract--that is, it has undefined
pure virtual methods. Here are some examples:
</p>
<div class="code">
<pre>
class Bar {
public:
Bar(); // Not wrapped. Bar is abstract.
virtual void spam(void) = 0;
};
class Grok : public Bar {
public:
Grok(); // Not wrapped. No implementation of abstract spam().
};
</pre>
</div>
<p>
Some users are surprised (or confused) to find missing constructor wrappers in their interfaces. In almost
all cases, this is caused when classes are determined to be abstract. To see if this is the case, run SWIG with
all of its warnings turned on:
</p>
<div class="shell">
<pre>
% swig -Wall -python module.i
</pre>
</div>
<p>
In this mode, SWIG will issue a warning for all abstract classes. It is possible to force a class to be
non-abstract using this:
</p>
<div class="code">
<pre>
%feature("notabstract") Foo;
class Foo : public Bar {
public:
Foo(); // Generated no matter what---not abstract.
...
};
</pre>
</div>
<p>
More information about <tt>%feature</tt> can be found in the <a href="Customization.html#Customization">Customization features</a> chapter.
</p>
<H3><a name="SWIGPlus_nn10"></a>6.6.4 Copy constructors</H3>
<p>
If a class defines more than one constructor, its behavior depends on the capabilities of the
target language. If overloading is supported, the copy constructor is accessible using
the normal constructor function. For example, if you have this:
</p>
<div class="code">
<pre>
class List {
public:
List();
List(const List &); // Copy constructor
...
};
</pre>
</div>
<p>
then the copy constructor can be used as follows:
</p>
<div class="targetlang">
<pre>
x = List() # Create a list
y = List(x) # Copy list x
</pre>
</div>
<p>
If the target language does not support overloading, then the copy constructor is available
through a special function like this:
</p>
<div class="code">
<pre>
List *copy_List(List *f) {
return new List(*f);
}
</pre>
</div>
<p>
<b>Note:</b> For a class <tt>X</tt>, SWIG only treats a constructor as
a copy constructor if it can be applied to an object of type
<tt>X</tt> or <tt>X *</tt>. If more than one copy constructor is
defined, only the first definition that appears is used as the copy
constructor--other definitions will result in a name-clash.
Constructors such as <tt>X(const X &)</tt>, <tt>X(X &)</tt>, and
<tt>X(X *)</tt> are handled as copy constructors in SWIG.
</p>
<p>
<b>Note:</b> SWIG does <em>not</em> generate a copy constructor
wrapper unless one is explicitly declared in the class. This differs
from the treatment of default constructors and destructors.
However, copy constructor wrappers can be generated if using the <tt>copyctor</tt>
<a href="Customization.html#Customization_feature_flags">feature flag</a>. For example:
</p>
<div class="code">
<pre>
%copyctor List;
class List {
public:
List();
};
</pre>
</div>
<p>
Will generate a copy constructor wrapper for <tt>List</tt>.
</p>
<p>
<b>Compatibility note:</b> Special support for copy constructors was
not added until SWIG-1.3.12. In previous versions, copy constructors
could be wrapped, but they had to be renamed. For example:
</p>
<div class="code">
<pre>
class Foo {
public:
Foo();
%name(CopyFoo) Foo(const Foo &);
...
};
</pre>
</div>
<p>
For backwards compatibility, SWIG does not perform any special
copy-constructor handling if the constructor has been manually
renamed. For instance, in the above example, the name of the
constructor is set to <tt>new_CopyFoo()</tt>. This is the same as in
older versions.
</p>
<H3><a name="SWIGPlus_nn11"></a>6.6.5 Member functions</H3>
<p>
All member functions are roughly translated into accessor functions like this :</p>
<div class="code"><pre>
int List_search(List *obj, char *value) {
return obj->search(value);
}
</pre></div>
<p>
This translation is the same even if the member function has been
declared as <tt>virtual</tt>.
</p>
<p>
It should be noted that SWIG does not <em>actually</em> create a C accessor
function in the code it generates. Instead, member access such as
<tt>obj->search(value)</tt> is directly inlined into the generated
wrapper functions. However, the name and calling convention of the
low-level procedural wrappers match the accessor function prototype described above.
</p>
<H3><a name="SWIGPlus_nn12"></a>6.6.6 Static members</H3>
<p>
Static member functions are called directly without making any special
transformations. For example, the static member function
<tt>print(List *l)</tt> directly invokes <tt>List::print(List *l)</tt>
in the generated wrapper code.
</p>
<H3><a name="SWIGPlus_member_data"></a>6.6.7 Member data</H3>
<p>
Member data is handled in exactly the same manner as for C
structures. A pair of accessor functions are effectively created. For example
:</p>
<div class="code"><pre>
int List_length_get(List *obj) {
return obj->length;
}
int List_length_set(List *obj, int value) {
obj->length = value;
return value;
}
</pre></div>
<p>
A read-only member can be created using the <tt>%immutable</tt> and <tt>%mutable</tt>
<a href="Customization.html#Customization_feature_flags">feature flag</a> directive.
For example, we probably wouldn't want
the user to change the length of a list so we could do the following
to make the value available, but read-only.</p>
<div class="code"><pre>
class List {
public:
...
%immutable;
int length;
%mutable;
...
};
</pre></div>
<p>
Alternatively, you can specify an immutable member in advance like this:
</p>
<div class="code">
<pre>
%immutable List::length;
...
class List {
...
int length; // Immutable by above directive
...
};
</pre>
</div>
<p>
Similarly, all data attributes declared as <tt>const</tt> are wrapped as read-only members.
</p>
<p>
By default, SWIG uses the const reference typemaps for members that are primitive types.
There are some subtle issues when wrapping data members that are
not primitive types, such as classes. For instance, if you had another class like this,
</p>
<div class="code">
<pre>
class Foo {
public:
List items;
...
</pre>
</div>
<p>
then the low-level accessor to the <tt>items</tt> member actually uses pointers.
For example:
</p>
<div class="code">
<pre>
List *Foo_items_get(Foo *self) {
return &self->items;
}
void Foo_items_set(Foo *self, List *value) {
self->items = *value;
}
</pre>
</div>
<p>
More information about this can be found in the SWIG Basics chapter,
<a href="SWIG.html#SWIG_structure_data_members">Structure data members</a> section.
</p>
<p>
The wrapper code to generate the accessors for classes comes from the pointer typemaps.
This can be somewhat unnatural for some types.
For example, a user would expect the STL std::string class member variables to be wrapped as a string in the target language,
rather than a pointer to this class.
The const reference typemaps offer this type of marshalling, so there is a feature to tell SWIG to use the const reference typemaps rather than the pointer typemaps.
It is the naturalvar feature and can be used to effectively change the way accessors are generated to the following:
</p>
<div class="code">
<pre>
const List &Foo_items_get(Foo *self) {
return self->items;
}
void Foo_items_set(Foo *self, const List &value) {
self->items = value;
}
</pre>
</div>
<p>
The <tt>%naturalvar</tt> directive is a macro for, and hence equivalent to, <tt>%feature("naturalvar")</tt>. It can be used as follows:
</p>
<div class="code">
<pre>
// All List variables will use const List& typemaps
%naturalvar List;
// Only Foo::myList will use const List& typemaps
%naturalvar Foo::myList;
struct Foo {
List myList;
};
// All non-primitive types will use const reference typemaps
%naturalvar;
</pre>
</div>
<p>
The observant reader will notice that <tt>%naturalvar</tt> works like any other
<a href="Customization.html#Customization_feature_flags">feature flag</a> directive but with some extra flexibility.
The first of the example usages above shows <tt>%naturalvar</tt> attaching to the <tt>myList</tt>'s variable type, that is the <tt>List</tt> class.
The second usage shows <tt>%naturalvar</tt> attaching to the variable name.
Hence the naturalvar feature can be used on either the variable's name or type.
Note that using the naturalvar feature on a variable's name overrides any naturalvar feature attached to the variable's type.
</p>
<p>
It is generally a good idea to use this feature globally as the reference typemaps have extra NULL checking compared to the pointer typemaps.
A pointer can be NULL, whereas a reference cannot, so the extra checking ensures that the target language user does not pass in a value that translates
to a NULL pointer and thereby preventing any potential NULL pointer dereferences.
The <tt>%naturalvar</tt> feature will apply to global variables in addition to member variables in some language modules, eg C# and Java.
</p>
<p>
The naturalvar behavior can also be turned on as a global setting via the <tt>-naturalvar</tt> commandline option
or the module mode option, <tt>%module(naturalvar=1)</tt>.
However, any use of <tt>%feature("naturalvar")</tt> will override the global setting.
</p>
<p>
<b>Compatibility note:</b> The <tt>%naturalvar</tt> feature was introduced in SWIG-1.3.28, prior to which it was necessary to manually apply the const reference
typemaps, eg <tt>%apply const std::string & { std::string * }</tt>, but this example would also apply the typemaps to methods taking a <tt>std::string</tt> pointer.
</p>
<p>
<b>Compatibility note:</b> Read-only access used to be controlled by a pair of directives
<tt>%readonly</tt> and <tt>%readwrite</tt>. Although these directives still work, they
generate a warning message. Simply change the directives to <tt>%immutable;</tt> and
<tt>%mutable;</tt> to silence the warning. Don't forget the extra semicolon!
</p>
<p>
<b>Compatibility note:</b> Prior to SWIG-1.3.12, all members of unknown type were
wrapped into accessor functions using pointers. For example, if you had a structure
like this
</p>
<div class="code">
<pre>
struct Foo {
size_t len;
};
</pre>
</div>
<p>
and nothing was known about <tt>size_t</tt>, then accessors would be
written to work with <tt>size_t *</tt>. Starting in SWIG-1.3.12, this
behavior has been modified. Specifically, pointers will <em>only</em>
be used if SWIG knows that a datatype corresponds to a structure or
class. Therefore, the above code would be wrapped into accessors
involving <tt>size_t</tt>. This change is subtle, but it smooths over
a few problems related to structure wrapping and some of SWIG's
customization features.
</p>
<H2><a name="SWIGPlus_default_args"></a>6.7 Default arguments</H2>
<p>
SWIG will wrap all types of functions that have default arguments. For example member functions:
</p>
<div class="code">
<pre>
class Foo {
public:
void bar(int x, int y = 3, int z = 4);
};
</pre>
</div>
<p>
SWIG handles default arguments by generating an extra overloaded method for each defaulted argument.
SWIG is effectively handling methods with default arguments as if it was wrapping the equivalent overloaded methods.
Thus for the example above, it is as if we had instead given the following to SWIG:
</p>
<div class="code">
<pre>
class Foo {
public:
void bar(int x, int y, int z);
void bar(int x, int y);
void bar(int x);
};
</pre>
</div>
<p>
The wrappers produced are exactly the same as if the above code was instead fed into SWIG.
Details of this are covered later in the <a href="#SWIGPlus_overloaded_methods">Wrapping Overloaded Functions and Methods</a> section.
This approach allows SWIG to wrap all possible default arguments, but can be verbose.
For example if a method has ten default arguments, then eleven wrapper methods are generated.
</p>
<p>
Please see the <a href="Customization.html#Customization_features_default_args">Features and default arguments</a>
section for more information on using <tt>%feature</tt> with functions with default arguments.
The <a href="#SWIGPlus_ambiguity_resolution_renaming">Ambiguity resolution and renaming</a> section
also deals with using <tt>%rename</tt> and <tt>%ignore</tt> on methods with default arguments.
If you are writing your own typemaps for types used in methods with default arguments, you may also need to write a <tt>typecheck</tt> typemap.
See the <a href="Typemaps.html#Typemaps_overloading">Typemaps and overloading</a> section for details or otherwise
use the <tt>compactdefaultargs</tt> feature flag as mentioned below.
</p>
<p>
<b>Compatibility note:</b> Versions of SWIG prior to SWIG-1.3.23 wrapped default arguments slightly differently.
Instead a single wrapper method was generated and the default values were copied into the C++ wrappers
so that the method being wrapped was then called with all the arguments specified.
If the size of the wrappers are a concern then this approach to wrapping methods with default arguments
can be re-activated by using the <tt>compactdefaultargs</tt>
<a href="Customization.html#Customization_feature_flags">feature flag</a>.
</p>
<div class="code">
<pre>
%feature("compactdefaultargs") Foo::bar;
class Foo {
public:
void bar(int x, int y = 3, int z = 4);
};
</pre>
</div>
<p>
This is great for reducing the size of the wrappers, but the caveat is it does not work for the statically typed languages,
such as C# and Java,
which don't have optional arguments in the language,
Another restriction of this feature is that it cannot handle default arguments that are not public.
The following example illustrates this:
</p>
<div class="code">
<pre>
class Foo {
private:
static const int spam;
public:
void bar(int x, int y = spam); // Won't work with %feature("compactdefaultargs") -
// private default value
};
</pre>
</div>
<p>
This produces uncompileable wrapper code because default values in C++ are
evaluated in the same scope as the member function whereas SWIG
evaluates them in the scope of a wrapper function (meaning that the
values have to be public).
</p>
<p>
This feature is automatically turned on when wrapping <a href="SWIG.html#SWIG_default_args">C code with default arguments</a>
and whenever keyword arguments (kwargs) are specified for either C or C++ code.
Keyword arguments are a language feature of some scripting languages, for example Ruby and Python.
SWIG is unable to support kwargs when wrapping overloaded methods, so the default approach cannot be used.
</p>
<H2><a name="SWIGPlus_nn15"></a>6.8 Protection</H2>
<p>
SWIG wraps class members that are public following the C++
conventions, i.e., by explicit public declaration or by the use of
the <tt> using</tt> directive. In general, anything specified in a
private or protected section will be ignored, although the internal
code generator sometimes looks at the contents of the private and
protected sections so that it can properly generate code for default
constructors and destructors. Directors could also modify the way
non-public virtual protected members are treated.
</p>
<p>
By default, members of a class definition are assumed to be private
until you explicitly give a `<tt>public:</tt>' declaration (This is
the same convention used by C++).
</p>
<H2><a name="SWIGPlus_nn16"></a>6.9 Enums and constants</H2>
<p>
Enumerations and constants are handled differently by the different language modules and are described in detail in the appropriate language chapter.
However, many languages map enums and constants in a class definition
into constants with the classname as a prefix. For example :</p>
<div class="code"><pre>
class Swig {
public:
enum {ALE, LAGER, PORTER, STOUT};
};
</pre></div>
<p>
Generates the following set of constants in the target scripting language :</p>
<div class="targetlang"><pre>
Swig_ALE = Swig::ALE
Swig_LAGER = Swig::LAGER
Swig_PORTER = Swig::PORTER
Swig_STOUT = Swig::STOUT
</pre></div>
<p>
Members declared as <tt>const</tt> are wrapped as read-only members and do not create constants.
</p>
<H2><a name="SWIGPlus_nn17"></a>6.10 Friends</H2>
<p>
Friend declarations are recognised by SWIG. For example, if
you have this code:
</p>
<div class="code">
<pre>
class Foo {
public:
...
friend void blah(Foo *f);
...
};
</pre>
</div>
<p>
then the <tt>friend</tt> declaration does result in a wrapper code
equivalent to one generated for the following declaration
</p>
<div class="code">
<pre>
class Foo {
public:
...
};
void blah(Foo *f);
</pre>
</div>
<p>
A friend declaration, as in C++, is understood to be in the same scope
where the class is declared, hence, you can have
</p>
<div class="code">
<pre>
%ignore bar::blah(Foo *f);
namespace bar {
class Foo {
public:
...
friend void blah(Foo *f);
...
};
}
</pre>
</div>
<p>
and a wrapper for the method 'blah' will not be generated.
</p>
<H2><a name="SWIGPlus_nn18"></a>6.11 References and pointers</H2>
<p>
C++ references are supported, but SWIG transforms them back into pointers. For example,
a declaration like this :</p>
<div class="code"><pre>
class Foo {
public:
double bar(double &a);
}
</pre></div>
<p>
has a low-level accessor
</p>
<div class="code"><pre>
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
</pre></div>
<p>
As a special case, most language modules pass <tt>const</tt> references to primitive datatypes (<tt>int</tt>, <tt>short</tt>,
<tt>float</tt>, etc.) by value instead of pointers. For example, if you have a function like this,
</p>
<div class="code">
<pre>
void foo(const int &x);
</pre>
</div>
<p>
it is called from a script as follows:
</p>
<div class="targetlang">
<pre>
foo(3) # Notice pass by value
</pre>
</div>
<p>
Functions that return a reference are remapped to return a pointer instead.
For example:
</p>
<div class="code"><pre>
class Bar {
public:
Foo &spam();
};
</pre>
</div>
<p>
Generates an accessor like this:
</p>
<div class="code">
<pre>
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
</pre>
</div>
<p>
However, functions that return <tt>const</tt> references to primitive datatypes (<tt>int</tt>, <tt>short</tt>, etc.) normally
return the result as a value rather than a pointer. For example, a function like this,
</p>
<div class="code">
<pre>
const int &bar();
</pre>
</div>
<p>
will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.
</p>
<P>
Don't return references to objects allocated as local variables on the
stack. SWIG doesn't make a copy of the objects so this will probably
cause your program to crash.
<p>
<b>Note:</b> The special treatment for references to primitive datatypes is necessary to provide
more seamless integration with more advanced C++ wrapping applications---especially related to
templates and the STL. This was first added in SWIG-1.3.12.
</p>
<H2><a name="SWIGPlus_nn19"></a>6.12 Pass and return by value</H2>
<p>
Occasionally, a C++ program will pass and return class objects by value. For example, a function
like this might appear:
</p>
<div class="code">
<pre>
Vector cross_product(Vector a, Vector b);
</pre>
</div>
<p>
If no information is supplied about <tt>Vector</tt>, SWIG creates a wrapper function similar to the
following:
</p>
<div class="code">
<pre>
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}</pre>
</div>
<p>
In order for the wrapper code to compile, <tt>Vector</tt> must define a copy constructor and a
default constructor.
</p>
<p>
If <tt>Vector</tt> is defined as a class in the interface, but it does not
support a default constructor, SWIG changes the wrapper code by encapsulating
the arguments inside a special C++ template wrapper class, through a process
called the "Fulton Transform". This produces a wrapper that looks like this:
</p>
<div class="code">
<pre>
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
</pre>
</div>
<p>
This transformation is a little sneaky, but it provides support for
pass-by-value even when a class does not provide a default constructor
and it makes it possible to properly support a number of SWIG's
customization options. The definition of <tt>SwigValueWrapper</tt>
can be found by reading the SWIG wrapper code. This class is really nothing more than a thin
wrapper around a pointer.
</p>
<p>
Although SWIG usually detects the classes to which the Fulton Transform should
be applied, in some situations it's necessary to override it. That's done with
<tt>%feature("valuewrapper")</tt> to ensure it is used and <tt>%feature("novaluewrapper")</tt>
to ensure it is not used:
</p>
<div class="code"><pre>
%feature("novaluewrapper") A;
class A;
%feature("valuewrapper") B;
struct B {
B();
// ....
};
</pre></div>
<p>
It is well worth considering turning this feature on for classes that do have a default constructor.
It will remove a redundant constructor call at the point of the variable declaration in the wrapper,
so will generate notably better performance for large objects or for classes with expensive construction.
Alternatively consider returning a reference or a pointer.
</p>
<p>
<b>Note:</b> this transformation has no effect on typemaps
or any other part of SWIG---it should be transparent except that you
may see this code when reading the SWIG output file.
</p>
<p>
<b>
Note: </b>This template transformation is new in SWIG-1.3.11 and may be refined in
future SWIG releases. In practice, it is only absolutely necessary to do this for
classes that don't define a default constructor.
</p>
<p>
<b>Note:</b> The use of this template only occurs when objects are passed or returned by value.
It is not used for C++ pointers or references.
</p>
<H2><a name="SWIGPlus_nn20"></a>6.13 Inheritance</H2>
<p>
SWIG supports C++ inheritance of classes and allows both single and
multiple inheritance, as limited or allowed by the target
language. The SWIG type-checker knows about the relationship between
base and derived classes and allows pointers to any object of a
derived class to be used in functions of a base class. The
type-checker properly casts pointer values and is safe to use with
multiple inheritance.
</p>
<p> SWIG treats private or protected inheritance as close to the C++
spirit, and target language capabilities, as possible. In most
cases, this means that SWIG will parse the non-public inheritance
declarations, but that will have no effect in the generated code,
besides the implicit policies derived for constructors and
destructors.
</p>
<p>
The following example shows how SWIG handles inheritance. For clarity,
the full C++ code has been omitted.</p>
<div class="code"><pre>
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
</pre></div>
<p>
When wrapped into Python, we can perform the following operations (shown using the low level Python accessors):
</p>
<div class="targetlang"><pre>
$ python
>>> import shapes
>>> circle = shapes.new_Circle(7)
>>> square = shapes.new_Square(10)
>>> print shapes.Circle_area(circle)
153.93804004599999757
>>> print shapes.Shape_area(circle)
153.93804004599999757
>>> print shapes.Shape_area(square)
100.00000000000000000
>>> shapes.Shape_set_location(square,2,-3)
>>> print shapes.Shape_perimeter(square)
40.00000000000000000
>>>
</pre></div>
<p>
In this example, Circle and Square objects have been created. Member
functions can be invoked on each object by making calls to
<tt>Circle_area</tt>, <tt>Square_area</tt>, and so on. However, the same
results can be accomplished by simply using the <tt>Shape_area</tt>
function on either object.
</p>
<p>
One important point concerning inheritance is that the low-level
accessor functions are only generated for classes in which they are
actually declared. For instance, in the above example, the method
<tt>set_location()</tt> is only accessible as
<tt>Shape_set_location()</tt> and not as
<tt>Circle_set_location()</tt> or <tt>Square_set_location()</tt>. Of
course, the <tt>Shape_set_location()</tt> function will accept any
kind of object derived from Shape. Similarly, accessor functions for
the attributes <tt>x</tt> and <tt>y</tt> are generated as
<tt>Shape_x_get()</tt>, <tt>Shape_x_set()</tt>,
<tt>Shape_y_get()</tt>, and <tt>Shape_y_set()</tt>. Functions such as
<tt>Circle_x_get()</tt> are not available--instead you should use
<tt>Shape_x_get()</tt>.
</p>
<p>
Note that there is a one to one correlation between the low-level accessor functions and
the proxy methods and therefore there is also a one to one correlation between
the C++ class methods and the generated proxy class methods.
</p>
<p>
<b>Note:</b> For the best results, SWIG requires all
base classes to be defined in an interface. Otherwise, you may get a
warning message like this:
</p>
<div class="shell">
<pre>
example.i:18: Warning 401: Nothing known about base class 'Foo'. Ignored.
</pre>
</div>
<p>
If any base class is undefined, SWIG still generates correct type
relationships. For instance, a function accepting a <tt>Foo *</tt>
will accept any object derived from <tt>Foo</tt> regardless of whether
or not SWIG actually wrapped the <tt>Foo</tt> class. If you really
don't want to generate wrappers for the base class, but you want to
silence the warning, you might consider using the <tt>%import</tt>
directive to include the file that defines <tt>Foo</tt>.
<tt>%import</tt> simply gathers type information, but doesn't generate
wrappers. Alternatively, you could just define <tt>Foo</tt> as an empty class
in the SWIG interface or use
<a href="Warnings.html#Warnings_suppression">warning suppression</a>.
</p>
<p>
<b>Note:</b> <tt>typedef</tt>-names <em>can</em> be used as base classes. For example:
</p>
<div class="code">
<pre>
class Foo {
...
};
typedef Foo FooObj;
class Bar : public FooObj { // Ok. Base class is Foo
...
};
</pre>
</div>
<p>
Similarly, <tt>typedef</tt> allows unnamed structures to be used as base classes. For example:
</p>
<div class="code">
<pre>
typedef struct {
...
} Foo;
class Bar : public Foo { // Ok.
...
};
</pre>
</div>
<p>
<b>Compatibility Note:</b> Starting in version 1.3.7, SWIG only
generates low-level accessor wrappers for the declarations that are
actually defined in each class. This differs from SWIG1.1 which used
to inherit all of the declarations defined in base classes and
regenerate specialized accessor functions such as
<tt>Circle_x_get()</tt>, <tt>Square_x_get()</tt>,
<tt>Circle_set_location()</tt>, and <tt>Square_set_location()</tt>.
This behavior resulted in huge amounts of replicated code for large
class hierarchies and made it awkward to build applications spread
across multiple modules (since accessor functions are duplicated in
every single module). It is also unnecessary to have such wrappers
when advanced features like proxy classes are used.
<b>Note:</b> Further optimizations are enabled when using the
<tt>-fvirtual</tt> option, which avoids the regenerating of wrapper
functions for virtual members that are already defined in a base
class.
</p>
<H2><a name="SWIGPlus_nn21"></a>6.14 A brief discussion of multiple inheritance, pointers, and type checking</H2>
<p>
When a target scripting language refers to a C++ object, it normally
uses a tagged pointer object that contains both the value of the
pointer and a type string. For example, in Tcl, a C++ pointer might
be encoded as a string like this:
</p>
<div class="diagram">
<pre>
_808fea88_p_Circle
</pre>
</div>
<p>
A somewhat common question is whether or not the type-tag could be safely
removed from the pointer. For instance, to get better performance, could you
strip all type tags and just use simple integers instead?
</p>
<p>
In general, the answer to this question is no. In the wrappers, all
pointers are converted into a common data representation in the target
language. Typically this is the equivalent of casting a pointer to <tt>void *</tt>.
This means that any C++ type information associated with the pointer is
lost in the conversion.
</p>
<p>
The problem with losing type information is that it is needed to
properly support many advanced C++ features--especially multiple
inheritance. For example, suppose you had code like this:
</p>
<div class="code">
<pre>
class A {
public:
int x;
};
class B {
public:
int y;
};
class C : public A, public B {
};
int A_function(A *a) {
return a->x;
}
int B_function(B *b) {
return b->y;
}
</pre>
</div>
<p>
Now, consider the following code that uses <tt>void *</tt>.
</p>
<div class="code">
<pre>
C *c = new C();
void *p = (void *) c;
...
int x = A_function((A *) p);
int y = B_function((B *) p);
</pre>
</div>
<p>
In this code, both <tt>A_function()</tt> and <tt>B_function()</tt> may
legally accept an object of type <tt>C *</tt> (via inheritance).
However, one of the functions will always return the wrong result when
used as shown. The reason for this is that even though <tt>p</tt>
points to an object of type <tt>C</tt>, the casting operation doesn't
work like you would expect. Internally, this has to do with the data
representation of <tt>C</tt>. With multiple inheritance, the data from
each base class is stacked together. For example:
</p>
<div class="diagram">
<pre>
------------ <--- (C *), (A *)
| A |
|------------| <--- (B *)
| B |
------------
</pre>
</div>
<p>
Because of this stacking, a pointer of type <tt>C *</tt> may change
value when it is converted to a <tt>A *</tt> or <tt>B *</tt>.
However, this adjustment does <em>not</em> occur if you are converting from a
<tt>void *</tt>.
</p>
<p>
The use of type tags marks all pointers with the real type of the
underlying object. This extra information is then used by SWIG
generated wrappers to correctly cast pointer values under inheritance
(avoiding the above problem).
</p>
<p>
Some of the language modules are able to solve the problem by storing multiple instances of the pointer, for example, <tt>A *</tt>,
in the A proxy class as well as <tt>C *</tt> in the C proxy class. The correct cast can then be made by choosing the correct <tt>void *</tt>
pointer to use and is guaranteed to work as the cast to a void pointer and back to the same type does not lose any type information:
</p>
<div class="code">
<pre>
C *c = new C();
void *p = (void *) c;
void *pA = (void *) c;
void *pB = (void *) c;
...
int x = A_function((A *) pA);
int y = B_function((B *) pB);
</pre>
</div>
<p>
In practice, the pointer is held as an integral number in the target language proxy class.
</p>
<H2><a name="SWIGPlus_overloaded_methods"></a>6.15 Wrapping Overloaded Functions and Methods</H2>
<p>
In many language modules, SWIG provides partial support for overloaded functions, methods, and
constructors. For example, if you supply SWIG with overloaded functions like this:
</p>
<div class="code">
<pre>
void foo(int x) {
printf("x is %d\n", x);
}
void foo(char *x) {
printf("x is '%s'\n", x);
}
</pre>
</div>
<p>
The function is used in a completely natural way. For example:
</p>
<div class="targetlang">
<pre>
>>> foo(3)
x is 3
>>> foo("hello")
x is 'hello'
>>>
</pre>
</div>
<p>
Overloading works in a similar manner for methods and constructors. For example if you have
this code,
</p>
<div class="code">
<pre>
class Foo {
public:
Foo();
Foo(const Foo &); // Copy constructor
void bar(int x);
void bar(char *s, int y);
};
</pre>
</div>
<p>
it might be used like this
</p>
<div class="targetlang">
<pre>
>>> f = Foo() # Create a Foo
>>> f.bar(3)
>>> g = Foo(f) # Copy Foo
>>> f.bar("hello",2)
</pre>
</div>
<H3><a name="SWIGPlus_nn24"></a>6.15.1 Dispatch function generation</H3>
<p>
The implementation of overloaded functions and methods is somewhat
complicated due to the dynamic nature of scripting languages. Unlike
C++, which binds overloaded methods at compile time, SWIG must
determine the proper function as a runtime check for scripting language targets. This check is
further complicated by the typeless nature of certain scripting languages. For instance,
in Tcl, all types are simply strings. Therefore, if you have two overloaded functions
like this,
</p>
<div class="code">
<pre>
void foo(char *x);
void foo(int x);
</pre>
</div>
<p>
the order in which the arguments are checked plays a rather critical role.
</p>
<p>
For statically typed languages, SWIG uses the language's method overloading mechanism.
To implement overloading for the scripting languages, SWIG generates a dispatch function that checks the
number of passed arguments and their types. To create this function, SWIG
first examines all of the overloaded methods and ranks them according
to the following rules:
</p>
<ol>
<li><b>Number of required arguments.</b> Methods are sorted by increasing number of
required arguments.
</li>
<li><p><b>Argument type precedence.</b> All C++ datatypes are assigned a numeric type precedence value
(which is determined by the language module).</p>
<div class="diagram">
<pre>
Type Precedence
---------------- ----------
TYPE * 0 (High)
void * 20
Integers 40
Floating point 60
char 80
Strings 100 (Low)
</pre>
</div>
<p>
Using these precedence values, overloaded methods with the same number of required arguments are sorted in increased
order of precedence values.
</p>
</li>
</ol>
<p>
This may sound very confusing, but an example will help. Consider the following collection of
overloaded methods:
</p>
<div class="code">
<pre>
void foo(double);
void foo(int);
void foo(Bar *);
void foo();
void foo(int x, int y, int z, int w);
void foo(int x, int y, int z = 3);
void foo(double x, double y);
void foo(double x, Bar *z);
</pre>
</div>
<p>
The first rule simply ranks the functions by required argument count.
This would produce the following list:
</p>
<div class="diagram">
<pre>
rank
-----
[0] foo()
[1] foo(double);
[2] foo(int);
[3] foo(Bar *);
[4] foo(int x, int y, int z = 3);
[5] foo(double x, double y)
[6] foo(double x, Bar *z)
[7] foo(int x, int y, int z, int w);
</pre>
</div>
<p>
The second rule, simply refines the ranking by looking at argument type precedence values.
</p>
<div class="diagram">
<pre>
rank
-----
[0] foo()
[1] foo(Bar *);
[2] foo(int);
[3] foo(double);
[4] foo(int x, int y, int z = 3);
[5] foo(double x, Bar *z)
[6] foo(double x, double y)
[7] foo(int x, int y, int z, int w);
</pre>
</div>
<p>
Finally, to generate the dispatch function, the arguments passed to an overloaded method are simply
checked in the same order as they appear in this ranking.
</p>
<p>
If you're still confused, don't worry about it---SWIG is probably doing the right thing.
</p>
<H3><a name="SWIGPlus_nn25"></a>6.15.2 Ambiguity in Overloading</H3>
<p>
Regrettably, SWIG is not able to support every possible use of valid C++ overloading. Consider
the following example:
</p>
<div class="code">
<pre>
void foo(int x);
void foo(long x);
</pre>
</div>
<p>
In C++, this is perfectly legal. However, in a scripting language, there is generally only one kind of integer
object. Therefore, which one of these functions do you pick? Clearly, there is no way to truly make a distinction
just by looking at the value of the integer itself (<tt>int</tt> and <tt>long</tt> may even be the same precision).
Therefore, when SWIG encounters this situation, it may generate a warning message like this for scripting languages:
</p>
<div class="shell">
<pre>
example.i:4: Warning 509: Overloaded method foo(long) effectively ignored,
example.i:3: Warning 509: as it is shadowed by foo(int).
</pre>
</div>
<p>
or for statically typed languages like Java:
</p>
<div class="shell">
<pre>
example.i:4: Warning 516: Overloaded method foo(long) ignored,
example.i:3: Warning 516: using foo(int) instead.
at example.i:3 used.
</pre>
</div>
<p>
This means that the second overloaded function will be inaccessible
from a scripting interface or the method won't be wrapped at all.
This is done as SWIG does not know how to disambiguate it from an earlier method.
</p>
<p>
Ambiguity problems are known to arise in the following situations:
</p>
<ul>
<li>Integer conversions. Datatypes such as <tt>int</tt>, <tt>long</tt>, and <tt>short</tt> cannot be disambiguated in some languages. Shown above.
</li>
<li>Floating point conversion. <tt>float</tt> and <tt>double</tt> can not be disambiguated in some languages.
</li>
<li>Pointers and references. For example, <tt>Foo *</tt> and <tt>Foo &</tt>.
</li>
<li>Pointers and arrays. For example, <tt>Foo *</tt> and <tt>Foo [4]</tt>.
</li>
<li>Pointers and instances. For example, <tt>Foo</tt> and <tt>Foo *</tt>. Note: SWIG converts all
instances to pointers.
</li>
<li>Qualifiers. For example, <tt>const Foo *</tt> and <tt>Foo *</tt>.
</li>
<li>Default vs. non default arguments. For example, <tt>foo(int a, int b)</tt> and <tt>foo(int a, int b = 3)</tt>.
</li>
</ul>
<p>
When an ambiguity arises, methods are checked in the same order as they appear in the interface file.
Therefore, earlier methods will shadow methods that appear later.
</p>
<p>
When wrapping an overloaded function, there is a chance that you will get an error message like this:
</p>
<div class="shell">
<pre>
example.i:3: Warning 467: Overloaded foo(int) not supported (no type checking
rule for 'int').
</pre>
</div>
<p>
This error means that the target language module supports overloading,
but for some reason there is no type-checking rule that can be used to
generate a working dispatch function. The resulting behavior is then
undefined. You should report this as a bug to the
<a href="http://www.swig.org/bugs.html">SWIG bug tracking database</a>.
</p>
<p>
If you get an error message such as the following,
</p>
<div class="shell">
<pre>
foo.i:6. Overloaded declaration ignored. Spam::foo(double )
foo.i:5. Previous declaration is Spam::foo(int )
foo.i:7. Overloaded declaration ignored. Spam::foo(Bar *,Spam *,int )
foo.i:5. Previous declaration is Spam::foo(int )
</pre>
</div>
<p>
it means that the target language module has not yet implemented support for overloaded
functions and methods. The only way to fix the problem is to read the next section.
</p>
<H3><a name="SWIGPlus_ambiguity_resolution_renaming"></a>6.15.3 Ambiguity resolution and renaming</H3>
<p>
If an ambiguity in overload resolution occurs or if a module doesn't
allow overloading, there are a few strategies for dealing with the
problem. First, you can tell SWIG to ignore one of the methods. This
is easy---simply use the <tt>%ignore</tt> directive. For example:
</p>
<div class="code">
<pre>
%ignore foo(long);
void foo(int);
void foo(long); // Ignored. Oh well.
</pre>
</div>
<p>
The other alternative is to rename one of the methods. This can be
done using <tt>%rename</tt>. For example:
</p>
<div class="code">
<pre>
%rename("foo_short") foo(short);
%rename(foo_long) foo(long);
void foo(int);
void foo(short); // Accessed as foo_short()
void foo(long); // Accessed as foo_long()
</pre>
</div>
<p>
Note that the quotes around the new name are optional, however,
should the new name be a C/C++ keyword they would be essential in order to avoid a parsing error.
The <tt>%ignore</tt> and <tt>%rename</tt> directives are both rather powerful
in their ability to match declarations. When used in their simple form, they apply to
both global functions and methods. For example:
</p>
<div class="code">
<pre>
/* Forward renaming declarations */
%rename(foo_i) foo(int);
%rename(foo_d) foo(double);
...
void foo(int); // Becomes 'foo_i'
void foo(char *c); // Stays 'foo' (not renamed)
class Spam {
public:
void foo(int); // Becomes 'foo_i'
void foo(double); // Becomes 'foo_d'
...
};
</pre>
</div>
<p>
If you only want the renaming to apply to a certain scope, the C++ scope resolution operator (::) can be used.
For example:
</p>
<div class="code">
<pre>
%rename(foo_i) ::foo(int); // Only rename foo(int) in the global scope.
// (will not rename class members)
%rename(foo_i) Spam::foo(int); // Only rename foo(int) in class Spam
</pre>
</div>
<p>
When a renaming operator is applied to a class as in <tt>Spam::foo(int)</tt>, it is applied to
that class and all derived classes. This can be used to apply a consistent renaming across
an entire class hierarchy with only a few declarations. For example:
</p>
<div class="code">
<pre>
%rename(foo_i) Spam::foo(int);
%rename(foo_d) Spam::foo(double);
class Spam {
public:
virtual void foo(int); // Renamed to foo_i
virtual void foo(double); // Renamed to foo_d
...
};
class Bar : public Spam {
public:
virtual void foo(int); // Renamed to foo_i
virtual void foo(double); // Renamed to foo_d
...
};
class Grok : public Bar {
public:
virtual void foo(int); // Renamed to foo_i
virtual void foo(double); // Renamed to foo_d
...
};
</pre>
</div>
<p>
It is also possible to include <tt>%rename</tt> specifications in the
class definition itself. For example:
</p>
<div class="code">
<pre>
class Spam {
%rename(foo_i) foo(int);
%rename(foo_d) foo(double);
public:
virtual void foo(int); // Renamed to foo_i
virtual void foo(double); // Renamed to foo_d
...
};
class Bar : public Spam {
public:
virtual void foo(int); // Renamed to foo_i
virtual void foo(double); // Renamed to foo_d
...
};
</pre>
</div>
<p>
In this case, the <tt>%rename</tt> directives still get applied across the entire
inheritance hierarchy, but it's no longer necessary to explicitly specify the
class prefix <tt>Spam::</tt>.
</p>
<p>
A special form of <tt>%rename</tt> can be used to apply a renaming just to class
members (of all classes):
</p>
<div class="code">
<pre>
%rename(foo_i) *::foo(int); // Only rename foo(int) if it appears in a class.
</pre>
</div>
<p>
Note: the <tt>*::</tt> syntax is non-standard C++, but the '*' is meant to be a
wildcard that matches any class name (we couldn't think of a better
alternative so if you have a better idea, send email to
the <a href="http://www.swig.org/mail.html">swig-devel mailing list</a>.
</p>
<p>
Although this discussion has primarily focused on <tt>%rename</tt> all of the same rules
also apply to <tt>%ignore</tt>. For example:
</p>
<div class="code">
<pre>
%ignore foo(double); // Ignore all foo(double)
%ignore Spam::foo; // Ignore foo in class Spam
%ignore Spam::foo(double); // Ignore foo(double) in class Spam
%ignore *::foo(double); // Ignore foo(double) in all classes
</pre>
</div>
<p>
When applied to a base class, <tt>%ignore</tt> forces all definitions in derived classes
to disappear. For example, <tt>%ignore Spam::foo(double)</tt> will eliminate <tt>foo(double)</tt> in
<tt>Spam</tt> and all classes derived from <tt>Spam</tt>.
</p>
<p>
<b>Notes on %rename and %ignore:</b>
</p>
<ul>
<li><p>Since, the <tt>%rename</tt> declaration is used to declare a renaming in advance, it can be
placed at the start of an interface file. This makes it possible to apply a consistent name
resolution without having to modify header files. For example:</p>
<div class="code">
<pre>
%module foo
/* Rename these overloaded functions */
%rename(foo_i) foo(int);
%rename(foo_d) foo(double);
%include "header.h"
</pre>
</div>
</li>
<li><p>The scope qualifier (::) can also be used on simple names. For example:</p>
<div class="code">
<pre>
%rename(bar) ::foo; // Rename foo to bar in global scope only
%rename(bar) Spam::foo; // Rename foo to bar in class Spam only
%rename(bar) *::foo; // Rename foo in classes only
</pre>
</div>
</li>
<li><p>Name matching tries to find the most specific match that is
defined. A qualified name such as <tt>Spam::foo</tt> always has
higher precedence than an unqualified name <tt>foo</tt>.
<tt>Spam::foo</tt> has higher precedence than <tt>*::foo</tt> and
<tt>*::foo</tt> has higher precedence than <tt>foo</tt>. A
parameterized name has higher precedence than an unparameterized name
within the same scope level. However, an unparameterized name with a
scope qualifier has higher precedence than a parameterized name in
global scope (e.g., a renaming of <tt>Spam::foo</tt> takes precedence
over a renaming of <tt>foo(int)</tt>).</p>
</li>
<li><p>
The order in which <tt>%rename</tt> directives are defined does not matter
as long as they appear before the declarations to be renamed. Thus, there is no difference
between saying:</p>
<div class="code">
<pre>
%rename(bar) foo;
%rename(foo_i) Spam::foo(int);
%rename(Foo) Spam::foo;
</pre>
</div>
<p>
and this
</p>
<div class="code">
<pre>
%rename(Foo) Spam::foo;
%rename(bar) foo;
%rename(foo_i) Spam::foo(int);
</pre>
</div>
<p>
(the declarations are not stored in a linked list and order has no
importance). Of course, a repeated <tt>%rename</tt> directive will
change the setting for a previous <tt>%rename</tt> directive if exactly the
same name, scope, and parameters are supplied.
</p>
</li>
<li>For multiple inheritance where renaming rules are defined for multiple base classes,
the first renaming rule found on a depth-first traversal of the class hierarchy
is used.
</li>
<li><p>The name matching rules strictly follow member qualification rules.
For example, if you have a class like this:</p>
<div class="code">
<pre>
class Spam {
public:
...
void bar() const;
...
};
</pre>
</div>
<p>
the declaration
</p>
<div class="code">
<pre>
%rename(name) Spam::bar();
</pre>
</div>
<p>
will not apply as there is no unqualified member <tt>bar()</tt>. The following will apply as
the qualifier matches correctly:
</p>
<div class="code">
<pre>
%rename(name) Spam::bar() const;
</pre>
</div>
<p>
An often overlooked C++ feature is that classes can define two different overloaded members
that differ only in their qualifiers, like this:
</p>
<div class="code">
<pre>
class Spam {
public:
...
void bar(); // Unqualified member
void bar() const; // Qualified member
...
};
</pre>
</div>
<p>
%rename can then be used to target each of the overloaded methods individually.
For example we can give them separate names in the target language:
</p>
<div class="code">
<pre>
%rename(name1) Spam::bar();
%rename(name2) Spam::bar() const;
</pre>
</div>
<p>
Similarly, if you
merely wanted to ignore one of the declarations, use <tt>%ignore</tt>
with the full qualification. For example, the following directive
would tell SWIG to ignore the <tt>const</tt> version of <tt>bar()</tt>
above:
</p>
<div class="code">
<pre>
%ignore Spam::bar() const; // Ignore bar() const, but leave other bar() alone
</pre>
</div>
</li>
<li><p>
Currently no resolution is performed in order to match function parameters. This means function parameter types must match exactly.
For example, namespace qualifiers and typedefs will not work. The following usage of typedefs demonstrates this:
<div class="code">
<pre>
typedef int Integer;
%rename(foo_i) foo(int);
class Spam {
public:
void foo(Integer); // Stays 'foo' (not renamed)
};
class Ham {
public:
void foo(int); // Renamed to foo_i
};
</pre>
</div>
<li><p>
The name matching rules also use default arguments for finer control when wrapping methods that have default arguments.
Recall that methods with default arguments are wrapped as if the equivalent overloaded methods had been parsed
(<a href="#SWIGPlus_default_args">Default arguments</a> section).
Let's consider the following example class:</p>
<div class="code">
<pre>
class Spam {
public:
...
void bar(int i=-1, double d=0.0);
...
};
</pre>
</div>
<p>
The following <tt>%rename</tt> will match exactly and apply to all the target language overloaded methods because the declaration with the default arguments
exactly matches the wrapped method:
</p>
<div class="code">
<pre>
%rename(newbar) Spam::bar(int i=-1, double d=0.0);
</pre>
</div>
<p>
The C++ method can then be called from the target language with the new name no matter how many arguments are specified, for example:
<tt>newbar(2, 2.0)</tt>, <tt>newbar(2)</tt> or <tt>newbar()</tt>.
However, if the <tt>%rename</tt> does not contain the default arguments, it will only apply to the single equivalent target language overloaded method.
So if instead we have:
</p>
<div class="code">
<pre>
%rename(newbar) Spam::bar(int i, double d);
</pre>
</div>
<p>
The C++ method must then be called from the target language with the new name <tt>newbar(2, 2.0)</tt> when both arguments are supplied
or with the original name as <tt>bar(2)</tt> (one argument) or <tt>bar()</tt> (no arguments).
In fact it is possible to use <tt>%rename</tt> on the equivalent overloaded methods, to rename all the equivalent overloaded methods:
</p>
<div class="code">
<pre>
%rename(bar_2args) Spam::bar(int i, double d);
%rename(bar_1arg) Spam::bar(int i);
%rename(bar_default) Spam::bar();
</pre>
</div>
<p>
Similarly, the extra overloaded methods can be selectively ignored using <tt>%ignore</tt>.
</p>
<p>
<b>Compatibility note:</b> The <tt>%rename</tt> directive introduced the default argument matching rules in SWIG-1.3.23 at the same time as the changes
to wrapping methods with default arguments was introduced.
</p>
</li>
</ul>
<H3><a name="SWIGPlus_nn27"></a>6.15.4 Comments on overloading</H3>
<p>
Support for overloaded methods was first added in SWIG-1.3.14. The implementation
is somewhat unusual when compared to similar tools. For instance, the order in which
declarations appear is largely irrelevant in SWIG. Furthermore, SWIG does not rely
upon trial execution or exception handling to figure out which method to invoke.
</p>
<p>
Internally, the overloading mechanism is completely configurable by the target language
module. Therefore, the degree of overloading support may vary from language to language.
As a general rule, statically typed languages like Java are able to provide more support
than dynamically typed languages like Perl, Python, Ruby, and Tcl.
</p>
<H2><a name="SWIGPlus_nn28"></a>6.16 Wrapping overloaded operators</H2>
<p>
C++ overloaded operator declarations can be wrapped.
For example, consider a class like this:
</p>
<div class="code">
<pre>
class Complex {
private:
double rpart, ipart;
public:
Complex(double r = 0, double i = 0) : rpart(r), ipart(i) { }
Complex(const Complex &c) : rpart(c.rpart), ipart(c.ipart) { }
Complex &operator=(const Complex &c) {
rpart = c.rpart;
ipart = c.ipart;
return *this;
}
Complex operator+(const Complex &c) const {
return Complex(rpart+c.rpart, ipart+c.ipart);
}
Complex operator-(const Complex &c) const {
return Complex(rpart-c.rpart, ipart-c.ipart);
}
Complex operator*(const Complex &c) const {
return Complex(rpart*c.rpart - ipart*c.ipart,
rpart*c.ipart + c.rpart*ipart);
}
Complex operator-() const {
return Complex(-rpart, -ipart);
}
double re() const { return rpart; }
double im() const { return ipart; }
};
</pre>
</div>
<p>
When operator declarations appear, they are handled in
<em>exactly</em> the same manner as regular methods. However, the
names of these methods are set to strings like "<tt>operator +</tt>"
or "<tt>operator -</tt>". The problem with these names is that they
are illegal identifiers in most scripting languages. For instance,
you can't just create a method called "<tt>operator +</tt>" in
Python--there won't be any way to call it.
</p>
<p>
Some language modules already know how to automatically handle certain
operators (mapping them into operators in the target language).
However, the underlying implementation of this is really managed in a
very general way using the <tt>%rename</tt> directive. For example,
in Python a declaration similar to this is used:
</p>
<div class="code">
<pre>
%rename(__add__) Complex::operator+;
</pre>
</div>
<p>
This binds the + operator to a method called <tt>__add__</tt> (which
is conveniently the same name used to implement the Python + operator).
Internally, the generated wrapper code for a wrapped operator will look
something like this pseudocode:
</p>
<div class="code">
<pre>
_wrap_Complex___add__(args) {
... get args ...
obj->operator+(args);
...
}
</pre>
</div>
<p>
When used in the target language, it may now be possible to use the overloaded
operator normally. For example:
</p>
<div class="targetlang">
<pre>
>>> a = Complex(3,4)
>>> b = Complex(5,2)
>>> c = a + b # Invokes __add__ method
</pre>
</div>
<p>
It is important to realize that there is nothing magical happening
here. The <tt>%rename</tt> directive really only picks a valid method
name. If you wrote this:
</p>
<div class="code">
<pre>
%rename(add) operator+;
</pre>
</div>
<p>
The resulting scripting interface might work like this:
</p>
<div class="targetlang">
<pre>
a = Complex(3,4)
b = Complex(5,2)
c = a.add(b) # Call a.operator+(b)
</pre>
</div>
<p>
All of the techniques described to deal with overloaded functions also
apply to operators. For example:
</p>
<div class="code">
<pre>
%ignore Complex::operator=; // Ignore = in class Complex
%ignore *::operator=; // Ignore = in all classes
%ignore operator=; // Ignore = everywhere.
%rename(__sub__) Complex::operator-;
%rename(__neg__) Complex::operator-(); // Unary -
</pre>
</div>
<p>
The last part of this example illustrates how multiple definitions of
the <tt>operator-</tt> method might be handled.
</p>
<p>
Handling operators in this manner is mostly straightforward. However, there are a few subtle
issues to keep in mind:
</p>
<ul>
<li><p>In C++, it is fairly common to define different versions of the operators to account for
different types. For example, a class might also include a friend function like this:</p>
<div class="code">
<pre>
class Complex {
public:
friend Complex operator+(Complex &, double);
};
Complex operator+(Complex &, double);
</pre>
</div>
<p>
SWIG simply ignores all <tt>friend</tt> declarations. Furthermore, it
doesn't know how to associate the associated <tt>operator+</tt> with
the class (because it's not a member of the class).
</p>
<p>
It's still possible to make a wrapper for this operator, but you'll
have to handle it like a normal function. For example:
</p>
<div class="code">
<pre>
%rename(add_complex_double) operator+(Complex &, double);
</pre>
</div>
</li>
<li><p>Certain operators are ignored by default. For instance, <tt>new</tt> and <tt>delete</tt> operators
are ignored as well as conversion operators.
</p></li>
<li>The semantics of certain C++ operators may not match those in the target language.
</li>
</ul>
<H2><a name="SWIGPlus_class_extension"></a>6.17 Class extension</H2>
<p>
New methods can be added to a class using the <tt>%extend</tt>
directive. This directive is primarily used in conjunction with proxy
classes to add additional functionality to an existing class. For
example :
</p>
<div class="code"><pre>
%module vector
%{
#include "vector.h"
%}
class Vector {
public:
double x,y,z;
Vector();
~Vector();
... bunch of C++ methods ...
%extend {
char *__str__() {
static char temp[256];
sprintf(temp,"[ %g, %g, %g ]", $self->x,$self->y,$self->z);
return &temp[0];
}
}
};
</pre></div>
<p>
This code adds a<tt> __str__</tt> method to our class for producing a
string representation of the object. In Python, such a method would
allow us to print the value of an object using the <tt>print</tt>
command.
</p>
<div class="targetlang"><pre>
>>>
>>> v = Vector();
>>> v.x = 3
>>> v.y = 4
>>> v.z = 0
>>> print(v)
[ 3.0, 4.0, 0.0 ]
>>>
</pre></div>
<p>
The C++ 'this' pointer is often needed to access member variables, methods etc.
The <tt>$self</tt> special variable should be used wherever you could use 'this'.
The example above demonstrates this for accessing member variables.
Note that the members dereferenced by <tt>$self</tt> must be public members as the code is ultimately generated
into a global function and so will not have any access to non-public members.
The implicit 'this' pointer that is present in C++ methods is not present in <tt>%extend</tt> methods.
In order to access anything in the extended class or its base class, an explicit 'this' is required.
The following example shows how one could access base class members:
</p>
<div class="code"><pre>
struct Base {
virtual void method(int v) {
...
}
int value;
};
struct Derived : Base {
};
%extend Derived {
virtual void method(int v) {
$self->Base::method(v); // akin to this->Base::method(v);
$self->value = v; // akin to this->value = v;
...
}
}
</pre></div>
<p>
The following special variables are expanded if used within a %extend block:
$name, $symname, $overname, $decl, $fulldecl, $parentname and $parentsymname.
The <a href="Customization.html#Customization_exception_special_variables">Special variables</a> section provides more information each of these special variables.
</p>
<p>
The<tt> %extend</tt> directive follows all of the same conventions
as its use with C structures. Please refer to the <a href="SWIG.html#SWIG_adding_member_functions">Adding member functions to C structures</a>
section for further details.
</p>
<p>
<b>Compatibility note:</b> The <tt>%extend</tt> directive is a new
name for the <tt>%addmethods</tt> directive in SWIG1.1. Since <tt>%addmethods</tt> could
be used to extend a structure with more than just methods, a more suitable
directive name has been chosen.
</p>
<H2><a name="SWIGPlus_nn30"></a>6.18 Templates</H2>
<p>
Template type names may appear anywhere a type
is expected in an interface file. For example:
</p>
<div class="code">
<pre>
void foo(vector<int> *a, int n);
void bar(list<int,100> *x);
</pre>
</div>
<p>
There are some restrictions on the use of non-type arguments. Simple literals
are supported, and so are some constant expressions. However, use of '<'
and '>' within a constant expressions currently is not supported by SWIG
('<=' and '>=' are though). For example:
</p>
<div class="code">
<pre>
void bar(list<int,100> *x); // OK
void bar(list<int,2*50> *x); // OK
void bar(list<int,(2>1 ? 100 : 50)> *x) // Not supported
</pre>
</div>
<p>
The type system is smart enough to figure out clever games
you might try to play with <tt>typedef</tt>. For instance, consider this code:
</p>
<div class="code">
<pre>
typedef int Integer;
void foo(vector<int> *x, vector<Integer> *y);
</pre>
</div>
<p>
In this case, <tt>vector<Integer></tt> is exactly the same type
as <tt>vector<int></tt>. The wrapper for <tt>foo()</tt> will
accept either variant.
</p>
<p>
Starting with SWIG-1.3.7, simple C++ template declarations can also be
wrapped. SWIG-1.3.12 greatly expands upon the earlier implementation. Before discussing this any further, there are a few things
you need to know about template wrapping. First, a bare C++ template
does not define any sort of runnable object-code for which SWIG can
normally create a wrapper. Therefore, in order to wrap a template,
you need to give SWIG information about a particular template
instantiation (e.g., <tt>vector<int></tt>,
<tt>array<double></tt>, etc.). Second, an instantiation name
such as <tt>vector<int></tt> is generally not a valid identifier
name in most target languages. Thus, you will need to give the
template instantiation a more suitable name such as <tt>intvector</tt>
when creating a wrapper.
</p>
<p>
To illustrate, consider the following template definition:
</p>
<div class="code"><pre>
template<class T> class List {
private:
T *data;
int nitems;
int maxitems;
public:
List(int max) {
data = new T [max];
nitems = 0;
maxitems = max;
}
~List() {
delete [] data;
};
void append(T obj) {
if (nitems < maxitems) {
data[nitems++] = obj;
}
}
int length() {
return nitems;
}
T get(int n) {
return data[n];
}
};
</pre></div>
<p>
By itself, this template declaration is useless--SWIG simply ignores it
because it doesn't know how to generate any code until unless a definition of
<tt>T</tt> is provided.
</p>
<p>
One way to create wrappers for a specific template instantiation is to simply
provide an expanded version of the class directly like this:
</p>
<div class="code">
<pre>
%rename(intList) List<int>; // Rename to a suitable identifier
class List<int> {
private:
int *data;
int nitems;
int maxitems;
public:
List(int max);
~List();
void append(int obj);
int length();
int get(int n);
};
</pre>
</div>
<p>
The <tt>%rename</tt> directive is needed to give the template class an appropriate identifier
name in the target language (most languages would not recognize C++ template syntax as a valid
class name). The rest of the code is the same as what would appear in a normal
class definition.
</p>
<p>
Since manual expansion of templates gets old in a hurry, the <tt>%template</tt> directive can
be used to create instantiations of a template class. Semantically, <tt>%template</tt> is
simply a shortcut---it expands template code in exactly the same way as shown above. Here
are some examples:
</p>
<div class="code">
<pre>
/* Instantiate a few different versions of the template */
%template(intList) List<int>;
%template(doubleList) List<double>;
</pre>
</div>
<p>
The argument to <tt>%template()</tt> is the name of the instantiation
in the target language. The name you choose should not conflict with
any other declarations in the interface file with one exception---it
is okay for the template name to match that of a typedef declaration.
For example:
</p>
<div class="code">
<pre>
%template(intList) List<int>;
...
typedef List<int> intList; // OK
</pre>
</div>
<p>
SWIG can also generate wrappers for function templates using a similar technique.
For example:
</p>
<div class="code">
<pre>
// Function template
template<class T> T max(T a, T b) { return a > b ? a : b; }
// Make some different versions of this function
%template(maxint) max<int>;
%template(maxdouble) max<double>;
</pre>
</div>
<p>
In this case, <tt>maxint</tt> and <tt>maxdouble</tt> become unique names for specific
instantiations of the function.
</p>
<p>
The number of arguments supplied to <tt>%template</tt> should match that in the
original template definition. Template default arguments are supported. For example:
</p>
<div class="code">
<pre>
template vector<typename T, int max=100> class vector {
...
};
%template(intvec) vector<int>; // OK
%template(vec1000) vector<int,1000>; // OK
</pre>
</div>
<p>
The <tt>%template</tt> directive should not be used to wrap the same
template instantiation more than once in the same scope. This will
generate an error. For example:
</p>
<div class="code">
<pre>
%template(intList) List<int>;
%template(Listint) List<int>; // Error. Template already wrapped.
</pre>
</div>
<p>
This error is caused because the template expansion results in two
identical classes with the same name. This generates a symbol table
conflict. Besides, it probably more efficient to only wrap a specific
instantiation only once in order to reduce the potential for code
bloat.
</p>
<p>
Since the type system knows how to handle <tt>typedef</tt>, it is
generally not necessary to instantiate different versions of a template
for typenames that are equivalent. For instance, consider this code:
</p>
<div class="code">
<pre>
%template(intList) vector<int>;
typedef int Integer;
...
void foo(vector<Integer> *x);
</pre>
</div>
<p>
In this case, <tt>vector<Integer></tt> is exactly the same type as
<tt>vector<int></tt>. Any use of <tt>Vector<Integer></tt> is mapped back to the
instantiation of <tt>vector<int></tt> created earlier. Therefore, it is
not necessary to instantiate a new class for the type <tt>Integer</tt> (doing so is
redundant and will simply result in code bloat).
</p>
<p>
When a template is instantiated using <tt>%template</tt>, information
about that class is saved by SWIG and used elsewhere in the program.
For example, if you wrote code like this,
</p>
<div class="code">
<pre>
...
%template(intList) List<int>;
...
class UltraList : public List<int> {
...
};
</pre>
</div>
<p>
then SWIG knows that <tt>List<int></tt> was already wrapped as a class called
<tt>intList</tt> and arranges to handle the inheritance correctly. If, on the other hand,
nothing is known about <tt>List<int></tt>, you will get a warning message similar to this:
</p>
<div class="shell">
<pre>
example.h:42: Warning 401. Nothing known about class 'List<int >'. Ignored.
example.h:42: Warning 401. Maybe you forgot to instantiate 'List<int >' using %template.
</pre>
</div>
<p>
If a template class inherits from another template class, you need to
make sure that base classes are instantiated before derived classes.
For example:
</p>
<div class="code">
<pre>
template<class T> class Foo {
...
};
template<class T> class Bar : public Foo<T> {
...
};
// Instantiate base classes first
%template(intFoo) Foo<int>;
%template(doubleFoo) Foo<double>;
// Now instantiate derived classes
%template(intBar) Bar<int>;
%template(doubleBar) Bar<double>;
</pre>
</div>
<p>
The order is important since SWIG uses the instantiation names to
properly set up the inheritance hierarchy in the resulting wrapper
code (and base classes need to be wrapped before derived classes).
Don't worry--if you get the order wrong, SWIG should generate a warning message.
</p>
<p>
Occasionally, you may need to tell SWIG about base classes that are defined by templates,
but which aren't supposed to be wrapped. Since SWIG is not able to automatically
instantiate templates for this purpose, you must do it manually. To do this, simply
use the empty template instantiation, that is, <tt>%template</tt> with no name. For example:
</p>
<div class="code">
<pre>
// Instantiate traits<double,double>, but don't wrap it.
%template() traits<double,double>;
</pre>
</div>
<p>
If you have to instantiate a lot of different classes for many different types,
you might consider writing a SWIG macro. For example:
</p>
<div class="code">
<pre>
%define TEMPLATE_WRAP(prefix, T...)
%template(prefix ## Foo) Foo<T >;
%template(prefix ## Bar) Bar<T >;
...
%enddef
TEMPLATE_WRAP(int, int)
TEMPLATE_WRAP(double, double)
TEMPLATE_WRAP(String, char *)
TEMPLATE_WRAP(PairStringInt, std::pair<string, int>)
...
</pre>
</div>
<p>
Note the use of a vararg macro for the type T. If this wasn't used, the comma in the templated type in the last example would not be possible.
</p>
<p>
The SWIG template mechanism <em>does</em> support specialization. For instance, if you define
a class like this,
</p>
<div class="code">
<pre>
template<> class List<int> {
private:
int *data;
int nitems;
int maxitems;
public:
List(int max);
~List();
void append(int obj);
int length();
int get(int n);
};
</pre>
</div>
<p>
then SWIG will use this code whenever the user expands <tt>List<int></tt>. In practice,
this may have very little effect on the underlying wrapper code since
specialization is often used to provide slightly modified method bodies (which
are ignored by SWIG). However, special SWIG
directives such as <tt>%typemap</tt>, <tt>%extend</tt>, and so forth can be attached
to a specialization to provide customization for specific types.
</p>
<p>
Partial template specialization is partially supported by SWIG. For example, this
code defines a template that is applied when the template argument is a pointer.
</p>
<div class="code">
<pre>
template<class T> class List<T*> {
private:
T *data;
int nitems;
int maxitems;
public:
List(int max);
~List();
void append(int obj);
int length();
T get(int n);
};
</pre>
</div>
<p>
SWIG supports both template explicit specialization and partial specialization. Consider:
</p>
<div class="code">
<pre>
template<class T1, class T2> class Foo { }; // (1) primary template
template<> class Foo<double *, int *> { }; // (2) explicit specialization
template<class T1, class T2> class Foo<T1, T2 *> { }; // (3) partial specialization
</pre>
</div>
<p>
SWIG is able to properly match explicit instantiations:
</p>
<div class="code">
<pre>
<tt>Foo<double *, int *></tt> // explicit specialization matching (2)
</pre>
</div>
<p>
SWIG implements template argument deduction so that the following partial specialization examples work just like they would with a C++ compiler:
</p>
<div class="code">
<pre>
<tt>Foo<int *, int *></tt> // partial specialization matching (3)
<tt>Foo<int *, const int *></tt> // partial specialization matching (3)
<tt>Foo<int *, int **></tt> // partial specialization matching (3)
</pre>
</div>
<p>
Member function templates are supported. The underlying principle is the same
as for normal templates--SWIG can't create a wrapper unless you provide
more information about types. For example, a class with a member template might
look like this:
</p>
<div class="code">
<pre>
class Foo {
public:
template<class T> void bar(T x, T y) { ... };
...
};
</pre>
</div>
<p>
To expand the template, simply use <tt>%template</tt> inside the class.
</p>
<div class="code">
<pre>
class Foo {
public:
template<class T> void bar(T x, T y) { ... };
...
%template(barint) bar<int>;
%template(bardouble) bar<double>;
};
</pre>
</div>
<p>
Or, if you want to leave the original class definition alone, just do this:
</p>
<div class="code">
<pre>
class Foo {
public:
template<class T> void bar(T x, T y) { ... };
...
};
...
%extend Foo {
%template(barint) bar<int>;
%template(bardouble) bar<double>;
};
</pre>
</div>
<p>
or simply
</p>
<div class="code">
<pre>
class Foo {
public:
template<class T> void bar(T x, T y) { ... };
...
};
...
%template(bari) Foo::bar<int>;
%template(bard) Foo::bar<double>;
</pre>
</div>
<p>
In this case, the <tt>%extend</tt> directive is not needed, and
<tt>%template</tt> does exactly the same job, i.e., it adds two new
methods to the Foo class.
</p>
<p>
Note: because of the way that templates are handled, the <tt>%template</tt> directive
must always appear <em>after</em> the definition of the template to be expanded.
</p>
<p>
Now, if your target language supports overloading, you can even try
</p>
<div class="code">
<pre>
%template(bar) Foo::bar<int>;
%template(bar) Foo::bar<double>;
</pre>
</div>
<p>
and since the two new wrapped methods have the same name 'bar', they will be
overloaded, and when called, the correct method will be dispatched
depending on the argument type.
</p>
<p>
When used with members, the <tt>%template</tt> directive may be placed in another
template class. Here is a slightly perverse example:
</p>
<div class="code">
<pre>
// A template
template<class T> class Foo {
public:
// A member template
template<class S> T bar(S x, S y) { ... };
...
};
// Expand a few member templates
%extend Foo {
%template(bari) bar<int>;
%template(bard) bar<double>;
}
// Create some wrappers for the template
%template(Fooi) Foo<int>;
%template(Food) Foo<double>;
</pre>
</div>
<p>
Miraculously, you will find that each expansion of <tt>Foo</tt> has member
functions <tt>bari()</tt> and <tt>bard()</tt> added.
</p>
<p>
A common use of member templates is to define constructors for copies
and conversions. For example:
</p>
<div class="code">
<pre>
template<class T1, class T2> struct pair {
T1 first;
T2 second;
pair() : first(T1()), second(T2()) { }
pair(const T1 &x, const T2 &y) : first(x), second(y) { }
template<class U1, class U2> pair(const pair<U1,U2> &x)
: first(x.first),second(x.second) { }
};
</pre>
</div>
<p>
This declaration is perfectly acceptable to SWIG, but the constructor template will be ignored
unless you explicitly expand it. To do that, you could expand a few versions of the constructor
in the template class itself. For example:
</p>
<div class="code">
<pre>
%extend pair {
%template(pair) pair<T1,T2>; // Generate default copy constructor
};
</pre>
</div>
<p>
When using <tt>%extend</tt> in this manner, notice how you can still use the template parameters in
the original template definition.
</p>
<p>
Alternatively, you could expand the constructor template in selected instantiations. For example:
</p>
<div class="code">
<pre>
// Instantiate a few versions
%template(pairii) pair<int,int>;
%template(pairdd) pair<double,double>;
// Create a default constructor only
%extend pair<int,int> {
%template(paird) pair<int,int>; // Default constructor
};
// Create default and conversion constructors
%extend pair<double,double> {
%template(paird) pair<double,dobule>; // Default constructor
%template(pairc) pair<int,int>; // Conversion constructor
};
</pre>
</div>
<p>And if your target language supports overloading, then you can try
instead:
</p>
<div class="code">
<pre>
// Create default and conversion constructors
%extend pair<double,double> {
%template(pair) pair<double,dobule>; // Default constructor
%template(pair) pair<int,int>; // Conversion constructor
};
</pre>
</div>
<p>
In this case, the default and conversion constructors have the same
name. Hence, SWIG will overload them and define an unique visible
constructor, that will dispatch the proper call depending on the argument
type.
</p>
<p>
If all of this isn't quite enough and you really want to make
someone's head explode, SWIG directives such as
<tt>%rename</tt>, <tt>%extend</tt>, and <tt>%typemap</tt> can be
included directly in template definitions. For example:
</p>
<div class="code"><pre>
// File : list.h
template<class T> class List {
...
public:
%rename(__getitem__) get(int);
List(int max);
~List();
...
T get(int index);
%extend {
char *__str__() {
/* Make a string representation */
...
}
}
};
</pre></div>
<p>
In this example, the extra SWIG directives are propagated to <em>every</em> template
instantiation.
</p>
<p>
It is also possible to separate these declarations from the template class. For example:
</p>
<div class="code">
<pre>
%rename(__getitem__) List::get;
%extend List {
char *__str__() {
/* Make a string representation */
...
}
/* Make a copy */
T *__copy__() {
return new List<T>(*$self);
}
};
...
template<class T> class List {
...
public:
List() { };
T get(int index);
...
};
</pre>
</div>
<p>
When <tt>%extend</tt> is decoupled from the class definition, it is
legal to use the same template parameters as provided in the class definition.
These are replaced when the template is expanded.
In addition, the <tt>%extend</tt> directive can be used to add
additional methods to a specific instantiation. For example:
</p>
<div class="code">
<pre>
%template(intList) List<int>;
%extend List<int> {
void blah() {
printf("Hey, I'm an List<int>!\n");
}
};
</pre>
</div>
<p>
SWIG even supports overloaded templated functions. As usual the <tt>%template</tt> directive
is used to wrap templated functions. For example:
</p>
<div class="code">
<pre>
template<class T> void foo(T x) { };
template<class T> void foo(T x, T y) { };
%template(foo) foo<int>;
</pre>
</div>
<p>
This will generate two overloaded wrapper methods, the first will take a single integer as an argument
and the second will take two integer arguments.
</p>
<p>
Needless to say, SWIG's template support provides plenty of
opportunities to break the universe. That said, an important final
point is that <b>SWIG does not perform extensive error checking of
templates!</b> Specifically, SWIG does not perform type checking nor
does it check to see if the actual contents of the template
declaration make any sense. Since the C++ compiler will hopefully
check this when it compiles the resulting wrapper file, there is no
practical reason for SWIG to duplicate this functionality (besides,
none of the SWIG developers are masochistic enough to want to
implement this right now).
</p>
<p>
<b>Compatibility Note</b>: The first implementation of template support relied heavily on
macro expansion in the preprocessor. Templates have been more tightly integrated into
the parser and type system in SWIG-1.3.12 and the preprocessor is no longer used. Code
that relied on preprocessing features in template expansion will no longer work. However,
SWIG still allows the # operator to be used to generate a string from a template argument.
</p>
<p>
<b>Compatibility Note</b>: In earlier versions of SWIG, the <tt>%template</tt> directive
introduced a new class name. This name could then be used with other directives. For example:
</p>
<div class="code">
<pre>
%template(vectori) vector<int>;
%extend vectori {
void somemethod() { }
};
</pre>
</div>
<p>
This behavior is no longer supported. Instead, you should use the original template name
as the class name. For example:
</p>
<div class="code">
<pre>
%template(vectori) vector<int>;
%extend vector<int> {
void somemethod() { }
};
</pre>
</div>
<p>
Similar changes apply to typemaps and other customization features.
</p>
<H2><a name="SWIGPlus_namespaces"></a>6.19 Namespaces</H2>
<p>
Support for C++ namespaces is comprehensive, but by default simple, however,
some target languages can turn on more advanced namespace support via the
<a href="#SWIGPlus_nspace">nspace feature</a>, described later.
Code within unnamed namespaces is ignored as there is no external
access to symbols declared within the unnamed namespace.
Before detailing the default implementation for named namespaces,
it is worth noting that the semantics of C++ namespaces is extremely
non-trivial--especially with regard to the C++ type system and class
machinery. At a most basic level, namespaces are sometimes used to
encapsulate common functionality. For example:
</p>
<div class="code">
<pre>
namespace math {
double sin(double);
double cos(double);
class Complex {
double im,re;
public:
...
};
...
};
</pre>
</div>
<p>
Members of the namespace are accessed in C++ by prepending the namespace prefix
to names. For example:
</p>
<div class="code">
<pre>
double x = math::sin(1.0);
double magnitude(math::Complex *c);
math::Complex c;
...
</pre>
</div>
<p>
At this level, namespaces are relatively easy to manage. However, things start to get
very ugly when you throw in the other ways a namespace can be used. For example,
selective symbols can be exported from a namespace with <tt>using</tt>.
</p>
<div class="code">
<pre>
using math::Complex;
double magnitude(Complex *c); // Namespace prefix stripped
</pre>
</div>
<p>
Similarly, the contents of an entire namespace can be made available like this:
</p>
<div class="code">
<pre>
using namespace math;
double x = sin(1.0);
double magnitude(Complex *c);
</pre>
</div>
<p>
Alternatively, a namespace can be aliased:
</p>
<div class="code">
<pre>
namespace M = math;
double x = M::sin(1.0);
double magnitude(M::Complex *c);
</pre>
</div>
<p>
Using combinations of these features, it is possible to write head-exploding code like this:
</p>
<div class="code">
<pre>
namespace A {
class Foo {
};
}
namespace B {
namespace C {
using namespace A;
}
typedef C::Foo FooClass;
}
namespace BIGB = B;
namespace D {
using BIGB::FooClass;
class Bar : public FooClass {
}
};
class Spam : public D::Bar {
};
void evil(A::Foo *a, B::FooClass *b, B::C::Foo *c, BIGB::FooClass *d,
BIGB::C::Foo *e, D::FooClass *f);
</pre>
</div>
<p>
Given the possibility for such perversion, it's hard to imagine how
every C++ programmer might want such code wrapped into the target
language. Clearly this code defines three different classes. However, one
of those classes is accessible under at least six different names!
</p>
<p>
SWIG fully supports C++ namespaces in its internal type system and
class handling code. If you feed SWIG the above code, it will be
parsed correctly, it will generate compilable wrapper code, and it
will produce a working scripting language module. However, the
default wrapping behavior is to flatten namespaces in the target
language. This means that the contents of all namespaces are merged
together in the resulting scripting language module. For example, if
you have code like this,
</p>
<div class="code">
<pre>
%module foo
namespace foo {
void bar(int);
void spam();
}
namespace bar {
void blah();
}
</pre>
</div>
<p>
then SWIG simply creates three wrapper functions <tt>bar()</tt>,
<tt>spam()</tt>, and <tt>blah()</tt> in the target language. SWIG
does not prepend the names with a namespace prefix nor are the
functions packaged in any kind of nested scope.
</p>
<p>
There is some rationale for taking this approach. Since C++
namespaces are often used to define modules in C++, there is a natural
correlation between the likely contents of a SWIG module and the contents of
a namespace. For instance, it would not be unreasonable to assume
that a programmer might make a separate extension module for each C++
namespace. In this case, it would be redundant to prepend everything
with an additional namespace prefix when the module itself already
serves as a namespace in the target language. Or put another way, if
you want SWIG to keep namespaces separate, simply wrap each namespace with its
own SWIG interface.
</p>
<p>
Because namespaces are flattened, it is possible for symbols defined in different
namespaces to generate a name conflict in the target language. For example:
</p>
<div class="code">
<pre>
namespace A {
void foo(int);
}
namespace B {
void foo(double);
}
</pre>
</div>
<p>
When this conflict occurs, you will get an error message that resembles this:
</p>
<div class="shell">
<pre>
example.i:26. Error. 'foo' is multiply defined in the generated target language module.
example.i:23. Previous declaration of 'foo'
</pre>
</div>
<p>
To resolve this error, simply use <tt>%rename</tt> to disambiguate the declarations. For example:
</p>
<div class="code">
<pre>
%rename(B_foo) B::foo;
...
namespace A {
void foo(int);
}
namespace B {
void foo(double); // Gets renamed to B_foo
}
</pre>
</div>
<p>
Similarly, <tt>%ignore</tt> can be used to ignore declarations.
</p>
<p>
<tt>using</tt> declarations do not have any effect on the generated wrapper
code. They are ignored by SWIG language modules and they do not result in any
code. However, these declarations <em>are</em> used by the internal type
system to track type-names. Therefore, if you have code like this:
</p>
<div class="code">
<pre>
namespace A {
typedef int Integer;
}
using namespace A;
void foo(Integer x);
</pre>
</div>
<p>
SWIG knows that <tt>Integer</tt> is the same as <tt>A::Integer</tt> which
is the same as <tt>int</tt>.
</p>
<P>
Namespaces may be combined with templates. If necessary, the
<tt>%template</tt> directive can be used to expand a template defined
in a different namespace. For example:
</p>
<div class="code">
<pre>
namespace foo {
template<typename T> T max(T a, T b) { return a > b ? a : b; }
}
using foo::max;
%template(maxint) max<int>; // Okay.
%template(maxfloat) foo::max<float>; // Okay (qualified name).
namespace bar {
using namespace foo;
%template(maxdouble) max<double>; // Okay.
}
</pre>
</div>
<p>
The combination of namespaces and other SWIG directives may introduce subtle scope-related problems.
The key thing to keep in mind is that all SWIG generated wrappers are produced
in the <em>global</em> namespace. Symbols from other namespaces are always accessed using fully
qualified names---names are never imported into the global space unless the interface happens to
do so with a <tt>using</tt> declaration. In almost all cases, SWIG adjusts typenames and symbols
to be fully qualified. However, this is not done in code fragments such as function bodies,
typemaps, exception handlers, and so forth. For example, consider the following:
</p>
<div class="code">
<pre>
namespace foo {
typedef int Integer;
class bar {
public:
...
};
}
%extend foo::bar {
Integer add(Integer x, Integer y) {
Integer r = x + y; // Error. Integer not defined in this scope
return r;
}
};
</pre>
</div>
<p>
In this case, SWIG correctly resolves the added method parameters and return type to
<tt>foo::Integer</tt>. However, since function bodies aren't parsed and such code is
emitted in the global namespace, this code produces a compiler error about <tt>Integer</tt>.
To fix the problem, make sure you use fully qualified names. For example:
</p>
<div class="code">
<pre>
%extend foo::bar {
Integer add(Integer x, Integer y) {
foo::Integer r = x + y; // Ok.
return r;
}
};
</pre>
</div>
<p>
<b>Note:</b> SWIG does <em>not</em> propagate <tt>using</tt> declarations to
the resulting wrapper code. If these declarations appear in an interface,
they should <em>also</em> appear in any header files that might have been
included in a <tt>%{ ... %}</tt> section. In other words, don't insert extra
<tt>using</tt> declarations into a SWIG interface unless they also appear
in the underlying C++ code.
</p>
<p>
<b>Note:</b> Code inclusion directives such as <tt>%{ ... %}</tt> or
<tt>%inline %{ ... %}</tt> should not be placed inside a namespace declaration.
The code emitted by these directives will not be enclosed in a namespace and
you may get very strange results. If you need to use namespaces with
these directives, consider the following:
</p>
<div class="code">
<pre>
// Good version
%inline %{
namespace foo {
void bar(int) { ... }
...
}
%}
// Bad version. Emitted code not placed in namespace.
namespace foo {
%inline %{
void bar(int) { ... } /* I'm bad */
...
%}
}
</pre>
</div>
<p>
<b>Note:</b> When the <tt>%extend</tt> directive is used inside a namespace, the namespace name is
included in the generated functions. For example, if you have code like this,
</p>
<div class="code">
<pre>
namespace foo {
class bar {
public:
%extend {
int blah(int x);
};
};
}
</pre>
</div>
<p>
the added method <tt>blah()</tt> is mapped to a function <tt>int foo_bar_blah(foo::bar *self, int x)</tt>.
This function resides in the global namespace.
</p>
<p>
<b>Note:</b> Although namespaces are flattened in the target language, the SWIG generated wrapper
code observes the same namespace conventions as used in the input file. Thus, if there are no symbol
conflicts in the input, there will be no conflicts in the generated code.
</p>
<p>
<b>Note:</b> In the same way that no resolution is performed on parameters, a conversion operator name must match exactly to how it is defined. Do not change the qualification of the operator. For example, suppose you had an interface like this:
</p>
<div class="code">
<pre>
namespace foo {
class bar;
class spam {
public:
...
operator bar(); // Conversion of spam -> bar
...
};
}
</pre>
</div>
<p>
The following is how the feature is expected to be written for a successful match:
</p>
<div class="code">
<pre>
%rename(tofoo) foo::spam::operator bar();
</pre>
</div>
<p>
The following does not work as no namespace resolution is performed in the matching of conversion operator names:
</p>
<div class="code">
<pre>
%rename(tofoo) foo::spam::operator <b>foo::</b>bar();
</pre>
</div>
<p>
Note, however, that if the operator is defined using a qualifier in its name, then the feature must use it too...
</p>
<div class="code">
<pre>
%rename(tofoo) foo::spam::operator bar(); // will not match
%rename(tofoo) foo::spam::operator foo::bar(); // will match
namespace foo {
class bar;
class spam {
public:
...
operator foo::bar();
...
};
}
</pre>
</div>
<p>
<b>Compatibility Note:</b> Versions of SWIG prior to 1.3.32 were inconsistent in this approach. A fully qualified name was usually required, but would not work in some situations.
</p>
<p>
<b>Note:</b> The flattening of namespaces is only intended to serve as
a basic namespace implementation.
None of the target language modules are currently programmed
with any namespace awareness. In the future, language modules may or may not provide
more advanced namespace support.
</p>
<H3><a name="SWIGPlus_nspace"></a>6.19.1 The nspace feature for namespaces</H3>
<p>
Some target languages provide support for the <tt>nspace</tt> <a href="Customization.html#Customization_features">feature</a>.
The feature can be applied to any class, struct, union or enum declared within a named namespace.
The feature wraps the type within the target language specific concept of a namespace,
for example, a Java package or C# namespace.
Please see the language specific sections to see if the target language you are interested in supports the nspace feature.
</p>
<p>
The feature is demonstrated below for C# using the following example:
</p>
<div class="code">
<pre>
%feature("nspace") MyWorld::Material::Color;
%nspace MyWorld::Wrapping::Color; // %nspace is a macro for %feature("nspace")
namespace MyWorld {
namespace Material {
class Color {
...
};
}
namespace Wrapping {
class Color {
...
};
}
}
</pre>
</div>
<p>
Without the <tt>nspace</tt> feature directives above or <tt>%rename</tt>, you would get the following warning resulting in just one of the <tt>Color</tt> classes being available for use from the target language:
</p>
<div class="shell">
<pre>
example.i:9: Error: 'Color' is multiply defined in the generated target language module.
example.i:5: Error: Previous declaration of 'Color'
</pre>
</div>
<p>
With the <tt>nspace</tt> feature the two <tt>Color</tt> classes are wrapped into the equivalent C# namespaces.
A fully qualified constructor call of each these two types in C# is then:
</p>
<div class="targetlang">
<pre>
MyWorld.Material.Color materialColor = new MyWorld.Material.Color();
MyWorld.Wrapping.Color wrappingColor = new MyWorld.Wrapping.Color();
</pre>
</div>
<p>
Note that the <tt>nspace</tt> feature does not apply to variables and functions simply declared in a namespace. For example, the following symbols cannot co-exist in the target language without renaming. This may change in a future version.
</p>
<div class="code">
<pre>
namespace MyWorld {
namespace Material {
int quantity;
void dispatch();
}
namespace Wrapping {
int quantity;
void dispatch();
}
}
</pre>
</div>
<p>
<b>Compatibility Note:</b> The nspace feature was first introduced in SWIG-2.0.0.
</p>
<H2><a name="SWIGPlus_renaming_templated_types_namespaces"></a>6.20 Renaming templated types in namespaces</H2>
<p>
As has been mentioned, when %rename includes parameters, the parameter types must match exactly (no typedef or namespace resolution is performed).
SWIG treats templated types slightly differently and has an additional matching rule so unlike non-templated types, an exact match is not always required.
If the fully qualified templated type is specified, it will have a higher precedence over the generic template type.
In the example below, the generic template type is used to rename to <tt>bbb</tt> and the fully qualified type is used to rename to <tt>ccc</tt>.
</p>
<div class="code">
<pre>
%rename(bbb) Space::ABC::aaa(T t); // will match but with lower precedence than ccc
%rename(ccc) Space::ABC<Space::XYZ>::aaa(Space::XYZ t);// will match but with higher precedence
// than bbb
namespace Space {
class XYZ {};
template<typename T> struct ABC {
void aaa(T t) {}
};
}
%template(ABCXYZ) Space::ABC<Space::XYZ>;
</pre>
</div>
<p>
It should now be apparent that there are many ways to achieve a renaming with %rename. This is demonstrated
by the following two examples, which are effectively the same as the above example.
Below shows how %rename can be placed inside a namespace.
</p>
<div class="code">
<pre>
namespace Space {
%rename(bbb) ABC::aaa(T t); // will match but with lower precedence than ccc
%rename(ccc) ABC<Space::XYZ>::aaa(Space::XYZ t);// will match but with higher precedence than bbb
%rename(ddd) ABC<Space::XYZ>::aaa(XYZ t); // will not match
}
namespace Space {
class XYZ {};
template<typename T> struct ABC {
void aaa(T t) {}
};
}
%template(ABCXYZ) Space::ABC<Space::XYZ>;
</pre>
</div>
<p>
Note that <tt>ddd</tt> does not match as there is no namespace resolution for parameter types and the fully qualified type must be specified for template type expansion.
The following example shows how %rename can be placed within %extend.
</p>
<div class="code">
<pre>
namespace Space {
%extend ABC {
%rename(bbb) aaa(T t); // will match but with lower precedence than ccc
}
%extend ABC<Space::XYZ> {
%rename(ccc) aaa(Space::XYZ t);// will match but with higher precedence than bbb
%rename(ddd) aaa(XYZ t); // will not match
}
}
namespace Space {
class XYZ {};
template<typename T> struct ABC {
void aaa(T t) {}
};
}
%template(ABCXYZ) Space::ABC<Space::XYZ>;
</pre>
</div>
<H2><a name="SWIGPlus_exception_specifications"></a>6.21 Exception specifications</H2>
<p>
When C++ programs utilize exceptions, exceptional behavior is sometimes specified as
part of a function or method declaration. For example:
</p>
<div class="code">
<pre>
class Error { };
class Foo {
public:
...
void blah() throw(Error);
...
};
</pre>
</div>
<p>
If an exception specification is used, SWIG automatically generates
wrapper code for catching the indicated exception and, when possible,
rethrowing it into the target language, or converting it into an error
in the target language otherwise. For example, in Python, you can
write code like this:
</p>
<div class="targetlang">
<pre>
f = Foo()
try:
f.blah()
except Error,e:
# e is a wrapped instance of "Error"
</pre>
</div>
<p>
Details of how to tailor code for handling the caught C++ exception and converting it into the target language's exception/error handling mechanism
is outlined in the <a href="Typemaps.html#throws_typemap">"throws" typemap</a> section.
</p>
<p>
Since exception specifications are sometimes only used sparingly, this alone may not be enough to
properly handle C++ exceptions. To do that, a different set of special SWIG directives are used.
Consult the "<a href="Customization.html#Customization_exception">Exception handling with %exception</a>" section for details.
The next section details a way of simulating an exception specification or replacing an existing one.
</p>
<H2><a name="SWIGPlus_catches"></a>6.22 Exception handling with %catches</H2>
<p>
Exceptions are automatically handled for methods with an exception specification.
Similar handling can be achieved for methods without exception specifications through the <tt>%catches</tt> feature.
It is also possible to replace any declared exception specification using the <tt>%catches</tt> feature.
In fact, <tt>%catches</tt> uses the same <a href="Typemaps.html#throws_typemap">"throws" typemaps</a> that SWIG uses for exception specifications in handling exceptions.
The <tt>%catches</tt> feature must contain a list of possible types that can be thrown.
For each type that is in the list, SWIG will generate a catch handler, in the same way that it would for types declared in the exception specification.
Note that the list can also include the catch all specification "...".
For example,
</p>
<div class="code">
<pre>
struct EBase { virtual ~EBase(); };
struct Error1 : EBase { };
struct Error2 : EBase { };
struct Error3 : EBase { };
struct Error4 : EBase { };
%catches(Error1,Error2,...) Foo::bar();
%catches(EBase) Foo::blah();
class Foo {
public:
...
void bar();
void blah() throw(Error1,Error2,Error3,Error4);
...
};
</pre>
</div>
<p>
For the <tt>Foo::bar()</tt> method, which can throw anything,
SWIG will generate catch handlers for <tt>Error1</tt>, <tt>Error2</tt> as well as a catch all handler (...).
Each catch handler will convert the caught exception and convert it into a target language error/exception.
The catch all handler will convert the caught exception into an unknown error/exception.
</p>
<p>
Without the <tt>%catches</tt> feature being attached to <tt>Foo::blah()</tt>,
SWIG will generate catch handlers for all of the types in the exception specification, that is, <tt>Error1, Error2, Error3, Error4</tt>.
However, with the <tt>%catches</tt> feature above,
just a single catch handler for the base class, <tt>EBase</tt> will be generated to convert the C++ exception into a target language error/exception.
</p>
<H2><a name="SWIGPlus_nn33"></a>6.23 Pointers to Members</H2>
<p>
Starting with SWIG-1.3.7, there is limited parsing support for pointers to C++ class members.
For example:
</p>
<div class="code">
<pre>
double do_op(Object *o, double (Object::*callback)(double,double));
extern double (Object::*fooptr)(double,double);
%constant double (Object::*FOO)(double,double) = &Object::foo;
</pre>
</div>
<p>
Although these kinds of pointers can be parsed and represented by the
SWIG type system, few language modules know how to handle them due to
implementation differences from standard C pointers. Readers are
<em>strongly</em> advised to consult an advanced text such as the "The
Annotated C++ Manual" for specific details.
</p>
<p>
When pointers to members are supported, the pointer value might appear as a special
string like this:
</p>
<div class="targetlang">
<pre>
>>> print example.FOO
_ff0d54a800000000_m_Object__f_double_double__double
>>>
</pre>
</div>
<p>
In this case, the hexadecimal digits represent the entire value of the
pointer which is usually the contents of a small C++ structure on most
machines.
</p>
<p>
SWIG's type-checking mechanism is also more limited when working with
member pointers. Normally SWIG tries to keep track of inheritance
when checking types. However, no such support is currently provided
for member pointers.
</p>
<H2><a name="SWIGPlus_smart_pointers"></a>6.24 Smart pointers and operator->()</H2>
<p>
In some C++ programs, objects are often encapsulated by smart-pointers
or proxy classes. This is sometimes done to implement automatic memory management (reference counting) or
persistence. Typically a smart-pointer is defined by a template class where
the <tt>-></tt> operator has been overloaded. This class is then wrapped
around some other class. For example:
</p>
<div class="code">
<pre>
// Smart-pointer class
template<class T> class SmartPtr {
T *pointee;
public:
SmartPtr(T *p) : pointee(p) { ... }
T *operator->() {
return pointee;
}
...
};
// Ordinary class
class Foo_Impl {
public:
int x;
virtual void bar();
...
};
// Smart-pointer wrapper
typedef SmartPtr<Foo_Impl> Foo;
// Create smart pointer Foo
Foo make_Foo() {
return SmartPtr<Foo_Impl>(new Foo_Impl());
}
// Do something with smart pointer Foo
void do_something(Foo f) {
printf("x = %d\n", f->x);
f->bar();
}
// Call the wrapped smart pointer proxy class in the target language 'Foo'
%template(Foo) SmartPtr<Foo_Impl>;
</pre>
</div>
<p>
A key feature of this approach is that by defining
<tt>operator-></tt> the methods and attributes of the object
wrapped by a smart pointer are transparently accessible. For example,
expressions such as these (from the previous example),
</p>
<div class="code">
<pre>
f->x
f->bar()
</pre>
</div>
<p>
are transparently mapped to the following
</p>
<div class="code">
<pre>
(f.operator->())->x;
(f.operator->())->bar();
</pre>
</div>
<p>
When generating wrappers, SWIG tries to emulate this functionality to
the extent that it is possible. To do this, whenever
<tt>operator->()</tt> is encountered in a class, SWIG looks at its
returned type and uses it to generate wrappers for accessing
attributes of the underlying object. For example, wrapping the above
code produces wrappers like this:
</p>
<div class="code">
<pre>
int Foo_x_get(Foo *f) {
return (*f)->x;
}
void Foo_x_set(Foo *f, int value) {
(*f)->x = value;
}
void Foo_bar(Foo *f) {
(*f)->bar();
}
</pre>
</div>
<p>
These wrappers take a smart-pointer instance as an argument, but
dereference it in a way to gain access to the object returned by
<tt>operator->()</tt>. You should carefully compare these wrappers
to those in the first part of this chapter (they are slightly
different).
</p>
<p>
The end result is that access looks very similar to C++. For
example, you could do this in Python:
</p>
<div class="targetlang">
<pre>
>>> f = make_Foo()
>>> print f.x
0
>>> f.bar()
>>>
</pre>
</div>
<p>
When generating wrappers through a smart-pointer, SWIG tries to
generate wrappers for all methods and attributes that might be
accessible through <tt>operator->()</tt>. This includes any methods
that might be accessible through inheritance. However, there are a number of restrictions:
</p>
<ul>
<li>Member variables and methods are wrapped through a smart
pointer. Enumerations, constructors, and destructors are not wrapped.
</li>
<li><p>If the smart-pointer class and the underlying object both define a method or
variable of the same name, then the smart-pointer version has precedence. For
example, if you have this code</p>
<div class="code">
<pre>
class Foo {
public:
int x;
};
class Bar {
public:
int x;
Foo *operator->();
};
</pre>
</div>
<p>
then the wrapper for <tt>Bar::x</tt> accesses the <tt>x</tt> defined in <tt>Bar</tt>, and
not the <tt>x</tt> defined in <tt>Foo</tt>.</p>
</li>
</ul>
<p>
If your intent is to only expose the smart-pointer class in the interface, it is not necessary to wrap both
the smart-pointer class and the class for the underlying object. However, you must still tell SWIG about both
classes if you want the technique described in this section to work. To only generate wrappers for the
smart-pointer class, you can use the %ignore directive. For example:
</p>
<div class="code">
<pre>
%ignore Foo;
class Foo { // Ignored
};
class Bar {
public:
Foo *operator->();
...
};
</pre>
</div>
<p>
Alternatively, you can import the definition of <tt>Foo</tt> from a separate file using
<tt>%import</tt>.
</p>
<p>
<b>Note:</b> When a class defines <tt>operator->()</tt>, the operator itself is wrapped
as a method <tt>__deref__()</tt>. For example:
</p>
<div class="targetlang">
<pre>
f = Foo() # Smart-pointer
p = f.__deref__() # Raw pointer from operator->
</pre>
</div>
<p>
<b>Note:</b> To disable the smart-pointer behavior, use <tt>%ignore</tt> to ignore
<tt>operator->()</tt>. For example:
</p>
<div class="code">
<pre>
%ignore Bar::operator->;
</pre>
</div>
<p>
<b>Note:</b> Smart pointer support was first added in SWIG-1.3.14.
</p>
<H2><a name="SWIGPlus_ref_unref"></a>6.25 C++ reference counted objects - ref/unref feature</H2>
<p>
Another similar idiom in C++ is the use of reference counted objects. Consider for example:
<div class="code">
<pre>
class RCObj {
// implement the ref counting mechanism
int add_ref();
int del_ref();
int ref_count();
public:
virtual ~RCObj() = 0;
int ref() const {
return add_ref();
}
int unref() const {
if (ref_count() == 0 || del_ref() == 0 ) {
delete this;
return 0;
}
return ref_count();
}
};
class A : RCObj {
public:
A();
int foo();
};
class B {
A *_a;
public:
B(A *a) : _a(a) {
a->ref();
}
~B() {
a->unref();
}
};
int main() {
A *a = new A(); // (count: 0)
a->ref(); // 'a' ref here (count: 1)
B *b1 = new B(a); // 'a' ref here (count: 2)
if (1 + 1 == 2) {
B *b2 = new B(a); // 'a' ref here (count: 3)
delete b2; // 'a' unref, but not deleted (count: 2)
}
delete b1; // 'a' unref, but not deleted (count: 1)
a->unref(); // 'a' unref and deleted (count: 0)
}
</pre>
</div>
<p>
In the example above, the 'A' class instance 'a' is a reference counted
object, which can't be deleted arbitrarily since it is shared between
the objects 'b1' and 'b2'. 'A' is derived from a <i>Reference Counted
Object</i> 'RCObj', which implements the ref/unref idiom.
</p>
<p>
To tell SWIG that 'RCObj' and all its derived classes are reference
counted objects, use the "ref" and "unref" <a href="Customization.html#Customization_features">features</a>.
These are also available as <tt>%refobject</tt> and <tt>%unrefobject</tt>, respectively.
For example:
</p>
<div class="code">
<pre>
%module example
...
%feature("ref") RCObj "$this->ref();"
%feature("unref") RCObj "$this->unref();"
%include "rcobj.h"
%include "A.h"
...
</pre>
</div>
<p>
where the code passed to the "ref" and "unref" features will be
executed as needed whenever a new object is passed to python, or when
python tries to release the proxy object instance, respectively.
</p>
<p>
On the python side, the use of a reference counted object is no
different to any other regular instance:
</p>
<div class="targetlang">
<pre>
def create_A():
a = A() # SWIG ref 'a' - new object is passed to python (count: 1)
b1 = B(a) # C++ ref 'a (count: 2)
if 1 + 1 == 2:
b2 = B(a) # C++ ref 'a' (count: 3)
return a # 'b1' and 'b2' are released and deleted, C++ unref 'a' twice (count: 1)
a = create_A() # (count: 1)
exit # 'a' is released, SWIG unref 'a' called in the destructor wrapper (count: 0)
</pre>
</div>
<p>
Note that the user doesn't explicitly need to call 'a->ref()' nor 'a->unref()'
(and neither 'delete a'). Instead, SWIG takes cares of executing the "ref"
and "unref" calls as needed. If the user doesn't specify the
"ref/unref" feature for a type, SWIG will produce code equivalent to defining these
features:
</p>
<div class="code">
<pre>
%feature("ref") ""
%feature("unref") "delete $this;"
</pre>
</div>
<p>
In other words, SWIG will not do anything special when a new object
is passed to python, and it will always 'delete' the underlying object when
python releases the proxy instance.
</p>
<p>
The <a href="Customization.html#Customization_ownership">%newobject feature</a> is designed to indicate to
the target language that it should take ownership of the returned object.
When used in conjunction with a type that has the "ref" feature associated with it, it additionally emits the
code in the "ref" feature into the C++ wrapper.
Consider wrapping the following factory function in addition to the above:
</p>
<div class="code">
<pre>
%newobject AFactory;
A *AFactory() {
return new A();
}
</pre>
</div>
<p>
The <tt>AFactory</tt> function now acts much like a call to the <tt>A</tt> constructor with respect to memory handling:
</p>
<div class="targetlang">
<pre>
a = AFactory() # SWIG ref 'a' due to %newobject (count: 1)
exit # 'a' is released, SWIG unref 'a' called in the destructor wrapper (count: 0)
</pre>
</div>
<H2><a name="SWIGPlus_nn35"></a>6.26 Using declarations and inheritance</H2>
<p>
<tt>using</tt> declarations are sometimes used to adjust access to members of
base classes. For example:
</p>
<div class="code">
<pre>
class Foo {
public:
int blah(int x);
};
class Bar {
public:
double blah(double x);
};
class FooBar : public Foo, public Bar {
public:
using Foo::blah;
using Bar::blah;
char *blah(const char *x);
};
</pre>
</div>
<p>
In this example, the <tt>using</tt> declarations make different
versions of the overloaded <tt>blah()</tt> method accessible from the
derived class. For example:
</p>
<div class="code">
<pre>
FooBar *f;
f->blah(3); // Ok. Invokes Foo::blah(int)
f->blah(3.5); // Ok. Invokes Bar::blah(double)
f->blah("hello"); // Ok. Invokes FooBar::blah(const char *);
</pre>
</div>
<p>
SWIG emulates the same functionality when creating wrappers. For example, if
you wrap this code in Python, the module works just like you would expect:
</p>
<div class="targetlang">
<pre>
>>> import example
>>> f = example.FooBar()
>>> f.blah(3)
>>> f.blah(3.5)
>>> f.blah("hello")
</pre>
</div>
<p>
<tt>using</tt> declarations can also be used to change access when applicable. For example:
</p>
<div class="code">
<pre>
class Foo {
protected:
int x;
int blah(int x);
};
class Bar : public Foo {
public:
using Foo::x; // Make x public
using Foo::blah; // Make blah public
};
</pre>
</div>
<p>
This also works in SWIG---the exposed declarations will be wrapped normally.
</p>
<p>
When <tt>using</tt> declarations are used as shown in these examples, declarations
from the base classes are copied into the derived class and wrapped normally. When
copied, the declarations retain any properties that might have been attached using
<tt>%rename</tt>, <tt>%ignore</tt>, or <tt>%feature</tt>. Thus, if a method is
ignored in a base class, it will also be ignored by a <tt>using</tt> declaration.
</p>
<p>
Because a <tt>using</tt> declaration does not provide fine-grained
control over the declarations that get imported, it may be difficult
to manage such declarations in applications that make heavy use of
SWIG customization features. If you can't get <tt>using</tt> to work
correctly, you can always change the interface to the following:
</p>
<div class="code">
<pre>
class FooBar : public Foo, public Bar {
public:
#ifndef SWIG
using Foo::blah;
using Bar::blah;
#else
int blah(int x); // explicitly tell SWIG about other declarations
double blah(double x);
#endif
char *blah(const char *x);
};
</pre>
</div>
<p>
<b>Notes:</b>
</p>
<ul>
<li><p>If a derived class redefines a method defined in a base class, then a <tt>using</tt> declaration
won't cause a conflict. For example:</p>
<div class="code">
<pre>
class Foo {
public:
int blah(int );
double blah(double);
};
class Bar : public Foo {
public:
using Foo::blah; // Only imports blah(double);
int blah(int);
};
</pre>
</div>
<li><p>Resolving ambiguity in overloading may prevent declarations from being
imported by <tt>using</tt>. For example:
</p>
<div class="code">
<pre>
%rename(blah_long) Foo::blah(long);
class Foo {
public:
int blah(int);
long blah(long); // Renamed to blah_long
};
class Bar : public Foo {
public:
using Foo::blah; // Only imports blah(int)
double blah(double x);
};
</pre>
</div>
</ul>
<H2><a name="SWIGPlus_nested_classes"></a>6.27 Nested classes</H2>
<p>
If the target language supports the nested classes concept (like Java), the nested C++ classes
are wrapped as nested target language proxy classes. (In case of Java - "static" nested classes.)
Only public nested classes are wrapped. Otherwise there is little difference between nested and
normal classes.
</p>
<p>
If the target language doesn't support nested classes directly, or the support is not implemented in the
language module (like for python currently), then the visible nested classes are moved to the same name
space as the containing class (nesting hierarchy is "flattened"). The same behaviour may be turned on for
C# and Java by the %feature ("flatnested"); If there is a class with the same name in the outer namespace
the inner class (or the global one) may be renamed or ignored:
</p>
<div class="code">
<pre>
%rename (Bar_Foo) Bar::Foo;
class Foo {};
class Bar {
public:
class Foo {};
};
</pre>
</div>
<p>
<b>Compatibility Note:</b>
Prior to SWIG-3.0.0, there was limited nested class support. Nested classes were treated as opaque pointers.
However, there was a workaround for nested class support in these older versions requiring the user to replicate
the nested class in the global scope, adding in a typedef for the nested class in the global scope and
using the "nestedworkaround" feature on the nested class. This resulted in approximately the
same behaviour as the "flatnested" feature. With proper nested class support now available in SWIG-3.0.0, this
feature has been deprecated and no longer works requiring code changes. If you see the following warning:
</p>
<div class="shell">
<pre>
example.i:8: Warning 126: The nestedworkaround feature is deprecated
</pre>
</div>
<p>
consider using the "flatnested" feature discussed above which generates a non-nested proxy class, like the
"nestedworkaround" feature did. Alternatively, use the default nested class code generation, which may generate an
equivalent to a nested proxy class in the target language, depending on the target language support.
</p>
<p>
SWIG-1.3.40 and earlier versions did not have the <tt>nestedworkaround</tt> feature
and the generated code resulting from parsing nested classes did not always compile.
Nested class warnings could also not be suppressed using %warnfilter.
</p>
<H2><a name="SWIGPlus_const"></a>6.28 A brief rant about const-correctness</H2>
<p>
A common issue when working with C++ programs is dealing with all
possible ways in which the <tt>const</tt> qualifier (or lack thereof)
will break your program, all programs linked against your program, and
all programs linked against those programs.
</p>
<p>
Although SWIG knows how to correctly deal with <tt>const</tt> in its
internal type system and it knows how to generate wrappers that are
free of const-related warnings, SWIG does not make any attempt to preserve
const-correctness in the target language. Thus, it is possible to
pass <tt>const</tt> qualified objects to non-const methods and functions.
For example, consider the following code in C++:
</p>
<div class="code">
<pre>
const Object * foo();
void bar(Object *);
...
// C++ code
void blah() {
bar(foo()); // Error: bar discards const
};
</pre>
</div>
<p>
Now, consider the behavior when wrapped into a Python module:
</p>
<div class="targetlang">
<pre>
>>> bar(foo()) # Okay
>>>
</pre>
</div>
<p>
Although this is clearly a violation of the C++ type-system, fixing
the problem doesn't seem to be worth the added implementation
complexity that would be required to support it in the SWIG run-time type
system. There are no plans to change this in future releases
(although we'll never rule anything out entirely).
</p>
<p>
The bottom line is that this particular issue does not appear to be a problem
for most SWIG projects. Of course, you might want to consider
using another tool if maintaining constness is the most important part
of your project.
</p>
<H2><a name="SWIGPlus_nn42"></a>6.29 Where to go for more information</H2>
<p>
If you're wrapping serious C++ code, you might want to pick up a copy
of "The Annotated C++ Reference Manual" by Ellis and Stroustrup. This
is the reference document we use to guide a lot of SWIG's C++ support.
</p>
</body>
</html>
<!-- LocalWords: destructors Enums Namespaces const SWIG's STL OO adaptor tcl
-->
<!-- LocalWords: debuggable cxx OBJS Wiki accessor nodefault makedefault
-->
<!-- LocalWords: notabstract CopyFoo
-->
|