lib/ffi/struct_wrapper.rb in ffi-libfuse-0.0.1.rctest12 vs lib/ffi/struct_wrapper.rb in ffi-libfuse-0.1.0.rc20220550

- old
+ new

@@ -3,10 +3,20 @@ require 'ffi' require_relative 'accessors' module FFI # Helper to wrap structs with ugly names and attribute clashes with FFI::Struct (eg size) + # + # @example + # class MyStruct + # include FFI::StructWrapper + # native_struct(MyNativeStruct) + # + # #!@attribute [rw] field + # ffi_attr_accessor :field + # end + # module StructWrapper # @!visibility private class ByReference < StructByReference def initialize(wrapper_class) super(wrapper_class.native_struct) @@ -76,12 +86,13 @@ end end # @!parse extend ClassMethods # @!parse include Accessors + # @!parse extend Accessors::ClassMethods - # @!visibility private + # @return [FFI::Struct] the underlying native struct attr_reader :native # @!visibility private def initialize(native = self.class.native_struct.new) @native = native @@ -93,8 +104,18 @@ end # Set attribute def []=(member_or_attr, val) @native[self.class.ffi_attr_writers.fetch(member_or_attr, member_or_attr)] = val + end + + # Pass unimplemented methods on to {#native} underlying struct + def method_missing(method, *args) + @native.send(method, *args) + end + + # @!visibility private + def respond_to_missing?(method, private = false) + @native.respond_to?(method, private) end end end