Class: Qt::Variant

Inherits:
Object show all
Defined in:
lib/utilrb/qt/variant/from_ruby.rb

Constant Summary collapse

@@saved_values =

This is a mapping from a QVariant object_id to the object it is supposed to hold. An entry gets removed as soon as the QVariant is finalized

Hash.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_ruby(obj, lifetime_object = nil) ⇒ Object


8
9
10
11
12
13
14
15
# File 'lib/utilrb/qt/variant/from_ruby.rb', line 8

def self.from_ruby(obj, lifetime_object = nil)
    variant = Qt::Variant.new("__##{obj.object_id}#__")
    lifetime_object ||= variant
    ObjectSpace.define_finalizer lifetime_object, from_ruby_finalizer
    @@saved_values[lifetime_object.object_id] ||= Set.new
    @@saved_values[lifetime_object.object_id] << obj
    variant
end

.from_ruby_finalizerObject


17
18
19
# File 'lib/utilrb/qt/variant/from_ruby.rb', line 17

def self.from_ruby_finalizer
    lambda { |variant_id| @@saved_values.delete(variant_id) }
end

Instance Method Details

#to_rubyObject


21
22
23
24
# File 'lib/utilrb/qt/variant/from_ruby.rb', line 21

def to_ruby
    raise "QVariant is not storing an Object ID"if (value =~ /__#(\d*)#__/) != 0
    ObjectSpace._id2ref(Integer($1))
end

#to_ruby?Boolean


26
27
28
# File 'lib/utilrb/qt/variant/from_ruby.rb', line 26

def to_ruby?
    (value =~ /__#(\d*)#__/) == 0
end