From: Cyrus Harmon <ch-sbcl@bo...> - 2005-08-05 22:06:41
So I've been trying to figure out how to map C enums to SBCL types.
UFFI's approach seems to be to do a define-alien-type <enum-name>
SIGNED and constants for the values. sb-alien, however, has a nice sb-
alien:enum type. Christophe was kind enough to provide an example of
how to do this. Here's a similar example:
this works great and I can do:
(defparameter moose-var (sb-alien:make-alien moose-enum))
and I can do
(setf (sb-alien:deref moose-var) :bear) to set the value to :bear.
Other keywords don't work, which is great. Is there a way to actually
get the value represented by the keyword :bear in the context of the
enum? I know this sort of defeats the purpose of the enum, but it
would still be nice to get.
From: Thomas F. Burdick <tfb@OCF.Berkeley.EDU> - 2005-08-06 07:45:36
Cyrus Harmon writes:
> (setf (sb-alien:deref moose-var) :bear) to set the value to :bear.
> Other keywords don't work, which is great. Is there a way to actually
> get the value represented by the keyword :bear in the context of the
> enum? I know this sort of defeats the purpose of the enum, but it
> would still be nice to get.
Sure. When you dereference a (* moose-enum), you get a valid
moose-enum value. If you want an int, you should be dereferencing a
(* int). How do you get from the one to the other? By casting it,
(defun moose-to-int (moose)
(deref (cast moose (* int))))
DEREF, CAST, and INT are all in the SB-ALIEN package of course, but if
you're writing code that uses Alien, you really want to be working in
a package the uses SB-ALIEN so you can see the content for the package
,' .\ / | Free Mumia Abu-Jamal! |
,--' _,' | Abolish the racist |
/ / | death penalty! |
( -. | `-----------------------'
| ) |