Subject: [ruby-ffi] Re: New wiki page: Enums. Please improve and correct. |
From: Wayne Meissner |
Date: 11/14/09 4:12 AM |
To: ruby-ffi@googlegroups.com |
2009/11/14 John Croisant <jacius@gmail.com>:
But, I thought you could use the enum name as a type, even in structs. I haven't tried it yet, but it certainly feels like that _should_ be supported, from a library user's perspective. Even if that doesn't work yet, "enum_type(:my_enum)" would work, right?
The problem is that Struct.layout() needs to be able to resolve the
module that encloses the Struct subclass definition, to be able to
lookup any custom typedefs (e.g. callbacks, enums).
e.g.
Module Foo
typedef :int, :my_int
class MyStruct < FFI::Struct
layout :a, :my_int
end
end
It kinda-sorta works, but is not rock solid, so I discourage its use.
P.S. Does FFI enforce the validity of values used where a particular enum type is expected? In other words, if a function (or struct) is expecting an enum that can be only :a, :b, or :c, would passing :d cause it to raise an error?
Passing :d should raise an error, since it will try to look it up in the enum table and fail.