summaryrefslogtreecommitdiffstats
path: root/lib/efl/eina/eina_list.rb
blob: fdbc176815140f239644bbc1099d59510e5ca53a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
require 'efl/eina'
require 'efl/ffi/eina/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