Simbody  3.5
SimTK::ReferencePtr< T > Class Template Reference

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...

Public Types

typedef T element_type
 
typedef T * pointer
 
typedef T & reference
 

Public Member Functions

 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...
 
ReferencePtroperator= (const ReferencePtr &r)
 Copy assignment sets the pointer to null (except for a self-assign); see class comments for why. More...
 
ReferencePtroperator= (T &t)
 This form of assignment replaces the currently-referenced object by a reference to the source object; no destruction occurs. More...
 
ReferencePtroperator= (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...
 

Related Functions

(Note that these are not member functions.)

template<class T >
void swap (SimTK::ReferencePtr< T > &p1, SimTK::ReferencePtr< T > &p2)
 This is a specialization of the STL std::swap() algorithm which uses the cheap built-in swap() member of the ReferencePtr class. More...
 

Detailed Description

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

Member Typedef Documentation

template<class T>
typedef T SimTK::ReferencePtr< T >::element_type
template<class T>
typedef T* SimTK::ReferencePtr< T >::pointer
template<class T>
typedef T& SimTK::ReferencePtr< T >::reference

Constructor & Destructor Documentation

template<class T>
SimTK::ReferencePtr< T >::ReferencePtr ( )
inline

Default constructor creates an empty object.

template<class T>
SimTK::ReferencePtr< T >::ReferencePtr ( T *  tp)
inlineexplicit

Construct from a given pointer stores the pointer.

template<class T>
SimTK::ReferencePtr< T >::ReferencePtr ( T &  t)
inlineexplicit

Construct from a reference stores the address of the supplied object.

template<class T>
SimTK::ReferencePtr< T >::ReferencePtr ( const ReferencePtr< T > &  )
inline

Copy constructor unconditionally sets the pointer to null; see class comments for why.

template<class T>
SimTK::ReferencePtr< T >::~ReferencePtr ( )
inline

Destructor does nothing.

Member Function Documentation

template<class T>
ReferencePtr& SimTK::ReferencePtr< T >::operator= ( const ReferencePtr< T > &  r)
inline

Copy assignment sets the pointer to null (except for a self-assign); see class comments for why.

template<class T>
ReferencePtr& SimTK::ReferencePtr< T >::operator= ( T &  t)
inline

This form of assignment replaces the currently-referenced object by a reference to the source object; no destruction occurs.

template<class T>
ReferencePtr& SimTK::ReferencePtr< T >::operator= ( T *  tp)
inline

This form of assignment replaces the current pointer with the given one; no destruction occurs.

template<class T>
T* SimTK::ReferencePtr< T >::operator-> ( ) const
inline

Return the contained pointer.

This will fail if the container is empty.

template<class T>
T& SimTK::ReferencePtr< T >::operator* ( ) const
inline

The "dereference" operator returns a reference to the target object.

This will fail if the container is empty.

template<class T>
SimTK::ReferencePtr< T >::operator T * ( ) const
inline

This is an implicit conversion from ReferencePtr<T> to T*.

template<class T>
SimTK::ReferencePtr< T >::operator bool ( ) const
inline

This is an implicit conversion to type bool that returns true if the container is non-null (that is, not empty).

template<class T>
T* SimTK::ReferencePtr< T >::get ( ) const
inline

Return the contained pointer, or null if the container is empty.

template<class T>
T& SimTK::ReferencePtr< T >::getRef ( ) const
inline

Return a reference to the target object.

Fails if the pointer is null.

template<class T>
bool SimTK::ReferencePtr< T >::empty ( ) const
inline

Return true if this container is empty.

template<class T>
void SimTK::ReferencePtr< T >::clear ( )
inline

Make this container empty; no destruction occurs.

template<class T>
T* SimTK::ReferencePtr< T >::release ( )
inline

Extract the pointer from this container, leaving the container empty.

The pointer is returned.

template<class T>
void SimTK::ReferencePtr< T >::reset ( T *  tp)
inline

Replace the stored pointer with a different one; no destruction occurs.

template<class T>
void SimTK::ReferencePtr< T >::swap ( ReferencePtr< T > &  other)
inline

Swap the contents of this ReferencePtr with another one.

Friends And Related Function Documentation

template<class T >
void swap ( SimTK::ReferencePtr< T > &  p1,
SimTK::ReferencePtr< T > &  p2 
)
related

This is a specialization of the STL std::swap() algorithm which uses the cheap built-in swap() member of the ReferencePtr class.


The documentation for this class was generated from the following file: