This is a smart pointer that implements "cross reference" semantics where a pointer data member of some object is intended to refer to some target object in a larger data structure.
More...
|
| ReferencePtr () |
| Default constructor creates an empty object. More...
|
|
| ReferencePtr (T *tp) |
| Construct from a given pointer stores the pointer. More...
|
|
| ReferencePtr (T &t) |
| Construct from a reference stores the address of the supplied object. More...
|
|
| ReferencePtr (const ReferencePtr &) |
| Copy constructor unconditionally sets the pointer to null; see class comments for why. More...
|
|
ReferencePtr & | operator= (const ReferencePtr &r) |
| Copy assignment sets the pointer to null (except for a self-assign); see class comments for why. More...
|
|
ReferencePtr & | operator= (T &t) |
| This form of assignment replaces the currently-referenced object by a reference to the source object; no destruction occurs. More...
|
|
ReferencePtr & | operator= (T *tp) |
| This form of assignment replaces the current pointer with the given one; no destruction occurs. More...
|
|
| ~ReferencePtr () |
| Destructor does nothing. More...
|
|
T * | operator-> () const |
| Return the contained pointer. More...
|
|
T & | operator* () const |
| The "dereference" operator returns a reference to the target object. More...
|
|
| operator T * () const |
| This is an implicit conversion from ReferencePtr<T> to T*. More...
|
|
| operator bool () const |
| This is an implicit conversion to type bool that returns true if the container is non-null (that is, not empty). More...
|
|
T * | get () const |
| Return the contained pointer, or null if the container is empty. More...
|
|
T & | getRef () const |
| Return a reference to the target object. More...
|
|
bool | empty () const |
| Return true if this container is empty. More...
|
|
void | clear () |
| Make this container empty; no destruction occurs. More...
|
|
T * | release () |
| Extract the pointer from this container, leaving the container empty. More...
|
|
void | reset (T *tp) |
| Replace the stored pointer with a different one; no destruction occurs. More...
|
|
void | swap (ReferencePtr &other) |
| Swap the contents of this ReferencePtr with another one. More...
|
|
template<class T>
class SimTK::ReferencePtr< T >
This is a smart pointer that implements "cross reference" semantics where a pointer data member of some object is intended to refer to some target object in a larger data structure.
Judicious use of this container will allow you to use compiler-generated copy constructors and copy assignment operators for classes which would otherwise have to implement their own in order to properly initialize these pointer data members, which must not be copied.
The contained pointer is initialized to null (0) on construction, and it is reinitialized to null upon copy construction or copy assignment. That's because we are assuming this is part of copying the entire data structure and copying the old pointer would create a reference into the old data structure rather than the new copy. This pointer does not own the target to which it points, and there is no reference counting so it will become stale if the target is deleted.
Whether you can write through the pointer is controlled by whether type T is a const type. For example ReferencePtr<int> is equivalent to an int*, while ReferencePtr<const int> is equivalent to a const int*.
This class is entirely inline and has no computational or space overhead; it contains just a single pointer.
- See also
- ClonePtr