Class: Efl::EinaHash::REinaHash

Inherits:
Object
  • Object
show all
Includes:
ClassHelper, Enumerable
Defined in:
lib/efl/eina_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassHelper

#===, #address, included, #method_missing, #null?, #to_a, #to_ary, #to_ptr, #to_s

Constructor Details

#initialize(o = nil, &block) ⇒ REinaHash

Returns a new instance of REinaHash



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/efl/eina_hash.rb', line 14

def initialize o=nil, &block
    cstr = ( block_given? ? block : Proc.new { Native.eina_hash_string_djb2_new FFI::Pointer::NULL } )
    @ptr = (
        case o
        when NilClass
            FFI::AutoPointer.new cstr.call, REinaHash.method(:release)
        when FFI::Pointer
            FFI::AutoPointer.new( (o==FFI::Pointer::NULL ? cstr.call : o), REinaHash.method(:release))
        when Hash
            ptr = cstr.call
            o.each do |k,v| Native.eina_hash_add ptr, k, v end
            FFI::AutoPointer.new ptr, REinaHash.method(:release)
        else
            raise ArgumentError.new "wrong argument #{o.class.name}"
        end
    )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Efl::ClassHelper

Class Method Details

.release(p) ⇒ Object



31
32
33
# File 'lib/efl/eina_hash.rb', line 31

def self.release p
    Native.eina_hash_free p
end

Instance Method Details

#add(k, v) ⇒ Object Also known as: []=

for fun and tests



58
59
60
61
# File 'lib/efl/eina_hash.rb', line 58

def add k, v
    Native.eina_hash_add @ptr, k, v
    v
end

#delObject



34
35
36
37
38
# File 'lib/efl/eina_hash.rb', line 34

def del
    @ptr.autorelease=false
    EinaHash.release @ptr
    @ptr=nil
end

#each(data = FFI::Pointer::NULL, &block) ⇒ Object



39
40
41
42
# File 'lib/efl/eina_hash.rb', line 39

def each data=FFI::Pointer::NULL, &block
    return if not block_given?
    Native.eina_hash_foreach @ptr, Proc.new{|h,k,v,d| block.call(k,v) }, data
end

#find(k) ⇒ Object Also known as: []



63
64
65
66
# File 'lib/efl/eina_hash.rb', line 63

def find k
    r = Native.eina_hash_find @ptr, k
    return ( r==FFI::Pointer::NULL ? nil : r )
end

#to_hObject



43
44
45
46
47
# File 'lib/efl/eina_hash.rb', line 43

def to_h
     rh = {}
     each { |k,v| rh[k]=v; true }
     rh
end

#to_h_conv(&block) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/efl/eina_hash.rb', line 48

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