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
|
Version 1.3.28 (unreleased).
===========================
01/16/2006: mmatus (Change disabled)
Add initial support for regexp via the external library
RxSpencer. SWIG doesn't require this library to compile
and/or run. But if you specify --with-rxspencer, and the
library is found during installation, then swig will use
it in three places:
- In %renames rules, via the new rxsmatch rules, for example:
%rename("%(lowercase)",rxsmatch$name="GSL_.*") "";
%rename("%(lowercase)",rxsmatch$nodeType="enum GSL_.*") "";
rxsmatch is similar to the match rule, it just uses
the RxSpencer regexp library to decide if there is a
match with the provided regexp. As with the match
rule, you can also use the negate rule notrxsmatch.
- In the %rename target name via the rxstarget option, for example:
%rename("%(lowercase)",rxstarget=1) "GSL_.*";
where the target name "GSL.*" is now understood as a
regexp to be matched.
- In the new encoder "rxspencer", which looks like:
%(rxspencer:[regexp][replace])s
where "regexp" is the regular expression and "replace"
is a string used as a replacement, where the @0,@1,...,@9
pseudo arguments are used to represent the
corresponding matching items in the reg expression.
For example:
%(rxspencer:[GSL.*][@0])s <- Hello ->
%(rxspencer:[GSL.*][@0])s <- GSLHello -> GSLHello
%(rxspencer:[GSL(.*)][@1])s <- GSLHello -> Hello
%(rxspencer:[GSL(.*)][gsl@1])s <- GSLHello -> gslHello
Another example could be:
%rename("%(lowercase)s",sourcefmt="%(rxspencer:[GSL_(.*)][@1])s",%$isfunction) "";
which take out the prefix "GSL_" and returns all the
function names in lower cases, as following:
void GSL_Hello(); -> hello();
void GSL_Hi(); -> hi();
const int GSL_MAX; -> GSL_MAX; // no change, is not a function
We use the RxSpencer as an initial test bed to
implemention while we decide which library will be
finally added to swig.
You can obtain the RxSpencer library from
http://arglist.com/regex (Unix)
or
http://gnuwin32.sourceforge.net/packages.html (Windows)
Once installed, use "man rxspencer" to get more info
about the regexp format, or just google rxspencer.
Since now you can enable the rxsmatch rules (see above),
the simple or '|' support for the match rules
(01/12/2006: mmatus) is disabled. Still, if you have
problems with the rxspencer library, you can re-enable
the simple 'match or' support using
-DSWIG_USE_SIMPLE_MATCHOR.
01/16/2006: mmatus
Change the %rename predicates to use the prefix '%$', as in:
%rename("%(utitle)s",%$isfunction,%$ismember) "";
to avoid clashings with other swig macros/directives.
01/14/2006: cfisavage
Added support for Ruby bang! methods via a new %bang feature.
Bang methods end in exclamation points and indicate that the
object being processed will be modified in-place as
opposed to being copied.
01/12/2006: cfisavage
Updated the Ruby module to automatically convert
method names to lower_case_with_underscores using the
new %rename functionality.
01/12/2006: mmatus
- Add aliases for 'case' encoders used with %rename/%namewarn
%(uppercase)s hello_world -> HELLO_WORLD
%(lowercase)s HelloWorld -> helloworld
%(camelcase)s hello_world -> HelloWorld
%(undercase)s HelloWorld -> hello_world
01/12/2006: mmatus
- Add the -dump_parse_module and -dump_parse_top options,
which are similar to -dump_module and -dump_top, but they
dump the node trees just after parsing, showing only the
attributes visible at the parsing stage, and not the added
later in typemap.cxx, allocate.cxx, lang.cxx or elsewhere.
Besides debugging porpuses, these options are very useful
if you plan to use %rename in an "advance way", since it
shows only and all the node's attributes you can use
inside the match rules.
01/12/2006: mmatus
- Add predicates to %rename, so, you don't need to
remember, for example, how to match a member function.
Now it is easy, for example to use the 'utitle' encoder
in all the member methods, you type:
%rename("%(utitle)s",%isfunction,%ismember) "";
or to ignore all the enumitems in a given class:
%rename("$ignore", %isenumitem, %classname="MyClass") "";
Available predicates are (see swig.swg):
%isenum
%isenumitem
%isaccess
%isclass
%isextend
%isextend
%isconstructor
%isdestructor
%isnamespace
%istemplate
%isconstant
%isunion
%isfunction
%isvariable
%isimmutable
%isstatic
%isfriend
%istypedef
%isvirtual
%isexplicit
%isextern
%ismember
%isglobal
%innamespace
%ispublic
%isprotected
%isprivate
%classname
These predicates correspond to specific 'match'
declarations, which sometimes are not as evident as the
predicates names.
- Add the or '|' operation in %rename match, for
example to capitalize all the constants (%constant or
const cdecl):
%rename("%(upper)s",match="cdecl|constant",%isimmutable) "";
01/12/2006: mgossage
- Partial fixed of errors under C89, bug #1356574
(converted C++ style comments to C style)
- Added patches from neomantra@users.sf.net #1379988 and #1388343
missing a 'return' statement for error conditions
also updated the %init block bug #1356586
01/10/2006: mmatus
- Ass the 'utitle' encoder, as an example of how to add
your own encoder. I added the encoder method in misc.c
but you can add another one, the same way, inside any
target language.
Well, 'utitle' is the reverse of 'ctitle', ie:
%rename("%(ctitle)s") camel_case; -> CamelCase;
%rename("%(utitle)s") CamelCase; -> camel_case;
01/10/2006: cfisavage
Updated Ruby Exception handling. Classes that are specified in throws clauses,
or are marked as %exceptionclass, are now inherited from rb_eRuntimeError.
This allows instances of these classes to be returned to Ruby as exceptions.
Thus if a C++ method throws an instance of MyException, the calling Ruby
method will get back a MyException object. To see an example,
ook at ruby/examples/exception_class.
01/10/2006: mmatus
- Add the %catches directive, which complements the %exception
directive in a more automatic way. For example, if you have
int foo() throw(E1);
swig generates the proper try/catch code to dispatch E1.
But if you have:
int barfoo(int i) {
if (i == 1) {
throw E1();
} else {
throw E2();
}
return 0;
}
ie, where there is no explicit throw list in the decl, you
end doing:
%exception barfoo {
try {
$action
} catch(E1) { ... }
} catch(E2) { ... }
}
which is very tedious. Well, the %catches directive defines
the list of exception to catch, and from swig:
%catch(E1,E2) barfoo(int i);
int barfoo(int i);
is equivalent to
int barfoo(int i) throw(E1,E2);
Note, however, that the %catches list doesn't have to
correspond to the C++ 'throw' list. For example, if you
have:
struct E {};
struct E1 : E {};
struct E2 : E {};
int barfoo(int i) throw(E1,E2);
you can define
%catches(E) barfoo(int i);
and swig will generate an action code equivalent to
try {
$action
} catch(E &_e) {
<raise _e>;
}
Of course, you still have to satisfy the C++ restrictions,
and the catchs list must be compatible (not the same)
than the original C++ throw list.
Also, you can now specify that you want to catch the
unknown exception '...', for example:
%catches(E1,E2,...) barfoo(int);
In any case, the %catches directive will emit the
'rethrowing' code using the 'throw' typemap.
For the same, for the '...' case to work, you need to
write the proper typemap in your target language. In the
UTL, this looks like:
%typemap(throws) (...) {
SWIG_exception(SWIG_RuntimeError,"unknown exception");
}
01/05/2006: wsfulton
[Java] Fix unsigned long long and const unsigned long long & typemaps
- Bug #1398394 with patch from Dries Decock
01/06/2006: mmatus
Add 'named' warning codes, now besides of:
%warnfilter(813);
you can use
%warnfilter(SWIGWARN_JAVA_MULTIPLE_INHERITANCE);
just use the same code name found in Source/Include/swigwarn.h
plus the 'SWIG' prefix.
If you add a new warning code, remember to run:
make Lib/swigwarn.swg
in the swig root directory and commit both files. You need
a machine with awk/echo to do that.
01/05/2006: cfisavage
Reimplemented object tracking for Ruby. The new implementation works
by expanding the swig_class structure for Ruby by adding a trackObjects
field. This field can be set/unset via %trackobjects as explained
in the Ruby documentation. The new implementation is more robust
and takes less code to implement.
01/05/2006: wsfulton
Fix for %extend and static const integral types, eg:
class Foo {
public:
%extend {
static const int bar = 42;
}
};
12/30/2005: mmatus
- Add info for old and new debug options:
-dump_top - Print information of the entire node tree, including system nodes
-dump_module - Print information of the module node tree, avoiding system nodes
-dump_classes - Print information about the classes found in the interface
-dump_typedef - Print information about the types and typedefs found in the interface
-dump_tags - Print information about the tags found in the interface
-debug_typemap - Print information for debugging typemaps
-debug_template - Print information for debugging templates
- Add the fakeversion. If you have a project that uses
configure/setup.py, or another automatic building system
and requires a specific swig version, let say 1.3.22
you can use:
SWIG_FEATURES="-fakeversion 1.3.22"
or
swig -fakeversion 1.3.22
and then swig -version will report 1.3.22 instead of the
current version.
Tipical use could be
SWIG_FEATURES="-fakeversion 1.3.22" ./configure
12/30/2005: mmatus
- Add option/format support to %rename and %namewarn.
Now %namewarn can force renaming, for example:
%namewarn("314: import is a keyword",rename="_%s") "import";
and rename can also support format forms:
%rename("swig_%s") import;
Now, since the format is processed via swig Printf, you
can use encoder as follows:
%rename("%(title)s") import; -> Import
%rename("%(upper)s") import; -> IMPORT
%rename("%(lower)s") Import; -> import
%rename("%(ctitle)s") camel_case; -> CamelCase
This will allow us to add more encoders, as the
expected one for regular expressions.
- Add the above 'ctitle' encoder, which does the camel case:
camel_case -> CamelCase
- Also, while we get the regexp support, add the 'command' encoder,
you can use it as follows
%rename("%(command:sed -e 's/\([a-z]\)/\U\\1/' <<< )s") import;
then swig will popen the command
"sed -e 's/\([a-z]\)/\U\\1/' <<< import"
see below for anonymous renames for better examples.
- The rename directive now also allows:
- simple match: only apply the rename if a type match
happen, for example
%rename(%(title)s,match="enumitem") hello;
enum Hello {
hi, hello -> hi, Hello
};
int hello() -> hello;
- extended match: only apply the rename if the 'extended attribute' match
occurred, for example:
// same as simple match
%rename(%(title)s,match$nodeType="enumitem") hello;
enum Hello {
hi, hello -> hi, Hello
};
Note that the symbol '$' is used to define the attribute name in
a 'recursive' way, for example:
// match only hello in 'enum Hello'
%rename(%(title)s,match$parentNode$type="enum Hello") hello;
enum Hello {
hi, hello -> hi, Hello // match
};
enum Hi {
hi, hello -> hi, hello // no match
};
here, for Hello::hi, the "parentNode" is "Hello", and its "type"
is "enum Hello".
- Anonymous renames: you can use 'anonymous' rename directives, for example:
// rename all the enum items in Hello
%rename(%(title)s,match$parentNode$type="enum Hello") "";
enum Hello {
hi, hello -> Hi, Hello // match both
};
enum Hi {
hi, hello -> hi, hello // no match
};
// rename all the enum items
%rename(%(title)s,match$nodeType="enumitem") "";
// rename all the items in given command (sloooow, but...)
%rename(%(command:<my external cmd>)s) "";
Anonymous renames with commands can be very powerful, since you
can 'outsource' all the renaming mechanism (or part of it) to an
external program:
// Uppercase all (and only) the names that start with 'i'
%rename("%(command:awk '/^i/{print toupper($1)}' <<<)s") "";
int imported() -> IMPORTED;
int hello() -> hello
Note that if the 'command' encoder returns an empty string, swig
understand that no rename is necessary.
Also note that %rename 'passes' the matched name. For example, in
this case
namespace ns1 {
int foo();
}
namespace ns2 {
int bar();
}
the external program only receives "foo" and "bar". If needed,
however, you can request the 'fullname'
%rename("%(command:awk 'awk '/ns1::/{l=split($1,a,"::"); print toupper(a[l])}'' <<<)s",fullname=1) "";
ns1::foo -> FOO
ns2::bar -> bar
- Mixing encoders and matching: of course, you can do mix commands
and match fields, for example:
%rename("%(<my encoder for fncs>)",match="cdecl") "";
%rename("%(<my encoder for enums>)",match="enumitem") "";
%rename("%(<my encoder for enums inside a class>)",match="enumitem",
match$parentNode$parentNode$nodeType="class") "";
Use "swig -dump_parse_module" to see the attribute names you can use to
match a specific case.
- 'sourcefmt' and 'targetfmt': sometimes you need to
process the 'source' name before comparing, for example
%namewarn("314: empty is a keyword",sourcefmt="%(lower)s") "empty";
then if you have
int Empty(); // "Empty" is the source
you will get the keyword warning since 'Empty' will be
lower cased, via the sourcefmt="%(lower)s" option,
before been compared to the 'target' "empty".
There is an additional 'targetfmt' option to process the
'target' before comparing.
- complementing 'match': you can use 'notmatch', for example
%namewarn("314: empty is a keyword",sourcefmt="%(lower)s",notmatch="namespace") "empty";
here, the name warning will be applied to all the symbols except namespaces.
12/30/2005: mmatus
- Add initial support for gcj and Java -> <target language> mechanism.
See examples in:
Examples/python/java
Examples/ruby/java
Examples/tcl/java
to see how to use gcj+swig to export java classes into
python/ruby/tcl.
The idea is to put all the common code for gcj inside
Lib/gcj
and localize especific issues as jstring, as can be found
in
Lib/python/jstring.i
Lib/ruby/jstring.i
Lib/tcl/jstring.i
Using the UTL, this is very easy, and the perl version for
jstring.i will be next.
12/29/2005: mmatus
- Add the copyctor feature/directive/option to enable the automatic
generation of copy constructors. Use as in:
%copyctor A;
struct A {
};
then this will work
a1 = A();
a2 = A(a1);
Also, since is a feature, if you type just
%copyctor;
that will enable the automatic generation for all the
classes. That is also equivalent to
swig -copyctor -c++ ...
Notes:
1.- The feature only works in C++ mode.
2.- The automatic creation of the copy constructor will
usually produce overloading. Hence, if the target
language doesn't support overloading, a special name
will be used (A_copy).
3.- For the same above, probably is not a good idea to
use the flag when, for example, in python if you are
using keywords.
4.- The copyctor automatic mechanism follows more or less
the same rules than the default constructor mechanism,
i.e., a copy constructor will not be added if the
class is abstract or if there is a pertinent non-public
copy ctor in the class or its hierarchy.
Hence, it could be necessary for you to complete the
class declaration with the proper non-public copy ctor
to avoid a wrong constructor addition.
- Fix features/rename for templates ctor/dtor and other
things around while adding the copyctor mechanism.
12/27/2005: mmatus
- Add the 'math' option to typemaps. Assume you have:
%typemap(in) SWIGTYPE * (int res) {..}
%typemap(freearg) SWIGTYPE * { if (res$argnum) ...}
then if you do
%typemap(in) A * {...}
swig will 'overload the 'in' typemap, but the 'freearg'
typemap will be also applied, even when is wrong. The old
solutions is to write:
%typemap(in) A * {...}
%typemap(freeag) A * "";
overload 'freearg' with an empty definition.
The problem is, however, there is no way to know you need
to do that until you start getting broken C++ code, or
worse, broken runtime code.
The same applies to the infamous 'typecheck' typemap,
which always confuses people, since the first thing you do
is just to write the 'in' typemap.
Well, the 'match' option solves the problem, and if you
write instead
%typemap(in) SWIGTYPE * (int res) {..}
%typemap(freearg,match="in") SWIGTYPE * { if (res$argnum) ...}
%typemap(typecheck,match="in",precedence...) SWIGTYPE * {...}
that will tell swig to apply the 'freearg/typecheck'
typemaps only if they 'match' the type of the 'in'
typemap. The same can be done with other typemaps as:
%typemap(directorout) SWIGTYPE * {...}
%typemap(directorfree,match="directorout") SWIGTYPE * {...}
12/27/2005: mmatus
- Add the 'naturalvar' option/mode/feature to treat member
variables in a more natural way, ie, similar to the global
variable behavior.
If you use
swig -naturalvar ...
or
%module(naturalvar="1")
or
%naturalvar std::string
then, in the following case:
std::string s;
struct Bar {
std::string s;
};
you can do:
b = Bar()
b.s ="hello"
cvar.s = "hello"
if (b.s != cvar.s):
raise RuntimeError
This is valid for all the languages, and the
implementation is based simply in forcing to use the
typemaps const SWIGTYPE& (C++)/SWIGTYPE (C) for the
get/set methods instead of the old SWIGTYPE *.
Hence, for 'naturalvar' to work, each target language
must implement 'typemap(in/out) const Type&' properly.
The 'naturalvar' option makes (little dangerous)
workarounds such as:
%apply(const std::string &) { std::string *}
obsoletes.
Note1: If your interface has other kind of workarounds to
deal with the old 'unnatural' way to deal with member
variables (returning/expexting pointers), the
'narturalvar' option could break them.
Note2: the option has no effect over unnamed types, such
as unnamed nested unions.
12/27/2005: mmatus
- Add more 'expressive' result states for the typemap
libraries.
In the old times, you just check something like:
if (ConvertPtr(obj,&vptr,ty,flags) != -1) {
// success
} else {
// error
}
Now the result state can carry more information,
including:
- Error state: as the old -1/0, but with error codes from swigerrors.swg.
int res = ConvertPtr(obj,&vptr,ty,flags);
if (SWIG_IsOK(res)) {
// success code
} else {
SWIG_Error(res); // res carries the error code
}
- Cast rank: when returning a simple successful
conversion, you just return SWIG_OK, but if you need
to do a 'cast', you can add the casting rank, ie:
if (PyFloat_Check(obj)) {
value = PyFloat_AsDouble(obj);
return SWIG_OK;
} else if (PyInt_Check(obj)) {
value = (double) PyInt_AsLong(obj);
return SWIG_AddCast(SWIG_OK);
}
later, the casting rank is used to properly dispatch
the overloaded function, for example. This of course
requires your language to support and use the new
dispatch cast/rank mechanism (Now mainly supported in
perl and python, and easily expandable to ruby and tcl).
- [UTL] Add support for the new 'expressive' result states.
12/27/2005: mmatus
- Add support for the C++ implicit conversion mechanism, which
required some modifications in parser.y (to recognize
'explicit') and overload.cxx (to replace $implicitconv as
needed).
Still, real support in each target language requires to modify
each module language. Python provides an example, see bellow.
- Add support for native C++ implicit conversions, ie, if you
have
%implicitconv A;
struct A {
int ii;
A() {ii = 1;}
A(int) {ii = 2;}
A(double) {ii = 3;}
explicit A(char *s) {ii = 4;}
};
int get(const A& a) {return a.ii;}
you can call:
a = A()
ai = A(1)
ad = A(1.0)
as = A("hello")
# old forms
get(a) -> 1
get(ai) -> 2
get(ad) -> 3
get(as) -> 4
#implicit conversions
get(1) -> 2
get(1.0) -> 3
get("hello") -> Error, explicit constructor
Also, as in C++, now implicit conversions are supported in
variable assigments, and if you have:
A ga;
struct Bar {
A a;
};
you can do:
cvar.ga = A(1)
cvar.ga = 1
cvar.ga = 1.0
cvar.ga = A("hello")
cvar.ga = "hello" -> error, explicit constructor
b = Bar()
b.a = A("hello")
b.a = 1
b.a = 1.0
b.a = "hello" -> error, explicit constructor
Note that the last case, assigning directly a member var,
requires also the 'naturalvar' option.
This support now makes the old '%implicit' macro, which
was found in 'implicit.i' and it was fragile in many ways,
obsolete, and you should use the new '%implicitconv'
directive instead.
Note that we follow the C++ conventions, ie, in the
following the implicit conversion is allowed:
int get(A a) {return a.ii;}
int get(const A& a) {return a.ii;}
but not in this cases:
int get(A *a) {return a->ii;}
int get(A& a) {return a.ii;}
Also, it works for director methods that return a by value
result, ie, the following will work:
virtual A get_a() = 0;
def get_a(self):
return 1
but not in this cases:
virtual const A& get_a() = 0;
virtual A& get_a() = 0;
virtual A* get_a() = 0;
Notes:
- the implicitconv mechanism is implemented by directly
calling/dispatching the python constructor, triggering a
call to the __init__method. Hence, if you expanded the
__init__ method, like in:
class A:
def __init__(self,args):
<swig code>
<my code here>
then 'my code' will also be executed.
- Since the %implicitconv directive is based in SWIG
features, if you type:
%implicitconv;
that will enable implicit conversion for all the classes in
your module.
But if you are worry about performance, maybe that will be
too much, especially if you have overloaded methods, since
to resolve the dispatching problem, python will efectively
try to call all the implicit constructors as needed.
- For the same reason, it is highly recommended that you use
the new 'castmode' when mixing implicit conversion and
overloading.
- [python] The %implicit directive is declared obsolete, and
you should use %implicitconv instead. If you include
the implicit.i file, a warning will remind you this.
Note: Since %implicit is fragile, just replacing it by
%implicitconv could lead to different behavior. Hence, we
don't automatically switch one by the other, and the user
must migrate to the new %implicitconv directive manually.
12/26/2005: wsfulton
[C#]
Modify std::vector wrappers to use std::vector::value_type as this is
closer to the real STL declarations for some methods, eg for push_back().
Fixes some compilation errors for some compilers eg when the templated
type is a pointer.
[Java]
std::vector improvements - a few more methods are wrapped and specializations are
no longer required. The specialize_std_vector macro is no longer needed (a
warning is issued if an attempt is made to use it).
12/26/2005: wsfulton
[Java, C#]
Add in pointer reference typemaps. This also enables one to easily wrap
std::vector<T> where T is a pointer.
12/24/2005: efuzzyone
[CFFI] The cffi module for SWIG:
- Fully supports C, but provides limited supports for C++, in
particular C++ support for templates and overloading needs to
be worked upon.
12/23/2005: mmatus
[python] Add the castmode that allows the python
type casting to occurr.
For example, if you have 'int foo(int)', now
class Ai():
def __init__(self,x):
self.x = x
def __int__(self):
return self.x
foo(1) // Ok
foo(1.0) // Ok
foo(1.3) // Error
a = Ai(4)
foo(ai) // Ok
The castmode, which can be enable either with the
'-castmode' option or the %module("castmode") option, uses
the new cast/rank dispatch mechanism. Hence, now if you
have 'int foo(int); int foo(double);', the following works
as expected:
foo(1) -> foo(int)
foo(1.0) -> foo(double)
ai = Ai(4)
foo(ai) -> foo(int)
Note1: the 'castmode' could disrupt some specialized
typemaps. In particular, the "implicit.i" library seems to
have problem with the castmode. But besides that one, the
entire test-suite compiles fine with and without the
castmode.
Note2: the cast mode can't be comnined with the fast
dispatch mode, ie, the -fastdispatch option has no effect
when the cast mode is selected. The penalties, however,
are minimum since the cast dispatch code is already based
in the same fast dispatch mechanism.
See the file overload_dispatch_cast_runme.py file for
new cases and examples.
12/22/2005: mmatus
Add the cast and rank mechanism to dispatch overloading
functions. The UTF supports it now, but for each language
it must be decided how to implement and/or when to use it.
[perl] Now perl uses the new cast and rank dispatch
mechanism, which solves all the past problems known
in perl, such as the old '+ 1' problem:
int foo(int);
$n = 1
$n = $n + 1
$r = foo(n)
also works:
foo(1);
foo("1");
foo(1.0);
foo("1.0");
but fails
foo("l");
and when overloading foo(int) and foo(double);
foo(1) -> foo(int)
foo(1.0) -> foo(double)
foo("1") -> foo(int)
foo("1.0") -> foo(double)
foo("l") -> error
foo($n) -> foo(int) for good perl versions
foo($n) -> foo(double) for old bad perl versions
when overloading foo(int), foo(char*) and foo(double):
foo(1) -> foo(int)
foo(1.0) -> foo(double)
foo("1") -> foo(char*)
foo("1.0") -> foo(char*)
foo("l") -> foo(char*)
Note: In perl the old dispatch mechanism was broken,
so, we don't provide and option to enable the old one
since, again, was really really broken.
See 'overload_simple_runme.pl' for more cases and tests.
PS: all the old known issues are declared resolved, any
new "problem" that could be discovered is declared,
a priori, as "features" of the new dispatch mechanism
(until we find another solution at least).
*** POTENTIAL INCOMPATIBILITY ***
As with the introduction of the UTF, some things could
now start to work as expected, and people used to deal or
workaround previous bugs related to the dispatch
mechanism, could see now a difference in perl behavior.
12/21/2005: mmatus
- The '-nodefault' flag (pragma and features) now generates
a warning, and recommends to use the explicit
-nodefaultctor and -nodefaultdtor options.
The reason to split the 'nodefault' behavior is that, in
general, ignoring the default destructor generates memory
leaks in the target language. Hence, is to risky just to
disable the both the default constructor and destructor
at the same time.
If you need to disable the default destructor, it is
also recommended you use the directive form:
%nodefaultdtor MyVerySpecialClass;
for specific classes, and always avoid using the global
-nodefault and -nodefaultdtor options.
12/21/2005: wsfulton
[Java, C#]
Fix incorrect code generation when the intermediary classname is changed
in the module directive from its default. For example:
%module(jniclassname="myimclassnewname") "mymodule" // Java
%module(imclassname="myimclassnewname") "mymodule" // C#
Add in new special variable $imclassname. See docs.
12/17/2005: mmatus
[Python]
- Add the -aliasobj0/-noaliasobj0 options to use with
-fastunpack and/or -O and old typemaps that use 'obj0'
directly.
So, if you compile your code using -O and get errors about
the undeclared 'obj0' variable, run again using
swig -O -aliasobj0 -python ....
For new typemaps, never use 'obj0' directly, if needed,
use the '$self' name that will be properly expanded to
'obj0' (nofastunpack) or 'swig_obj[0]' (fastunpack).
If you have no idea what I am talking about, better, that
means you have no typemap with this problem.
12/14/2005: mmatus
[Python]
- Add the -fastunpack/-nofastunpack options to enable/disable
the use of the internal UnpackTuple method, instead of
calling the one from the python C API.
The option -O now also implies -fastunpack.
12/11/2005: mmatus
[Python]
- Add the -proxydel/-noproxydel options to enable/disable
the generation of proxy/shadow __del__ methods, even
when now are redundant, since they are empty.
However, old interfaces could relay in calling them.
The default behavior is to generate the __del__ methods
as in 1.3.27 or older swig versions.
The option -O now also implies -noproxydel.
12/10/2005: mmatus
[UTF]
- Fix inneccessary calls to SWIG_TypeQuery for 'char *'
and 'wchar_t *', problem found by Clay Culver while
profiling the PyOgre project.
[Python]
- Add the -dirvtable/-nodirvtable to enable/disable
a pseudo virtual table used for directors, avoiding
to resolv the python method at each call.
- Add the -safecstrings/-nosafecstrings options to
enable/disable the use of safe conversions from PyString
to char *. Python requires you never change the internal
buffer directly, and henve 'safectrings' warranties that
but returning a copy of the internal python string buffer.
The default, as in previous releases, is to return a
pointer to the buffer (nosafecstrings), so, is the user
responsability to avoid its modification.
- Add the -O option to enable all the optimization options
at once, initially equivalent to
-modern -fastdispatch -dirvtable -nosafecstrings -fvirtual
12/08/2005: mmatus
- Add the -fastdispatch option (fastdispatch feature). This
enable the "fast dispatch" mechanism for overloaded
methods provided by Salvador Fandi~no Garc'ia (#930586).
The resulting code is smaller and faster since less type
checking are performed. However, the error messages you
get when the overloading is not resolved could be
different from what the traditional method returns.
With the old method you always get an error such as
"No matching function for overloaded ..."
with the new method you can also get errors such as
"Type error in argument 1 of type ..."
See bug report #930586 for more details.
So, we let to the user the option to enable this
optimization.
You use this new mechanism as
swig -fastdispatch
or using the feature form
%feature("fastdispatch") method;
or
%fastdispatch method;
12/06/2005: mmatus
- Several memory and speed improvements, specially for
templates. Now swig is up to 20 faster than before for
large template interfaces, such as the std_containers.i
and template_matrix.i files in the python test-suite.
Memory footprint is also reduced in consideration of small
pcs/architectures.
- add options "cpperraswarn/nocpperraswarn" to force the swig
preprocessor to treat the #error directive as a #warning.
the default behavior is cpperraswarn, so, swig doesn't
stop while encountering an #error directive.
the pragmas
#pragma SWIG cpperraswarn=1
#pragma SWIG cpperraswarn=0
are equivalent to the command line options, respectively.
12/06/2005: mmatus
[Python] The generated code is now more portable, specially
for Windows. Following
http://www.python.org/doc/faq/windows.html
Py_None is never accessed as a structure, plus other
tricks mentioned there.
12/06/2005: mmatus
[Python] Added initial support for threads based in the
proposal by Joseph Winston.
The user interface is as follows:
1.- the module thread support is enable via the "threads" module
option, i.e.
%module("threads"=1)
2.- Equivalent to that, is the new '-threads' swig option
swig -threads -python ...
3.- You can partially disable thread support for a given
method using:
%feature("nothread") method;
or
%nothread method;
also, you can disable sections of the thread support,
for example
%feature("nothreadblock") method;
or
%nothreadblock method;
%feature("nothreadallow") method;
or
%nothreadallow method;
the first disables the C++/python thread protection, and the
second disables the python/C++ thread protection.
4.- The current thread support is based in the PyGIL
extension present in python version 2.3 or later, but
you can provide the thread code for older versions by
defining the macros in pythreads.swg.
If you get a working implementation for older versions,
please send us a patch.
For the curious about performance, here are some numbers
for the profiletest.i test, which is used to check the speed
of the wrapped code:
nothread 9.6s (no thread code)
nothreadblock 12.2s (only 'allow' code)
nothreadallow 13.6s (only 'block' code)
full thread 15.5s ('allow' + 'block' code)
i.e., full thread code decreases the wrapping performance
around 60%. If that is important to your application, you
can tune each method using the different 'nothread',
'nothreadblock' or 'nothreadallow' features as
needed. Note that for some methods deactivating the
'thread block' or 'thread allow' code is not an option,
so, be careful.
11/26/2005: wsfulton
SWIG library files use system angle brackets everywhere for %include, eg
%include "std_common.i"
becomes
%include <std_common.i>
11/26/2005: wsfulton
[Java, C#]
Typesafe enums and proper enums have an extra constructor so that enum item values that
are initialised by another enum item value can be wrapped without having to use %javaconstvalue/
%csconstvalue for when using %javaconst(1)/%csconst(1). Suggestion by
Bob Marinier/Douglas Pearson.
For example:
typedef enum
{
xyz,
last = xyz
} repeat;
11/21/2005: mmatus
[ruby + python]
Fixes for directors + pointers, ugly problem with not easy
solution. Before we identified this case as problematic:
virtual const MyClass& my_method();
but it turns out that all the cases where a pointer, array or
reference is returned, are problematic, even for
primitive types (as int, double, char*, etc).
To try to fix the issue, a new typemap was added,
'directorfree', which is used to 'free' the resources
allocated during the 'directorout' phase. At the same
time, a primitive garbage collector engine was added to
deal with orphans addresses, when needed.
The situation now is much better, but still you can have
memory exaustation if recursion is used.
So, still you need to avoid returning pointers, arrays or
references when using director methods.
- Added stdint.i
11/14/2005: wsfulton
More types added to windows.i, eg UINT8, WORD, BYTE etc.
Including windows.i will also enable SWIG to parse the __declspec Microsoft
extension, eg __declspec(dllimport). Also other Windows calling conventions
such as __stdcall.
11/10/2005: wsfulton
New library file for Windows - windows.i. This file will contain useful type
information for users who include windows.h. Initial support is for the
non ISO integral types: __int8, __int16, __int32, __int64 and unsigned versions.
The unsigned versions previously could not be parsed by SWIG. SF #872013.
11/09/2005: wsfulton
[Java, C#] Portability warning for files which will overwrite each other on case
insensitive file systems such as FAT32/NTFS. This will occur, for example, when two
class names are the same barring case. The warning is issued on all platforms and
can be suppressed with the usual warning suppression techniques. SF bug #1084507.
11/09/2005: wsfulton
./configure --with-python --with-ruby --with-perl5 etc enable these languages,
ie the --with-xxxx options, where no path is specified, work the same as if
the option was not specified at all. Based on patches #1335042 #1329048 #1329047.
11/09/2005: dancy
[Allegrocl]
Add C++ support to the Allegrocl module. Further
enhances the C support as well. Some of the
features:
- MUCH better generation of foreign types based on
the C/C++ types for use in defining the FFI on
the lisp side. We don't pass everything as a (* :void)
any longer.
- Uses typemaps for better control of type conversions
and code generation in the generated lisp and c++ wrapper
code.
- CLOS wrapping of pointers returned from foreign space
makes it easier to differentiate pointers in user code.
The wrapping objects can be passed directly to FF calls.
- Defun wrapping of FF calls, allowing for more lispy
interface. Conversion, GCing, of lisp objects to
foreign objects can be done in the wrapping defun via
the use of typemaps.
- overload dispatching implemented on the lisp side
using generic functions.
- Templates and synonymous types supported.
11/07/2005: mmatus
[Python] Adding proper support for multi-inheritance in
the python side, ie, if you have two C++ wrapped class, Foo
and Bar, now:
class MyPythonClass(Foo,Bar):
....
will properly work, even with directors, and the
deallocation of Foo.this and Bar.this will follow
correctly. Before, since a class can only have one 'this'
instance (not as in C++), only the last base class was
properly deletted, or detected with directors.
Now 'self.this' can be a list, which will contain the C++
instance pointers for all the base classes.
Also, swig.this is responsible for deallocating the C++
instance(s), and the __del__ method is not emitted unless
the user preppend/append some code to it.
- Swig now can detect memory leaks, ie, if you still
use the non-shadow module, and type something like
import _example
f = _example.new_Foo()
and forgot to call _example.delete_Foo(f), then swig will
tell you that there is a memory leak.
Otherwise, if you always use the shadow module, probably
you will never ever see this warning unless there is
something wrong inside the swig wrapping code.
*** POTENTIAL INCOMPATIBILITY ***
If you overloaded the __del__ method, and call the base
one without a try block, as in
class MyClass(SwigClass):
def __del__(self):
<your code here>
SwigClass.__del__(self)
python could complain that the method SwigClass.__del__ is
undefined. Try to use instead:
def __del__(self):
<your code here>
try: SwigClass.__del__(self)
except: pass
or simply
def __del__(self):
<your code here>
11/02/2005: mmatus
[Python] Adding more fun to STL/STD containers, now you
can do
%template(pyset) std::set<PyObject *>;
%template(pyvector) std::vector<PyObject *>;
%template() std::pair<PyObject *,PyObject *>;
%template(pyvector) std::map<PyObject *,PyObject *>;
....
The same applies to std::list, std::deque, std::multiset, etc.
Then, at the python side you can do now:
# C++ std::vector as native python sequence
v = pyvector([1,"hello",(1,2)])
print v[1]
>> 'hello'
print v[2]
>> (1,2)
# C++ std::set as native python sequence
s = pyset()
s.insert((1,2))
s.insert(1)
s.insert("hello")
sum=()
for i in s:
sum +=(i,)
print sum
>>> (1, 'hello', (1, 2))
# C++ std::map as native python sequence
m = pymap()
m["foo"] = "hello"
m[1] = (1,2)
pm = {}
for k in m:
pm[k] = m[k]
print pm
>>> {1: (1, 2), 'foo': 'hello'}
ie, the STD/STL containers work as real native python
container, with arbitrary item types and so.
But since normal C++ containers do not properly ref/unref
their items, you should use the safer versions:
%template(pyset) std::set<swig::PyObject_ptr>;
%template(pyvector) std::vector<swig::PyObject_ptr>;
%template() std::pair<swig::PyObject_ptr, swig::PyObject_ptr>;
%template(pyvector) std::map<swig::PyObject_ptr,swig::PyObject_ptr>;
....
where swig::PyObject_ptr is a PyObject * envelope class provided
to safely incref/decref the python object.
So, now you can use all the STL/STD containers as native
Python containers.
Note 1: std::map, std::set and the other 'ordered'
containers will properly use PyObject_Compare for sorting,
when needed.
Note 2: all the STL/STD containers have a limit size of
SIZE_MAX, ie, you can have manage containers larger than
INT_MAX, the python limit.
11/02/2005: mmatus
[Python]
- add 'iterator()' method for all sequences and additionally
'key_iterator()' for maps.
'iterator()' will always return the native C++ iterator.
Additionally, in maps, 'key_iterator()' will return a python
iterator using only the map keys.
In general the sequence method __iter__ will call
'iterator()', returning the native C++ iterator, but in
maps it will call 'key_iterator()', maintaining
backward compatibility.
Hence, for std::maps, you can play then with the native
C++ iterator, which value is a (key, value) pair, by
calling map.iterator(), as with map.begin(), map.end(), etc.
The difference is that map.iterator() returns a safe
'close' iterator, while map.begin() and map.end() are
'open' iterators.
A 'close' iterator knows the begin and the end of the
sequence, and it never can seg. fault. A 'open'
iterator, as in C++, can seg. fault at the C++ side.
# a close iterator is safe in the following example.
# the next() method will throw a StopIteration
# exception as needed
i = seq.iterator()
try:
while True:
sum += i.next()
except: pass
# an open iterator always need to be checked,
# or it will crash at the C++ side
current = seq.begin()
end = seq.end()
while (current != end):
sum += current.next()
[Python]
- Finally, when we call
f = Foo()
the construction is 'one-way'. Before construction was done
something like
Foo() (python) -> _new_Foo() (C++)
new_Foo() (C++) -> FooPtr() (python)
FooPtr() (python) -> Foo() (python)
and returning a pointer was done like
NewPointerObj() (C++) -> FooPtr() (python)
FooPtr(python) -> Foo() (python)
ie, we when going back and forward between the C++ and
python side.
Now since there is no FooPtr the construction process is
Foo() (python) -> _new_Foo() (C++)
_new_Foo() (C++) -> NewPointerObj() (C++) (no shadow class)
and returning a pointer is done
NewPointerObj() (C++) (with shadow class) -> NewInstaceObj() (C++)
where NewInstanceObj creates a new instance without
calling __init__ and it doesn't go 'back' to python, is
'pure' C API.
- With this change, and the other ones in the
PySwigObject type, which now carries the thisown and
swig_type_info pointer, the generated code should be as
fast as boost::Python and/or the other python wrappers
based in pure Python/C API calls.
As a reference, the profiletest_runme.py example, which
does a simple call function many times, such as this code:
import profiletest
a = profiletest.A()
b = profiletest.B()
for i in range(0,1000000)
a = b.fn(a)
where fn is defined as 'A* B::fn(A *a) {return a;}',
produces the following times
nomodern modern
swig-1.3.26 19.70s 5.98s
swig-CVS 0.99s 0.98s
Clearly, there is a large improvement for the python
'nomodern' mode. Still, the 'modern' mode is around
6 times faster than before. For the same test, but
using the non-shadow version of the module, we get
_profiletest (non-shadow)
swig-1.3.26 0.80s
swig-CVS 0.60s
Hence, now for practical porpuses, the proxy overhead
is insignificant.
Note that the performance numbers we are showing is for
a simple module (two types) and a simple function (one
argument). For real situations, with modules with many
more types and/or functions with many more parameters,
you will see even better results.
10/31/2005: mmatus
[Python]
- Finally, no more ClassPtr shadow classes. You will see
only a clean Class shadow class in the .py file.
- No more 'real' thisown attribute either, the PySwigObject now
carries the ownership info.
You can also do something like
print self.this.own()
>>> True
self.this.disown()
self.this.own(0)
print self.this.own()
>>> False
self.this.acquire()
self.this.own(1)
print self.this.own()
>>> True
Still the old way,
print self.thisown
>>> True
self.thisown = 0
print self.thisown
>>> False
self.thisown = 1
print self.thisown
>>> True
is supported, and python dispatch the proper method
calls as needed.
- Support for iterators in STL/STD containers, for example, if you have
%template<set_string> std::set<std::string>;
you can use the C++ iterators as:
s = set_string()
s.append("c")
s.append("a")
s.append("b")
b = s.begin()
e = s.end()
sum = ""
while (b != e):
sum += b.next()
print sum
>>> "abc"
advance the iterator as in C++
current = s.begin()
current += 1
print current.value()
>>> "b"
now using the reverse operators
b = s.rbegin()
e = s.rend()
sum = ""
while (b != e):
sum += b.next()
print sum
>>> "cba"
or the 'previous' method
b = s.begin()
e = s.end()
sum = ""
while (b != e):
sum += e.previous()
print sum
>>> "cba"
or just as in a python fashion
for i in s:
sum += i
Note 1: Iterators in C++ are very powerful, but
dangerous too. And in python you can shoot your foot
as well as in C++, so, be careful.
Note 2: the iterators are 'light', ie, they do not
convert sequence elements until you request so, via
next(), value() or previous(). If you just increment/decrement one
no conversion is performed, for example:
b = s.begin()
b += 1
b.incr()
b.incr(2)
b.decr(2)
b.decr()
b -= 1
only the iterator is modified, and not value wrapper
is generated. Other typical C++ operations are also
available, such as:
print s.end() - s.begin()
>>> 3
f = s.begin() + 1
print f.value()
>>> "b"
l = s.end() - 1
print l.value()
>>> "c"
etc. Of course, the 'find', 'insert', 'erase', and
so on methods also supports iterators now, ie:
i = s.begin()
i += 1
s.erase(i)
for i in s:
sum += i
print sum
>>> "ac"
*** POTENTIAL INCOMPATIBILITY ***
There is no more 'thisown' attribute. If you use it, python
will translate the following code as follows:
if (self.thisown): ==> if (self.this.own()):
self.thisown = 1 ==> self.this.own(1)
self.thisown = 0 ==> self.this.own(0)
Still, maybe in some strange case the translation is not
100% secure,so if you have a problem, please report it
and/or use the new 'self.this.own()' accessor.
*** POTENTIAL INCOMPATIBILITY ***
There is no more ClassPtr classes in the python code. Hence,
if in the past you needed to resort in some kind of trick
to use them, or overcome their presence, it is no longer
required, but the extra code you added could now break
things arounds.
If needed, you can use the option -classptr, i.e.,
swig -classptr -python ...
to generate the old ClassPtr classes.
10/30/2005: mkoeppe
[Guile] Make declared and defined linkage of SWIG_init consistent.
Reported by Steven G. Johnson (SF patch 1315498).
10/26/2005: mmatus
- Added the attribute.i file to the global library director.
Now it can be used from other languages that do not use
the unified typemap library as well.
So, if you have something like:
%include attribute.i
%attribute(A, int, a, get_a, set_a);
struct A
{
int get_a() const;
void set_a(int aa);
};
%attribute_ref(B, int, c);
struct B
{
int& c();
};
then in the target language the 'A.a' and 'B.c' attributes will
be visible, ie, you can access them as plain variables:
f = A()
f.a = 3
g = B()
g.c = 3
h = f.a + g.c
and the proper get/set methods will be dispatched. See
attribute.i for more info.
- More cleanups around and adding more test-cases. The
DISOWN typemap now is tested and working in all the
languages that use the unified typemap library, ie, tcl,
ruby, perl and python.
10/25/2005: mmatus
- Perl, complete the DISOWN typemap.
- added the attribute.i file to the unified typemap
library (before was only usable from python).
- uniform the names for the setter and getter methods in
perl,tcl,ruby and python, so, the attribute.i library
can work across them.
- see the li_attribute.i test-case or the library file
Lib/typemaps/attribute.swg
for more info about how to use it.
10/24/2005: mmatus
- Perl uses now the unified typemap library.
- Changes in ruby to use the $track option in typemaps.
- Changes in the unified typemap library to follow the
convention that all macros that are not used in the
C/C++ side starts with %, such as
%delete
%new_array
etc.
- Documenting fragments, see fragments.swg.
- Cleaner way to use the unified typemap library, include
just <typemaps/swigtypes.swg>.
Check some of the supported languages: perl, tcl, ruby,
python.
Always start with the head file, such as
python/python.swg
tcl/tcl8.swg
ruby/ruby.swg
perl5/perl5.swg
and the principal file that invokes the unified library, such as
python/pytypemaps.swg
tcl/tcltypemaps.swg
ruby/rubytypemaps.swg
perl/perltypemaps.swg
The file that provide the specialization for each
language are the one that provides the basic types:
python/pyprimtypes.swg
ruby/rubyprimtypes.swg
tcl/tclprimtypes.swg
perl5/perlprimtypes.swg
and the string manipulation:
python/pystrings.swg
ruby/rubystrings.swg
tcl/tclstrings.swg
perl5/perlstrings.swg
The rest of the files, such as carray.i, are mostly one
line files that include the proper typemap library
version.
*** POTENTIAL INCOMPATIBILITY in Perl ***
Some missing/wrong typemaps could start working properly,
and change the old expected behavior in Perl.
10/23/2005: wuzzeb
Chicken:
+ pointers to member functions finally work properly
+ add test of member function pointers to cpp_basic.i
10/20/2005: dancy
[allegrocl] Added C++ support. Large update, many changes. See
newly added Allegro Common Lisp section in lisp.html
10/20/2005: mmatus
Ruby, Tcl, Python:
- Uniform way to fail (label fail:), now finally
SWIG_exception works across the three languages and all
the typemaps.
- Add proper cleanup code to ruby
- More valgrind fixes
- Simplify the inline use, it seems a small interface of
20,000 lines (plus many many templates0 can break
gcc -O3 easily.
- Finalize the typemaps library. All the old *.i files
(carray.i, cpointer.i, exception.i) had been implemented
in the new typemaps library.
10/19/2005: wuzzeb
Update the Runtime Typemap documentation in Typemaps.html
10/18/2005: wuzzeb
Chicken:
- Correctly handle %ignored classes
- Correctly convert long, long long, unsigned long, etc
to chicken primitives. (Thanks to Felix Winkelmann)
- Using argout parameters when the return value was a
wrapped pointer caused a memory corruption. The chicken
garbage collector moved a pointer out from under us.
This is now fixed by running all the proxy creation
functions as continuations after the wrapper function
returns. As part of this, we no longer need the
chickenfastproxy flag on output typemaps.
- using -proxy and -nocollection together works now
Before, it was not exporting the destructor in the proxy
wrapper.
10/18/2005: mmatus
Unifying the typemaps for
python, ruby, tcl
and in the process, fix several problems in three
languages to work in the "canonical" way now stablished in
the typemap library
SWIG/Lib/typempas
The current status of the unification is that everything
compiles and runs inside the test-suite and examples
directories. And for the first type we have three
languages than pass the primitive_types.i case.
Also, we have uniform way to treat the errors, for example
if you do something like
>>> from primitive_types import *
>>> print val_uchar(10)
10
>>> print val_uchar(1000)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OverflowError: in argument 1 of type 'unsigned char'
you get the same exception in all the three languages.
And well, many more good things will come from this
unification, as proper support of the STL/STD classes
for all the languages, and hopefully, we can keep
adding other languages.
The hardest part, writting a common typemap library
that suites the three different languages, is done,
and adding another language it is easy now.
Still the global unification is not complete, the STL/STD
part is next, and probably adding one or two more
languages.
If you are curious, look at the python, ruby and/or tcl
directories to see what is needed to support the new
common typemaps library. Still, the final way to
integrate a new language could change as we move to
integrate the STD/STL.
*** POTENTIAL INCOMPATIBILITY in Ruby/Tcl ***
Some missing/wrong typemaps could start working properly,
and change the old expected behavior, specially in ruby
and tcl.
|