eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'WeakReference':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: WeakReference


Inheritance:

   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

Description:


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)

copyright

COPYRIGHT (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.

Class protocol:

element disposal
o  growReferences
MUST be called with interrupts blocked

o  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
o  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.

o  with: anObject
compatibility with ValueHolder and Array


Instance protocol:

accessing
o  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

o  value
my value; the object I hold a weak reference on.
Answer nil, if the referenced object was garbage collected.

enumerating
o  nonNilElementsDo: aBlock
evaluate the argument, aBlock for every non-nil element in the collection.
Implemented for WeakArray protocol compatibility.

printing & storing
o  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
o  includes: anElement
defined here, so we can use it like a WeakIdentitySet

o  includesIdentical: anElement
defined here, so we can use it like a WeakIdentitySet

testing
o  isEmpty
I am empty, if I don't hold a reference

o  isEmptyOrNil
I am empty, if I don't hold a reference

o  isWeak
return true if the receiver has weak references to its elements.

o  isWeakReference

o  notEmptyOrNil
I am empty, if I don't hold a reference


Examples:


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]).
]


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Mon, 18 Nov 2024 06:31:58 GMT