diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2012-01-22 17:32:09 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2012-01-22 17:32:09 +0100 |
commit | e19b473d4ef2b3317b061dceebef10e02e3f67d0 (patch) | |
tree | f944a452d4cc690e6a1fd2d4f9f2e1b2c602c2b7 | |
parent | 7c66d97b65d886d6375e7a7477d61cdf112872ab (diff) | |
download | ffi-efl-e19b473d4ef2b3317b061dceebef10e02e3f67d0.zip ffi-efl-e19b473d4ef2b3317b061dceebef10e02e3f67d0.tar.gz |
lib/efl: takes care of ruby classes
-rw-r--r-- | lib/efl.rb | 104 |
1 files changed, 58 insertions, 46 deletions
@@ -1,56 +1,11 @@ #! /usr/bin/env ruby # -*- coding: UTF-8 -*- # -require 'ffi' -# module Efl # VERSION = '0.0.10' # - module Native - # - extend FFI::Library - # - def self.attach_fcts fcts - fcts.each do |func| - begin - attach_function(*func) - rescue Object => e - puts "Could not attach #{func} #{e.message}" - end - end - end - # - class << self - # keep - alias :ffi_lib_orig :ffi_lib - def ffi_lib *names - @all_ffi_libs||=[] - @all_ffi_libs += ffi_lib_orig(names) - @all_ffi_libs.uniq! - end - def find_variable name - @all_ffi_libs.each do |lib| - address = lib.find_variable name - return address if not address.nil? - end - return nil - end - end - # - class VersionStruct < FFI::Struct - layout :major, :int, - :minor, :int, - :micro, :int, - :revision, :int - - def full - [:major,:minor,:micro,:revision].collect { |e| self[e].to_s }.join '.' - end - end - end - # - module MethResolver + module MethodResolver def self.resolve mod, meth, prefix meth_s = meth.to_s if meth_s =~/^(.*)=$/ @@ -79,6 +34,63 @@ module Efl sym end end + # + module ClassHelper + def to_a; [self] end + def to_ary; [self] end + def to_s; Object.instance_method(:to_s).bind(self).call+' ['+self.to_ptr.to_s+']' end + def to_ptr; @ptr; end + def === o; @ptr === o.to_ptr; end + def null?; @ptr.null?; end + def address; @ptr.address; end + def self.included kls + # create class instance @search_prefixes + kls.class_eval "@search_prefixes ||=[]" + # access and prepend *args to @search_prefixes + kls.class_eval "def self.search_prefixes *args; @search_prefixes.unshift *args unless args.empty?; @search_prefixes; end" + # on inheritance, copy ancestor's @search_prefixes + kls.class_eval <<-EOF + def self.inherited sub + sub.class_eval '@search_prefixes = []' + sub.search_prefixes *self.search_prefixes + end + EOF + end + def method_missing m, *args, &block + m_s = m.to_s + if m_s =~/^(.*)=$/ + m_s = $1+'_set' + args_s = '*args[0]' + elsif m_s =~/^(.*)\?$/ + m_s = $1+'_get' + args_s = '*args' + else + args_s = '*args' + end + (['']+self.class.search_prefixes).each do |p| + sym = p+m_s + meth = ( + if Efl::Native.respond_to? sym + sym + elsif Efl::Native.respond_to? m_s + m_s + elsif Efl::Native.respond_to? sym+'_get' + sym+'_get' + elsif Efl::Native.respond_to? m_s+'_get' + m_s+'_get' + else + nil + end + ) + if not meth.nil? + self.class.class_eval "def #{m} *args, █ r=Efl::Native.#{meth}(@ptr,#{args_s}); yield r if block_given?; r; end" + return self.send m, *args, &block + end + end + Kernel.raise NameError.new "#{self.class.name} is unable to resolve #{m} within #{self.class.search_prefixes.inspect}" + end + end + # end # # EOF |