#include <allocatingpointer.h>
Inheritance diagram for OwningPointer< T, Cloner, default_own >:
The differences from regular pointer semantics are intended to be the following:
1) On construction a value may be allocated (if called with a scalar). 2) On destruction any allocated value will be deallocated. 3) When copied, the copy will only point to the same memory location if that location has its own lifetime, i.e. was an external object; otherwise a new object is allocated and set to have the same value as the original.
A few operations not supported by pointers are also provided, and not all operations supported by pointers have necessarily been implemented. (E.g. use (*ap). instead of ap-> for now.)
Using the default Cloner template argument, this class cannot be used with an object of an unknown type derived from the nominal templated type, since it needs to be able to create a copy of the object. If the objects offer a virtual clone() procedure, you can supply a different Cloner (e.g. CopyUsingClone) which supports copying of the actual types; this will allow unknown derived types to be supplied without fear of slicing.
Definition at line 63 of file allocatingpointer.h.
Public Methods | |
OwningPointer (const T &value) | |
Constructor for an internally-allocated variable. | |
OwningPointer (T *value_pointer, bool owns_object=default_own) | |
Constructor for a pointer to an external value. | |
OwningPointer (const self &other) | |
Copy constructor; copies other's pointer only if it refers to an externally-managed object. | |
void | operator= (const T &new_value) |
Override the underlying value with a new one. | |
self & | operator= (const self &o) |
Copy assignment. | |
operator bool () const | |
Conversion to Boolean; acts like underlying pointer. | |
const T & | operator * () const |
Returns the current underlying value. | |
const T * | operator-> () const |
Const pointer dereference. | |
T * | operator-> () |
Pointer dereference. | |
T * | valueptr () const |
Returns a pointer to the underlying value; use with caution. | |
T *& | valueptrref () |
Returns a modifiable pointer to the underlying value; use with extreme caution. | |
Operations that may be overridden by derived classes. | |
Could support clone() here except that the Cray compiler doesn't allow the return type to vary even trivially, and a type ValueGenerator was chosen for the standard return type. | |
void | relink (T *ptr) |
If our pointer is external, overwrite it with the given one. | |
Protected Methods | |
T * | allocate (const T &value) const |
Allocates and returns a pointer to a new item set to the given value. |