Skip to content

Commit

Permalink
Move FFI::DataConverter to Ruby
Browse files Browse the repository at this point in the history
* Include DataConverter in StructByReference in Ruby code to simplify
  ordering.
  • Loading branch information
eregon committed Jan 7, 2019
1 parent d976d4a commit 7864b82
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 103 deletions.
91 changes: 0 additions & 91 deletions ext/ffi_c/DataConverter.c

This file was deleted.

8 changes: 1 addition & 7 deletions ext/ffi_c/StructByReference.c
Expand Up @@ -172,19 +172,13 @@ sbr_from_native(VALUE self, VALUE value, VALUE ctx)
void
rbffi_StructByReference_Init(VALUE moduleFFI)
{
/*
* Document-class: FFI::StructByReference
* This class includes {FFI::DataConverter} module.
*/
rbffi_StructByReferenceClass = rb_define_class_under(moduleFFI, "StructByReference", rb_cObject);
rb_global_variable(&rbffi_StructByReferenceClass);
rb_include_module(rbffi_StructByReferenceClass, rb_const_get(moduleFFI, rb_intern("DataConverter")));


rb_define_alloc_func(rbffi_StructByReferenceClass, sbr_allocate);
rb_define_method(rbffi_StructByReferenceClass, "initialize", sbr_initialize, 1);
rb_define_method(rbffi_StructByReferenceClass, "struct_class", sbr_struct_class, 0);
rb_define_method(rbffi_StructByReferenceClass, "native_type", sbr_native_type, 0);
rb_define_method(rbffi_StructByReferenceClass, "to_native", sbr_to_native, 2);
rb_define_method(rbffi_StructByReferenceClass, "from_native", sbr_from_native, 2);
}

3 changes: 0 additions & 3 deletions ext/ffi_c/ffi.c
Expand Up @@ -74,8 +74,6 @@ Init_ffi_c(void)
/* FFI::Type needs to be initialized before most other classes */
rbffi_Type_Init(moduleFFI);

rbffi_DataConverter_Init(moduleFFI);

rbffi_ArrayType_Init(moduleFFI);
rbffi_LastError_Init(moduleFFI);
rbffi_Call_Init(moduleFFI);
Expand All @@ -95,4 +93,3 @@ Init_ffi_c(void)
rbffi_Types_Init(moduleFFI);
rbffi_MappedType_Init(moduleFFI);
}

2 changes: 0 additions & 2 deletions ext/ffi_c/rbffi.h
Expand Up @@ -44,7 +44,6 @@ extern void rbffi_Type_Init(VALUE ffiModule);
extern void rbffi_Buffer_Init(VALUE ffiModule);
extern void rbffi_Invoker_Init(VALUE ffiModule);
extern void rbffi_Variadic_Init(VALUE ffiModule);
extern void rbffi_DataConverter_Init(VALUE ffiModule);
extern VALUE rbffi_AbstractMemoryClass, rbffi_InvokerClass;
extern int rbffi_type_size(VALUE type);
extern void rbffi_Thread_Init(VALUE moduleFFI);
Expand All @@ -54,4 +53,3 @@ extern void rbffi_Thread_Init(VALUE moduleFFI);
#endif

#endif /* RBFFI_RBFFI_H */

37 changes: 37 additions & 0 deletions lib/ffi/data_converter.rb
@@ -0,0 +1,37 @@
module FFI
# This module is used to extend somes classes and give then a common API.
#
# Most of methods defined here must be overriden.
module DataConverter
# Get native type.
#
# @overload native_type(type)
# @param [String, Symbol, Type] type
# @return [Type]
# Get native type from +type+.
#
# @overload native_type
# @raise {NotImplementedError} This method must be overriden.
def native_type(type = nil)
if type
@native_type = FFI.find_type(type)
else
native_type = @native_type
unless native_type
raise NotImplementedError, 'native_type method not overridden and no native_type set'
end
native_type
end
end

# Convert to a native type.
def to_native(value, ctx)
value
end

# Convert from a native type.
def from_native(value, ctx)
value
end
end
end
1 change: 1 addition & 0 deletions lib/ffi/ffi.rb
Expand Up @@ -29,6 +29,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

require 'ffi/platform'
require 'ffi/data_converter'
require 'ffi/types'
require 'ffi/library'
require 'ffi/errno'
Expand Down
5 changes: 5 additions & 0 deletions lib/ffi/struct.rb
Expand Up @@ -368,4 +368,9 @@ def array_layout(builder, spec)
end
end
end

# This class includes the {FFI::DataConverter} module.
class StructByReference
include DataConverter
end
end

0 comments on commit 7864b82

Please sign in to comment.