Sha256: 6d141ad6258bdca5de40836a519746df0395f469444fd3f062e9e67e402d1f15

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

require 'ffi-glib/container_class_methods'

module GLib
  module ListMethods
    include Enumerable
    attr_accessor :element_type

    def self.included base
      base.extend ContainerClassMethods
      # Override default field accessors.
      replace_method base, :next, :tail
      replace_method base, :data, :head
    end

    def self.replace_method base, old, new
      base.class_eval do
        remove_method old
        alias_method old, new
      end
    end

    def each
      reset_iterator
      while (elem = next_element)
        yield elem
      end
    end

    def tail
      self.class.wrap(element_type, @struct[:next])
    end

    def head
      GirFFI::ArgHelper.cast_from_pointer(element_type, @struct[:data])
    end

    def reset_typespec typespec
      self.element_type = typespec
      self
    end

    private

    def reset_iterator
      @current = self
    end

    def next_element
      return if @current.nil?
      element = @current.head
      @current = @current.tail
      element
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gir_ffi-0.5.2 lib/ffi-glib/list_methods.rb
gir_ffi-0.5.1 lib/ffi-glib/list_methods.rb
gir_ffi-0.5.0 lib/ffi-glib/list_methods.rb
gir_ffi-0.4.3 lib/ffi-glib/list_methods.rb
gir_ffi-0.4.2 lib/ffi-glib/list_methods.rb
gir_ffi-0.4.1 lib/ffi-glib/list_methods.rb