Skip to content

Commit

Permalink
Move FFI::StructByReference to Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Feb 18, 2019
1 parent d1ddd66 commit 2d09ca5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 239 deletions.
184 changes: 0 additions & 184 deletions ext/ffi_c/StructByReference.c

This file was deleted.

50 changes: 0 additions & 50 deletions ext/ffi_c/StructByReference.h

This file was deleted.

8 changes: 3 additions & 5 deletions ext/ffi_c/ffi.c
Expand Up @@ -40,7 +40,6 @@
#include "MemoryPointer.h"
#include "Struct.h"
#include "StructByValue.h"
#include "StructByReference.h"
#include "DynamicLibrary.h"
#include "Platform.h"
#include "Types.h"
Expand All @@ -59,9 +58,9 @@ VALUE rbffi_FFIModule = Qnil;
static VALUE moduleFFI = Qnil;

void
Init_ffi_c(void)
Init_ffi_c(void)
{
/*
/*
* Document-module: FFI
*
* This module embbed type constants from {FFI::NativeType}.
Expand All @@ -70,7 +69,7 @@ Init_ffi_c(void)
rb_global_variable(&rbffi_FFIModule);

rbffi_Thread_Init(rbffi_FFIModule);

/* FFI::Type needs to be initialized before most other classes */
rbffi_Type_Init(moduleFFI);

Expand All @@ -86,7 +85,6 @@ Init_ffi_c(void)
rbffi_MemoryPointer_Init(moduleFFI);
rbffi_Buffer_Init(moduleFFI);
rbffi_StructByValue_Init(moduleFFI);
rbffi_StructByReference_Init(moduleFFI);
rbffi_Struct_Init(moduleFFI);
rbffi_DynamicLibrary_Init(moduleFFI);
rbffi_Variadic_Init(moduleFFI);
Expand Down
36 changes: 36 additions & 0 deletions lib/ffi/struct_by_reference.rb
Expand Up @@ -32,5 +32,41 @@ module FFI
# This class includes the {FFI::DataConverter} module.
class StructByReference
include DataConverter

attr_reader :struct_class

# @param [Struct] struct_class
def initialize(struct_class)
unless Class === struct_class and struct_class < FFI::Struct
raise TypeError, 'wrong type (expected subclass of FFI::Struct)'
end
@struct_class = struct_class
end

# Always get {FFI::Type}::POINTER.
def native_type
FFI::Type::POINTER
end

# @param [nil, Struct] value
# @param [nil] ctx
# @return [AbstractMemory] Pointer on +value+.
def to_native(value, ctx)
return Pointer::NULL if value.nil?

unless @struct_class === value
raise TypeError, "wrong argument type #{value.class} (expected #{@struct_class})"
end

value.pointer
end

# @param [AbstractMemory] value
# @param [nil] ctx
# @return [Struct]
# Create a struct from content of memory +value+.
def from_native(value, ctx)
@struct_class.new(value)
end
end
end

0 comments on commit 2d09ca5

Please sign in to comment.