ffi 0.6.3-x86-mingw32 → 1.0.1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ffi might be problematic. Click here for more details.

Files changed (64) hide show
  1. data/History.txt +7 -0
  2. data/LICENSE +10 -21
  3. data/README.rdoc +1 -0
  4. data/Rakefile +4 -2
  5. data/ext/ffi_c/AbstractMemory.c +103 -38
  6. data/ext/ffi_c/AbstractMemory.h +15 -22
  7. data/ext/ffi_c/Buffer.c +61 -22
  8. data/ext/ffi_c/Call.c +52 -540
  9. data/ext/ffi_c/Call.h +1 -1
  10. data/ext/ffi_c/DataConverter.c +62 -0
  11. data/ext/ffi_c/DynamicLibrary.c +21 -1
  12. data/ext/ffi_c/Function.c +315 -30
  13. data/ext/ffi_c/MappedType.c +146 -0
  14. data/ext/ffi_c/MappedType.h +57 -0
  15. data/ext/ffi_c/MemoryPointer.c +12 -33
  16. data/ext/ffi_c/Platform.c +2 -0
  17. data/ext/ffi_c/Pointer.c +66 -28
  18. data/ext/ffi_c/Struct.c +19 -306
  19. data/ext/ffi_c/Struct.h +6 -0
  20. data/ext/ffi_c/StructByReference.c +150 -0
  21. data/ext/ffi_c/StructByReference.h +50 -0
  22. data/ext/ffi_c/StructLayout.c +25 -14
  23. data/ext/ffi_c/Type.c +39 -68
  24. data/ext/ffi_c/Type.h +12 -22
  25. data/ext/ffi_c/Types.c +20 -5
  26. data/ext/ffi_c/Types.h +7 -7
  27. data/ext/ffi_c/Variadic.c +21 -17
  28. data/ext/ffi_c/extconf.rb +4 -0
  29. data/ext/ffi_c/ffi.c +8 -2
  30. data/ext/ffi_c/rbffi.h +1 -0
  31. data/lib/ffi/autopointer.rb +23 -22
  32. data/lib/ffi/enum.rb +36 -21
  33. data/lib/ffi/errno.rb +20 -0
  34. data/lib/ffi/ffi.rb +13 -80
  35. data/lib/ffi/io.rb +12 -20
  36. data/lib/ffi/library.rb +109 -92
  37. data/lib/ffi/managedstruct.rb +1 -1
  38. data/lib/ffi/memorypointer.rb +15 -21
  39. data/lib/ffi/platform.rb +24 -28
  40. data/lib/ffi/pointer.rb +14 -21
  41. data/lib/ffi/struct.rb +98 -49
  42. data/lib/ffi/struct_layout_builder.rb +158 -0
  43. data/lib/ffi/types.rb +99 -128
  44. data/lib/ffi/union.rb +20 -0
  45. data/lib/ffi/variadic.rb +33 -22
  46. data/lib/ffi_c.so +0 -0
  47. data/spec/ffi/async_callback_spec.rb +23 -0
  48. data/spec/ffi/callback_spec.rb +62 -0
  49. data/spec/ffi/custom_param_type.rb +31 -0
  50. data/spec/ffi/custom_type_spec.rb +73 -0
  51. data/spec/ffi/enum_spec.rb +19 -0
  52. data/spec/ffi/ffi_spec.rb +24 -0
  53. data/spec/ffi/pointer_spec.rb +15 -0
  54. data/spec/ffi/rbx/memory_pointer_spec.rb +7 -1
  55. data/spec/ffi/strptr_spec.rb +36 -0
  56. data/spec/ffi/struct_packed_spec.rb +46 -0
  57. data/spec/ffi/struct_spec.rb +19 -5
  58. data/spec/ffi/typedef_spec.rb +14 -0
  59. data/tasks/setup.rb +2 -1
  60. metadata +15 -6
  61. data/ext/ffi_c/AutoPointer.c +0 -60
  62. data/ext/ffi_c/AutoPointer.h +0 -18
  63. data/lib/1.8/ffi_c.so +0 -0
  64. data/lib/1.9/ffi_c.so +0 -0
@@ -1,28 +1,19 @@
1
1
  /*
2
2
  * Copyright (c) 2009, Wayne Meissner
3
- * All rights reserved.
4
3
  *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
4
+ * This file is part of ruby-ffi.
7
5
  *
8
- * * Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- * * Redistributions in binary form must reproduce the above copyright notice
11
- * this list of conditions and the following disclaimer in the documentation
12
- * and/or other materials provided with the distribution.
13
- * * The name of the author or authors may not be used to endorse or promote
14
- * products derived from this software without specific prior written permission.
6
+ * This code is free software: you can redistribute it and/or modify it under
7
+ * the terms of the GNU Lesser General Public License version 3 only, as
8
+ * published by the Free Software Foundation.
15
9
  *
16
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10
+ * This code is distributed in the hope that it will be useful, but WITHOUT
11
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13
+ * version 3 for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
26
17
  */
27
18
 
28
19
  #ifndef RBFFI_TYPE_H
@@ -44,8 +35,7 @@ struct Type_ {
44
35
  ffi_type* ffiType;
45
36
  };
46
37
 
47
- extern VALUE rbffi_TypeClass, rbffi_EnumTypeClass;
48
- extern int rbffi_Type_GetIntValue(VALUE type);
38
+ extern VALUE rbffi_TypeClass;
49
39
  extern VALUE rbffi_Type_Lookup(VALUE type);
50
40
  extern VALUE rbffi_Type_Find(VALUE type);
51
41
 
@@ -34,9 +34,10 @@
34
34
  #include "StructByValue.h"
35
35
  #include "Types.h"
36
36
  #include "Struct.h"
37
+ #include "MappedType.h"
37
38
  #include "MemoryPointer.h"
38
39
 
39
- static ID id_find = 0;
40
+ static ID id_from_native = 0;
40
41
 
41
42
 
42
43
  VALUE
@@ -77,13 +78,14 @@ rbffi_NativeValue_ToRuby(Type* type, VALUE rbType, const void* ptr, VALUE enums)
77
78
  return rbffi_Pointer_NewInstance(*(void **) ptr);
78
79
  case NATIVE_BOOL:
79
80
  return ((unsigned char) *(ffi_arg *) ptr) ? Qtrue : Qfalse;
80
- case NATIVE_ENUM:
81
- return rb_funcall(rbType, id_find, 1, INT2NUM((unsigned int) *(ffi_arg *) ptr));
82
81
 
83
82
  case NATIVE_FUNCTION:
84
83
  case NATIVE_CALLBACK: {
85
- return rbffi_Function_NewInstance(rbType, rbffi_Pointer_NewInstance(*(void **) ptr));
84
+ return *(void **) ptr != NULL
85
+ ? rbffi_Function_NewInstance(rbType, rbffi_Pointer_NewInstance(*(void **) ptr))
86
+ : Qnil;
86
87
  }
88
+
87
89
  case NATIVE_STRUCT: {
88
90
  StructByValue* sbv = (StructByValue *)type;
89
91
  AbstractMemory* mem;
@@ -95,6 +97,19 @@ rbffi_NativeValue_ToRuby(Type* type, VALUE rbType, const void* ptr, VALUE enums)
95
97
  return rb_class_new_instance(1, &rbMemory, sbv->rbStructClass);
96
98
  }
97
99
 
100
+ case NATIVE_MAPPED: {
101
+ // For mapped types, first convert to the real native type, then upcall to
102
+ // ruby to convert to the expected return type
103
+ MappedType* m;
104
+ VALUE values[2];
105
+
106
+ Data_Get_Struct(rbType, MappedType, m);
107
+ values[0] = rbffi_NativeValue_ToRuby(m->type, m->rbType, ptr, enums);
108
+ values[1] = Qnil;
109
+
110
+ return rb_funcall2(m->rbConverter, id_from_native, 2, values);
111
+ }
112
+
98
113
  default:
99
114
  rb_raise(rb_eRuntimeError, "Unknown type: %d", type->nativeType);
100
115
  return Qnil;
@@ -104,6 +119,6 @@ rbffi_NativeValue_ToRuby(Type* type, VALUE rbType, const void* ptr, VALUE enums)
104
119
  void
105
120
  rbffi_Types_Init(VALUE moduleFFI)
106
121
  {
107
- id_find = rb_intern("find");
122
+ id_from_native = rb_intern("from_native");
108
123
  }
109
124
 
@@ -57,20 +57,20 @@ typedef enum {
57
57
  NATIVE_CHAR_ARRAY,
58
58
  NATIVE_BOOL,
59
59
 
60
- /**
61
- * An immutable string. Nul terminated, but only copies in to the native function
62
- */
60
+ /** An immutable string. Nul terminated, but only copies in to the native function */
63
61
  NATIVE_STRING,
64
- /** A Rubinus :string arg - copies data both ways, and nul terminates */
65
- NATIVE_RBXSTRING,
62
+
66
63
  /** The function takes a variable number of arguments */
67
64
  NATIVE_VARARGS,
68
- /** A typedef-ed enum */
69
- NATIVE_ENUM,
65
+
70
66
  /** Struct-by-value param or result */
71
67
  NATIVE_STRUCT,
68
+
72
69
  /** An array type definition */
73
70
  NATIVE_ARRAY,
71
+
72
+ /** Custom native type */
73
+ NATIVE_MAPPED,
74
74
  } NativeType;
75
75
 
76
76
  #include <ffi.h>
@@ -155,7 +155,7 @@ variadic_invoke(VALUE self, VALUE parameterTypes, VALUE parameterValues)
155
155
  void** ffiValues;
156
156
  ffi_type** ffiParamTypes;
157
157
  ffi_type* ffiReturnType;
158
- NativeType* paramTypes;
158
+ Type** paramTypes;
159
159
  VALUE* argv;
160
160
  int paramCount = 0, i;
161
161
  ffi_status ffiStatus;
@@ -165,7 +165,7 @@ variadic_invoke(VALUE self, VALUE parameterTypes, VALUE parameterValues)
165
165
 
166
166
  Data_Get_Struct(self, VariadicInvoker, invoker);
167
167
  paramCount = RARRAY_LEN(parameterTypes);
168
- paramTypes = ALLOCA_N(NativeType, paramCount);
168
+ paramTypes = ALLOCA_N(Type *, paramCount);
169
169
  ffiParamTypes = ALLOCA_N(ffi_type *, paramCount);
170
170
  params = ALLOCA_N(FFIStorage, paramCount);
171
171
  ffiValues = ALLOCA_N(void*, paramCount);
@@ -173,36 +173,40 @@ variadic_invoke(VALUE self, VALUE parameterTypes, VALUE parameterValues)
173
173
  retval = alloca(MAX(invoker->returnType->ffiType->size, FFI_SIZEOF_ARG));
174
174
 
175
175
  for (i = 0; i < paramCount; ++i) {
176
- VALUE entry = rb_ary_entry(parameterTypes, i);
177
- int paramType = rbffi_Type_GetIntValue(entry);
178
- Type* type;
179
- Data_Get_Struct(entry, Type, type);
176
+ VALUE rbType = rb_ary_entry(parameterTypes, i);
177
+
178
+ if (!rb_obj_is_kind_of(rbType, rbffi_TypeClass)) {
179
+ rb_raise(rb_eTypeError, "wrong type. Expected (FFI::Type)");
180
+ }
181
+ Data_Get_Struct(rbType, Type, paramTypes[i]);
180
182
 
181
- switch (paramType) {
183
+ switch (paramTypes[i]->nativeType) {
182
184
  case NATIVE_INT8:
183
185
  case NATIVE_INT16:
184
186
  case NATIVE_INT32:
185
- case NATIVE_ENUM:
186
- paramType = NATIVE_INT32;
187
- ffiParamTypes[i] = &ffi_type_sint;
187
+ rbType = rb_const_get(rbffi_TypeClass, rb_intern("INT32"));
188
+ Data_Get_Struct(rbType, Type, paramTypes[i]);
188
189
  break;
189
190
  case NATIVE_UINT8:
190
191
  case NATIVE_UINT16:
191
192
  case NATIVE_UINT32:
192
- paramType = NATIVE_UINT32;
193
- ffiParamTypes[i] = &ffi_type_uint;
193
+ rbType = rb_const_get(rbffi_TypeClass, rb_intern("UINT32"));
194
+ Data_Get_Struct(rbType, Type, paramTypes[i]);
194
195
  break;
196
+
195
197
  case NATIVE_FLOAT32:
196
- paramType = NATIVE_FLOAT64;
197
- ffiParamTypes[i] = &ffi_type_double;
198
+ rbType = rb_const_get(rbffi_TypeClass, rb_intern("DOUBLE"));
199
+ Data_Get_Struct(rbType, Type, paramTypes[i]);
198
200
  break;
201
+
199
202
  default:
200
- ffiParamTypes[i] = type->ffiType;
201
203
  break;
202
204
  }
203
- paramTypes[i] = paramType;
205
+
206
+
207
+ ffiParamTypes[i] = paramTypes[i]->ffiType;
204
208
  if (ffiParamTypes[i] == NULL) {
205
- rb_raise(rb_eArgError, "Invalid parameter type #%x", paramType);
209
+ rb_raise(rb_eArgError, "Invalid parameter type #%x", paramTypes[i]);
206
210
  }
207
211
  argv[i] = rb_ary_entry(parameterValues, i);
208
212
  }
@@ -15,9 +15,13 @@ unless Config::CONFIG['host_os'] =~ /mswin32|mingw32/
15
15
  end
16
16
 
17
17
  have_func('rb_thread_blocking_region')
18
+ have_func('ruby_thread_has_gvl_p')
19
+ have_func('ruby_native_thread_p')
20
+ have_func('rb_thread_call_with_gvl')
18
21
 
19
22
  $defs << "-DHAVE_EXTCONF_H" if $defs.empty? # needed so create_header works
20
23
  $defs << "-DUSE_INTERNAL_LIBFFI" unless libffi_ok
24
+ $defs << "-DRUBY_1_9" if RUBY_VERSION >= "1.9.0"
21
25
 
22
26
  create_header
23
27
 
@@ -35,9 +35,9 @@
35
35
  #include "AbstractMemory.h"
36
36
  #include "Pointer.h"
37
37
  #include "MemoryPointer.h"
38
- #include "AutoPointer.h"
39
38
  #include "Struct.h"
40
39
  #include "StructByValue.h"
40
+ #include "StructByReference.h"
41
41
  #include "DynamicLibrary.h"
42
42
  #include "Platform.h"
43
43
  #include "Types.h"
@@ -47,6 +47,7 @@
47
47
  #include "MethodHandle.h"
48
48
  #include "Call.h"
49
49
  #include "ArrayType.h"
50
+ #include "MappedType.h"
50
51
 
51
52
  void Init_ffi_c(void);
52
53
 
@@ -59,8 +60,12 @@ Init_ffi_c(void) {
59
60
  rbffi_FFIModule = moduleFFI = rb_define_module("FFI");
60
61
  rb_global_variable(&moduleFFI);
61
62
 
63
+
62
64
  // FFI::Type needs to be initialized before most other classes
63
65
  rbffi_Type_Init(moduleFFI);
66
+
67
+ rbffi_DataConverter_Init(moduleFFI);
68
+
64
69
  rbffi_ArrayType_Init(moduleFFI);
65
70
  rbffi_LastError_Init(moduleFFI);
66
71
  rbffi_Call_Init(moduleFFI);
@@ -69,14 +74,15 @@ Init_ffi_c(void) {
69
74
  rbffi_Platform_Init(moduleFFI);
70
75
  rbffi_AbstractMemory_Init(moduleFFI);
71
76
  rbffi_Pointer_Init(moduleFFI);
72
- rbffi_AutoPointer_Init(moduleFFI);
73
77
  rbffi_Function_Init(moduleFFI);
74
78
  rbffi_MemoryPointer_Init(moduleFFI);
75
79
  rbffi_Buffer_Init(moduleFFI);
76
80
  rbffi_StructByValue_Init(moduleFFI);
81
+ rbffi_StructByReference_Init(moduleFFI);
77
82
  rbffi_Struct_Init(moduleFFI);
78
83
  rbffi_DynamicLibrary_Init(moduleFFI);
79
84
  rbffi_Variadic_Init(moduleFFI);
80
85
  rbffi_Types_Init(moduleFFI);
86
+ rbffi_MappedType_Init(moduleFFI);
81
87
  }
82
88
 
@@ -15,6 +15,7 @@ extern void rbffi_Type_Init(VALUE ffiModule);
15
15
  extern void rbffi_Buffer_Init(VALUE ffiModule);
16
16
  extern void rbffi_Invoker_Init(VALUE ffiModule);
17
17
  extern void rbffi_Variadic_Init(VALUE ffiModule);
18
+ extern void rbffi_DataConverter_Init(VALUE ffiModule);
18
19
  extern VALUE rbffi_AbstractMemoryClass, rbffi_InvokerClass;
19
20
  extern int rbffi_type_size(VALUE type);
20
21
 
@@ -1,34 +1,27 @@
1
1
  #
2
- # Copyright (C) 2008, 2009 Wayne Meissner
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
3
  # Copyright (C) 2008 Mike Dalessio
4
4
  #
5
5
  # All rights reserved.
6
6
  #
7
- # Redistribution and use in source and binary forms, with or without
8
- # modification, are permitted provided that the following conditions are met:
7
+ # This file is part of ruby-ffi.
9
8
  #
10
- # * Redistributions of source code must retain the above copyright notice, this
11
- # list of conditions and the following disclaimer.
12
- # * Redistributions in binary form must reproduce the above copyright notice
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- # * Neither the name of the Evan Phoenix nor the names of its contributors
16
- # may be used to endorse or promote products derived from this software
17
- # without specific prior written permission.
9
+ # This code is free software: you can redistribute it and/or modify it under
10
+ # the terms of the GNU Lesser General Public License version 3 only, as
11
+ # published by the Free Software Foundation.
12
+ #
13
+ # This code is distributed in the hope that it will be useful, but WITHOUT
14
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16
+ # version 3 for more details.
17
+ #
18
+ # You should have received a copy of the GNU Lesser General Public License
19
+ # version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
18
20
  #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
23
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
21
 
30
22
  module FFI
31
23
  class AutoPointer < Pointer
24
+ extend DataConverter
32
25
 
33
26
  # call-seq:
34
27
  # AutoPointer.new(pointer, method) => the passed Method will be invoked at GC time
@@ -57,6 +50,7 @@ module FFI
57
50
  # release(), which by default does nothing.
58
51
  #
59
52
  def initialize(ptr, proc=nil, &block)
53
+ super(ptr)
60
54
  raise TypeError, "Invalid pointer" if ptr.nil? || !ptr.kind_of?(Pointer) \
61
55
  || ptr.kind_of?(MemoryPointer) || ptr.kind_of?(AutoPointer)
62
56
 
@@ -69,7 +63,6 @@ module FFI
69
63
  DefaultReleaser.new(ptr, self.class)
70
64
  end
71
65
 
72
- self.parent = ptr
73
66
  ObjectSpace.define_finalizer(self, @releaser)
74
67
  self
75
68
  end
@@ -115,6 +108,14 @@ module FFI
115
108
  end
116
109
  end
117
110
 
111
+ def self.native_type
112
+ raise RuntimeError.new("no release method defined for #{self.inspect}") unless self.respond_to?(:release)
113
+ Type::POINTER
114
+ end
115
+
116
+ def self.from_native(val, ctx)
117
+ self.new(val)
118
+ end
118
119
  end
119
120
 
120
121
  end
@@ -1,30 +1,23 @@
1
1
  #
2
+ # Copyright (C) 2009, 2010 Wayne Meissner
2
3
  # Copyright (C) 2009 Luc Heinrich
3
4
  #
4
5
  # All rights reserved.
5
6
  #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
7
+ # This file is part of ruby-ffi.
8
8
  #
9
- # * Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- # * Redistributions in binary form must reproduce the above copyright notice
12
- # this list of conditions and the following disclaimer in the documentation
13
- # and/or other materials provided with the distribution.
14
- # * Neither the name of the Evan Phoenix nor the names of its contributors
15
- # may be used to endorse or promote products derived from this software
16
- # without specific prior written permission.
9
+ # This code is free software: you can redistribute it and/or modify it under
10
+ # the terms of the GNU Lesser General Public License version 3 only, as
11
+ # published by the Free Software Foundation.
12
+ #
13
+ # This code is distributed in the hope that it will be useful, but WITHOUT
14
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16
+ # version 3 for more details.
17
+ #
18
+ # You should have received a copy of the GNU Lesser General Public License
19
+ # version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
17
20
  #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
21
 
29
22
  module FFI
30
23
 
@@ -57,6 +50,8 @@ module FFI
57
50
  end
58
51
 
59
52
  class Enum
53
+ include DataConverter
54
+
60
55
  attr_reader :tag
61
56
 
62
57
  def initialize(info, tag=nil)
@@ -99,8 +94,28 @@ module FFI
99
94
  def symbol_map
100
95
  @kv_map
101
96
  end
97
+
102
98
  alias to_h symbol_map
99
+ alias to_hash symbol_map
100
+
101
+ def native_type
102
+ Type::INT
103
+ end
104
+
105
+ def to_native(val, ctx)
106
+ @kv_map[val] || if val.is_a?(Integer)
107
+ val
108
+ elsif val.respond_to?(:to_int)
109
+ val.to_int
110
+ else
111
+ raise ArgumentError, "invalid enum value, #{val.inspect}"
112
+ end
113
+ end
114
+
115
+ def from_native(val, ctx)
116
+ @vk_map[val] || val
117
+ end
103
118
 
104
119
  end
105
120
 
106
- end
121
+ end
@@ -1,3 +1,23 @@
1
+ #
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
+ #
4
+ # All rights reserved.
5
+ #
6
+ # This file is part of ruby-ffi.
7
+ #
8
+ # This code is free software: you can redistribute it and/or modify it under
9
+ # the terms of the GNU Lesser General Public License version 3 only, as
10
+ # published by the Free Software Foundation.
11
+ #
12
+ # This code is distributed in the hope that it will be useful, but WITHOUT
13
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15
+ # version 3 for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
19
+ #
20
+
1
21
  module FFI
2
22
  def self.errno
3
23
  FFI::LastError.error