An introduction to ptr may be found at http://www.bonner.rice.edu/adams/ptr .
The description below includes links to the code contained in this package. This version of the code may not be the most recent.
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.
The package includes the following components:
AbsPtr is not used elsewhere in this package but is provided to define an interface for other packages impelemnting pointer-like classes.
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.
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.
Questions or comments to adams@physics.rice.edu.