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 () noexcept |
| Default constructor creates an empty object. More...
|
|
| ReferencePtr (std::nullptr_t) noexcept |
| Constructor from nullptr is the same as the default constructor. More...
|
|
| ReferencePtr (T *tp) noexcept |
| Construct from a given pointer stores the pointer. More...
|
|
| ReferencePtr (T &t) noexcept |
| Construct from a reference stores the address of the supplied object. More...
|
|
| ReferencePtr (const ReferencePtr &) noexcept |
| Copy constructor unconditionally sets the pointer to null; see class comments for why. More...
|
|
| ReferencePtr (ReferencePtr &&src) noexcept |
| Move constructor copies the pointer from the source and leaves the source empty. More...
|
|
| ReferencePtr (int mustBeZero) noexcept |
| (Deprecated) Use ReferencePtr(nullptr) or just ReferencePtr() instead. More...
|
|
|
ReferencePtr & | operator= (const ReferencePtr &src) noexcept |
| Copy assignment sets the pointer to nullptr (except for a self-assign); see class comments for why. More...
|
|
ReferencePtr & | operator= (ReferencePtr &&src) noexcept |
| Move assignment copies the pointer from the source and leaves the source empty. More...
|
|
ReferencePtr & | operator= (T &t) noexcept |
| This form of assignment replaces the currently-referenced object by a reference to the source object; no destruction occurs. More...
|
|
ReferencePtr & | operator= (T *tp) noexcept |
| This form of assignment replaces the current pointer with the given one; no destruction occurs. More...
|
|
|
| ~ReferencePtr () noexcept |
| Destructor does nothing. More...
|
|
|
T * | get () const noexcept |
| Return the contained pointer, or null if the container is empty. More...
|
|
T & | getRef () const |
| Return a reference to the target object. More...
|
|
T * | operator-> () const |
| Return the contained pointer. More...
|
|
T & | operator* () const |
| The "dereference" operator returns a reference to the target object. More...
|
|
|
void | reset (T *tp=nullptr) noexcept |
| Replace the stored pointer with a different one; no destruction occurs. More...
|
|
void | swap (ReferencePtr &other) noexcept |
| Swap the contents of this ReferencePtr with another one. More...
|
|
bool | empty () const noexcept |
| Return true if this container is empty. More...
|
|
| operator bool () const noexcept |
| This is a conversion to type bool that returns true if the container is non-null (that is, not empty). More...
|
|
T * | release () noexcept |
| Extract the pointer from this container, leaving the container empty. More...
|
|
void | clear () noexcept |
| (Deprecated) Use reset() instead. More...
|
|
| operator T* () const noexcept |
| (Deprecated) Use get() rather than implicit conversion from ReferencePtr<T> to T*. More...
|
|
|
(Note that these are not member functions.)
|
template<class T > |
void | swap (ReferencePtr< T > &p1, ReferencePtr< T > &p2) noexcept |
| This is an overload of the STL std::swap() algorithm which uses the cheap built-in swap() member of the ReferencePtr class. More...
|
|
template<class charT , class traits , class T > |
std::basic_ostream< charT, traits > & | operator<< (std::basic_ostream< charT, traits > &os, const ReferencePtr< T > &p) |
| Output the system-dependent representation of the pointer contained in a ReferencePtr object. More...
|
|
template<class T , class U > |
bool | operator== (const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs) |
| Compare for equality the managed pointers contained in two compatible ReferencePtr containers. More...
|
|
template<class T > |
bool | operator== (const ReferencePtr< T > &lhs, std::nullptr_t) |
| Comparison against nullptr ; same as lhs.empty() . More...
|
|
template<class T > |
bool | operator== (std::nullptr_t, const ReferencePtr< T > &rhs) |
| Comparison against nullptr ; same as rhs.empty() . More...
|
|
template<class T , class U > |
bool | operator< (const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs) |
| Less-than operator for two compatible ReferencePtr containers, comparing the pointers, not the objects they point to. More...
|
|
template<class T > |
bool | operator< (const ReferencePtr< T > &lhs, std::nullptr_t) |
| Less-than comparison against a nullptr . More...
|
|
template<class T > |
bool | operator< (std::nullptr_t, const ReferencePtr< T > &rhs) |
| Less-than comparison of a nullptr against this container. More...
|
|
template<class T , class U > |
bool | operator!= (const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs) |
| Pointer inequality test defined as !(lhs==rhs) . More...
|
|
template<class T > |
bool | operator!= (const ReferencePtr< T > &lhs, std::nullptr_t) |
| nullptr inequality test defined as !(lhs==nullptr) . More...
|
|
template<class T > |
bool | operator!= (std::nullptr_t, const ReferencePtr< T > &rhs) |
| nullptr inequality test defined as !(nullptr==rhs) . More...
|
|
template<class T , class U > |
bool | operator> (const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs) |
| Pointer greater-than test defined as rhs < lhs . More...
|
|
template<class T > |
bool | operator> (const ReferencePtr< T > &lhs, std::nullptr_t) |
| nullptr greater-than test defined as nullptr < lhs . More...
|
|
template<class T > |
bool | operator> (std::nullptr_t, const ReferencePtr< T > &rhs) |
| nullptr greater-than test defined as rhs < nullptr . More...
|
|
template<class T , class U > |
bool | operator>= (const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs) |
| Pointer greater-or-equal test defined as !(lhs < rhs) . More...
|
|
template<class T > |
bool | operator>= (const ReferencePtr< T > &lhs, std::nullptr_t) |
| nullptr greater-or-equal test defined as !(lhs < nullptr) . More...
|
|
template<class T > |
bool | operator>= (std::nullptr_t, const ReferencePtr< T > &rhs) |
| nullptr greater-or-equal test defined as !(nullptr < rhs) . More...
|
|
template<class T , class U > |
bool | operator<= (const ReferencePtr< T > &lhs, const ReferencePtr< U > &rhs) |
| Pointer less-or-equal test defined as !(rhs < lhs) (note reversed arguments). More...
|
|
template<class T > |
bool | operator<= (const ReferencePtr< T > &lhs, std::nullptr_t) |
| nullptr less-or-equal test defined as !(nullptr < lhs) (note reversed arguments). More...
|
|
template<class T > |
bool | operator<= (std::nullptr_t, const ReferencePtr< T > &rhs) |
| nullptr less-or-equal test defined as !(rhs < nullptr) (note reversed arguments). More...
|
|
template<class T > |
bool | operator!= (const ReferencePtr< T > &lhs, int mustBeZero) |
| (Deprecated) Use nullptr instead of 0 . 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 nullptr
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.
The pointer is moved intact for move construction or move assignment. That allows std::vector<ReferencePtr<T>>
or SimTK::Array_<ReferencePtr<T>>
to behave properly when their contents have to be moved for expansion.
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, CloneOnWritePtr