summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortduehr <tduehr@gmail.com>2014-01-16 11:36:14 -0600
committertduehr <tduehr@gmail.com>2014-10-13 15:27:12 -0500
commita129108a69da828b194a0870c3dd7a6fc29875b4 (patch)
treec0d035796db74b0780e4822f442bce8d560b2d9d
parent31a15dfec6186e9eae32d39eeb65617b6ef5a518 (diff)
downloadffi-a129108a69da828b194a0870c3dd7a6fc29875b4.tar.gz
Add optional FFI::Type argument to Library#enum and Enum.new
alll tests pass
-rw-r--r--lib/ffi/enum.rb9
-rw-r--r--lib/ffi/library.rb3
2 files changed, 7 insertions, 5 deletions
diff --git a/lib/ffi/enum.rb b/lib/ffi/enum.rb
index 9dcf4fa..dc8711a 100644
--- a/lib/ffi/enum.rb
+++ b/lib/ffi/enum.rb
@@ -88,8 +88,9 @@ module FFI
# @param [nil, Enumerable] info
# @param tag enum tag
- def initialize(info, tag=nil)
- @tag = tag
+ def initialize(*args)
+ @native_type = args.shift if args.first.kind_of?(FFI::Type)
+ info, @tag = *args
@kv_map = Hash.new
unless info.nil?
last_cst = nil
@@ -144,9 +145,9 @@ module FFI
alias to_hash symbol_map
# Get native type of Enum
- # @return [Type::INT]
+ # @return [Type]
def native_type
- Type::INT
+ @native_type || Type::INT
end
# @param [Symbol, Integer, #to_int] val
diff --git a/lib/ffi/library.rb b/lib/ffi/library.rb
index 47a0fa8..9711d8e 100644
--- a/lib/ffi/library.rb
+++ b/lib/ffi/library.rb
@@ -447,6 +447,7 @@ module FFI
# @return [FFI::Enum]
# Create a new {FFI::Enum}.
def enum(*args)
+ native_type = args.first.kind_of?(FFI::Type) ? args.shift : nil
name, values = if args[0].kind_of?(Symbol) && args[1].kind_of?(Array)
[ args[0], args[1] ]
elsif args[0].kind_of?(Array)
@@ -455,7 +456,7 @@ module FFI
[ nil, args ]
end
@ffi_enums = FFI::Enums.new unless defined?(@ffi_enums)
- @ffi_enums << (e = FFI::Enum.new(values, name))
+ @ffi_enums << (e = native_type ? FFI::Enum.new(native_type, values, name) : FFI::Enum.new(values, name))
# If called as enum :foo, [ :zero, :one, :two ], add a typedef alias
typedef(e, name) if name