Module: Finalizer

Defined in:
lib/finalize.rb

Overview

--

finalizer.rb - 
	$Release Version: 0.3$
	$Revision: 1.4 $
	$Date: 1998/02/27 05:34:33 $
	by Keiju ISHITSUKA

++

Usage:

add dependency R_method(obj, dependant)

add(obj, dependant, method = :finalize, *opt)
add_dependency(obj, dependant, method = :finalize, *opt)

delete dependency R_method(obj, dependant)

delete(obj_or_id, dependant, method = :finalize)
delete_dependency(obj_or_id, dependant, method = :finalize)

delete dependency R_*(obj, dependant)

delete_all_dependency(obj_or_id, dependant)

delete dependency R_method(*, dependant)

delete_by_dependant(dependant, method = :finalize)

delete dependency R_*(*, dependant)

delete_all_by_dependant(dependant)

delete all dependency R_*(*, *)

delete_all

finalize the dependant connected by dependency R_method(obj, dependtant).

finalize(obj_or_id, dependant, method = :finalize)
finalize_dependency(obj_or_id, dependant, method = :finalize)

finalize all dependants connected by dependency R_*(obj, dependtant).

finalize_all_dependency(obj_or_id, dependant)

finalize the dependant connected by dependency R_method(*, dependtant).

finalize_by_dependant(dependant, method = :finalize)

finalize all dependants connected by dependency R_*(*, dependant).

finalize_all_by_dependant(dependant)

finalize all dependency registered to the Finalizer.

finalize_all

stop invoking Finalizer on GC.

safe{..}

Constant Summary collapse

RCS_ID =
'-$Id: finalize.rb,v 1.4 1998/02/27 05:34:33 keiju Exp keiju $-'

Class Method Summary collapse

Class Method Details

.add_dependency(obj, dependant, method = :finalize, *opt) ⇒ Object Also known as: add

add dependency R_method(obj, dependant)



58
59
60
61
62
63
64
65
66
67
# File 'lib/finalize.rb', line 58

def add_dependency(obj, dependant, method = :finalize, *opt)
  ObjectSpace.call_finalizer(obj)
  method = method.intern unless method.kind_of?(Integer)
  assoc = [dependant, method].concat(opt)
  if dep = @dependency[obj.object_id]
	dep.push assoc
  else
	@dependency[obj.object_id] = [assoc]
  end
end

.delete_all_by_dependant(dependant) ⇒ Object

delete dependency R_*(*, dependant)



106
107
108
109
110
# File 'lib/finalize.rb', line 106

def delete_all_by_dependant(dependant)
  for id in @dependency.keys
	delete_all_dependency(id, dependant)
  end
end

.delete_all_dependency(id, dependant) ⇒ Object

delete dependency R_*(obj, dependant)



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/finalize.rb', line 85

def delete_all_dependency(id, dependant)
  id = id.object_id unless id.kind_of?(Integer)
  method = method.intern unless method.kind_of?(Integer)
  for assoc in @dependency[id]
	assoc.delete_if do
	  |d, m, *o|
	  d == dependant
	end
	@dependency.delete(id) if assoc.empty?
  end
end

.delete_by_dependant(dependant, method = :finalize) ⇒ Object

delete dependency R_method(*, dependant)



98
99
100
101
102
103
# File 'lib/finalize.rb', line 98

def delete_by_dependant(dependant, method = :finalize)
  method = method.intern unless method.kind_of?(Integer)
  for id in @dependency.keys
	delete(id, dependant, method)
  end
end

.delete_dependency(id, dependant, method = :finalize) ⇒ Object Also known as: delete

delete dependency R_method(obj, dependant)



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/finalize.rb', line 71

def delete_dependency(id, dependant, method = :finalize)
  id = id.object_id unless id.kind_of?(Integer)
  method = method.intern unless method.kind_of?(Integer)
  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

.finalize_allObject

finalize all dependants registered to the Finalizer.



156
157
158
159
160
161
162
163
# File 'lib/finalize.rb', line 156

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_all_by_dependant(dependant) ⇒ Object

finalize all dependants connected by dependency R_*(*, dependtant)



149
150
151
152
153
# File 'lib/finalize.rb', line 149

def finalize_all_by_dependant(dependant)
  for id in @dependency.keys
	finalize_all_dependency(id, dependant)
  end
end

.finalize_all_dependency(id, dependant) ⇒ Object

finalize all dependants connected by dependency R_*(obj, dependtant)



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/finalize.rb', line 128

def finalize_all_dependency(id, dependant)
  id = id.object_id unless id.kind_of?(Integer)
  method = method.intern unless method.kind_of?(Integer)
  for assoc in @dependency[id]
	assoc.delete_if do
	  |d, m, *o|
	  d.send(m, id, *o) if ret = d == dependant
	end
	@dependency.delete(id) if assoc.empty?
  end
end

.finalize_by_dependant(dependant, method = :finalize) ⇒ Object

finalize the dependant connected by dependency R_method(*, dependtant)



141
142
143
144
145
146
# File 'lib/finalize.rb', line 141

def finalize_by_dependant(dependant, method = :finalize)
  method = method.intern unless method.kind_of?(Integer)
  for id in @dependency.keys
	finalize(id, dependant, method)
  end
end

.finalize_dependency(id, dependant, method = :finalize) ⇒ Object Also known as: finalize

finalize the depandant connected by dependency R_method(obj, dependtant)



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/finalize.rb', line 113

def finalize_dependency(id, dependant, method = :finalize)
  id = id.object_id unless id.kind_of?(Integer)
  method = method.intern unless method.kind_of?(Integer)
  for assocs in @dependency[id]
	assocs.delete_if do
	  |d, m, *o|
	  d.send(m, id, *o) if ret = d == dependant && m == method
	  ret
	end
	@dependency.delete(id) if assoc.empty?
  end
end

.safeObject

method to call finalize_* safely.



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/finalize.rb', line 166

def safe
  old_status = Thread.critical
  Thread.critical = true
  ObjectSpace.remove_finalizer(@proc)
  begin
	yield
  ensure
	ObjectSpace.add_finalizer(@proc)
	Thread.critical = old_status
  end
end