WeakReference Class Reference

#include <coherence/lang/WeakReference.hpp>

Inherits Object, and Reference.

List of all members.

Detailed Description

WeakReferences allow one Object to safely reference another without blocking it from being destroyed.

WeakReferences are necessary when building Object graphs to avoid leaking the Object graph due to cyclical references.

WeakReferences provide getter methods to safely obtain a normal Handle to the weakly referenced Object if it still exists. Once the Object becomes only weakly referenced its associated WeakReference will be automatically NULL'd out so that the Object may be reclaimed.

Consider a simple case of an Object tree, where parent's reference their children, and children reference their parents. With normal reference counting handles, the tree would be leaked even once all external references to the tree have been dropped. To resolve this issue the tree could be constructed such that parents reference their children via normal Handles, but children reference their parents via WeakReferences. This way as long as the parent is externaly refrenced its entire sub-tree will be retained. Once all external Handles to the parent are dropped the entire tree will be reclaimed as only WeakReferences would hold it together.

Similar to MemberHandles, WeakReferences tranfer their constness to their referenced Object. That is if a View to a WeakReference is held then only the View to the referenced Object will be available.

Do to the need to call the get() which returns an Object::View or Handle and then perform a cast, WeakReferences are somewhat cumbersome to make use of. WeakHandle and WeakView are TypedHandle like wrappers for WeakReference which makes using WeakReferences much simpler. In most cases these should be used rather than direct use of WeakReferences.

See also:


mf 2008.04.10

Public Types

typedef spec::Handle Handle
 WeakReference Handle definition.
typedef spec::View View
 WeakReference View definition.
typedef spec::Holder Holder
 WeakReference Holder definition.

Public Member Functions

virtual Object::Holder get () const =0
 Return a Holder, containing a View to the referenced Object.
virtual Object::Holder get ()=0
 Return a Holder to the referenced Object.

Member Function Documentation

virtual Object::Holder get (  )  const [pure virtual]

Return a Holder, containing a View to the referenced Object.

a view to the referenced Object

Implements Reference.

virtual Object::Holder get (  )  [pure virtual]

Return a Holder to the referenced Object.

a Handle unless the Object has become soley referend by Views.

Implements Reference.

The documentation for this class was generated from the following file: Copyright (c) 2000-2008 Oracle. All rights reserved.