2011-06-30 04:37:00 +04:00
|
|
|
require "delegate"
|
|
|
|
|
2011-08-27 02:22:37 +04:00
|
|
|
# Weak Reference class that allows a referenced object to be
|
2011-06-30 04:37:00 +04:00
|
|
|
# garbage-collected. A WeakRef may be used exactly like the object it
|
|
|
|
# references.
|
1998-01-16 15:19:09 +03:00
|
|
|
#
|
|
|
|
# Usage:
|
2011-06-30 04:37:00 +04:00
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
# foo = Object.new
|
1999-08-13 09:45:20 +04:00
|
|
|
# foo = Object.new
|
2011-05-19 04:07:25 +04:00
|
|
|
# p foo.to_s # original's class
|
1998-01-16 15:19:09 +03:00
|
|
|
# foo = WeakRef.new(foo)
|
2011-05-19 04:07:25 +04:00
|
|
|
# p foo.to_s # should be same class
|
1998-01-16 15:19:09 +03:00
|
|
|
# ObjectSpace.garbage_collect
|
2011-05-19 04:07:25 +04:00
|
|
|
# p foo.to_s # should raise exception (recycled)
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
class WeakRef < Delegator
|
|
|
|
|
2011-06-30 04:37:00 +04:00
|
|
|
##
|
|
|
|
# RefError is raised when a referenced object has been recycled by the
|
|
|
|
# garbage collector
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
class RefError < StandardError
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2012-03-13 07:37:06 +04:00
|
|
|
@@__map = ::ObjectSpace::WeakMap.new
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2011-06-30 04:37:00 +04:00
|
|
|
##
|
|
|
|
# Creates a weak reference to +orig+
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
def initialize(orig)
|
2012-03-13 07:37:06 +04:00
|
|
|
case orig
|
|
|
|
when true, false, nil
|
|
|
|
@delegate_sd_obj = orig
|
|
|
|
else
|
|
|
|
@@__map[self] = orig
|
|
|
|
end
|
2005-08-12 11:17:36 +04:00
|
|
|
super
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
2011-06-30 04:37:00 +04:00
|
|
|
def __getobj__ # :nodoc:
|
2012-03-13 07: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 15:19:09 +03:00
|
|
|
end
|
2011-06-30 04:37:00 +04:00
|
|
|
|
|
|
|
def __setobj__(obj) # :nodoc:
|
2005-08-12 11:17:36 +04:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2011-06-30 04:37:00 +04:00
|
|
|
##
|
|
|
|
# Returns true if the referenced object is still alive.
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
def weakref_alive?
|
2012-03-13 07:37:06 +04:00
|
|
|
!!(@@__map[self] or defined?(@delegate_sd_obj))
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if __FILE__ == $0
|
2005-08-12 11:17:36 +04:00
|
|
|
# require 'thread'
|
1999-01-20 07:59:39 +03:00
|
|
|
foo = Object.new
|
2011-05-19 04:07:25 +04:00
|
|
|
p foo.to_s # original's class
|
1999-01-20 07:59:39 +03:00
|
|
|
foo = WeakRef.new(foo)
|
2011-05-19 04:07:25 +04:00
|
|
|
p foo.to_s # should be same class
|
1999-01-20 07:59:39 +03:00
|
|
|
ObjectSpace.garbage_collect
|
2005-08-12 11:17:36 +04:00
|
|
|
ObjectSpace.garbage_collect
|
2011-05-19 04:07:25 +04:00
|
|
|
p foo.to_s # should raise exception (recycled)
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|