-
Notifications
You must be signed in to change notification settings - Fork 15
Enumerated Types
j3pic edited this page Jul 13, 2023
·
5 revisions
(defmacro define-enum (name size (&key signed
(signed-representation :twos-complement)
(reader '#'read-integer)
(writer '#'write-integer)
(byte-order :little-endian))
&rest values)
Defines an enumerated type. What this does is allow LISP-BINARY
to automatically map between
keywords and integer values that are expected to be found in a binary file. The SIZE is in bytes.
By specifying alternative :reader
and :writer
functions that are compatible with
read-integer
and write-integer
, it's also possible to use custom integer representations
not built into the library.
Example:
(define-enum speed 2 ()
slow ;; Implicitly 0
light-speed ;; Implicitly 1
(ridiculous-speed 5) ;; Explicit value
ludicrous-speed) ;; Implicitly 6
Reading:
(read-binary-type 'speed stream)
Writing:
(write-binary-type 'speed 'ridiculous-speed stream)
In DEFBINARY
:
(defbinary speed-and-heading ()
(speed 'slow :type speed)
(direction 0.0 :type double-float))
Enums can be used to specify the representation of booleans:
;; 0 for false and 1 for true
(define-enum binary-boolean 1 ()
nil t)
;; #xff for true and #xfe for false
(define-enum weird-boolean 1 ()
(nil #xfe)
(t #xff))