summaryrefslogtreecommitdiffstats
path: root/lib/efl/eina/eina_hash.rb
blob: 76cd5d299695540d1b728502f11882f2ba342543 (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
79
80
81
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
require 'efl/eina'
require 'efl/ffi/eina/eina_hash'
#
class Hash
    def self.from_eina_hash o
        if o.is_a? Efl::Eina::EinaHash
            o.to_h
        elsif o.is_a? ::FFI::Pointer
            Efl::Eina::EinaHash.new(o).to_h
        else
            raise ArgumentError.new " wrong argument #{o.class.name}"
        end
    end
end
module Efl
    module Eina
        #
        class EinaHash
            include Efl::Helper
            include Enumerable
            @func_prefixes = [ 'eina_hash_' ].freeze
            def initialize o=nil, &block
                cstr = ( block_given? ? block : Proc.new { Efl::FFI.eina_hash_string_djb2_new ::FFI::Pointer::NULL } )
                @ptr = (
                    case o
                    when ::FFI::Pointer
                        ( o==::FFI::Pointer::NULL ? cstr.call : o )
                    when NilClass
                        cstr.call
                    when self.class
                        o.ptr
                    when Hash
                        ptr = cstr.call
                        o.each do |k,v| Efl::FFI.eina_hash_add ptr, k, v end
                        ptr
                    else
                        raise ArgumentError.new "#{ptr.class} valid argument"
                    end
                )
            end
            def free
                return if @ptr==::FFI::Pointer::NULL
                @ptr = Efl::FFI.eina_hash_free @ptr
            end
            def each data=::FFI::Pointer::NULL, &block
                return if not block_given?
                Efl::FFI::eina_hash_foreach @ptr, Proc.new{|h,k,v,d| block.call(k,v) }, data
            end
            def to_h
                 rh = {}
                 each { |k,v| rh[k]=v; true }
                 rh
            end
            def to_h_conv &block
                 rh = {}
                 if block_given?
                    each { |k,v| rh[block.call(k)]=v; true }
                 else
                    each { |k,v| rh[k.read_string]=v; true }
                 end
                 rh
            end
            # for fun and tests
            def add k, v
                Efl::FFI.eina_hash_add @ptr, k, v
                v
            end
            alias :[]= :add
            def find k
                r = Efl::FFI.eina_hash_find @ptr, k
                return ( r==::FFI::Pointer::NULL ? nil : r )
            end
            alias :[] :find
        end
    end
end
#
# EOF