Module: Rbind::Typelib
- Defined in:
- lib/rbind/typelib.rb
Class Attribute Summary collapse
-
.registry ⇒ Typelib::Registry
The typelib registry that should be used by Typelib.to_typelib.
Class Method Summary collapse
-
.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). -
.to_typelib(klass, rbind, typename) ⇒ Typelib::Type?
Converts
rbind
into a Typelib value.
Class Attribute Details
.registry ⇒ Typelib::Registry
Returns 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)
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
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 |