cee::PtrRef

template<typename T>
class PtrRef

Smart pointer class used for handling reference counted objects (that derive from Object).

The PtrRef<T> class encapsulates reference counting by calling Object::addRef() and Object::release() on the internally stored object pointer.

Public Types

typedef T *PtrRef::* unspecified_bool_type

Helper type to implement the safe-bool idiom.

Public Functions

PtrRef(T *object = NULL)

Constructs an object from naked pointer.

Will call addRef() on the passed object.

PtrRef(const PtrRef &other)

Construct and object as a copy of other.

Copies the internal pointer from the passed PtrRef object and calls addRef() on the object.

template<typename T2>
PtrRef(const PtrRef<T2> &other)

Constructs from related.

Copies the internal pointer from the passed PtrRef object and calls addRef() on the object.

PtrRef &operator=(T *rhs)

Assigns from raw pointer.

PtrRef &operator=(PtrRef rhs)

Assigns from rhs.

Copies the internal pointer from rhs and calls addRef(). If we’re already storing an internal pointer, this pointer will be release()’ed.

template<typename T2>
PtrRef &operator=(const PtrRef<T2> &rhs)

Assigns from related.

Copies the internal pointer from rhs and calls addRef(). If we’re already storing an internal pointer, this pointer will be release()’ed.

inline T *operator->()

Returns the naked pointer this object is associated with.

Added to be able to write the same code for smart pointers as for normal naked pointers.

inline const T *operator->() const

Returns naked const pointer this object is associated with.

inline T &operator*()

Dereference operator returning a modifiable reference to the associated object.

Added to be able to write the same code for smart pointers as for normal naked pointers.

PtrRef<MyObject> myObject = new MyObject;
*myObject.doSomething();

inline const T &operator*() const

Dereference operator returning a const reference to the associated object.

bool operator<(const PtrRef &rhs) const

Does less than comparison of two PtrRef objects.

Returns true if this object’s internal pointer is less than the internal pointer of rhs.

The comparison is done by comparing the internal pointer values ( this.p() < rhs.p() ) This operator is used in several STL collections (e.g. std::set) as well as in many STL algorithms (e.g. std::sort() ).

inline const T *get() const

Returns naked const pointer.

inline T *get()

Returns the naked pointer.

void swap(PtrRef &other)

Exchanges the contents of the two smart pointers.

other is a modifiable reference to the PtrRef object that should have its contents swapped.

Swap the associated pointer in this and the passed PtrRef object. Will not modify the reference count of any of the associated objects.

T *detach()

Detach the PtrRef from the internal object WITHOUT calling release on the object.

After calling, *this == NULL and associated object will have the same refCount as before the call.

Note! Use with caution! The associated object will not be release()’ed and it is the callers responsibility to ensure proper life cycle management of the object.

operator unspecified_bool_type() const

Operator for supporting tests if the objects internal pointer is NULL or not.

Using the safe bool idiom, PtrRef allows for code like

PtrRef<cee::Image> myImage = new cee::Image;
...
if (myImage)
{
    // Do something
}
without defining the operator bool(), which have many potential side effects.

template<typename T2>
PtrRef<T> &operator=(const PtrRef<T2> &rhs)