Sha256: 38be4cd4c2e886d92ff9a4cccc824571ef84bf76e6ba3f5455b63b4c5662fef1

Contents?: true

Size: 989 Bytes

Versions: 3

Compression:

Stored size: 989 Bytes

Contents

module GLib
  load_class :Array

  # Overrides for GArray, GLib's automatically growing array. It should not
  # be necessary to create objects of this class from Ruby directly.
  class Array
    include Enumerable

    attr_accessor :element_type

    class << self
      undef :new
      def new type
        ffi_type = GirFFI::TypeMap.map_basic_type_or_string(type)
        wrap(Lib.g_array_new(0, 0, FFI.type_size(ffi_type))).tap {|it|
          it.element_type = type}
      end
    end

    def append_vals data
      bytes = GirFFI::InPointer.from_array element_type, data
      len = data.length
      Lib.g_array_append_vals(self, bytes, len)
      self
    end

    # FIXME: Make GirFII::InPointer support #each and use that.
    def each &block
      to_typed_array.each(&block)
    end

    private

    def to_typed_array
      GirFFI::ArgHelper.ptr_to_typed_array(self.element_type,
                                           @struct[:data], @struct[:len])
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gir_ffi-0.2.2 lib/ffi-glib/array.rb
gir_ffi-0.2.1 lib/ffi-glib/array.rb
gir_ffi-0.2.0 lib/ffi-glib/array.rb