Module: Rbind::Typelib

Defined in:
lib/rbind/typelib.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registryTypelib::Registry

Returns The typelib registry that should be used by Typelib.to_typelib.

Returns:

  • (Typelib::Registry)

    The typelib registry that should be used by Typelib.to_typelib



6
7
8
# File 'lib/rbind/typelib.rb', line 6

def registry
  @registry
end

Class Method Details

.from_typelib(klass, obj, *typenames) ⇒ FFI::Struct?

Converts obj into the FFI::Struct that rbind uses to initialize its Ruby types (one of the XXXXStruct types generated by Rbind)

Parameters:

  • klass (Class<FFI::Struct>)

    the FFI::Struct type

  • obj (Object)

    the object to be converted

  • typenames (Array<String>)

    the Typelib type names that are known to be equivalents of the FFI::Struct represented by klass

Returns:

  • (FFI::Struct, nil)

    the converted struct or nil if the obj argument is not a typelib type

Raises:

  • ArgumentError if ‘obj’ is a typelib type, but not of one of the expected typenames



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rbind/typelib.rb', line 48

def self.from_typelib(klass, obj, *typenames)
    typelib_value = nil
    typenames.each do |tn|
        target_t = registry.build(tn)
        begin
            typelib_value = ::Typelib.from_ruby(obj, target_t)
        rescue ArgumentError
        end
    end
    if typelib_value
        s = klass.rbind_struct.new
        s[:obj_ptr]  = typelib_value.to_memory_ptr.zone_address
        s[:bowner] = false
        s[:size] = 0 #obj.marshalling_size
        s[:type_id] = nil
        s[:version] = 1
        s.instance_variable_set :@__typelib_original_value__, typelib_value
        s
    elsif obj.kind_of?(::Typelib::Type)
        raise ArgumentError, "#{obj} is a typelib type, but I don't know how to convert it to #{klass}. Expected one of #{typenames.sort.join(", ")}"
    end
end

.to_typelib(klass, rbind, typename) ⇒ Typelib::Type?

Converts rbind into a Typelib value

Parameters:

  • rbind (Object)

    the Rbind-generated representation of the type. It has to own the underlying memory

  • typename (String)

    the Typelib type name into which this type should be represented

Returns:

  • (Typelib::Type, nil)

    the converted value or nil if Typelib.registry does not contain the requested type



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rbind/typelib.rb', line 18

def self.to_typelib(klass, rbind, typename)
    if rbind.kind_of?(FFI::Pointer)
        rbind = klass.rbind_struct.new(rbind)
    end
    if rbind.kind_of?(klass.rbind_struct)
        if !rbind[:bowner]
            raise ArgumentError, "cannot convert #{rbind}: it does not own the underlying memory"
        end
        if registry.include?(typename)
            rbind[:bowner] = false
            typelib_t = registry.get(typename)
            typelib_value = ::Typelib.to_ruby(typelib_t.from_address(rbind[:obj_ptr].address))
            rbind.instance_variable_set :@__typelib_original_value__, typelib_value
            typelib_value
        end
    end

end