Sha256: f7701826893a2fdf89e159177d20e1f62fe6e7361a912924b59f6d61d79fb573

Contents?: true

Size: 869 Bytes

Versions: 2

Compression:

Stored size: 869 Bytes

Contents

# frozen_string_literal: true
require 'gir_ffi/class_base'

module GirFFI
  # Base class for generated classes representing boxed types.
  class BoxedBase < ClassBase
    extend FFI::DataConverter

    def self.native_type
      FFI::Type::Struct.new(self::Struct)
    end

    def self.to_ffi_type
      self
    end

    # NOTE: Needed for JRuby's FFI
    def self.to_native(value, _context)
      value.struct
    end

    def self.get_value_from_pointer(pointer, offset)
      pointer + offset
    end

    def self.copy_value_to_pointer(value, pointer, offset = 0)
      size = self::Struct.size
      bytes = if value
                value.to_ptr.read_bytes(size)
              else
                "\x00" * size
              end
      pointer.put_bytes offset, bytes, 0, size
    end

    def initialize
      @struct = self.class::Struct.new
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gir_ffi-0.9.4 lib/gir_ffi/boxed_base.rb
gir_ffi-0.9.3 lib/gir_ffi/boxed_base.rb