chickadee » jiffi » define-array-accessors

(define-array-accessors ...)syntax

Defines various procedures to get or set items of a C array of structs/unions. You can omit any procedures that you don't need. Also generates type declarations for the defined procedures.

Usage:

(define-array-accessors
  (ARMOR-NAME "STRUCT_NAME" PRED UNWRAP LENGTH)
  (ITEM-ARMOR-NAME ITEM-PRED ITEM-WRAP ITEM-UNWRAP)
  ref:       REF         ; optional
  set:       SET         ; optional
  map:       MAP         ; optional
  for-each:  FOR-EACH    ; optional
  ref*:      REF*        ; optional
  map*:      MAP*        ; optional
  for-each*: FOR-EACH*)  ; optional

ARMOR-NAME is the record type name for the array, such as the record name passed to define-armor-type.

"STRUCT_NAME" is a string containing the name of the C struct/union (not the array type), exactly as it appears in C. For example, for an array of FOO_Event structs, just write "FOO_Event".

PRED is an existing type predicate for the array record type, such as a procedure defined with define-armor-type.

UNWRAP is an existing armor unwrapper procedure, such as a procedure defined with define-armor-type.

LENGTH is an existing procedure that returns the array length, such as an extra slot getter defined with define-armor-type.

ITEM-ARMOR-NAME is the record type name that was passed to define-armor-type for the item type (not the array type).

ITEM-PRED, ITEM-WRAP, and ITEM-UNWRAP are existing procedures for the item type (not the array type), such as the procedures defined with define-armor-type. ITEM-WRAP must return an instance of a type defined with define-armor-type.

REF is the procedure name to define as an array item getter, which returns an armor instance wrapping the bare data of an item in the array. If REF is #f or the keyword clause is omitted, the procedure will not be defined.

SET is the procedure name to define as an array item setter, which overwrites the memory for an item in the array. If SET is #f or the keyword clause is omitted, the procedure will not be defined and REF will not be settable. If SET is the form (setter REF), no setter procedure will be defined, but the array will be settable using (set! (REF array i) struct). If you want to have a setter procedure and also make the array settable with set!, use (set! (setter REF) SET) after defining the array accessors.

MAP is the procedure name to define as an array item mapper, which repeatedly calls a procedure with each item in one or more arrays, and returns a list of the results. If MAP is #f or the keyword clause is omitted, the procedure will not be defined.

FOR-EACH is the procedure name to define as an array item iterator, which repeatedly calls a procedure with each item in one or more arrays, in a guaranteed order, but does not return anything. If FOR-EACH is #f or the keyword clause is omitted, the procedure will not be defined.

REF* is the procedure name to define as an array item pointer getter, which returns a pointer or locative to an item in an array. If REF* is #f or the keyword clause is omitted, the procedure will not be defined.

MAP* is the procedure name to define as an array item pointer mapper, which repeatedly calls a procedure with a pointer or locative to each item in one or more arrays, and returns a list of the results. If MAP* is #f or the keyword clause is omitted, the procedure will not be defined.

FOR-EACH* is the procedure name to define as an array item pointer iterator, which repeatedly calls a procedure with a pointer or locative to each item in one or more arrays, in a guaranteed order, but does not return anything. If FOR-EACH* is #f or the keyword clause is omitted, the procedure will not be defined.

Example:

(define-array-accessors
  (event-array "FOO_Event" event-array? unwrap-event-array event-array-length)
  (event event? wrap-event unwrap-event)
  ref:       event-array-ref
  set:       (setter event-array-ref)
  map:       event-array-map
  for-each:  event-array-for-each
  ref*:      event-array-ref*
  map*:      event-array-map*
  for-each*: event-array-for-each*)