r1742: *** empty log message ***
[uffi.git] / doc / ref.sgml
index 46f91a5f40fbc037a1ef6b5f35c6df46dabd3bbd..607edfced873810076050e650872e0270ec179d5 100644 (file)
        </para>
        <itemizedlist>
          <listitem>
-           <para><constant>:char</constant> - Signed 8-bits</para>
+           <para><constant>:char</constant> - Signed 8-bits. A
+dereferenced :char pointer returns an character.</para>
          </listitem>
          <listitem>
-           <para><constant>:unsigned-char</constant> - Unsigned 8-bits</para>
+           <para><constant>:unsigned-char</constant> - Unsigned 8-bits. A dereferenced :unsigned-char
+pointer returns an character.</para>
          </listitem>
          <listitem>
-           <para><constant>:short</constant> - Signed 16-bits</para>
+           <para><constant>:byte</constant> - Unsigned 8-bits. A
+dereferenced :byte pointer returns an integer.</para>
          </listitem>
          <listitem>
-           <para><constant>:unsigned-short</constant> - Unsigned 16-bits</para>
+           <para><constant>:short</constant> - Signed 16-bits.</para>
          </listitem>
          <listitem>
-           <para><constant>:int</constant> - Signed 32-bits</para>
+           <para><constant>:unsigned-short</constant> - Unsigned 16-bits.</para>
          </listitem>
          <listitem>
-           <para><constant>:unsigned-int</constant> - Unsigned 32-bits</para>
+           <para><constant>:int</constant> - Signed 32-bits.</para>
          </listitem>
          <listitem>
-           <para><constant>:long</constant> - Signed 32-bits</para>
+           <para><constant>:unsigned-int</constant> - Unsigned 32-bits.</para>
          </listitem>
          <listitem>
-           <para><constant>:unsigned-long</constant> - Unsigned 32-bits</para>
+           <para><constant>:long</constant> - Signed 32-bits.</para>
          </listitem>
          <listitem>
-           <para><constant>:float</constant> - 32-bit floating point</para>
+           <para><constant>:unsigned-long</constant> - Unsigned 32-bits.</para>
          </listitem>
          <listitem>
-           <para><constant>:double</constant> - 64-bit floating point</para>
+           <para><constant>:float</constant> - 32-bit floating point.</para>
+         </listitem>
+         <listitem>
+           <para><constant>:double</constant> - 64-bit floating point.</para>
          </listitem>
          <listitem>
            <para><constant>:cstring</constant> - 
-A null-terminated string used for passing and returning with a function.
+A &null; terminated string used for passing and returning characters strings with a &c; function.
            </para>
          </listitem>
          <listitem>
            <para><constant>:void</constant> - 
-The absence of a value. Used in generic pointers and in return types from functions.</para>
+The absence of a value. Used to indicate that a function does not return a value.</para>
+         </listitem>
+         <listitem>
+           <para><constant>:pointer-void</constant> - 
+Points to a generic object.</para>
          </listitem>
          <listitem>
            <para><constant>*</constant> - Used to declare a pointer to an object</para>
@@ -305,7 +315,7 @@ abstracts the difference in implementations where some return a
       <refsect1>
        <title>Examples</title>
        <programlisting>
-(def-array ca :char)
+(def-array-pointer ca :unsigned-char)
 (let ((fs (convert-to-foreign-string "ab")))
    (values (null-char-p (deref-array fs 'ca 0))
            (null-char-p (deref-array fs 'ca 2))))
@@ -327,86 +337,20 @@ abstracts the difference in implementations where some return a
        <para>None.</para>
       </refsect1>
     </refentry>
+  </reference>
 
-    <refentry id="ensure-char">
-      <refnamediv>
-       <refname>ensure-char</refname>
-       <refpurpose>Ensures value is a character.
-       </refpurpose>
-       <refclass>Macro</refclass>
-      </refnamediv>
-      <refsynopsisdiv>
-       <title>Syntax</title>
-       <synopsis>
-         <function>ensure-char</function> <replaceable>obj</replaceable> => <returnvalue>char</returnvalue>
-       </synopsis>
-      </refsynopsisdiv>
-      <refsect1>
-       <title>Arguments and Values</title>
-       <variablelist>
-         <varlistentry>
-           <term><parameter>obj</parameter></term>
-           <listitem>
-             <para>A character or integer.
-             </para>
-           </listitem>
-         </varlistentry>
-         <varlistentry>
-           <term><parameter>char</parameter></term>
-           <listitem>
-             <para>A character value.
-             </para>
-           </listitem>
-         </varlistentry>
-       </variablelist>
-      </refsect1>
-      <refsect1>
-       <title>Description</title>
-       <para>
-         Enscapsulates the fact that some implementations return a character
-and others return an integer when dereferencing a character pointer.
-       </para>
-      </refsect1>
-      <refsect1>
-       <title>Examples</title>
-       <para>
-<programlisting>
-(let ((fs (convert-to-foreign-string "a")))
-  (prog1 
-    (ensure-char (deref-pointer fs :char))
-    (free-foreign-object fs)))
-=> #\a
-</programlisting>
-       </para>
-      </refsect1>
-      <refsect1>
-       <title>Side Effects</title>
-       <para>None.</para>
-      </refsect1>
-      <refsect1>
-       <title>Affected by</title>
-       <para>None.</para>
-      </refsect1>
-      <refsect1>
-       <title>Exceptional Situations</title>
-       <para>Signals an error if <parameter>obj</parameter> is not
-an integer or character.</para>
-      </refsect1>
-    </refentry>
-</reference>
-
-<reference>
-      <title>Aggregate Types</title>
+  <reference>
+    <title>Aggregate Types</title>
     <partintro>
       <title>Overview</title>
       <para>
        Aggregate types are comprised of one or more primitive types.
       </para>
-</partintro>
+    </partintro>
 
-      <refentry id="def-enum">
-       <refnamediv>
-         <refname>def-enum</refname>
+    <refentry id="def-enum">
+      <refnamediv>
+       <refname>def-enum</refname>
        <refpurpose>Defines a &c; enumeration.
        </refpurpose>
        <refclass>Macro</refclass>
@@ -552,7 +496,7 @@ structure. It's type is <constant>:pointer-self</constant>.
 
     <refentry id="get-slot-value">
       <refnamediv>
-       <refname>def-slot-value</refname>
+       <refname>get-slot-value</refname>
        <refpurpose>Retrieves a value from a slot of a structure.
        </refpurpose>
        <refclass>Macro</refclass>
@@ -696,17 +640,17 @@ structure. It's type is <constant>:pointer-self</constant>.
     </refentry>
 
 
-    <refentry id="def-array">
+    <refentry id="def-array-pointer">
       <refnamediv>
-       <refname>def-array</refname>
-       <refpurpose>Defines a foreign array type.
+       <refname>def-array-pointer</refname>
+       <refpurpose>Defines a pointer to a array of type.
        </refpurpose>
        <refclass>Macro</refclass>
       </refnamediv>
       <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
-         <function>def-array</function> <replaceable>name type</replaceable>
+         <function>def-array-pointer</function> <replaceable>name type</replaceable>
        </synopsis>
       </refsynopsisdiv>
       <refsect1>
@@ -731,13 +675,13 @@ structure. It's type is <constant>:pointer-self</constant>.
       <refsect1>
        <title>Description</title>
        <para>
-         Defines a foreign array type.
+         Defines a type tat is a pointer to an array of type.
        </para>
       </refsect1>
       <refsect1>
        <title>Examples</title>
        <programlisting>
-(def-array byte-array :unsigned-char)
+(def-array-pointer byte-array-pointer :unsigned-char)
        </programlisting>
       </refsect1>
       <refsect1>
@@ -832,6 +776,75 @@ the array.
        <para>None.</para>
       </refsect1>
     </refentry>
+
+    <refentry id="def-union">
+      <refnamediv>
+       <refname>def-union</refname>
+       <refpurpose>Defines a foreign union type.
+       </refpurpose>
+       <refclass>Macro</refclass>
+      </refnamediv>
+      <refsynopsisdiv>
+       <title>Syntax</title>
+       <synopsis>
+         <function>def-union</function> <replaceable>name &amp;rest fields</replaceable>
+       </synopsis>
+      </refsynopsisdiv>
+      <refsect1>
+       <title>Arguments and Values</title>
+       <variablelist>
+         <varlistentry>
+           <term><parameter>name</parameter></term>
+           <listitem>
+             <para>A name of the new union type.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>fields</parameter></term>
+           <listitem>
+             <para>A list of fields of the union.
+             </para>
+           </listitem>
+         </varlistentry>
+       </variablelist>
+      </refsect1>
+      <refsect1>
+       <title>Description</title>
+       <para>
+         Defines a foreign union type.
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Examples</title>
+       <programlisting>
+(def-union test-union
+  (a-char :char)
+  (an-int :int))
+
+(let ((u (allocate-foreign-object 'test-union))
+  (setf (get-slot-value u 'test-union 'an-int) (+ 65 (* 66 256)))
+  (prog1 
+    (ensure-char-character (get-slot-value u 'test-union 'a-char))
+    (free-foreign-object u)))
+=> #\A
+       </programlisting>
+      </refsect1>
+      <refsect1>
+       <title>Side Effects</title>
+       <para>Defines a new foreign type.</para>
+      </refsect1>
+      <refsect1>
+       <title>Affected by</title>
+       <para>None.</para>
+      </refsect1>
+      <refsect1>
+       <title>Exceptional Situations</title>
+       <para>None.</para>
+      </refsect1>
+    </refentry>
+
+
 </reference>
 
 <reference>
@@ -839,7 +852,8 @@ the array.
 <partintro>
 <title>Overview</title>
     <para>
-      Objects are entities that can allocated and freed.
+      Objects are entities that can allocated, referred to by pointers, and
+can be freed.
     </para>
 </partintro>
 
@@ -854,7 +868,7 @@ the array.
       <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
-         <function>allocate-foreign-object</function> <replaceable>type</replaceable> => <returnvalue>ptr</returnvalue>
+         <function>allocate-foreign-object</function> <replaceable>type &amp;optional size</replaceable> => <returnvalue>ptr</returnvalue>
        </synopsis>
       </refsynopsisdiv>
       <refsect1>
@@ -863,7 +877,15 @@ the array.
          <varlistentry>
            <term><parameter>type</parameter></term>
            <listitem>
-             <para>A type of foreign object to allocate.
+             <para>The type of foreign object to allocate. This parameter is evaluated.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>size</parameter></term>
+           <listitem>
+             <para>An optional size parameter. If specified, allocates and returns an
+array of <parameter>type</parameter> that is <parameter>size</parameter> members long.
              </para>
            </listitem>
          </varlistentry>
@@ -951,6 +973,79 @@ the array.
     </refentry>
 
 
+    <refentry id="with-foreign-object">
+      <refnamediv>
+       <refname>with-foreign-object</refname>
+       <refpurpose>Wraps the allocation of a foreign object around a body of code.
+       </refpurpose>
+       <refclass>Macro</refclass>
+      </refnamediv>
+      <refsynopsisdiv>
+       <title>Syntax</title>
+       <synopsis>
+         <function>with-foreign-object</function> <replaceable>(var type) &amp;body body</replaceable> => <returnvalue>form-return</returnvalue>
+       </synopsis>
+      </refsynopsisdiv>
+      <refsect1>
+       <title>Arguments and Values</title>
+       <variablelist>
+         <varlistentry>
+           <term><parameter>var</parameter></term>
+           <listitem>
+             <para>The variable name to bind.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>type</parameter></term>
+           <listitem>
+             <para>The type of foreign object to allocate. This parameter is evaluated.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><returnvalue>form-return</returnvalue></term>
+           <listitem>
+             <para>The result of evaluating the <parameter>body</parameter>.
+             </para>
+           </listitem>
+         </varlistentry>
+       </variablelist>
+      </refsect1>
+      <refsect1>
+       <title>Description</title>
+       <para>
+This function wraps the allocation, binding, and destruction of a foreign object.
+On &cmucl; and
+&lw; platforms the object is stack allocated for efficiency. Benchmarks show that &acl; performs
+much better with static allocation.
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Examples</title>
+       <programlisting>
+(defun gethostname2 ()
+  "Returns the hostname"
+  (uffi:with-foreign-object (name '(:array :unsigned-char 256))
+    (if (zerop (c-gethostname (uffi:char-array-to-pointer name) 256))
+       (uffi:convert-from-foreign-string name)
+       (error "gethostname() failed."))))
+       </programlisting>
+      </refsect1>
+      <refsect1>
+       <title>Side Effects</title>
+       <para>None.</para>
+      </refsect1>
+      <refsect1>
+       <title>Affected by</title>
+       <para>None.</para>
+      </refsect1>
+      <refsect1>
+       <title>Exceptional Situations</title>
+       <para>None.</para>
+      </refsect1>
+    </refentry>
+
     <refentry id="pointer-address">
       <refnamediv>
        <refname>pointer-address</refname>
@@ -1014,7 +1109,7 @@ the array.
       <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
-         <function>def-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
+         <function>deref-pointer</function> <replaceable>ptr type</replaceable> => <returnvalue>value</returnvalue>
        </synopsis>
       </refsynopsisdiv>
       <refsect1>
@@ -1035,7 +1130,7 @@ the array.
            </listitem>
          </varlistentry>
          <varlistentry>
-           <term><parameter>value</parameter></term>
+           <term><returnvalue>value</returnvalue></term>
            <listitem>
              <para>The value of the object where the pointer points.
              </para>
@@ -1053,9 +1148,76 @@ the array.
        <title>Examples</title>
        <para>
 <programlisting>
+(let ((intp (allocate-foreign-object :int)))
+  (setf (deref-pointer intp :int) 10)
+  (prog1
+    (deref-pointer intp :int)
+    (free-foreign-object intp)))
+=> 10
+</programlisting>
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Side Effects</title>
+       <para>None.</para>
+      </refsect1>
+      <refsect1>
+       <title>Affected by</title>
+       <para>None.</para>
+      </refsect1>
+      <refsect1>
+       <title>Exceptional Situations</title>
+       <para>None.</para>
+      </refsect1>
+    </refentry>
+
+    <refentry id="ensure-char-character">
+      <refnamediv>
+       <refname>ensure-char-character</refname>
+       <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
+a character.
+       </refpurpose>
+       <refclass>Macro</refclass>
+      </refnamediv>
+      <refsynopsisdiv>
+       <title>Syntax</title>
+       <synopsis>
+         <function>ensure-char-character</function> <replaceable>object</replaceable> => <returnvalue>char</returnvalue>
+       </synopsis>
+      </refsynopsisdiv>
+      <refsect1>
+       <title>Arguments and Values</title>
+       <variablelist>
+         <varlistentry>
+           <term><parameter>object</parameter></term>
+           <listitem>
+             <para>Either a character or a integer specifying a character code.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><returnvalue>char</returnvalue></term>
+           <listitem>
+             <para>A character.
+             </para>
+           </listitem>
+         </varlistentry>
+       </variablelist>
+      </refsect1>
+      <refsect1>
+       <title>Description</title>
+       <para>
+         Ensures that an object obtained by dereferencing a 
+<constant>:char</constant> pointer is a character.
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Examples</title>
+       <para>
+<programlisting>
 (let ((fs (convert-to-foreign-string "a")))
   (prog1 
-    (ensure-char (deref-pointer fs :char))
+    (ensure-char-character (deref-pointer fs :char))
     (free-foreign-object fs)))
 => #\a
 </programlisting>
@@ -1071,8 +1233,78 @@ the array.
       </refsect1>
       <refsect1>
        <title>Exceptional Situations</title>
+       <para>Depending upon the implementation and what &uffi; expects, this
+macro may signal an error if the object is not a character or
+integer.</para>
+      </refsect1>
+    </refentry>
+
+    <refentry id="ensure-char-integer">
+      <refnamediv>
+       <refname>ensure-char-integer</refname>
+       <refpurpose>Ensures that a dereferenced <constant>:char</constant> pointer is
+an integer.
+       </refpurpose>
+       <refclass>Macro</refclass>
+      </refnamediv>
+      <refsynopsisdiv>
+       <title>Syntax</title>
+       <synopsis>
+         <function>ensure-char-integer</function> <replaceable>object</replaceable> => <returnvalue>int</returnvalue>
+       </synopsis>
+      </refsynopsisdiv>
+      <refsect1>
+       <title>Arguments and Values</title>
+       <variablelist>
+         <varlistentry>
+           <term><parameter>object</parameter></term>
+           <listitem>
+             <para>Either a character or a integer specifying a character code.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><returnvalue>int</returnvalue></term>
+           <listitem>
+             <para>An integer.
+             </para>
+           </listitem>
+         </varlistentry>
+       </variablelist>
+      </refsect1>
+      <refsect1>
+       <title>Description</title>
+       <para>
+         Ensures that an object obtained by dereferencing a 
+<constant>:char</constant> pointer is an integer.
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Examples</title>
+       <para>
+<programlisting>
+(let ((fs (convert-to-foreign-string "a")))
+  (prog1 
+    (ensure-char-integer (deref-pointer fs :char))
+    (free-foreign-object fs)))
+=> 96
+</programlisting>
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Side Effects</title>
        <para>None.</para>
       </refsect1>
+      <refsect1>
+       <title>Affected by</title>
+       <para>None.</para>
+      </refsect1>
+      <refsect1>
+       <title>Exceptional Situations</title>
+       <para>Depending upon the implementation and what &uffi; expects, this
+macro may signal an error if the object is not a character or
+integer.</para>
+      </refsect1>
     </refentry>
 
     <refentry id="make-null-pointer">
@@ -1599,7 +1831,7 @@ Can translated ASCII and binary strings.
          <varlistentry>
            <term><parameter>unsigned</parameter></term>
            <listitem>
-             <para>A boolean flag with a default value of &nil;. When true,
+             <para>A boolean flag with a default value of &t;. When true,
 marks the pointer as an <constant>:unsigned-char</constant>.
              </para>
            </listitem>
@@ -1665,7 +1897,7 @@ marks the pointer as an <constant>:unsigned-char</constant>.
          <varlistentry>
            <term><parameter>args</parameter></term>
            <listitem>
-             <para>A list of argument declarations. Use &nil; to specify no arguments.
+             <para>A list of argument declarations. If &nil;, indicates that the function does not take any arguments.
              </para>
            </listitem>
          </varlistentry>
@@ -1679,7 +1911,7 @@ marks the pointer as an <constant>:unsigned-char</constant>.
            <term><returnvalue>returning</returnvalue></term>
            <listitem>
              <para>A declaration specifying the result type of the
-foreign function.
+foreign function. If <constant>:void</constant> indicates module does not return any value.
              </para>
            </listitem>
          </varlistentry>
@@ -1694,7 +1926,7 @@ foreign function.
        <title>Examples</title>
        <programlisting>
 (def-function "gethostname" 
-  ((name :cstring)
+  ((name (* :unsigned-char))
    (len :int))
   :returning :int)
        </programlisting>
@@ -1796,6 +2028,91 @@ a session.
       </refsect1>
     </refentry>
 
+      <refentry id="find-foreign-library">
+       <refnamediv>
+         <refname>find-foreign-library</refname>
+       <refpurpose>Finds a foreign library file.
+       </refpurpose>
+       <refclass>Function</refclass>
+      </refnamediv>
+      <refsect1>
+       <title>Syntax</title>
+<synopsis>
+         <function>find-foreign-library</function> <replaceable>names directories &amp; drive-letters types</replaceable> => <returnvalue>path</returnvalue>
+</synopsis>
+      </refsect1>
+      <refsect1>
+       <title>Arguments and Values</title>
+       <variablelist>
+         <varlistentry>
+           <term><parameter>names</parameter></term>
+           <listitem>
+             <para>A string or list of strings containing the base name of the library file.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>directories</parameter></term>
+           <listitem>
+             <para>A string or list of strings containing the directory the library file.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>drive-letters</parameter></term>
+           <listitem>
+             <para>A string or list of strings containing the drive letters for the library file.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><parameter>types</parameter></term>
+           <listitem>
+             <para>A string or list of strings containing the file type of the library file. Default
+is &nil;. If &nil;, will use a default type based on the currently running implementation.
+             </para>
+           </listitem>
+         </varlistentry>
+         <varlistentry>
+           <term><returnvalue>path</returnvalue></term>
+           <listitem>
+             <para>A path containing the path found, or &nil; if the library file was not found.
+             </para>
+           </listitem>
+         </varlistentry>
+       </variablelist>
+      </refsect1>
+      <refsect1>
+       <title>Description</title>
+       <para>Finds a foreign library by searching through a number of possible locations. Returns
+the path of the first found file.
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Examples</title>
+       <programlisting>
+(find-foreign-library '("libmysqlclient" "libmysql")
+    '("/opt/mysql/lib/mysql/" "/usr/local/lib/" "/usr/lib/" "/mysql/lib/opt/")
+    :types '("so" "dll")
+    :drive-letters '("C" "D" "E"))
+=> #P"D:\\mysql\\lib\\opt\\libmysql.dll"
+       </programlisting>
+      </refsect1>
+      <refsect1>
+       <title>Side Effects</title>
+       <para>None.
+       </para>
+      </refsect1>
+      <refsect1>
+       <title>Affected by</title>
+       <para>None.</para>
+      </refsect1>
+      <refsect1>
+       <title>Exceptional Situations</title>
+       <para>None.</para>
+      </refsect1>
+    </refentry>
+
 </reference>