chickadee » jiffi » define-foreign-values

(define-foreign-values ...)syntax

Defines (and optionally exports) variables with the values of C constants or expressions. Each value can be the name of a const, enum, or #define; or it can be a string containing a C expression which will be evaluated at run time.

If you want to define procedures to convert integer constants to symbols, and vice versa, use define-enum-group instead.

Usage:

(define-foreign-values
  export: EXPORT         ; optional
  type: FOREIGN-TYPE
  (VAR EXPRESSION)       ; or just VAR
  ...)

If EXPORT is #t, every VAR will be automatically exported from the current module, so you don't need to write a long (export ...) form with all the variables. If EXPORT is #f or the keyword clause is omitted, they are not automatically exported.

FOREIGN-TYPE is a foreign type specifier that describes all the foreign values.

Each VAR is a variable name to define in Scheme.

Each EXPRESSION is a non-quoted symbol or string containing the name of a foreign constant in C, or a string containing a C expression.

If VAR and EXPRESSION are the same, you can write VAR instead of (VAR EXPRESSION). This must be a variable name (non-quoted symbol), not a string.

Examples:

;; C enum definitions
(foreign-declare "
#define FOO_BAR  \"Bar!\"
#define FOO_BUZZ \"Buzz!\"

const float FOO_PHI = 2.718281828459045;
const float FOO_PI  = 3.141592653589793;

typedef enum {
  FOO_BLEND_NONE = 0,
  FOO_BLEND_ADD = 1,
  FOO_BLEND_SUB = 2,
  FOO_BLEND_MUL = 4
} FOO_BlendMode;

#define FOO_ADD1(x) ((x) + 1)
")

;; #define
(define-foreign-values
  export: #t
  type: c-string
  FOO_BAR
  FOO_BUZZ)

;; const
(define-foreign-values
  export: #t
  type: float
  (φ FOO_PHI)
  (π FOO_PI))

;; enum
(define-foreign-values
  export: #t
  type: int
  FOO_BLEND_NONE
  FOO_BLEND_ADD
  FOO_BLEND_SUB
  FOO_BLEND_MUL)

;; C expression
(define-foreign-values
  export: #t
  type: double
  (four "1.5 + 2.5")
  (five "FOO_ADD1(4)")
  (sqrt2 "sqrt(2.0)"))

FOO_BAR        ; ⇒ "Bar!"
φ              ; ⇒ 2.71828174591064
FOO_BLEND_SUB  ; ⇒ 2
four           ; ⇒ 4.0
five           ; ⇒ 5.0
sqrt2          ; ⇒ 1.4142135623731

define-foreign-values is based on foreign-value. It is basically equivalent to:

(export VAR ...)
(define VAR (foreign-value EXPRESSION FOREIGN-TYPE))
...