1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/finalize.rb

206 lines
5.8 KiB
Ruby
Raw Normal View History

#
# finalize.rb -
# $Release Version: $
# $Revision: 1.2 $
# $Date: 1997/07/25 02:43:00 $
# by Keiju ISHITSUKA(SHL Japan Inc.)
#
# --
#
# Usage:
#
# add(obj, dependant, method = :finalize, *opt)
# add_dependency(obj, dependant, method = :finalize, *opt)
# <09><>¸<EFBFBD>ط<EFBFBD> R_method(obj, dependant) <20><><EFBFBD>ɲ<EFBFBD>
#
# delete(obj_or_id, dependant, method = :finalize)
# delete_dependency(obj_or_id, dependant, method = :finalize)
# <09><>¸<EFBFBD>ط<EFBFBD> R_method(obj, dependant) <20>κ<EFBFBD><CEBA><EFBFBD>
# delete_all_dependency(obj_or_id, dependant)
# <09><>¸<EFBFBD>ط<EFBFBD> R_*(obj, dependant) <20>κ<EFBFBD><CEBA><EFBFBD>
# delete_by_dependant(dependant, method = :finalize)
# <09><>¸<EFBFBD>ط<EFBFBD> R_method(*, dependant) <20>κ<EFBFBD><CEBA><EFBFBD>
# delete_all_by_dependant(dependant)
# <09><>¸<EFBFBD>ط<EFBFBD> R_*(*, dependant) <20>κ<EFBFBD><CEBA><EFBFBD>
# delete_all
# <09><><EFBFBD>Ƥΰ<C6A4>¸<EFBFBD>ط<EFBFBD><D8B7>κ<EFBFBD><CEBA><EFBFBD>.
#
# finalize(obj_or_id, dependant, method = :finalize)
# finalize_dependency(obj_or_id, dependant, method = :finalize)
# <09><>¸<EFBFBD><C2B8>Ϣ R_method(obj, dependtant) <20>Ƿ<EFBFBD><C7B7>Ф<EFBFBD><D0A4><EFBFBD>dependant<6E><74>
# finalize<7A><65><EFBFBD><EFBFBD>.
# finalize_all_dependency(obj_or_id, dependant)
# <09><>¸<EFBFBD><C2B8>Ϣ R_*(obj, dependtant) <20>Ƿ<EFBFBD><C7B7>Ф<EFBFBD><D0A4><EFBFBD>dependant<6E><74>finalize<7A><65><EFBFBD><EFBFBD>.
# finalize_by_dependant(dependant, method = :finalize)
# <09><>¸<EFBFBD><C2B8>Ϣ R_method(*, dependtant) <20>Ƿ<EFBFBD><C7B7>Ф<EFBFBD><D0A4><EFBFBD>dependant<6E><74>finalize<7A><65><EFBFBD><EFBFBD>.
# fainalize_all_by_dependant(dependant)
# <09><>¸<EFBFBD><C2B8>Ϣ R_*(*, dependtant) <20>Ƿ<EFBFBD><C7B7>Ф<EFBFBD><D0A4><EFBFBD>dependant<6E><74>finalize<7A><65><EFBFBD><EFBFBD>.
# finalize_all
# Finalizer<65><72><EFBFBD><EFBFBD>Ͽ<EFBFBD><CFBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƥ<EFBFBD>dependant<6E><74>finalize<7A><65><EFBFBD><EFBFBD>
#
# safe{..}
# gc<67><63><EFBFBD><EFBFBD>Finalizer<65><72><EFBFBD><EFBFBD>ư<EFBFBD><C6B0><EFBFBD><EFBFBD><EFBFBD>Τ<EFBFBD><CEA4>ߤ<EFBFBD><DFA4><EFBFBD>.
#
#
module Finalizer
RCS_ID='-$Header: /home/keiju/var/src/var.lib/ruby/RCS/finalize.rb,v 1.2 1997/07/25 02:43:00 keiju Exp keiju $-'
# @dependency: {id => [[dependant, method, *opt], ...], ...}
# <20><>¸<EFBFBD>ط<EFBFBD> R_method(obj, dependant) <20><><EFBFBD>ɲ<EFBFBD>
def add_dependency(obj, dependant, method = :finalize, *opt)
ObjectSpace.call_finalizer(obj)
method = method.id unless method.kind_of?(Fixnum)
assoc = [dependant, method].concat(opt)
if dep = @dependency[obj.id]
dep.push assoc
else
@dependency[obj.id] = [assoc]
end
end
alias add add_dependency
# <20><>¸<EFBFBD>ط<EFBFBD> R_method(obj, dependant) <20>κ<EFBFBD><CEBA><EFBFBD>
def delete_dependency(id, dependant, method = :finalize)
id = id.id unless id.kind_of?(Fixnum)
method = method.id unless method.kind_of?(Fixnum)
for assoc in @dependency[id]
assoc.delete_if do
|d, m, *o|
d == dependant && m == method
end
@dependency.delete(id) if assoc.empty?
end
end
alias delete delete_dependency
# <20><>¸<EFBFBD>ط<EFBFBD> R_*(obj, dependant) <20>κ<EFBFBD><CEBA><EFBFBD>
def delete_all_dependency(id, dependant)
id = id.id unless id.kind_of?(Fixnum)
method = method.id unless method.kind_of?(Fixnum)
for assoc in @dependency[id]
assoc.delete_if do
|d, m, *o|
d == dependant
end
@dependency.delete(id) if assoc.empty?
end
end
# <20><>¸<EFBFBD>ط<EFBFBD> R_method(*, dependant) <20>κ<EFBFBD><CEBA><EFBFBD>
def delete_by_dependant(dependant, method = :finalize)
method = method.id unless method.kind_of?(Fixnum)
for id in @dependency.keys
delete(id, dependant, method)
end
end
# <20><>¸<EFBFBD>ط<EFBFBD> R_*(*, dependant) <20>κ<EFBFBD><CEBA><EFBFBD>
def delete_all_by_dependant(dependant)
for id in @dependency.keys
delete_all_dependency(id, dependant)
end
end
# <20><>¸<EFBFBD><C2B8>Ϣ R_method(obj, dependtant) <20>Ƿ<EFBFBD><C7B7>Ф<EFBFBD><D0A4><EFBFBD>dependant<6E><74>finalize<7A><65>
# <20><>.
def finalize_dependency(id, dependant, method = :finalize)
id = id.id unless id.kind_of?(Fixnum)
method = method.id unless method.kind_of?(Fixnum)
for assocs in @dependency[id]
assocs.delete_if do
|d, m, *o|
d.send(m, *o) if ret = d == dependant && m == method
ret
end
@dependency.delete(id) if assoc.empty?
end
end
alias finalize finalize_dependency
# <20><>¸<EFBFBD><C2B8>Ϣ R_*(obj, dependtant) <20>Ƿ<EFBFBD><C7B7>Ф<EFBFBD><D0A4><EFBFBD>dependant<6E><74>finalize<7A><65><EFBFBD><EFBFBD>.
def finalize_all_dependency(id, dependant)
id = id.id unless id.kind_of?(Fixnum)
method = method.id unless method.kind_of?(Fixnum)
for assoc in @dependency[id]
assoc.delete_if do
|d, m, *o|
d.send(m, *o) if ret = d == dependant
end
@dependency.delete(id) if assoc.empty?
end
end
# <20><>¸<EFBFBD><C2B8>Ϣ R_method(*, dependtant) <20>Ƿ<EFBFBD><C7B7>Ф<EFBFBD><D0A4><EFBFBD>dependant<6E><74>finalize<7A><65><EFBFBD><EFBFBD>.
def finalize_by_dependant(dependant, method = :finalize)
method = method.id unless method.kind_of?(Fixnum)
for id in @dependency.keys
finalize(id, dependant, method)
end
end
# <20><>¸<EFBFBD><C2B8>Ϣ R_*(*, dependtant) <20>Ƿ<EFBFBD><C7B7>Ф<EFBFBD><D0A4><EFBFBD>dependant<6E><74>finalize<7A><65><EFBFBD><EFBFBD>.
def fainalize_all_by_dependant(dependant)
for id in @dependency.keys
finalize_all_dependency(id, dependant)
end
end
# Finalizer<65><72><EFBFBD><EFBFBD>Ͽ<EFBFBD><CFBF><EFBFBD><EFBFBD><EFBFBD>Ƥ<EFBFBD><C6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƥ<EFBFBD>dependant<6E><74>finalize<7A><65><EFBFBD><EFBFBD>
def finalize_all
for id, assocs in @dependency
for dependant, method, *opt in assocs
dependant.send(method, id, *opt)
end
assocs.clear
end
end
# finalize_* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸ƤӽФ<D3BD><D0A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Υ<EFBFBD><CEA5>ƥ졼<C6A5><ECA1BC>
def safe
old_status = Thread.critical
Thread.critical = TRUE
ObjectSpace.remove_finalizer(@proc)
yield
ObjectSpace.add_finalizer(@proc)
Thread.critical = old_status
end
# ObjectSpace#add_finalizer<65>ؤ<EFBFBD><D8A4><EFBFBD>Ͽ<EFBFBD>ؿ<EFBFBD>
def final_of(id)
if assocs = @dependency.delete(id)
for dependant, method, *opt in assocs
dependant.send(method, id, *opt)
end
end
end
@dependency = Hash.new
@proc = proc{|id| final_of(id)}
ObjectSpace.add_finalizer(@proc)
module_function :add
module_function :add_dependency
module_function :delete
module_function :delete_dependency
module_function :delete_all_dependency
module_function :delete_by_dependant
module_function :delete_all_by_dependant
module_function :finalize
module_function :finalize_dependency
module_function :finalize_all_dependency
module_function :finalize_by_dependant
module_function :fainalize_all_by_dependant
module_function :finalize_all
module_function :safe
module_function :final_of
private_class_method :final_of
end