Weak pointers

When all pointers and references to an object go out of scope, the object gets automatically destroyed. An ordinary pointer can be converted to a weak pointer. When all ordinary pointers and and references go out of scope, the object gets destroyed even if there's a weak pointer to the object:

#include <x/obj.H>
#include <x/ref.H>
#include <x/weakptr.H>

class widgetObj : virtual public x::obj {

// ...

};

typedef x::ref<widgetObj> widget;

typedef x::ptr<widgetObj> widgetptr;

typedef x::weakptr<widgetptr> weakwidget;

// ...

widget w(widget::create());

// ...

weakwidget weakw(w);

// ...

widgetptr origw(weakw.getptr());

if (!origw.null())
{
    // ...
}

The weakptr template takes a x::ptr (not a x::ref), and makes a weak version of it. A weak pointer is similarly instantiated from an existing, ordinary pointer. The only method implemented by a weakptr is getptr(), which returns an ordinary, strong pointer to the underlying object, if it still exists. If all other existing references to the object previously went out of scope and the object got destroyed, getptr() returns an unbound reference.

Like x::ptr, the implementation of weakptr is thread-safe, however see important comments about thread safety of references.