Sha256: e41d336b57bd8f0825bd8db04e3e6623c372510e93a2d3a522ab72acae5efce0

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module GLib
  load_class :PtrArray

  # Overrides for GPtrArray, GLib's automatically growing array of
  # pointers.
  class PtrArray
    include Enumerable

    attr_accessor :element_type

    class << self
      remove_method :new
      # Remove stub generated by builder.
      remove_method :add if method_defined? :add
    end

    def self.new type
      wrap(type, Lib.g_ptr_array_new)
    end

    def self.wrap type, ptr
      super(ptr).tap {|it|
        it.element_type = type}
    end

    def self.from type, it
      case it
      when self then it
      when FFI::Pointer then wrap type, it
      else self.new(type).tap {|arr| arr.add_array it}
      end
    end

    def self.add array, data
      array.add data
    end

    def add data
      ptr = GirFFI::InPointer.from element_type, data
      Lib.g_ptr_array_add self, ptr
    end

    def add_array ary
      ary.each {|item| add item}
    end

    # Re-implementation of the g_ptr_array_index macro
    def index idx
      sz = FFI.type_size :pointer
      ptr = @struct[:pdata].get_pointer(idx * sz)
      GirFFI::ArgHelper.cast_from_pointer(element_type, ptr)
    end

    def each
      @struct[:len].times.each do |idx|
        yield index(idx)
      end
    end

    def ==(other)
      self.to_a == other.to_a
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gir_ffi-0.6.6 lib/ffi-glib/ptr_array.rb
gir_ffi-0.6.5 lib/ffi-glib/ptr_array.rb