Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move FFI::StructByReference to Ruby #681

Merged
merged 3 commits into from Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
71 changes: 3 additions & 68 deletions lib/ffi/struct.rb
Expand Up @@ -32,72 +32,12 @@
#

require 'ffi/platform'
require 'ffi/struct_layout'
require 'ffi/struct_layout_builder'
require 'ffi/struct_by_reference'

module FFI

class StructLayout

# @return [Array<Array(Symbol, Numeric)>
# Get an array of tuples (field name, offset of the field).
def offsets
members.map { |m| [ m, self[m].offset ] }
end

# @return [Numeric]
# Get the offset of a field.
def offset_of(field_name)
self[field_name].offset
end

# An enum {Field} in a {StructLayout}.
class Enum < Field

# @param [AbstractMemory] ptr pointer on a {Struct}
# @return [Object]
# Get an object of type {#type} from memory pointed by +ptr+.
def get(ptr)
type.find(ptr.get_int(offset))
end

# @param [AbstractMemory] ptr pointer on a {Struct}
# @param value
# @return [nil]
# Set +value+ into memory pointed by +ptr+.
def put(ptr, value)
ptr.put_int(offset, type.find(value))
end

end

class InnerStruct < Field
def get(ptr)
type.struct_class.new(ptr.slice(self.offset, self.size))
end

def put(ptr, value)
raise TypeError, "wrong value type (expected #{type.struct_class})" unless value.is_a?(type.struct_class)
ptr.slice(self.offset, self.size).__copy_from__(value.pointer, self.size)
end
end

class Mapped < Field
def initialize(name, offset, type, orig_field)
super(name, offset, type)
@orig_field = orig_field
end

def get(ptr)
type.from_native(@orig_field.get(ptr), nil)
end

def put(ptr, value)
@orig_field.put(ptr, type.to_native(value, nil))
end
end
end


class Struct

# Get struct size
Expand Down Expand Up @@ -296,7 +236,7 @@ def packed(packed = 1)
@packed = packed
end
alias :pack :packed

def aligned(alignment = 1)
@min_alignment = alignment
end
Expand Down Expand Up @@ -368,9 +308,4 @@ def array_layout(builder, spec)
end
end
end

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