Class: Wref
- Inherits:
-
Object
- Object
- Wref
- Defined in:
- lib/wref.rb
Overview
A simple weak-reference framework with mapping. Only handles the referencing of objects.
Examples
user_obj = ob.get(:User, 1) weak_ref = Wref.new(user_obj) user_obj = nil sleep 0.5 GC.start
begin
user_obj = weak_ref.get
print "The user still exists in memory and has ID #{user.id}."
rescue Wref::Recycled
print "The user has been removed from memory."
end
Defined Under Namespace
Classes: Recycled
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the classname of the object.
-
#id ⇒ Object
readonly
Returns the object-ID which is used to look up the ObjectSpace (if not running JRuby).
Instance Method Summary collapse
-
#alive? ⇒ Boolean
(also: #weakref_alive?)
Returns true if the reference is still alive.
-
#destroy(*args) ⇒ Object
Destroyes most variables on the object, releasing memory and returning ‘Wref::Recycled’ all the time.
-
#get ⇒ Object
(also: #__getobj__)
Returns the object that this weak reference holds or raises Wref::Recycled.
-
#initialize(obj) ⇒ Wref
constructor
Initializes various variables.
Constructor Details
#initialize(obj) ⇒ Wref
Initializes various variables.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/wref.rb', line 25 def initialize(obj) if RUBY_ENGINE == "jruby" @weakref = java.lang.ref.WeakReference.new(obj) else @id = obj.__id__ @class_name = obj.class.name.to_sym ObjectSpace.define_finalizer(obj, self.method(:destroy)) if obj.respond_to?(:__object_unique_id__) @unique_id = obj.__object_unique_id__ end end end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the classname of the object.
19 20 21 |
# File 'lib/wref.rb', line 19 def class_name @class_name end |
#id ⇒ Object (readonly)
Returns the object-ID which is used to look up the ObjectSpace (if not running JRuby).
22 23 24 |
# File 'lib/wref.rb', line 22 def id @id end |
Instance Method Details
#alive? ⇒ Boolean Also known as: weakref_alive?
Returns true if the reference is still alive. print “The object still exists in memory.” if wref.alive?
89 90 91 92 93 94 95 96 |
# File 'lib/wref.rb', line 89 def alive? begin self.get return true rescue Wref::Recycled return false end end |
#destroy(*args) ⇒ Object
Destroyes most variables on the object, releasing memory and returning ‘Wref::Recycled’ all the time. It takes arguments because it can be called from destructor of the original object. It doesnt use the arguments for anything.
40 41 42 43 44 45 |
# File 'lib/wref.rb', line 40 def destroy(*args) @id = nil @class_name = nil @unique_id = nil @weakref = nil end |
#get ⇒ Object Also known as: __getobj__
Returns the object that this weak reference holds or raises Wref::Recycled. begin
obj = wref.get
print "Object still exists in memory."
rescue Wref::Recycled
print "Object has been garbage-collected."
end
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/wref.rb', line 54 def get begin if RUBY_ENGINE == "jruby" raise Wref::Recycled if !@weakref obj = @weakref.get if obj == nil raise Wref::Recycled else return obj end else raise Wref::Recycled if !@class_name or !@id obj = ObjectSpace._id2ref(@id) #Some times this class-name will be nil for some reason - knj obj_class_name = obj.class.name if !obj_class_name or @class_name != obj_class_name.to_sym or @id != obj.__id__ raise Wref::Recycled end if @unique_id raise Wref::Recycled if !obj.respond_to?(:__object_unique_id__) or obj.__object_unique_id__ != @unique_id end return obj end rescue RangeError, TypeError raise Wref::Recycled end end |