next up previous contents index
Next: The Heap Walker Up: Finalization Previous: Finalization

Weak Pointers

It is frequently very useful to be able to keep track of garbage collectable objects through the use of ``weak pointers.'' Collectors ignore the presence of weak pointers to garbage collected objects; objects with only referencing weak pointers will still be collected. Weak pointers to objects that become garbage will be automatically reset to null.

Weak pointers are provided through a smart pointer template WeakPointer , whose definition is shown below:

template <class T> class WeakPointer {
   public:
      inline WeakPointer();
      inline WeakPointer(const WeakPointer<T>& wp);
      inline WeakPointer(T * ptr);
      inline WeakPointer<T>& operator = (const WeakPointer<T>& wp);
      inline WeakPointer<T>& operator = (T * ptr);
      inline bool is_null() const;
      inline operator const T * () const;
      inline operator       T * ();
      inline const T * operator -> () const;
      inline       T * operator -> ();
      inline const T&  operator *  () const;
      inline       T&  operator *  ();
   };

A weakpointer to a garbage collectable class A can be defined as WeakPointer<A>. If a Prop datatype T has been defined, a weakpointer to type T can defined using the classof  keyword. For example:

   datatype Wff :: collectable = True | False | And(...)  | ...;

   WeakPointer<classof Wff> a = And(True,False);



Allen Leung
Mon Apr 7 14:33:55 EDT 1997