chickadee » jiffi » define-enum-unpacker

(define-enum-unpacker ...)syntax

Defines a procedure that will "unpack" an integer (a bitfield) into a list of enum symbols. This supplements define-enum-group in cases where the enums are bitmasks or flags.

See also define-enum-packer to go in the opposite direction, converting a list of symbols into an integer.


(define-enum-unpacker UNPACKER
  masks: BITMASKS)

UNPACKER is the procedure name to define as the enum unpacker.

INT->SYMBOL is an existing procedure that converts an integer value (bitmask) into a symbol, such as a procedure defined with define-enum-group.

BITMASKS is an expression that evaluates to a list of all the integer bitmasks that should be recognized. The expression will be evaluated once, and each bitmask will be converted using INT->SYMBOL once, at the time UNPACKER is defined.

Bitmasks that have a value of 0 (zero) will always match, so it is not useful to include them in the BITMASKS list.


(foreign-declare "
typedef enum {
  FOO_KMOD_NONE   = 0b0000,
  FOO_KMOD_LCTRL  = 0b0001,
  FOO_KMOD_RCTRL  = 0b0010,
  FOO_KMOD_CTRL   = 0b0011
} FOO_KeyMod;

  type: int
  symbol->int: keymod->int
  int->symbol: int->keymod
  ((none  FOO_KMOD_NONE)
   (lctrl FOO_KMOD_LCTRL)
   (rctrl FOO_KMOD_RCTRL)
   (ctrl  FOO_KMOD_CTRL)))

(define-enum-unpacker unpack-keymods
  masks: (list FOO_KMOD_LCTRL

(unpack-keymods FOO_KMOD_NONE)     ; ⇒ '()
(unpack-keymods FOO_KMOD_LCTRL)    ; ⇒ '(lctrl)
(unpack-keymods FOO_KMOD_CTRL)     ; ⇒ '(lctrl rctrl ctrl)