Sha256: 8b884ff27f1bda8047b9ecf73e3fbae167942cafa0b8c18c8815bfca882e84de

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true
module GirFFI
  # Base module providing class methods for generated classes representing GLib
  # structs, unions and boxed types.
  module StructLikeBase
    def native_type
      FFI::Type::Struct.new(self::Struct)
    end

    def to_ffi_type
      self
    end

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

    def get_value_from_pointer(pointer, offset)
      pointer + offset
    end

    def size
      self::Struct.size
    end

    def 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

    # Create an unowned copy of the struct represented by val
    def copy_from(val)
      disown copy from(val)
    end

    # Wrap an owned copy of the struct represented by val
    def wrap_copy(val)
      own copy wrap(val)
    end

    def own(val)
      val.struct.owned = true if val
      val
    end

    def disown(val)
      val.struct.owned = false if val
      val
    end

    # Create a copy of the struct represented by val
    def copy(val)
      return unless val
      new.tap do |copy|
        copy_value_to_pointer(val, copy.to_ptr)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gir_ffi-0.10.2 lib/gir_ffi/struct_like_base.rb
gir_ffi-0.10.1 lib/gir_ffi/struct_like_base.rb