diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2011-04-28 15:09:41 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2011-04-28 15:09:41 +0200 |
commit | 19324a30a17c6ea0b3d77d34160841ef90ce3ed1 (patch) | |
tree | a795d324e7822e433dcd66a4057f69d4ecc20d86 /lib/efl/eina/eina_list.rb | |
parent | 0035feb9b4f29f06ffd430b763534f40604af4a9 (diff) | |
download | ffi-efl-19324a30a17c6ea0b3d77d34160841ef90ce3ed1.zip ffi-efl-19324a30a17c6ea0b3d77d34160841ef90ce3ed1.tar.gz |
set hierarchy n lib and spec directories
Diffstat (limited to 'lib/efl/eina/eina_list.rb')
-rw-r--r-- | lib/efl/eina/eina_list.rb | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/efl/eina/eina_list.rb b/lib/efl/eina/eina_list.rb new file mode 100644 index 0000000..823fe72 --- /dev/null +++ b/lib/efl/eina/eina_list.rb @@ -0,0 +1,78 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +require 'efl/eina' +require 'efl/ffi/eina_list' +# +class Array + def self.from_eina_list o + if o.is_a? Efl::Eina::EinaList + o.to_ary + elsif o.is_a? ::FFI::Pointer + Efl::Eina::EinaList.new(o).to_ary + else + raise ArgumentError.new " wrong argument #{o.class.name}" + end + end +end +module Efl + module FFI + class EinaList < ::FFI::Struct + layout :data, :pointer, + :next, :pointer, + :prev, :pointer, + :accounting, :pointer, + :magic, :uint, + end + end + module Eina + # + class EinaList + include Efl::Helper + include Enumerable + @func_prefixes = [ 'eina_list_' ].freeze + def initialize o=nil + @ptr = ( + case o + when ::FFI::Pointer + o + when NilClass + ::FFI::Pointer::NULL + when self.class + o.ptr + when Array + o.inject(::FFI::Pointer::NULL) { |p,e| Efl::FFI::eina_list_append p, e } + else + raise ArgumentError.new "#{ptr.class} valid argument" + end + ) + end + def free + return if @ptr==::FFI::Pointer::NULL + @ptr = Efl::FFI.eina_list_free @ptr + end + def each + p = @ptr + while p!=::FFI::Pointer::NULL + l = Efl::FFI::EinaList.new p + yield l[:data] + p = l[:next] + end + end + def to_ary + inject([]) { |s,e| s<<e } + end + # for fun and tests + def append p + @ptr = Efl::FFI.eina_list_append @ptr, p + end + alias :<< :append + def prepend p + @ptr = Efl::FFI.eina_list_prepend @ptr, p + end + alias :unshift :prepend + end + end +end +# +# EOF |