Pointer classes (package ptr)

29jan99 1120
Code version 2.06

An introduction to ptr may be found at http://www.bonner.rice.edu/adams/ptr .


Links to code

The description below includes links to the code contained in this package. This version of the code may not be the most recent.

Versions

Version 2.0 is a complete rewrite which is, for the most part, not backward compatible with versions 1.07 and earlier.

Description

The package ptr provides pointer classes which provide an extensible list of policies for memory management. Objects of templated type Ptr<T,P> behave like pointers of type T* with management policy P. Policies maintain (possibly shared) management of an object and the pointer uses them to determine whether to delete the object and whether the object is in a valid state.

Pointers may be copied or assigned according to the same rules as bare C++ pointers, i.e. mutable-to-const and derived-to-base are allowed but the converses are not. Copy or assignment may be made between pointers with policies of the same or different type. The policies will negotiate to determine which, if either, retains management. A run-time error (exception) will result if this negotiation is not successful.

For compilers which do not support templated member functions, all policies derive from the abstract base AbsPolicy and the interface is defined by a series of virtual member functions. On standards-compliant compilers (such as KCC), the same functions are implemented in classes which do not inherit from this base. Typically all functions are inclined to minimize overhead. Users can easily add their own policies meeting this interface.


Code

The package includes the following components:


Exceptions

The header PtrException defines the list of known exception classes. Most interesting is PtrInvalidPointer which is thrown when an attempt is made to dereference an invalid pointer. Others are thrown when two pointers are not able to negotiate for management. All ptr exceptions inherit from the base PtrException.

Abstract pointer

The template AbsPtr<T> defines the user interface for a class which behaves like a pointer to type T. The usual dereference (*px) and member selection operators (px->method()) are provided and the bare pointer may be fetched with method pointer(). Automatic conversion to const void* allows pointer comparison and validity checking (if ( px ) ...).

AbsPtr is not used elsewhere in this package but is provided to define an interface for other packages impelemnting pointer-like classes.

Pointer template

The top-level pointer template class Ptr<T,P> is described in component Ptr. It should work with any built-in or user-defined class T and any of the policies P.

Ptr pointers can be constructed or assigned from bare pointers or any other bare pointers following the usual rules for assigning C++ pointers. The pointer template does not inherit from AbsPtr but it does provide the same interface.

Validity is checked by conversion to const void*, e.g. "if ( px ) ..." will take action oly if the pointer is valid. The accuracy of this information depends on the type of policy and whether it has been created from a bare pointer or another policy. No policy can know if the user has deleted the object through the bare pointer. Policies negotiate and, in some cases, share management so the information in multiple pointers may be correct.

Policies

Five different policies are presently provided. The first of these, NullPolicy is the default. It provides no management and will return return valid until it is explicitly set invalid. In normal use, it will always return valid after being constructed or assigned from a bare pointer or valid ptr pointer.

DeletePolicy provides simple single-owner management. It takes mangement when constructed from a bare pointer or another policy. An exception is thrown if the original policy does not have management or will not relinquish management. It is inflexible but safe.

AutoPolicy provides behavior similar to that of the C++ auto_ptr class. It takes management when constructed from a bare pointer. If it is copied or assigned from another policy, it takes management if the latter has management and is willing to relinquish it. It gives up management on request. In particular, if one AutoPolicy is copied to a second, management is handed off to the second and the first becomes invalid.

SharedNullPolicy holds a pointer to a shared policy table. If its is copied from another pointer holding such a table, it shares the table. Ohterwise it creates a new table. The pointer remains valid as long as the table remains valid.

SharedDeletePolicy also holds a pointer to a shared policy table and also shares an existing table or, if not present, creates a new one. When copied from a bare pointer, it takes shared management, i.e. sets a managing reference in the table. When copied from a pointer with a shared policy table, it shares management, i.e. sets a managing reference in the table and does not ask the original pointer to give up management. When copied fom any other kind of pointer, it takes management. If the original policy will not relinquish management, an exception is thrown.

Shared policy table

Objects of type SharedPolicyTable maintain reference counts and validity. The reference counts include a total count and a count of managing references. A table is held by one or more policies which add a reference when they are constructed or assigned and drop a reference when they are deleted.


Questions or comments to adams@physics.rice.edu.