summaryrefslogtreecommitdiffstats
path: root/lib/efl/eina_list.rb
blob: ba2bde3ce6f6c4c99d405b8afda632c0517df9a6 (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
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
require 'efl/native/eina_list'
#
module Efl
    #
    module Native
        #
        class EinaListStruct < FFI::Struct
            layout  :data,          :pointer,
                    :next,          :pointer,
                    :prev,          :pointer,
                    :accounting,    :pointer,
                    :magic,         :uint
        end
    end
    #
    module EinaList
        #
        class REinaList
            include Enumerable
            include Efl::ClassHelper
            search_prefixes 'eina_list_'
            def initialize o=nil
                @ptr = (
                    case o
                    when FFI::Pointer
                        o
                    when NilClass
                        FFI::Pointer::NULL
                    when Array
                        o.inject(FFI::Pointer::NULL) { |p,e| Native.eina_list_append p, e }
                    else
                        raise ArgumentError.new "wrong argument #{o.class.name}"
                    end
                )
            end
            def free
                Native.eina_list_free @ptr
                @ptr = nil
            end
            def self.from_a ary, ptrt
                REinaList.new ary.inject(FFI::Pointer::NULL) { |p,e|
                    ptr = FFI::MemoryPointer.new ptrt
                    ptr.send 'write_'+ptrt.to_s, e
                    Native.eina_list_append p, ptr
                }
            end
            def each
                return if not block_given?
                p = @ptr
                while p!=::FFI::Pointer::NULL
                    l = Native::EinaListStruct.new p
                    yield l[:data]
                    p = l[:next]
                end
            end
            def to_a ptrt=nil
                return inject([]) { |s,e| s<<e } if ptrt.nil?
            end
            alias :to_ary :to_a
            # for fun and tests
            def append p
                @ptr = Native.eina_list_append @ptr, p
            end
            alias :<< :append
            def prepend p
                @ptr = Native.eina_list_prepend @ptr, p
            end
            alias :unshift :prepend
        end
    end
end
#
# EOF