All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CountingPtr – an intrusive reference counting pointer

CountingPtr is an implementation of intrusive reference counting.

This is similar, but not identical to boost or C++ TR1's shared_ptr. Intrusive reference counting requires the counted class to contain the counter, which is not required by std::shared_ptr.

Intrusive counting is often faster due to fewer cache faults. Furthermore, CountingPtr is a single pointer, whereas std::shared_ptr actually contains (at least) two pointers. CountingPtr also creates a lot less debug info due to reduced complexity.

CountingPtr is accompanied by ReferenceCounter, which contains the actual reference counter (a single integer). A reference counted object must derive from ReferenceCounter :

struct Something : public tlx::ReferenceCounter

Code that now wishes to use pointers referencing this object, will typedef an CountingPtr, which is used to increment and decrement the included reference counter automatically.

using SomethingPtr = tlx::CountingPtr<Something>;
// create new instance of something
SomethingPtr p1 = new something;
// create a new reference to the same instance (no deep copy!)
SomethingPtr p2 = p1;
// this block end will decrement the reference count, but not delete the object
// this block end will delete the object

The CountingPtr can generally be used like a usual pointer or std::shared_ptr (see the docs for more).