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
|
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>Foreign Data Structure Examples</title><meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /><link rel="home" href="index.html" title="SBCL User Manual" /><link rel="up" href="ffi.html" title="Chapter5.The Foreign Function Interface" /><link rel="previous" href="foreign-variables.html" title="Foreign Variables" /><link rel="next" href="load-object.html" title="Loading Unix Object Files" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Foreign Data Structure Examples</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="foreign-variables.html">Prev</a></td><th width="60%" align="center">Chapter5.The Foreign Function Interface</th><td width="20%" align="right"><a accesskey="n" href="load-object.html">Next</a></td></tr></table><hr /></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="foreign-data-structure"></a>Foreign Data Structure Examples</h2></div></div><div></div></div><p>
Now that we have alien types, operations and variables, we can manipulate
foreign data structures. This C declaration
</p><pre class="programlisting">
struct foo {
int a;
struct foo *b[100];
};</pre><p>
can be translated into the following alien type:
</p><pre class="programlisting">(define-alien-type nil
(struct foo
(a int)
(b (array (* (struct foo)) 100))))</pre><p>
</p><p>
Once the <tt class="type">foo</tt> alien type has been defined as above,
the C expression
</p><pre class="programlisting">
struct foo f;
f.b[7].a</pre><p>
can be translated in this way:
</p><pre class="programlisting">
(with-alien ((f (struct foo)))
(slot (deref (slot f 'b) 7) 'a)
;;
;; Do something with f...
)</pre><p>
</p><p>
Or consider this example of an external C variable and some accesses:
</p><pre class="programlisting">
struct c_struct {
short x, y;
char a, b;
int z;
c_struct *n;
};
extern struct c_struct *my_struct;
my_struct->x++;
my_struct->a = 5;
my_struct = my_struct->n;</pre><p>
which can be manipulated in Lisp like this:
</p><pre class="programlisting">
(define-alien-type nil
(struct c-struct
(x short)
(y short)
(a char)
(b char)
(z int)
(n (* c-struct))))
(define-alien-variable "my_struct" (* c-struct))
(incf (slot my-struct 'x))
(setf (slot my-struct 'a) 5)
(setq my-struct (slot my-struct 'n))</pre><p>
</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="foreign-variables.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="ffi.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="load-object.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Foreign Variables</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">Loading Unix Object Files</td></tr></table></div></body></html>
|