2017-01-26 10:14:00 -05:00
|
|
|
# frozen_string_literal: true
|
2011-06-29 20:37:00 -04:00
|
|
|
require "delegate"
|
|
|
|
|
2011-08-26 18:22:37 -04:00
|
|
|
# Weak Reference class that allows a referenced object to be
|
2012-12-02 02:48:42 -05:00
|
|
|
# garbage-collected.
|
|
|
|
#
|
|
|
|
# A WeakRef may be used exactly like the object it references.
|
1998-01-16 07:19:09 -05:00
|
|
|
#
|
|
|
|
# Usage:
|
2011-06-29 20:37:00 -04:00
|
|
|
#
|
2012-12-02 02:48:42 -05:00
|
|
|
# foo = Object.new # create a new object instance
|
2011-05-18 20:07:25 -04:00
|
|
|
# p foo.to_s # original's class
|
2012-12-02 02:48:42 -05:00
|
|
|
# foo = WeakRef.new(foo) # reassign foo with WeakRef instance
|
2011-05-18 20:07:25 -04:00
|
|
|
# p foo.to_s # should be same class
|
2012-12-02 02:48:42 -05:00
|
|
|
# GC.start # start the garbage collector
|
2011-05-18 20:07:25 -04:00
|
|
|
# p foo.to_s # should raise exception (recycled)
|
2012-12-02 02:48:42 -05:00
|
|
|
#
|
|
|
|
# == Example
|
|
|
|
#
|
2013-11-09 17:42:20 -05:00
|
|
|
# With help from WeakRef, we can implement our own rudimentary WeakHash class.
|
2012-12-02 02:48:42 -05:00
|
|
|
#
|
|
|
|
# We will call it WeakHash, since it's really just a Hash except all of it's
|
|
|
|
# keys and values can be garbage collected.
|
|
|
|
#
|
|
|
|
# require 'weakref'
|
|
|
|
#
|
|
|
|
# class WeakHash < Hash
|
|
|
|
# def []= key, obj
|
|
|
|
# super WeakRef.new(key), WeakRef.new(obj)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This is just a simple implementation, we've opened the Hash class and changed
|
|
|
|
# Hash#store to create a new WeakRef object with +key+ and +obj+ parameters
|
|
|
|
# before passing them as our key-value pair to the hash.
|
|
|
|
#
|
2013-11-09 17:42:20 -05:00
|
|
|
# With this you will have to limit your self to String keys, otherwise you
|
2012-12-02 02:48:42 -05:00
|
|
|
# will get an ArgumentError because WeakRef cannot create a finalizer for a
|
|
|
|
# Symbol. Symbols are immutable and cannot be garbage collected.
|
|
|
|
#
|
|
|
|
# Let's see it in action:
|
|
|
|
#
|
|
|
|
# omg = "lol"
|
|
|
|
# c = WeakHash.new
|
|
|
|
# c['foo'] = "bar"
|
|
|
|
# c['baz'] = Object.new
|
|
|
|
# c['qux'] = omg
|
|
|
|
# puts c.inspect
|
2012-12-02 02:57:45 -05:00
|
|
|
# #=> {"foo"=>"bar", "baz"=>#<Object:0x007f4ddfc6cb48>, "qux"=>"lol"}
|
2012-12-02 02:48:42 -05:00
|
|
|
#
|
|
|
|
# # Now run the garbage collector
|
|
|
|
# GC.start
|
|
|
|
# c['foo'] #=> nil
|
|
|
|
# c['baz'] #=> nil
|
|
|
|
# c['qux'] #=> nil
|
|
|
|
# omg #=> "lol"
|
|
|
|
#
|
|
|
|
# puts c.inspect
|
|
|
|
# #=> WeakRef::RefError: Invalid Reference - probably recycled
|
|
|
|
#
|
2013-11-09 17:42:20 -05:00
|
|
|
# You can see the local variable +omg+ stayed, although its reference in our
|
2012-12-02 02:48:42 -05:00
|
|
|
# hash object was garbage collected, along with the rest of the keys and
|
2013-11-09 17:42:20 -05:00
|
|
|
# values. Also, when we tried to inspect our hash, we got a WeakRef::RefError.
|
|
|
|
# This is because these objects were also garbage collected.
|
1998-01-16 07:19:09 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
class WeakRef < Delegator
|
|
|
|
|
2011-06-29 20:37:00 -04:00
|
|
|
##
|
|
|
|
# RefError is raised when a referenced object has been recycled by the
|
|
|
|
# garbage collector
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
class RefError < StandardError
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
2012-03-12 23:37:06 -04:00
|
|
|
@@__map = ::ObjectSpace::WeakMap.new
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2011-06-29 20:37:00 -04:00
|
|
|
##
|
|
|
|
# Creates a weak reference to +orig+
|
2012-12-02 02:48:42 -05:00
|
|
|
#
|
|
|
|
# Raises an ArgumentError if the given +orig+ is immutable, such as Symbol,
|
2017-01-26 10:14:02 -05:00
|
|
|
# Integer, or Float.
|
2011-06-29 20:37:00 -04:00
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
def initialize(orig)
|
2012-03-12 23:37:06 -04:00
|
|
|
case orig
|
|
|
|
when true, false, nil
|
|
|
|
@delegate_sd_obj = orig
|
|
|
|
else
|
|
|
|
@@__map[self] = orig
|
|
|
|
end
|
2005-08-12 03:17:36 -04:00
|
|
|
super
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
|
2011-06-29 20:37:00 -04:00
|
|
|
def __getobj__ # :nodoc:
|
2012-03-12 23:37:06 -04:00
|
|
|
@@__map[self] or defined?(@delegate_sd_obj) ? @delegate_sd_obj :
|
|
|
|
Kernel::raise(RefError, "Invalid Reference - probably recycled", Kernel::caller(2))
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2011-06-29 20:37:00 -04:00
|
|
|
|
|
|
|
def __setobj__(obj) # :nodoc:
|
2005-08-12 03:17:36 -04:00
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
|
2011-06-29 20:37:00 -04:00
|
|
|
##
|
|
|
|
# Returns true if the referenced object is still alive.
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
def weakref_alive?
|
2013-10-18 02:59:12 -04:00
|
|
|
@@__map.key?(self) or defined?(@delegate_sd_obj)
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
end
|