Sha256: f75c131575672e6487ef63ba8e4aad321a3934a879b7f019a69aaa1c17844188

Contents?: true

Size: 1.67 KB

Versions: 5

Compression:

Stored size: 1.67 KB

Contents

require 'forwardable'
module GirFFI
  # Base class for all generated classes. Contains code for dealing with
  # the generated Struct classes.
  class ClassBase
    # TODO: Make separate base for :struct, :union, :object.
    extend Forwardable
    def_delegators :@struct, :[], :[]=, :to_ptr

    def initialize(*args)
      @struct = ffi_structure.new(*args)
    end

    def ffi_structure
      self.class.ffi_structure
    end

    def _builder
      self.class._builder
    end

    def setup_and_call method, *arguments, &block
      unless _builder.setup_instance_method method.to_s
        raise RuntimeError, "Unable to set up instance method #{method} in #{self}"
      end
      self.send method, *arguments, &block
    end

    def self.setup_and_call method, *arguments, &block
      unless _builder.setup_method method.to_s
        raise RuntimeError, "Unable to set up method #{method} in #{self}"
      end
      self.send method, *arguments, &block
    end

    class << self
      def ffi_structure
	self.const_get(:Struct)
      end

      def gir_info
	self.const_get :GIR_INFO
      end

      def _builder
	self.const_get :GIR_FFI_BUILDER
      end

      def _find_signal name
        _builder.find_signal name
      end

      def _setup_method name
        _builder.setup_method name
      end

      def _setup_instance_method name
        _builder.setup_instance_method name
      end

      alias_method :_real_new, :new
      undef new

      def wrap ptr
	return nil if ptr.nil? or ptr.null?
	_real_new ptr
      end

      # TODO: Only makes sense for :objects.
      def constructor_wrap ptr
        wrap ptr
      end

      def allocate
	_real_new
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gir_ffi-0.1.0 lib/gir_ffi/class_base.rb
gir_ffi-0.0.14 lib/gir_ffi/class_base.rb
gir_ffi-0.0.13 lib/gir_ffi/class_base.rb
gir_ffi-0.0.12 lib/gir_ffi/class_base.rb
gir_ffi-0.0.11 lib/gir_ffi/class_base.rb