All Manuals > Foreign Language Interface User Guide and Reference Manual > 7 Function, Macro and Variable Reference

define-opaque-pointer Macro

Summary

Defines an opaque foreign pointer type.

Package

fli

Signature

define-opaque-pointer pointer-type structure-type

Arguments
pointer-type
A symbol.
structure-type
A symbol.
Description

The macro define-opaque-pointer defines an opaque foreign pointer type named pointer-type and foreign structure type with a name based on structure-type. An opaque pointer is a pointer to a structure which does not have a structure description. It is the equivalent to the C declaration:

typedef struct structure-type *pointer-type;

An opaque pointer is useful for dealing with pointers that are returned by foreign functions and are then passed to other foreign functions. It checks the type of the foreign pointer, and thus prevents passing pointers of the wrong type.

Examples

Using the C standard file* pointer:

(fli:define-opaque-pointer file-pointer file)
 
(fli:define-foreign-function fopen
    ((name (:reference-pass :ef-mb-string))
     (mode (:reference-pass :ef-mb-string)))
  :result-type file-pointer)
 
(fli:define-foreign-function fgetc
    ((file file-pointer))
  :result-type :int)
 
(fli:define-foreign-function fclose
    ((file file-pointer)))
 
(fli:define-foreign-function fgets
    ((string
      (:reference-return (:ef-mb-string :limit 200)))
     (:constant 200 :int)
     (file file-pointer))
  :result-type (:pointer-integer :int)
  :lambda-list (file &aux string))
 
(defun print-a-file (name)
  (let ((file-pointer (fopen name "r")))
    (if (fli:null-pointer-p file-pointer)
        (error "failed to open ~a" name)
      (unwind-protect 
          (loop (multiple-value-bind (res line)
                    (fgets file-pointer)
                  (when (zerop res) (return))
                  (princ line)))
        (fclose file-pointer)))))
See also

define-foreign-type


Foreign Language Interface User Guide and Reference Manual - 01 Dec 2021 19:34:58