Sha256: 0d8105033c2052d51cdca36f2b10ee8dfa3d438f706c4e409bfd32e467f6e6f6

Contents?: true

Size: 770 Bytes

Versions: 5

Compression:

Stored size: 770 Bytes

Contents

# frozen_string_literal: true
require 'ffi'
require 'gir_ffi-base/glib'

module GLib
  # Represents a null-terminated array of strings. GLib uses this
  # construction, but does not provide any actual functions for this class.
  class Strv
    include Enumerable

    POINTER_SIZE = FFI.type_size(:pointer)

    def initialize(ptr)
      @ptr = ptr
    end

    def to_ptr
      @ptr
    end

    def each
      return if @ptr.null?
      reset_iterator
      while (ptr = next_ptr)
        yield ptr.read_string
      end
    end

    def self.wrap(ptr)
      new ptr
    end

    private

    def reset_iterator
      @offset = 0
    end

    def next_ptr
      ptr = @ptr.get_pointer @offset
      @offset += POINTER_SIZE
      ptr unless ptr.null?
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gir_ffi-0.10.2 lib/gir_ffi-base/glib/strv.rb
gir_ffi-0.10.1 lib/gir_ffi-base/glib/strv.rb
gir_ffi-0.10.0 lib/gir_ffi-base/glib/strv.rb
gir_ffi-0.10.0.pre1 lib/gir_ffi-base/glib/strv.rb
gir_ffi-0.9.5 lib/gir_ffi-base/glib/strv.rb