From e19b473d4ef2b3317b061dceebef10e02e3f67d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 22 Jan 2012 17:32:09 +0100 Subject: lib/efl: takes care of ruby classes --- lib/efl.rb | 104 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/lib/efl.rb b/lib/efl.rb index 7e9482e..6f29dbd 100644 --- a/lib/efl.rb +++ b/lib/efl.rb @@ -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 -- cgit v1.1-2-g2b99