PageRenderTime 69ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Doc/Manual/Allegrocl.html

#
HTML | 2027 lines | 1671 code | 353 blank | 3 comment | 0 complexity | 886f5a6988c8b604cbf992f7f8fbfa1f MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <!-- Hand-written HTML -->
  3. <html>
  4. <head>
  5. <title>SWIG and Allegro Common Lisp</title>
  6. <link rel="stylesheet" type="text/css" href="style.css">
  7. </head>
  8. <body bgcolor="#ffffff">
  9. <H1><a name="Allegrocl"></a>17 SWIG and Allegro Common Lisp</H1>
  10. <!-- INDEX -->
  11. <div class="sectiontoc">
  12. <ul>
  13. <li><a href="#Allegrocl_nn2">Basics</a>
  14. <ul>
  15. <li><a href="#Allegrocl_nn3">Running SWIG</a>
  16. <li><a href="#Allegrocl_nn4">Command Line Options</a>
  17. <li><a href="#Allegrocl_nn5">Inserting user code into generated files</a>
  18. </ul>
  19. <li><a href="#Allegrocl_nn6">Wrapping Overview</a>
  20. <ul>
  21. <li><a href="#Allegrocl_nn7">Function Wrapping</a>
  22. <li><a href="#Allegrocl_nn8">Foreign Wrappers</a>
  23. <li><a href="#Allegrocl_nn9">FFI Wrappers</a>
  24. <li><a href="#Allegrocl_nn10">Non-overloaded Defuns</a>
  25. <li><a href="#Allegrocl_nn11">Overloaded Defuns</a>
  26. <li><a href="#Allegrocl_nn12">What about constant and variable access?</a>
  27. <li><a href="#Allegrocl_nn13">Object Wrapping</a>
  28. </ul>
  29. <li><a href="#Allegrocl_nn14">Wrapping Details</a>
  30. <ul>
  31. <li><a href="#Allegrocl_nn15">Namespaces</a>
  32. <li><a href="#Allegrocl_nn16">Constants</a>
  33. <li><a href="#Allegrocl_nn17">Variables</a>
  34. <li><a href="#Allegrocl_nn18">Enumerations</a>
  35. <li><a href="#Allegrocl_nn19">Arrays</a>
  36. <li><a href="#Allegrocl_nn20">Classes and Structs and Unions (oh my!)</a>
  37. <ul>
  38. <li><a href="#Allegrocl_nn21">CLOS wrapping of</a>
  39. <li><a href="#Allegrocl_nn22">CLOS Inheritance</a>
  40. <li><a href="#Allegrocl_nn23">Member fields and functions</a>
  41. <li><a href="#Allegrocl_nn24">Why not directly access C++ classes using foreign types?</a>
  42. </ul>
  43. <li><a href="#Allegrocl_nn25">Templates</a>
  44. <ul>
  45. <li><a href="#Allegrocl_nn26">Generating wrapper code for templates</a>
  46. <li><a href="#Allegrocl_nn27">Implicit Template instantiation</a>
  47. </ul>
  48. <li><a href="#Allegrocl_nn28">Typedef, Templates, and Synonym Types</a>
  49. <ul>
  50. <li><a href="#Allegrocl_nn29">Choosing a primary type</a>
  51. </ul>
  52. <li><a href="#Allegrocl_nn30">Function overloading/Parameter defaulting</a>
  53. <li><a href="#Allegrocl_nn31">Operator wrapping and Operator overloading</a>
  54. <li><a href="#Allegrocl_nn32">Varargs</a>
  55. <li><a href="#Allegrocl_nn33">C++ Exceptions</a>
  56. <li><a href="#Allegrocl_nn34">Pass by value, pass by reference</a>
  57. </ul>
  58. <li><a href="#Allegrocl_nn35">Typemaps</a>
  59. <ul>
  60. <li><a href="#Allegrocl_nn36">Code Generation in the C++ Wrapper</a>
  61. <ul>
  62. <li><a href="#Allegrocl_nn37">IN Typemap</a>
  63. <li><a href="#Allegrocl_nn38">OUT Typemap</a>
  64. <li><a href="#Allegrocl_nn39">CTYPE Typemap</a>
  65. </ul>
  66. <li><a href="#Allegrocl_nn40">Code generation in Lisp wrappers</a>
  67. <ul>
  68. <li><a href="#Allegrocl_nn41">LIN Typemap</a>
  69. <li><a href="#Allegrocl_nn42">LOUT Typemap</a>
  70. <li><a href="#Allegrocl_nn43">FFITYPE Typemap</a>
  71. <li><a href="#Allegrocl_nn44">LISPTYPE Typemap</a>
  72. <li><a href="#Allegrocl_nn45">LISPCLASS Typemap</a>
  73. </ul>
  74. <li><a href="#Allegrocl_nn46">Modifying SWIG behavior using typemaps</a>
  75. </ul>
  76. <li><a href="#Allegrocl_nn47">Identifier Converter functions</a>
  77. <ul>
  78. <li><a href="#Allegrocl_nn48">Creating symbols in the lisp environment</a>
  79. <li><a href="#Allegrocl_nn49">Existing identifier-converter functions</a>
  80. <ul>
  81. <li><a href="#Allegrocl_nn50">identifier-convert-null</a>
  82. <li><a href="#Allegrocl_nn51">identifier-convert-lispify</a>
  83. <li><a href="#Allegrocl_nn52">Default identifier to symbol conversions</a>
  84. </ul>
  85. <li><a href="#Allegrocl_nn53">Defining your own identifier-converter</a>
  86. <li><a href="#Allegrocl_nn54">Instructing SWIG to use a particular identifier-converter</a>
  87. </ul>
  88. </ul>
  89. </div>
  90. <!-- INDEX -->
  91. <p>
  92. This chapter describes SWIG's support of Allegro Common Lisp. Allegro
  93. CL is a full-featured implementation of the Common Lisp language
  94. standard that includes many vendor-specific enhancements and add-on
  95. modules for increased usability.
  96. </p>
  97. <p>
  98. One such module included in Allegro CL is the Foreign Functions
  99. Interface (FFI). This module, tailored primarily toward interfacing
  100. with C/C++ and, historically, Fortran, provides a means by which
  101. compiled foreign code can be loaded into a running lisp
  102. environment and executed. The interface supports the calling of
  103. foreign functions and methods, allows for executing lisp routines
  104. from foreign code (callbacks), and the passing of data between foreign
  105. and lisp code.
  106. </p>
  107. <p>
  108. The goal of this module is to make it possible to quickly generate the
  109. necessary foreign function definitions so one can make use of C/C++
  110. foreign libraries directly from lisp without the tedium of having to
  111. code them by hand. When necessary, it will also generate further C/C++
  112. code that will need to be linked with the intended library for proper
  113. interfacing from lisp. It has been designed with an eye toward
  114. flexibility. Some foreign function calls may release the heap, while
  115. other should not. Some foreign functions should automatically convert
  116. lisp strings into native strings, while others should not. These
  117. adjustments and many more are possible with the current module.
  118. </p>
  119. <p>
  120. It is significant to note that, while this is a vendor-specific
  121. module, we would like to acknowledge the current and ongoing
  122. work by developers in the open source lisp community that are
  123. working on similar interfaces to implementation-independent
  124. foreign function interfaces (UFFI or CFFI, for example). Such
  125. work can only benefit the lisp community, and we would not
  126. be unhappy to see some enterprising folk use this work to add
  127. to it.
  128. </p>
  129. <H2><a name="Allegrocl_nn2"></a>17.1 Basics</H2>
  130. <H3><a name="Allegrocl_nn3"></a>17.1.1 Running SWIG</H3>
  131. <p>
  132. If you're reading this, you must have some library you need to
  133. generate an interface for. In order for SWIG to do this work, however,
  134. it needs a bit of information about how it should go about creating
  135. your interface, and what you are interfacing to.
  136. </p>
  137. <p>
  138. SWIG expects a description of what in the foreign interface you wish
  139. to connect to. It must consisting of C/C++ declarations and special
  140. SWIG directives. SWIG can be furnished with a header file, but an
  141. interface can also be generated without library headers by supplying a
  142. simple text file--called the interface file, which is typically named
  143. with a <tt>.i</tt> extension--containing any foreign declarations of
  144. identifiers you wish to use. The most common approach is to use a an
  145. interface file with directives to parse the needed headers. A straight
  146. parse of library headers will result in usable code, but SWIG
  147. directives provides much freedom in how a user might tailor the
  148. generated code to their needs or style of coding.
  149. </p>
  150. <p>
  151. Note that SWIG does not require any function definitions; the
  152. declarations of those functions is all that is necessary. Be careful
  153. when tuning the interface as it is quite possible to generate code
  154. that will not load or compile.
  155. </p>
  156. <p>
  157. An example interface file is shown below. It makes use of two SWIG
  158. directives, one of which requests that the declarations in a header
  159. file be used to generate part of the interface, and also includes an
  160. additional declaration to be added.</p>
  161. <div class="code">example.i
  162. <pre>
  163. %module example
  164. %include "header.h"
  165. int fact(int n);
  166. </pre>
  167. </div>
  168. <p>The contents of header.h are very simple:</p>
  169. <div class="code">header.h
  170. <pre>
  171. int fact(char *statement); // pass it a fact, and it will rate it.
  172. </pre>
  173. </div>
  174. <p>The contents of example.cl will look like this:</p>
  175. <div class="targetlang">example.cl
  176. <pre>
  177. (defpackage :example
  178. (:use :common-lisp :swig :ff :excl))
  179. ... helper routines for defining the interface ...
  180. (swig-in-package ())
  181. (swig-defun ("fact")
  182. ((PARM0_statement cl:string (* :char) ))
  183. (:returning (:int )
  184. :strings-convert t)
  185. (let ((SWIG_arg0 PARM0_statement))
  186. (swig-ff-call SWIG_arg0)))
  187. (swig-defun ("fact")
  188. ((PARM0_n cl:integer :int ))
  189. (:returning (:int )
  190. :strings-convert t)
  191. (let ((SWIG_arg0 PARM0_n))
  192. (swig-ff-call SWIG_arg0)))
  193. (swig-dispatcher ("fact" :type :function :arities (1)))
  194. </pre>
  195. </div>
  196. <p>
  197. The generated file contains calls to internal swig helper
  198. functions. In this case there are two calls to swig-defun.
  199. These calls will expand into code that will make the appropriate
  200. definitions using the Allegro FFI. Note also, that this code is
  201. <b>erroneous</b>. Function overloading is not supported in C, and this
  202. code will not compile even though SWIG did not complain.
  203. </p>
  204. <p>
  205. In order to generate a C interface to Allegro CL using this code run
  206. swig using the <tt>-allegrocl</tt> option, as below:
  207. </p>
  208. <div class="shell">
  209. <pre>
  210. % swig -allegrocl example.i
  211. </pre>
  212. </div>
  213. <p>
  214. When building an interface to C++ code, include the <tt>-c++</tt> option:
  215. </p>
  216. <div class="shell">
  217. <pre>
  218. % swig -allegrocl -c++ example.i
  219. </pre>
  220. </div>
  221. <p>
  222. As a result of running one of the above commands, a file named <tt>example.cl</tt>
  223. will be generated containing the lisp side of the interface. As well, a file
  224. <tt>example_wrap.cxx</tt> is also generated, containing C/C++ wrapper code to
  225. facilitate access to C++ methods, enumeration values, and constant values.
  226. Wrapper functions are necessary in C++ due to the lack of a standard for mangling
  227. the names of symbols across all C++ compilers. These wrapper functions are
  228. exported from the shared library as appropriate, using the C name mangling
  229. convention. The lisp code that is generated will interface to your foreign
  230. library through these wrappers.
  231. </p>
  232. <p>
  233. It is possible to disable the creation of the .cxx file when generating a C
  234. interface by using the -nocwrap command-line argument. For interfaces that
  235. don't contain complex enum or constant expressions, contain nested struct/union
  236. declarations, or doesn't need to use many of the SWIG customization featuers,
  237. this will result in a more streamlined, direct interface to the
  238. intended module.
  239. </p>
  240. <p>
  241. The generated wrapper file is below. It contains very simple
  242. wrappers by default, that simply pass the arguments to the
  243. actual function.
  244. </p>
  245. <div class="code">example_wrap.i
  246. <pre>
  247. ... lots of SWIG internals ...
  248. EXPORT int ACL___fact__SWIG_0 (char *larg1) {
  249. int lresult = (int)0 ;
  250. char *arg1 = (char *) 0 ;
  251. int result;
  252. arg1 = larg1;
  253. try {
  254. result = (int)fact(arg1);
  255. lresult = result;
  256. return lresult;
  257. } catch (...) {
  258. return (int)0;
  259. }
  260. }
  261. EXPORT int ACL___fact__SWIG_1 (int larg1) {
  262. int lresult = (int)0 ;
  263. int arg1 ;
  264. int result;
  265. arg1 = larg1;
  266. try {
  267. result = (int)fact(arg1);
  268. lresult = result;
  269. return lresult;
  270. } catch (...) {
  271. return (int)0;
  272. }
  273. }
  274. </pre>
  275. </div>
  276. <p>
  277. And again, the generated lisp code. Note that it differs from
  278. what is generated when parsing C code:
  279. </p>
  280. <div class="targetlang">
  281. <pre>
  282. ...
  283. (swig-in-package ())
  284. (swig-defmethod ("fact" "ACL___fact__SWIG_0" :type :function :arity 1)
  285. ((PARM0_statement cl:string (* :char) ))
  286. (:returning (:int )
  287. :strings-convert t)
  288. (let ((SWIG_arg0 PARM0_statement))
  289. (swig-ff-call SWIG_arg0)))
  290. (swig-defmethod ("fact" "ACL___fact__SWIG_1" :type :function :arity 1)
  291. ((PARM0_n cl:integer :int ))
  292. (:returning (:int )
  293. :strings-convert t)
  294. (let ((SWIG_arg0 PARM0_n))
  295. (swig-ff-call SWIG_arg0)))
  296. (swig-dispatcher ("fact" :type :function :arities (1)))
  297. </pre>
  298. </div>
  299. <p>In this case, the interface generates two swig-defmethod forms and
  300. a swig-dispatcher form. This provides a single functional interface for
  301. all overloaded routines. A more detailed description of this features
  302. is to be found in the section titled <b>Function overloading/Parameter defaulting</b>.
  303. <p>
  304. In order to load a C++ interface, you will need to build a shared library
  305. from example_wrap.cxx. Be sure to link in the actual library you created
  306. the interface for, as well as any other dependent shared libraries. For
  307. example, if you intend to be able to call back into lisp, you will also
  308. need to link in the Allegro shared library. The library you create from
  309. the C++ wrapper will be what you then load into Allegro CL.
  310. </p>
  311. <H3><a name="Allegrocl_nn4"></a>17.1.2 Command Line Options</H3>
  312. <p>
  313. There are three Allegro CL specific command-line option:
  314. </p>
  315. <div class="shell">
  316. <pre>
  317. swig -allegrocl [ options ] filename
  318. -identifier-converter [name] - Binds the variable swig:*swig-identifier-convert*
  319. in the generated .cl file to <tt>name</tt>.
  320. This function is used to generate symbols
  321. for the lisp side of the interface.
  322. -cwrap - [default] Generate a .cxx file containing C wrapper function when
  323. wrapping C code. The interface generated is similar to what is
  324. done for C++ code.
  325. -nocwrap - Explicitly turn off generation of .cxx wrappers for C code. Reasonable
  326. for modules with simple interfaces. Can not handle all legal enum
  327. and constant constructs, or take advantage of SWIG customization features.
  328. -isolate - With this command-line argument, all lisp helper functions are defined
  329. in a unique package named <tt>swig.&lt;module-name&gt;</tt> rather than
  330. <tt>swig</tt>. This prevents conflicts when the module is
  331. intended to be used with other swig generated interfaces that may,
  332. for instance, make use of different identifier converters.
  333. </pre>
  334. </div>
  335. <p>
  336. See <a href="#Allegrocl_nn47">Section 17.5 Identifier converter
  337. functions</a> for more details.
  338. </p>
  339. <H3><a name="Allegrocl_nn5"></a>17.1.3 Inserting user code into generated files</H3>
  340. <p>
  341. It is often necessary to include user-defined code into the
  342. automatically generated interface files. For example, when building
  343. a C++ interface, example_wrap.cxx will likely not compile unless
  344. you add a <tt>#include "header.h"</tt> directive. This can be done
  345. using the SWIG <tt>%insert(section) %{ ...code... %}</tt> directive:
  346. </p>
  347. <div class="code">
  348. <pre>
  349. %module example
  350. %{
  351. #include "header.h"
  352. %}
  353. %include "header.h"
  354. int fact(int n);
  355. </pre>
  356. </div>
  357. <p>
  358. Additional sections have been added for inserting into the
  359. generated lisp interface file
  360. </p>
  361. <ul>
  362. <li><tt>lisphead</tt> - inserts before type declarations</li>
  363. <li><tt>lisp</tt> - inserts after type declarations according to
  364. where it appears in the .i file</li>
  365. </ul>
  366. <p>
  367. Note that the block <tt>%{ ... %}</tt> is effectively a shortcut for
  368. <tt>%insert("header") %{ ... %}</tt>.
  369. </p>
  370. <H2><a name="Allegrocl_nn6"></a>17.2 Wrapping Overview</H2>
  371. <p>
  372. New users to SWIG are encouraged to read
  373. <a href="SWIG.html#SWIG">SWIG Basics</a>, and
  374. <a href="SWIGPlus.html#SWIGPlus">SWIG and C++</a>, for those
  375. interested in generating an interface to C++.
  376. </p>
  377. <H3><a name="Allegrocl_nn7"></a>17.2.1 Function Wrapping</H3>
  378. <p>
  379. Writing lisp code that directly invokes functions at the foreign
  380. function interface level can be cumbersome. Data must often be
  381. translated between lisp and foreign types, data extracted from
  382. objects, foreign objects allocated and freed upon completion of
  383. the foreign call. Dealing with pointers can be unwieldy when it
  384. comes to keeping them distinct from other valid integer values.
  385. </p>
  386. <p>
  387. We make an attempt to ease some of these burdens by making the
  388. interface to foreign code much more lisp-like, rather than C
  389. like. How this is done is described in later chapters. The
  390. layers themselves, appear as follows:
  391. </p>
  392. <div class="diagram">
  393. <pre>
  394. ______________
  395. | | (foreign side)
  396. | Foreign Code | What we're generating an interface to.
  397. |______________|
  398. |
  399. |
  400. _______v______
  401. | | (foreign side)
  402. | Wrapper code | extern "C" wrappers calling C++
  403. |______________| functions and methods.
  404. |
  405. . . . - - + - - . . .
  406. _______v______
  407. | | (lisp side)
  408. | FFI Layer | Low level lisp interface. ff:def-foreign-call,
  409. |______________| ff:def-foreign-variable
  410. |
  411. +----------------------------
  412. _______v______ _______v______
  413. | | | | (lisp side)
  414. | Defuns | | Defmethods | wrapper for overloaded
  415. |______________| |______________| functions or those with
  416. (lisp side) | defaulted arguments
  417. Wrapper for non-overloaded |
  418. functions and methods _______v______
  419. | | (lisp side)
  420. | Defuns | dispatch function
  421. |______________| to overloads based
  422. on arity
  423. </pre>
  424. </div>
  425. <H3><a name="Allegrocl_nn8"></a>17.2.2 Foreign Wrappers</H3>
  426. <p>
  427. These wrappers are as generated by SWIG default. The types of
  428. function parameters can be transformed in place using the CTYPE
  429. typemap. This is use for converting pass-by-value parameters to
  430. pass-by-reference where necessary. All wrapper parameters are then
  431. bound to local variables for possible transformation of values
  432. (see LIN typemap). Return values can be transformed via the OUT
  433. typemap.
  434. </p>
  435. <H3><a name="Allegrocl_nn9"></a>17.2.3 FFI Wrappers</H3>
  436. <p>
  437. These are the generated ff:def-foreign-call forms. No typemaps are
  438. applicable to this layer, but the <tt>%ffargs</tt> directive is
  439. available for use in .i files, to specify which keyword arguments
  440. should be specified for a given function.
  441. </p>
  442. <div class="code">ffargs.i:
  443. <pre>
  444. %module ffargs
  445. %ffargs(strings_convert="nil",call_direct="t") foo;
  446. %ffargs(strings_convert="nil",release_heap=":never",optimize_for_space="t") bar;
  447. int foo(float f1, float f2);
  448. int foo(float f1, char c2);
  449. void bar(void *lisp_fn);
  450. char *xxx();
  451. </pre>
  452. </div>
  453. <p>Generates:
  454. </p>
  455. <div class="targetlang">ffargs.cl:
  456. <pre>
  457. (swig-in-package ())
  458. (swig-defmethod ("foo" "ACL___foo__SWIG_0" :type :function :arity 2)
  459. ((PARM0_f1 cl:single-float :float )
  460. (PARM1_f2 cl:single-float :float ))
  461. (:returning (:int )
  462. :call-direct t
  463. :strings-convert nil)
  464. (let ((SWIG_arg0 PARM0_f1))
  465. (let ((SWIG_arg1 PARM1_f2))
  466. (swig-ff-call SWIG_arg0 SWIG_arg1))))
  467. (swig-defmethod ("foo" "ACL___foo__SWIG_1" :type :function :arity 2)
  468. ((PARM0_f1 cl:single-float :float )
  469. (PARM1_c2 cl:character :char character))
  470. (:returning (:int )
  471. :call-direct t
  472. :strings-convert nil)
  473. (let ((SWIG_arg0 PARM0_f1))
  474. (let ((SWIG_arg1 PARM1_c2))
  475. (swig-ff-call SWIG_arg0 SWIG_arg1))))
  476. (swig-dispatcher ("foo" :type :function :arities (2)))
  477. (swig-defun ("bar" "ACL___bar__SWIG_0" :type :function)
  478. ((PARM0_lisp_fn (* :void) ))
  479. (:returning (:void )
  480. :release-heap :never
  481. :optimize-for-space t
  482. :strings-convert nil)
  483. (let ((SWIG_arg0 PARM0_lisp_fn))
  484. (swig-ff-call SWIG_arg0)))
  485. (swig-defun ("xxx" "ACL___xxx__SWIG_0" :type :function)
  486. (:void)
  487. (:returning ((* :char) )
  488. :strings-convert t)
  489. (swig-ff-call))
  490. </pre>
  491. </div>
  492. <div class="code">
  493. <pre>%ffargs(strings_convert="t");</pre>
  494. </div>
  495. <p>
  496. Is the only default value specified in <tt>allegrocl.swg</tt> to force
  497. the muffling of warnings about automatic string conversion when defining
  498. ff:def-foreign-call's.
  499. </p>
  500. <H3><a name="Allegrocl_nn10"></a>17.2.4 Non-overloaded Defuns</H3>
  501. <p>
  502. These are simple defuns. There is no typechecking of arguments.
  503. Parameters are bound to local variables for possible
  504. transformation of values, such as pulling values out of instance
  505. slots or allocating temporary stack allocated structures, via the
  506. <tt>lin</tt> typemap. These arguments are then passed to the
  507. foreign-call (where typechecking may occur). The return value from
  508. this function can be manipulated via the <tt>lout</tt> typemap.
  509. </p>
  510. <H3><a name="Allegrocl_nn11"></a>17.2.5 Overloaded Defuns</H3>
  511. <p>
  512. In the case of overloaded functions, mulitple layers are
  513. generated. First, all the overloads for a given name are separated
  514. out into groups based on arity, and are wrapped in
  515. defmethods. Each method calls a distinct wrapper function, but are
  516. themselves distinguished by the types of their arguments
  517. (see <tt>lispclass</tt> typemap). These are further wrapped in a
  518. dispatching function (defun) which will invoke the appropriate
  519. generic-function based on arity. This provides a single functional
  520. interface to all overloads. The return value from this function
  521. can be manipulated via the <tt>lout</tt> typemap.
  522. </p>
  523. <H3><a name="Allegrocl_nn12"></a>17.2.6 What about constant and variable access?</H3>
  524. <p>
  525. Along with the described functional layering, when creating a .cxx wrapper,
  526. this module will generate getter and--if not immutable--setter,
  527. functions for variables and constants. If the -nocwrap option is used,
  528. <tt>defconstant</tt> and <tt>ff:def-foreign-variable</tt> forms will be
  529. generated for accessing constants and global variables. These, along with
  530. the <tt>defuns</tt> listed above are the intended API for calling
  531. into the foreign module.
  532. </p>
  533. <H3><a name="Allegrocl_nn13"></a>17.2.7 Object Wrapping</H3>
  534. <p>
  535. All non-primitive types (Classes, structs, unions, and typedefs
  536. involving same) have a corresponding foreign-type defined on the
  537. lisp side via ff:def-foreign-type.
  538. </p>
  539. <p>
  540. All non-primitive types are further represented by a CLOS class,
  541. created via defclass. An attempt is made to create the same class
  542. hierarchy, with all classes inheriting directly or indirectly from
  543. ff:foreign-pointer. Further, wherever it is apparent, all pointers
  544. returned from foreign code are wrapped in a CLOS instance of the
  545. appropriate class. For ff:def-foreign-calls that have been defined
  546. to expect a :foreign-address type as argument, these CLOS instances
  547. can legally be passed and the pointer to the C++ object
  548. automatically extracted. This is a natural feature of Allegro's
  549. foreign function interface.
  550. </p>
  551. <H2><a name="Allegrocl_nn14"></a>17.3 Wrapping Details</H2>
  552. <p>
  553. In this section is described how particular C/C++ constructs are
  554. translated into lisp.
  555. </p>
  556. <H3><a name="Allegrocl_nn15"></a>17.3.1 Namespaces</H3>
  557. <p>
  558. C++ namespaces are translated into Lisp packages by SWIG. The
  559. Global namespace is mapped to a package named by the <tt>%module</tt>
  560. directive or the <tt>-module</tt> command-line argument. Further
  561. namespaces are generated by the <tt>swig-defpackage</tt> utility
  562. function and given names based on Allegro CLs nested namespace
  563. convention. For example:
  564. </p>
  565. <div class="code">foo.i:
  566. <pre>
  567. %module foo
  568. %{
  569. #include "foo.h"
  570. %}
  571. %include "foo.h"
  572. namespace car {
  573. ...
  574. namespace tires {
  575. int do_something(int n);
  576. }
  577. }
  578. </pre>
  579. </div>
  580. <p>Generates the following code.
  581. </p>
  582. <div class="targetlang">foo.cl
  583. <pre>
  584. (defpackage :foo
  585. (:use :common-lisp :swig :ff :excl))
  586. ...
  587. (swig-defpackage ("car"))
  588. (swig-defpackage ("car" "tires"))
  589. ...
  590. (swig-in-package ("car" "tires"))
  591. (swig-defun ("do_something" "ACL_car_tires__do_something__SWIG_0" :type :function)
  592. ((PARM0_n :int ))
  593. (:returning (:int )
  594. :strings-convert t)
  595. (let ((SWIG_arg0 PARM0_n))
  596. (swig-ff-call SWIG_arg0)))
  597. </pre>
  598. </div>
  599. <p>
  600. The above interface file would cause packages foo, foo.car, and
  601. foo.car.tires to be created. One would find the function wrapper
  602. for do_something defined in the foo.car.tires package(*).
  603. </p>
  604. <p>(<b>*</b>) Except for the package named by the module, all
  605. namespace names are passed to the identifier-converter-function
  606. as strings with a <tt>:type</tt> of <tt>:namespace</tt>. It is the
  607. job of this function to generate the desired symbol, accounting for
  608. case preferences, additional naming cues, etc.
  609. </p>
  610. <p>
  611. Note that packages created by <tt>swig-defpackage</tt> do not
  612. use the COMMON-LISP or EXCL package. This reduces possible
  613. conflicts when defining foreign types via the SWIG interface
  614. in <b>all but the toplevel modules package</b>. This may
  615. lead to confusion if, for example, the current package is
  616. <tt>foo.car.tires</tt> and you attempt to use a common-lisp
  617. function such as <tt>(car '(1 2 3)</tt>.
  618. </p>
  619. <H3><a name="Allegrocl_nn16"></a>17.3.2 Constants</H3>
  620. <p>
  621. Constants, as declared by the preprocessor #define macro or SWIG
  622. <tt>%constant</tt> directive, are included in SWIGs parse tree
  623. when it can be determined that they are, or could be reduced to,
  624. a literal value. Such values are translated into defconstant
  625. forms in the generated lisp wrapper when the -nocwrap command-line
  626. options is used. Else, wrapper functions are generated as in the
  627. case of variable access (see section below).
  628. </p>
  629. <p>
  630. Here are examples of simple preprocessor constants when using -nocwrap.
  631. </p>
  632. <div class="code">
  633. <pre>
  634. #define A 1 =&gt; (swig-defconstant "A" 1)
  635. #define B 'c' =&gt; (swig-defconstant "B" #\c)
  636. #define C B =&gt; (swig-defconstant "C" #\c)
  637. #define D 1.0e2 =&gt; (swig-defconstant "D" 1.0d2)
  638. #define E 2222 =&gt; (swig-defconstant "E" 2222)
  639. #define F (unsigned int)2222 =&gt; no code generated
  640. #define G 1.02e2f =&gt; (swig-defconstant "G" 1.02f2)
  641. #define H foo =&gt; no code generated
  642. </pre>
  643. </div>
  644. <p>
  645. Note that where SWIG is unable to determine if a constant is
  646. a literal, no node is added to the SWIG parse tree, and so
  647. no values can be generated.
  648. </p>
  649. <p>
  650. For preprocessor constants containing expressions which can be
  651. reduced to literal values, nodes are created, but with no simplification
  652. of the constant value. A very very simple infix to prefix converter
  653. has been implemented that tries to do the right thing for simple cases, but
  654. does not for more complex expressions. If the literal parser determines
  655. that something is wrong, a warning will be generated and the literal
  656. expression will be included in the generated code, but commented out.
  657. </p>
  658. <div class="code">
  659. <pre>
  660. #define I A + E =&gt; (swig-defconstant "I" (+ 1 2222))
  661. #define J 1|2 =&gt; (swig-defconstant "J" (logior 1 2))
  662. #define Y 1 + 2 * 3 + 4 =&gt; (swig-defconstant "Y" (* (+ 1 2) (+ 3 4)))
  663. #define Y1 (1 + 2) * (3 + 4) =&gt; (swig-defconstant "Y1" (* (+ 1 2) (+ 3 4)))
  664. #define Y2 1 * 2 + 3 * 4 =&gt; (swig-defconstant "Y2" (* 1 (+ 2 3) 4)) ;; WRONG
  665. #define Y3 (1 * 2) + (3 * 4) =&gt; (swig-defconstant "Y3" (* 1 (+ 2 3) 4)) ;; WRONG
  666. #define Z 1 + 2 - 3 + 4 * 5 =&gt; (swig-defconstant "Z" (* (+ 1 (- 2 3) 4) 5)) ;; WRONG
  667. </pre>
  668. </div>
  669. <p>
  670. Users are cautioned to get to know their constants before use, or
  671. not use the <tt>-nocwrap</tt> command-line option.
  672. </p>
  673. <H3><a name="Allegrocl_nn17"></a>17.3.3 Variables</H3>
  674. <p>
  675. For C wrapping, a def-foreign-variable call is generated for access
  676. to global variables.
  677. </p>
  678. <p>
  679. When wrapping C++ code, both global and member variables, getter
  680. wrappers are generated for accessing their value, and if not immutable,
  681. setter wrappers as well. In the example below, note the lack of a
  682. setter wrapper for global_var, defined as const.
  683. </p>
  684. <div class="code">vars.h
  685. <pre>
  686. namespace nnn {
  687. int const global_var = 2;
  688. float glob_float = 2.0;
  689. }
  690. </pre>
  691. </div>
  692. <p>
  693. Generated code:
  694. </p>
  695. <div class="targetlang">vars.cl
  696. <pre>
  697. (swig-in-package ("nnn"))
  698. (swig-defun ("global_var" "ACL_nnn__global_var_get__SWIG_0" :type :getter)
  699. (:void)
  700. (:returning (:int )
  701. :strings-convert t)
  702. (swig-ff-call))
  703. (swig-defun ("glob_float" "ACL_nnn__glob_float_set__SWIG_0" :type :setter)
  704. ((PARM0_glob_float :float ))
  705. (:returning (:void )
  706. :strings-convert t)
  707. (let ((SWIG_arg0 PARM0_glob_float))
  708. (swig-ff-call SWIG_arg0)))
  709. (swig-defun ("glob_float" "ACL_nnn__glob_float_get__SWIG_0" :type :getter)
  710. (:void)
  711. (:returning (:float )
  712. :strings-convert t)
  713. (swig-ff-call))
  714. </pre>
  715. </div>
  716. <p>
  717. Note also, that where applicable, setter wrappers are implemented
  718. as setf methods on the getter function, providing a lispy interface
  719. to the foreign code.
  720. </p>
  721. <div class="targetlang">
  722. <pre>
  723. user&gt; (load "globalvar.dll")
  724. ; Foreign loading globalvar.dll.
  725. t
  726. user&gt; (load "globalvar.cl")
  727. ; Loading c:\mikel\src\swig\test\globalvar.cl
  728. t
  729. user&gt;
  730. globalvar&gt; (globalvar.nnn::global_var)
  731. 2
  732. globalvar&gt; (globalvar.nnn::glob_float)
  733. 2.0
  734. globalvar&gt; (setf (globalvar.nnn::glob_float) 3.0)
  735. 3.0
  736. globalvar&gt; (globalvar.nnn::glob_float)
  737. 3.0
  738. </pre>
  739. </div>
  740. <H3><a name="Allegrocl_nn18"></a>17.3.4 Enumerations</H3>
  741. <p>
  742. In C, an enumeration value is an integer value, while in C++ an
  743. enumeration value is implicitly convertible to an integer value,
  744. but can also be distinguished by it's enum type. For each enum
  745. declaration a def-foreign-type is generated, assigning the enum
  746. a default type of :int. Users may adjust the foreign type of
  747. enums via SWIG <tt>typemaps</tt>.
  748. </p>
  749. <p>
  750. Enum values are a bit trickier as they can be initialized using
  751. any valid C/C++ expression. In C with the -nocwrap command-line option,
  752. we handle the typical cases (simple integer initialization) and
  753. generate a defconstant form for each enum value. This has the advantage
  754. of it not being necessary to probe into foreign space to retrieve enum
  755. values. When generating a .cxx wrapper file, a more general solution is
  756. employed. A wrapper variable is created in the module_wrap.cxx file, and
  757. a ff:def-foreign-variable call is generated to retrieve it's value into lisp.
  758. </p>
  759. <p>For example, the following header file
  760. <div class="code">enum.h:
  761. <pre>
  762. enum COL { RED, GREEN, BLUE };
  763. enum FOO { FOO1 = 10, FOO2, FOO3 };
  764. </pre>
  765. </div>
  766. <p>
  767. In -nocwrap mode, generates
  768. </p>
  769. <div class="targetlang">enum.cl:
  770. <pre>
  771. (swig-def-foreign-type "COL" :int)
  772. (swig-defconstant "RED" 0)
  773. (swig-defconstant "GREEN" (+ #.(swig-insert-id "RED" () :type :constant) 1))
  774. (swig-defconstant "BLUE" (+ #.(swig-insert-id "GREEN" () :type :constant) 1))
  775. (swig-def-foreign-type "FOO" :int)
  776. (swig-defconstant "FOO1" 10)
  777. (swig-defconstant "FOO2" (+ #.(swig-insert-id "FOO1" () :type :constant) 1))
  778. (swig-defconstant "FOO3" (+ #.(swig-insert-id "FOO2" () :type :constant) 1))
  779. </pre>
  780. </div>
  781. <p>And when generating a .cxx wrapper
  782. <div class="code">enum_wrap.cxx:
  783. <pre>
  784. EXPORT const int ACL_ENUM___RED__SWIG_0 = RED;
  785. EXPORT const int ACL_ENUM___GREEN__SWIG_0 = GREEN;
  786. EXPORT const int ACL_ENUM___BLUE__SWIG_0 = BLUE;
  787. EXPORT const int ACL_ENUM___FOO1__SWIG_0 = FOO1;
  788. EXPORT const int ACL_ENUM___FOO2__SWIG_0 = FOO2;
  789. EXPORT const int ACL_ENUM___FOO3__SWIG_0 = FOO3;
  790. </pre>
  791. </div>
  792. <p>
  793. and
  794. </p>
  795. <div class="targetlang">enum.cl:
  796. <pre>
  797. (swig-def-foreign-type "COL" :int)
  798. (swig-defvar "RED" "ACL_ENUM___RED__SWIG_0" :type :constant)
  799. (swig-defvar "GREEN" "ACL_ENUM___GREEN__SWIG_0" :type :constant)
  800. (swig-defvar "BLUE" "ACL_ENUM___BLUE__SWIG_0" :type :constant)
  801. (swig-def-foreign-type "FOO" :int)
  802. (swig-defvar "FOO1" "ACL_ENUM___FOO1__SWIG_0" :type :constant)
  803. (swig-defvar "FOO2" "ACL_ENUM___FOO2__SWIG_0" :type :constant)
  804. (swig-defvar "FOO3" "ACL_ENUM___FOO3__SWIG_0" :type :constant)
  805. </pre>
  806. </div>
  807. <H3><a name="Allegrocl_nn19"></a>17.3.5 Arrays</H3>
  808. <p>
  809. One limitation in the Allegro CL foreign-types module, is that,
  810. without macrology, expressions may not be used to specify the
  811. dimensions of an array declaration. This is not a horrible
  812. drawback unless it is necessary to allocate foreign structures
  813. based on the array declaration using ff:allocate-fobject. When it
  814. can be determined that an array bound is a valid numeric value,
  815. SWIG will include this in the generated array declaration on the
  816. lisp side, otherwise the value will be included, but commented out.
  817. </p>
  818. <p>
  819. Below is a comprehensive example, showing a number of legal
  820. C/C++ array declarations and how they are translated
  821. into foreign-type specifications in the generated lisp code.
  822. </p>
  823. <div class="code">array.h
  824. <pre>
  825. #define MAX_BUF_SIZE 1024
  826. namespace FOO {
  827. int global_var1[13];
  828. float global_var2[MAX_BUF_SIZE];
  829. }
  830. enum COLOR { RED = 10, GREEN = 20, BLUE, PURPLE = 50, CYAN };
  831. namespace BAR {
  832. char global_var3[MAX_BUF_SIZE + 1];
  833. float global_var4[MAX_BUF_SIZE][13];
  834. signed short global_var5[MAX_BUF_SIZE + MAX_BUF_SIZE];
  835. int enum_var5[GREEN];
  836. int enum_var6[CYAN];
  837. COLOR enum_var7[CYAN][MAX_BUF_SIZE];
  838. }
  839. </pre>
  840. </div>
  841. <p>
  842. Generates:
  843. </p>
  844. <div class="targetlang">array.cl
  845. <pre>
  846. (in-package #.*swig-module-name*)
  847. (swig-defpackage ("FOO"))
  848. (swig-defpackage ("BAR"))
  849. (swig-in-package ())
  850. (swig-def-foreign-type "COLOR" :int)
  851. (swig-defvar "RED" "ACL_ENUM___RED__SWIG_0" :type :constant)
  852. (swig-defvar "GREEN" "ACL_ENUM___GREEN__SWIG_0" :type :constant)
  853. (swig-defvar "BLUE" "ACL_ENUM___BLUE__SWIG_0" :type :constant)
  854. (swig-defvar "PURPLE" "ACL_ENUM___PURPLE__SWIG_0" :type :constant)
  855. (swig-defvar "CYAN" "ACL_ENUM___CYAN__SWIG_0" :type :constant)
  856. (swig-in-package ())
  857. (swig-defconstant "MAX_BUF_SIZE" 1024)
  858. (swig-in-package ("FOO"))
  859. (swig-defun ("global_var1" "ACL_FOO__global_var1_get__SWIG_0" :type :getter)
  860. (:void)
  861. (:returning ((* :int) )
  862. :strings-convert t)
  863. (make-instance 'ff:foreign-pointer :foreign-address (swig-ff-call)))
  864. (swig-defun ("global_var2" "ACL_FOO__global_var2_set__SWIG_0" :type :setter)
  865. ((global_var2 (:array :float 1024) ))
  866. (:returning (:void )
  867. :strings-convert t)
  868. (let ((SWIG_arg0 global_var2))
  869. (swig-ff-call SWIG_arg0)))
  870. (swig-in-package ())
  871. (swig-in-package ("BAR"))
  872. (swig-defun ("global_var3" "ACL_BAR__global_var3_set__SWIG_0" :type :setter)
  873. ((global_var3 (:array :char #|1024+1|#) ))
  874. (:returning (:void )
  875. :strings-convert t)
  876. (let ((SWIG_arg0 global_var3))
  877. (swig-ff-call SWIG_arg0)))
  878. (swig-defun ("global_var4" "ACL_BAR__global_var4_set__SWIG_0" :type :setter)
  879. ((global_var4 (:array (:array :float 13) 1024) ))
  880. (:returning (:void )
  881. :strings-convert t)
  882. (let ((SWIG_arg0 global_var4))
  883. (swig-ff-call SWIG_arg0)))
  884. (swig-defun ("global_var4" "ACL_BAR__global_var4_get__SWIG_0" :type :getter)
  885. (:void)
  886. (:returning ((* (:array :float 13)) )
  887. :strings-convert t)
  888. (make-instance 'ff:foreign-pointer :foreign-address (swig-ff-call)))
  889. (swig-defun ("global_var5" "ACL_BAR__global_var5_set__SWIG_0" :type :setter)
  890. ((global_var5 (:array :short #|1024+1024|#) ))
  891. (:returning (:void )
  892. :strings-convert t)
  893. (let ((SWIG_arg0 global_var5))
  894. (swig-ff-call SWIG_arg0)))
  895. (swig-defun ("enum_var5" "ACL_BAR__enum_var5_set__SWIG_0" :type :setter)
  896. ((enum_var5 (:array :int #|GREEN|#) ))
  897. (:returning (:void )
  898. :strings-convert t)
  899. (let ((SWIG_arg0 enum_var5))
  900. (swig-ff-call SWIG_arg0)))
  901. (swig-defun ("enum_var6" "ACL_BAR__enum_var6_set__SWIG_0" :type :setter)
  902. ((enum_var6 (:array :int #|CYAN|#) ))
  903. (:returning (:void )
  904. :strings-convert t)
  905. (let ((SWIG_arg0 enum_var6))
  906. (swig-ff-call SWIG_arg0)))
  907. (swig-defun ("enum_var7" "ACL_BAR__enum_var7_set__SWIG_0" :type :setter)
  908. ((enum_var7 (:array (:array #.(swig-insert-id "COLOR" ()) 1024) #|CYAN|#) ))
  909. (:returning (:void )
  910. :strings-convert t)
  911. (let ((SWIG_arg0 enum_var7))
  912. (swig-ff-call SWIG_arg0)))
  913. (swig-defun ("enum_var7" "ACL_BAR__enum_var7_get__SWIG_0" :type :getter)
  914. (:void)
  915. (:returning ((* (:array #.(swig-insert-id "COLOR" ()) 1024)) )
  916. :strings-convert t)
  917. (make-instance 'ff:foreign-pointer :foreign-address (swig-ff-call)))
  918. </pre>
  919. </div>
  920. <H3><a name="Allegrocl_nn20"></a>17.3.6 Classes and Structs and Unions (oh my!)</H3>
  921. <H4><a name="Allegrocl_nn21"></a>17.3.6.1 CLOS wrapping of</H4>
  922. <p>
  923. Classes, unions, and structs are all treated the same way by the
  924. interface generator. For any of these objects, a
  925. def-foreign-type and a defclass form are generated. For every
  926. function that returns an object (or pointer/reference) of C/C++
  927. type <tt>X</tt>, the wrapping defun (or defmethod) on the Lisp
  928. side will automatically wrap the pointer returned in an instance
  929. of the apropriate class. This makes it much easier to write and
  930. debug code than if pointers were passed around as a jumble of
  931. integer values.
  932. </p>
  933. <H4><a name="Allegrocl_nn22"></a>17.3.6.2 CLOS Inheritance</H4>
  934. <p>
  935. The CLOS class schema generated by the interface mirrors the
  936. inheritance of the classes in foreign code, with the
  937. ff:foreign-pointer class at its root. ff:foreign-pointer is a thin
  938. wrapper for pointers that is made available by the foreign function
  939. interface. It's key benefit is that it may be passed as an argument
  940. to any ff:def-foreign-call that is expecting a pointer as the
  941. parameter.
  942. </p>
  943. <H4><a name="Allegrocl_nn23"></a>17.3.6.3 Member fields and functions</H4>
  944. <p>
  945. All public fields will have accessor getter/setter functions
  946. generated for them, as appropriate. All public member functions
  947. will have wrapper functions generated.
  948. </p>
  949. <p>
  950. We currently ignore anything that isn't <tt>public</tt> (i.e.
  951. <tt>private</tt> or <tt>protected</tt>), because the C++ compiler
  952. won't allow the wrapper functions to access such fields. Likewise,
  953. the interface does nothing for <tt>friend</tt> directives,
  954. </p>
  955. <H4><a name="Allegrocl_nn24"></a>17.3.6.4 Why not directly access C++ classes using foreign types?</H4>
  956. <p>
  957. The def-foreign-type generated by the SWIG interface is
  958. currently incomplete. We can reliably generate the object layout
  959. of simple structs and unions; they can be allocated via
  960. ff:allocate-fobject, and their member variables accessed
  961. directly using the various ff:fslot-value-* functions. However,
  962. the layout of C++ classes is more complicated. Different
  963. compilers adjust class layout based on inheritance patterns, and
  964. the presence of virtual member functions. The size of member
  965. function pointers vary across compilers as well. As a result, it
  966. is recommended that users of any generated interface not attempt
  967. to access C++ instances via the foreign type system, but instead
  968. use the more robust wrapper functions.
  969. </p>
  970. <H3><a name="Allegrocl_nn25"></a>17.3.7 Templates</H3>
  971. <H4><a name="Allegrocl_nn26"></a>17.3.7.1 Generating wrapper code for templates</H4>
  972. <p>
  973. SWIG provides support for dealing with templates, but by
  974. default, it will not generate any member variable or function
  975. wrappers for templated classes. In order to create these
  976. wrappers, you need to explicitly tell SWIG to instantiate
  977. them. This is done via the
  978. <a href="SWIGPlus.html#SWIGPlus_nn30"><tt>%template</tt></a>
  979. directive.
  980. </p>
  981. <H4><a name="Allegrocl_nn27"></a>17.3.7.2 Implicit Template instantiation</H4>
  982. <p>
  983. While no wrapper code is generated for accessing member
  984. variables, or calling member functions, type code is generated
  985. to include these templated classes in the foreign-type and CLOS
  986. class schema.
  987. </p>
  988. <H3><a name="Allegrocl_nn28"></a>17.3.8 Typedef, Templates, and Synonym Types</H3>
  989. <p>
  990. In C/C++ it is possible, via typedef, to have many names refer to
  991. the same <tt>type</tt>. In general, this is not a problem, though
  992. it can lead to confusion. Assume the below C++ header file:
  993. </p>
  994. <div class="code">synonyms.h
  995. <pre>
  996. class A {
  997. int x;
  998. int y;
  999. };
  1000. typedef A Foo;
  1001. A *xxx(int i); /* sets A-&gt;x = A-&gt;y = i */
  1002. Foo *yyy(int i); /* sets Foo-&gt;x = Foo-&gt;y = i */
  1003. int zzz(A *inst = 0); /* return inst-&gt;x + inst-&gt;y */
  1004. </pre>
  1005. </div>
  1006. <p>
  1007. The function <tt>zzz</tt> is an overloaded functions; the
  1008. foreign function call to it will be wrapped in a
  1009. generic-function whose argument will be checked against a type
  1010. of <tt>A</tt>. Assuming a simple implementation, a call
  1011. to <tt>xxx(1)</tt> will return a pointer to an A object, which
  1012. will be wrapped in a CLOS instance of class <tt>A</tt>, and a
  1013. call to <tt>yyy(1)</tt> will result in a CLOS instance of
  1014. type <tt>Foo</tt> being returned. Without establishing a clear
  1015. type relationship between <tt>Foo</tt> and <tt>A</tt>, an
  1016. attempt to call <tt>zzz(yyy(1))</tt> will result in an error.
  1017. </p>
  1018. <p>
  1019. We resolve this issue, by noting synonym relationships between
  1020. types while generating the interface. A Primary type is selected
  1021. (more on this below) from the candidate list of synonyms. For
  1022. all other synonyms, intead of generating a distinct CLOS class
  1023. definition, we generate a form that expands to:
  1024. </p>
  1025. <div class="targetlang">
  1026. <tt>(setf (find-class &lt;synonym&gt;) &lt;primary&gt;)</tt>
  1027. </div>
  1028. <p>
  1029. The result is that all references to synonym types in foreign
  1030. code, are wrapped in the same CLOS wrapper, and, in particular,
  1031. method specialization in wrapping generic functions works as
  1032. expected.
  1033. </p>
  1034. <p>
  1035. Given the above header file, synonym.h, a Lisp session would
  1036. appear as follows:
  1037. </p>
  1038. <div class="targetlang">
  1039. <pre>
  1040. CL-USER&gt; (load "synonym.dll")
  1041. ; Foreign loading synonym.dll.
  1042. t
  1043. CL-USER&gt; (load "synonym.cl")
  1044. ; Loading c:\mikel\src\swig\test\synonym.cl
  1045. t
  1046. CL-USER&gt;
  1047. synonym&gt; (setf a (xxx 3))
  1048. #&lt;A nil #x3261a0 @ #x207299da&gt;
  1049. synonym&gt; (setf foo (yyy 10))
  1050. #&lt;A nil #x3291d0 @ #x2072e982&gt;
  1051. synonym&gt; (zzz a)
  1052. 6
  1053. synonym&gt; (zzz foo)
  1054. 20
  1055. synonym&gt;
  1056. </pre>
  1057. </div>
  1058. <H4><a name="Allegrocl_nn29"></a>17.3.8.1 Choosing a primary type</H4>
  1059. <p>
  1060. The choice of a primary type is selected by the following
  1061. criteria from a set of synonym types.
  1062. </p>
  1063. <ul>
  1064. <li>
  1065. If a synonym type has a class definition, it is the primary type.
  1066. </li>
  1067. <li>
  1068. If a synonym type is a class template and has been explicitly
  1069. instantiated via <tt>%template</tt>, it is the primary type.
  1070. </li>
  1071. <li>
  1072. For all other sets of synonymous types, the synonym which is
  1073. parsed first becomes the primary type.
  1074. </li>
  1075. </ul>
  1076. <H3><a name="Allegrocl_nn30"></a>17.3.9 Function overloading/Parameter defaulting</H3>
  1077. <p>
  1078. For each possible argument combination, a distinct wrapper
  1079. function is created in the .cxx file. On the Lisp side, a
  1080. generic functions is defined for each possible arity the
  1081. overloaded/defaulted call may have. Each distinct wrapper is
  1082. then called from within a defmethod on the appropriate generic
  1083. function. These are further wrapped inside a dispatch function
  1084. that checks the number of arguments it is called with and passes
  1085. them via apply to the appropriate generic-function. This allows
  1086. for a single entry point to overloaded functions on the lisp
  1087. side.
  1088. </p>
  1089. <p>Example:
  1090. </p>
  1091. <div class="code">overload.h:
  1092. <pre>
  1093. class A {
  1094. public:
  1095. int x;
  1096. int y;
  1097. };
  1098. float xxx(int i, int x = 0); /* return i * x */
  1099. float xxx(A *inst, int x); /* return x + A-&gt;x + A-&gt;y */
  1100. </pre>
  1101. </div>
  1102. <p>Creates the following three wrappers, for each of the possible argument
  1103. combinations
  1104. </p>
  1105. <div class="code">overload_wrap.cxx
  1106. <pre>
  1107. EXPORT void ACL___delete_A__SWIG_0 (A *larg1) {
  1108. A *arg1 = (A *) 0 ;
  1109. arg1 = larg1;
  1110. try {
  1111. delete arg1;
  1112. } catch (...) {
  1113. }
  1114. }
  1115. EXPORT float ACL___xxx__SWIG_0 (int larg1, int larg2) {
  1116. float lresult = (float)0 ;
  1117. int arg1 ;
  1118. int arg2 ;
  1119. float result;
  1120. arg1 = larg1;
  1121. arg2 = larg2;
  1122. try {
  1123. result = (float)xxx(arg1,arg2);
  1124. lresult = result;
  1125. return lresult;
  1126. } catch (...) {
  1127. return (float)0;
  1128. }
  1129. }
  1130. EXPORT float ACL___xxx__SWIG_1 (int larg1) {
  1131. float lresult = (float)0 ;
  1132. int arg1 ;
  1133. float result;
  1134. arg1 = larg1;
  1135. try {
  1136. result = (float)xxx(arg1);
  1137. lresult = result;
  1138. return lresult;
  1139. } catch (...) {
  1140. return (float)0;
  1141. }
  1142. }
  1143. EXPORT float ACL___xxx__SWIG_2 (A *larg1, int larg2) {
  1144. float lresult = (float)0 ;
  1145. A *arg1 = (A *) 0 ;
  1146. int arg2 ;
  1147. float result;
  1148. arg1 = larg1;
  1149. arg2 = larg2;
  1150. try {
  1151. result = (float)xxx(arg1,arg2);
  1152. lresult = result;
  1153. return lresult;
  1154. } catch (...) {
  1155. return (float)0;
  1156. }
  1157. }
  1158. </pre>
  1159. </div>
  1160. <p>
  1161. And the following foreign-function-call and method definitions on the
  1162. lisp side:
  1163. </p>
  1164. <div class="targetlang">overload.cl
  1165. <pre>
  1166. (swig-defmethod ("xxx" "ACL___xxx__SWIG_0" :type :function :arity 2)
  1167. ((PARM0_i cl:integer :int )
  1168. (PARM1_x cl:integer :int ))
  1169. (:returning (:float )
  1170. :strings-convert t)
  1171. (let ((SWIG_arg0 PARM0_i))
  1172. (let ((SWIG_arg1 PARM1_x))
  1173. (swig-ff-call SWIG_arg0 SWIG_arg1))))
  1174. (swig-defmethod ("xxx" "ACL___xxx__SWIG_1" :type :function :arity 1)
  1175. ((PARM0_i cl:integer :int ))
  1176. (:returning (:float )
  1177. :strings-convert t)
  1178. (let ((SWIG_arg0 PARM0_i))
  1179. (swig-ff-call SWIG_arg0)))
  1180. (swig-defmethod ("xxx" "ACL___xxx__SWIG_2" :type :function :arity 2)
  1181. ((PARM0_inst #.(swig-insert-id "A" () :type :class) (* #.(swig-insert-id "A" ())) )
  1182. (PARM1_x cl:integer :int ))
  1183. (:returning (:float )
  1184. :strings-convert t)
  1185. (let ((SWIG_arg0 PARM0_inst))
  1186. (let ((SWIG_arg1 PARM1_x))
  1187. (swig-ff-call SWIG_arg0 SWIG_arg1))))
  1188. (swig-dispatcher ("xxx" :type :function :arities (1 2)))
  1189. </pre>
  1190. </div>
  1191. <p>And their usage in a sample lisp session:
  1192. </p>
  1193. <div class="targetlang">
  1194. <pre>
  1195. overload&gt; (setf a (new_A))
  1196. #&lt;A nil #x329268 @ #x206cf612&gt;
  1197. overload&gt; (setf (A_x a) 10)
  1198. 10
  1199. overload&gt; (setf (A_y a) 20)
  1200. 20
  1201. overload&gt; (xxx 1)
  1202. 0.0
  1203. overload&gt; (xxx 3 10)
  1204. 30.0
  1205. overload&gt; (xxx a 1)
  1206. 31.0
  1207. overload&gt; (xxx a 2)
  1208. 32.0
  1209. overload&gt;
  1210. </pre>
  1211. </div>
  1212. <H3><a name="Allegrocl_nn31"></a>17.3.10 Operator wrapping and Operator overloading</H3>
  1213. <p>
  1214. Wrappers to defined C++ Operators are automatically renamed, using
  1215. <tt>%rename</tt>, to the following defaults:
  1216. </p>
  1217. <div class="code">
  1218. <pre>
  1219. /* name conversion for overloaded operators. */
  1220. #ifdef __cplusplus
  1221. %rename(__add__) *::operator+;
  1222. %rename(__pos__) *::operator+();
  1223. %rename(__pos__) *::operator+() const;
  1224. %rename(__sub__) *::operator-;
  1225. %rename(__neg__) *::operator-() const;
  1226. %rename(__neg__) *::operator-();
  1227. %rename(__mul__) *::operator*;
  1228. %rename(__deref__) *::operator*();
  1229. %rename(__deref__) *::operator*() const;
  1230. %rename(__div__) *::operator/;
  1231. %rename(__mod__) *::operator%;
  1232. %rename(__logxor__) *::operator^;
  1233. %rename(__logand__) *::operator&amp;;
  1234. %rename(__logior__) *::operator|;
  1235. %rename(__lognot__) *::operator~();
  1236. %rename(__lognot__) *::operator~() const;
  1237. %rename(__not__) *::operator!();
  1238. %rename(__not__) *::operator!() const;
  1239. %rename(__assign__) *::operator=;
  1240. %rename(__add_assign__) *::operator+=;
  1241. %rename(__sub_assign__) *::operator-=;
  1242. %rename(__mul_assign__) *::operator*=;
  1243. %rename(__div_assign__) *::operator/=;
  1244. %rename(__mod_assign__) *::operator%=;
  1245. %rename(__logxor_assign__) *::operator^=;
  1246. %rename(__logand_assign__) *::operator&amp;=;
  1247. %rename(__logior_assign__) *::operator|=;
  1248. %rename(__lshift__) *::operator&lt;&lt;;
  1249. %rename(__lshift_assign__) *::operator&lt;&lt;=;
  1250. %rename(__rshift__) *::operator&gt;&gt;;
  1251. %rename(__rshift_assign__) *::operator&gt;&gt;=;
  1252. %rename(__eq__) *::operator==;
  1253. %rename(__ne__) *::operator!=;
  1254. %rename(__lt__) *::operator&lt;;
  1255. %rename(__gt__) *::operator&gt;;
  1256. %rename(__lte__) *::operator&lt;=;
  1257. %rename(__gte__) *::operator&gt;=;
  1258. %rename(__and__) *::operator&amp;&amp;;
  1259. %rename(__or__) *::operator||;
  1260. %rename(__preincr__) *::operator++();
  1261. %rename(__postincr__) *::operator++(int);
  1262. %rename(__predecr__) *::operator--();
  1263. %rename(__postdecr__) *::operator--(int);
  1264. %rename(__comma__) *::operator,();
  1265. %rename(__comma__) *::operator,() const;
  1266. %rename(__member_ref__) *::operator-&gt;;
  1267. %rename(__member_func_ref__) *::operator-&gt;*;
  1268. %rename(__funcall__) *::operator();
  1269. %rename(__aref__) *::operator[];
  1270. </pre>
  1271. </div>
  1272. <p>
  1273. Name mangling occurs on all such renamed identifiers, so that wrapper name
  1274. generated by <tt>B::operator=</tt> will be <tt>B___eq__</tt>, i.e.
  1275. <tt>&lt;class-or-namespace&gt;_</tt> has been added. Users may modify
  1276. these default names by adding <tt>%rename</tt> directives in their own .i files.
  1277. </p>
  1278. <p>
  1279. Operator overloading can be achieved by adding functions based
  1280. on the mangled names of the function. In the following example,
  1281. a class B is defined with a Operator== method defined. The
  1282. swig <tt>%extend</tt> directive is used to add an overload method
  1283. on Operator==.
  1284. </p>
  1285. <div class="code">opoverload.h
  1286. <pre>
  1287. class B {
  1288. public:
  1289. int x;
  1290. int y;
  1291. bool operator==(B const&amp; other) const;
  1292. };
  1293. </pre>
  1294. </div>
  1295. <p>
  1296. and
  1297. </p>
  1298. <div class="code">opoverload.i
  1299. <pre>
  1300. %module opoverload
  1301. %{
  1302. #include &lt;fstream&gt;
  1303. #include "opoverload.h"
  1304. %}
  1305. %{
  1306. bool B___eq__(B const *inst, int const x)
  1307. {
  1308. // insert the function definition into the wrapper code before
  1309. // the wrapper for it.
  1310. // ... do stuff ...
  1311. }
  1312. %}
  1313. %include "opoverload.h"
  1314. %extend B {
  1315. public:
  1316. bool __eq__(int const x) const;
  1317. };
  1318. </pre>
  1319. </div>
  1320. <p>
  1321. Either operator can be called via a single call
  1322. to t

Large files files are truncated, but you can click here to view the full file