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

- (REinaHash) initialize(o = nil, &block)



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

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

+ (Object) release(p)



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

def self.release p
    Native.eina_hash_free p
end

Instance Method Details

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

for fun and tests



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

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

- (Object) del



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

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

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



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

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

- (Object) find(k) Also known as: []



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

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

- (Object) to_h



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

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

- (Object) to_h_conv(&block)



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

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