Sha256: 069a2d37e65b74c3c71d8577bfffb22a56aa4fc01ef85cefc523bd759ebc5a26

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require 'forwardable'
module GirFFI
  # Base class for all generated classes. Contains code for dealing with
  # the generated Struct classes.
  class ClassBase
    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 gir_ffi_builder
      self.class.gir_ffi_builder
    end

    def setup_and_call method, *arguments, &block
      result = gir_ffi_builder.setup_instance_method method.to_s
      return super unless result
      self.send method, *arguments, &block
    end

    def self.setup_and_call method, *arguments, &block
      result = gir_ffi_builder.setup_method method.to_s
      return super unless result
      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 gir_ffi_builder
	self.const_get :GIR_FFI_BUILDER
      end

      alias_method :_real_new, :new
      undef new

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

      def allocate
	_real_new
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gir_ffi-0.0.6 lib/gir_ffi/class_base.rb
gir_ffi-0.0.5 lib/gir_ffi/class_base.rb