|
Class: WeakReference
Object
|
+--WeakReference
- Package:
- stx:libbasic
- Category:
- Collections-Weak
- Version:
- rev:
1.20
date: 2023/06/06 22:10:36
- user: cg
- file: WeakReference.st directory: libbasic
- module: stx stc-classLibrary: libbasic
I represent a weak reference to some object.
I behave a little bit like a ValueHolder
The implementation is tuned and able to support a great number
of such references without producing too much garbage collector overhead.
(by using a single weakArray holding all such references,
instead of many many single-slot weak arrays, the garbage collector's overhead
in weak array processing is minimized)
copyrightCOPYRIGHT (c) 2020 by eXept Software AG
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
element disposal
-
growReferences
-
MUST be called with interrupts blocked
-
updateFreeSlotIndexList
-
Reclaim unused (garbage collected) entries in References
and rebuild the free slot index chain.
Make sure that there is no instance pointing to a free References entry.
MUST be called with interrupts blocked
Usage example(s):
self updateFreeSlotIndexList.
|
instance creation
-
on: anObject
-
WeakReference on:'Fidibus'
(WeakReference on:'Hokus') changeReferenceTo:'Pokus'
Usage example(s):
|t|
(t := WeakReference on:'Hokus') changeReferenceTo:'Pokus' copy.
ObjectMemory collectGarbage.
ObjectMemory collectGarbage.
t inspect.
|
-
with: anObject
-
compatibility with ValueHolder and Array
accessing
-
changeReferenceTo: anObject
-
change my reference to anObject.
We handle both cases:
- initial setup
- changing a reference of an already existing instance.
Usage example(s):
initial lazy setup.
Doing this here makes 'WeakReference new changeReferenceTo:something' work
|
-
value
-
my value; the object I hold a weak reference on.
Answer nil, if the referenced object was garbage collected.
enumerating
-
nonNilElementsDo: aBlock
-
evaluate the argument, aBlock for every non-nil element in the collection.
Implemented for WeakArray protocol compatibility.
printing & storing
-
printOn: aStream
-
(comment from inherited method)
append a user printed representation of the receiver to aStream.
The format is suitable for a human - not meant to be read back.
The default here is to output the receiver's class name.
BUT: this method is heavily redefined for objects which
can print prettier.
queries
-
includes: anElement
-
defined here, so we can use it like a WeakIdentitySet
-
includesIdentical: anElement
-
defined here, so we can use it like a WeakIdentitySet
testing
-
isEmpty
-
I am empty, if I don't hold a reference
-
isEmptyOrNil
-
I am empty, if I don't hold a reference
-
isWeak
-
return true if the receiver has weak references to its elements.
-
isWeakReference
-
-
notEmptyOrNil
-
I am empty, if I don't hold a reference
References := Instances := NextFreeSlotIndex := nil.
|holder p|
holder := WeakReference on:(p := Point new).
self assert:(holder value class == Point).
ObjectMemory garbageCollect.
self assert:(holder value class == Point).
p := nil.
ObjectMemory garbageCollect; finalize.
self assert:(holder value isNil).
|
100 timesRepeat:[
|holders|
holders := (1 to:10) collect:[:i |WeakReference on:(Point new)].
self assert:(holders conform:[:holder | holder value class == Point]).
ObjectMemory garbageCollect; finalize.
self assert:(holders conform:[:holder | holder value isNil]).
]
|
|