x::const_ref and x::const_ptr

x::const_ref and x::const_ptr are versions of x::ref and x::ptr whose * and -> operators resolve to a constant object, similar to the relation between std::iterator and std::const_iterator in the C++ library. They are subclasses of x::ref and x::ptr (actually they're superclasses, but it's easier to think of them as subclasses). Passing an x::ref or an x::ptr to a function that takes an equivalent const argument does not require the creation of a temporary object:

class buttonObj : virtual public x::obj {

// ...

};

typedef x::ref<buttonObj> button;

typedef x::ptr<buttonObj> buttonptr;


typedef x::const_ref<buttonObj> const_button;

typedef x::const_ptr<buttonObj> const_buttonptr;

// ...

void draw(const const_button &b);

// ...

button buttonref=button::create();

// ...

draw(buttonref);

In this example, draw() does not create a temporary. It just passes a reference to the x::const_ref superclass of the x::ref, with the contract that the x::const_ref gives access only to const class methods and members.

The above example shows LIBCXX's naming convention for reference-counted objects and reference types:

Name What it is
class somethingObj Virtually derived from x::obj, a reference-counted class.
typedef x::ref<somethingObjsomething A non-nullptr reference to a reference-counted object. something::create(args) calls new somethingObj(args), and returns a reference to the newly-constructed object.
typedef x::const_ref<somethingObjconst_something A non-nullptr reference to a constant reference-counted object. * and -> operators resolve to a constant object.
typedef x::ptr<somethingObjsomethingptr A reference-counted pointer, which may be a nullptr. somethingptr::create(args) calls new somethingObj(args), and returns a pointer reference to the newly-constructed object. Dereferencing a nullptr somethingptr throws an exception.
typedef x::const_ptr<const_somethingObjconst_somethingptr A reference-counted pointer to a constant object, which may be a nullptr. * and -> operators resolve to a constant object. Dereferencing a nullptr const_somethingptr throws an exception.