Sha256: bd5b4920e76d6969b97662329a35d373fca94d659008c94db584b1d69ad6da84

Contents?: true

Size: 700 Bytes

Versions: 5

Compression:

Stored size: 700 Bytes

Contents

module GirFFI
  # Represents a null-terminated array.
  class ZeroTerminated
    include Enumerable

    attr_reader :element_type

    def initialize elm_t, ptr
      @element_type = elm_t
      @ptr = ptr
    end

    def to_ptr
      @ptr
    end

    def self.from type, arg
      self.new type, InPointer.from_array(type, arg)
    end

    def self.wrap type, arg
      self.new type, arg
    end

    def each
      return if @ptr.null?
      offset = 0
      while val = read_value(offset)
        offset += FFI.type_size(:int32)
        yield val
      end
    end

    private

    def read_value offset
      val = @ptr.get_int32(offset)
      return val unless val == 0
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gir_ffi-0.6.3 lib/gir_ffi/zero_terminated.rb
gir_ffi-0.6.2 lib/gir_ffi/zero_terminated.rb
gir_ffi-0.6.1 lib/gir_ffi/zero_terminated.rb
gir_ffi-0.6.0 lib/gir_ffi/zero_terminated.rb
gir_ffi-0.5.2 lib/gir_ffi/zero_terminated.rb