CsPointer  1.0.0
Fundamentals

What is CsPointer

CsPointer is a standalone header only C++ library. All of the smart pointer classes except CsIntrusivePointer are implemented using C++ Standard Library smart pointer classes. The API for this library has been expanded to include methods using the names found in other common smart pointer libraries. For example CsSharedPointer contains both get() and data().

The CsIntrusivePointer class is a new smart pointer class which was partially based on the intrusive pointer class in Boost. There is currently no corresponding intrusive pointer class in the C++ Standard Library.

An intrusive smart pointer is similar to a shared pointer. The key difference is where the reference count is stored and who is responsible for maintaining it. With an intrusive pointer there is usually a base class which contains an atomic integer and methods to increment and decrement the reference count.

Our implementation of CsIntrusivePointer uses a Policy class to manage the reference count. This provides an easy mechanism for other developers to use a custom policy class. As an example, a Windows COM object already contains a reference count. A different policy class can be specified using the second template parameter in CsIntrusivePointer.

Why CsPointer was Created

By isolating the pointer classes into a standalone library the implementation of unique pointer, shared pointer, and weak pointer classes in CopperSpice was simplified and improved. The pointer classes were hand crafted and did not conform to the Standard Library.

QObject is the main base widget class and is used by almost every component in CopperSpice. The reference count was originally part of QObject which created lifetime issues and the destructor for QObject was overly complicated.

A new class called NodeManager was added to the CsPointer library which maintains a container of intrusive pointers. This class contains methods to add and remove elements, query a given node, and to walk a tree of nodes.

During the integration of CsPointer with CopperSpice Deleters for unique pointer were improved and standardized. The new CsIntrusivePointer class was brought in and QObject was redesigned to inherit from NodeManager. The reference count for a QObject is now part of the intrusive pointer class. This adds the ability to use an intrusive pointer to keep a child widget alive after its parent has been destroyed. As an example, an application has the ability to easily query a value of the child widget or move it to another parent widget.

Benefits of the CsPointer Library

CsPointer does not require or depend on any of the CopperSpice libraries or any other third party libraries. Since the license is BSD 2 Clause, any C++ developer can directly link CsPointer into their application.