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

Wrap a pointer to an abstract base class in a way that makes it behave like a concrete class. More...

Public Types

typedef T element_type
 
typedef T * pointer
 
typedef T & reference
 

Public Member Functions

 ClonePtr ()
 Default constructor creates an empty object. More...
 
 ClonePtr (T *obj)
 Given a pointer to a writable heap-allocated object, take over ownership of that object. More...
 
 ClonePtr (T **obj)
 Given a pointer to a writable heap-allocated object, take over ownership of that object and set the original pointer to null. More...
 
 ClonePtr (const T *obj)
 Given a pointer to a read-only object, create a new heap-allocated copy of that object via its clone() method and make this ClonePtr object the owner of the copy. More...
 
 ClonePtr (const T &obj)
 Given a read-only reference to an object, create a new heap-allocated copy of that object via its clone() method and make this ClonePtr object the owner of the copy. More...
 
 ClonePtr (const ClonePtr &c)
 Copy constructor is deep; the new ClonePtr object contains a new copy of the object in the source, created via the source object's clone() method. More...
 
ClonePtroperator= (const ClonePtr &c)
 Copy assignment replaces the currently-held object by a heap-allocated copy of the object held in the source container. More...
 
ClonePtroperator= (const T &t)
 This form of assignment replaces the currently-held object by a heap-allocated copy of the source object. More...
 
ClonePtroperator= (T *tp)
 This form of assignment replaces the currently-held object by the given source object and takes over ownership of the source object. More...
 
 ~ClonePtr ()
 Destructor deletes the referenced object. More...
 
bool operator== (const ClonePtr &other) const
 Compare the contained objects for equality using the contained objects' operator==() operator. More...
 
bool operator!= (const ClonePtr &other) const
 Compare the contained objects for inequality using operator==(). More...
 
bool operator< (const ClonePtr &other) const
 Provide an ordering for use in sorted containers using the contained objects' operator<(). More...
 
const T * operator-> () const
 Dereference a const pointer to the contained object. More...
 
T * operator-> ()
 Dereference a writable pointer to the contained object. More...
 
const T & operator* () const
 This "dereference" operator returns a const reference to the contained object. More...
 
T & operator* ()
 This "dereference" operator returns a writable reference to the contained object. More...
 
const T * operator& () const
 This "address of" operator returns a const pointer to the contained object (or null if none). More...
 
T * operator& ()
 This "address of" operator returns a writable pointer to the contained object (or null if none). More...
 
 operator const T & () const
 This is an implicit conversion from ClonePtr<T> to a const reference to the contained object. More...
 
 operator T & ()
 This is an implicit conversion from ClonePtr<T> to a writable reference to the contained object. 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 * updPtr ()
 Return a writable pointer to the contained object if any, or null. More...
 
const T * getPtr () const
 Return a const pointer to the contained object if any, or null. More...
 
T & updRef ()
 Return a writable reference to the contained object. More...
 
const T & getRef () const
 Return a const reference to the contained object. More...
 
bool empty () const
 Return true if this container is empty. More...
 
void clear ()
 Make this container empty, deleting the currently contained object if there is one. More...
 
T * release ()
 Extract the object from this container, leaving the container empty and transferring ownership to the caller. More...
 
void reset (T *tp)
 Replace the contents of this container with the supplied heap-allocated object, taking over ownership of that object and deleting the current one first if necessary. More...
 
void reset (T **tpp)
 Replace the contents of this container with the supplied heap-allocated object, taking over ownership of that object, deleting the current one first if necessary, and setting the caller's supplied pointer to null. More...
 
void swap (ClonePtr &other)
 Swap the contents of this ClonePtr with another one, with ownership changing hands but no copying performed. More...
 

Related Functions

(Note that these are not member functions.)

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

Detailed Description

template<class T>
class SimTK::ClonePtr< T >

Wrap a pointer to an abstract base class in a way that makes it behave like a concrete class.

This is similar to std::unique_ptr in that it does not permit shared ownership of the object. However, unlike std::unique_ptr, ClonePtr supports copy and assigment operations, by insisting that the contained object have a clone() method that returns a pointer to a heap-allocated deep copy of the concrete object.

We define operator==() and operator<() here that delegate to the contained object; if you want to use those the contained type must support that operator.

This class is entirely inline and has no computational or space overhead; it contains just a single pointer and does no reference counting.

See also
ReferencePtr

Member Typedef Documentation

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

Constructor & Destructor Documentation

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

Default constructor creates an empty object.

template<class T>
SimTK::ClonePtr< T >::ClonePtr ( T *  obj)
inlineexplicit

Given a pointer to a writable heap-allocated object, take over ownership of that object.

template<class T>
SimTK::ClonePtr< T >::ClonePtr ( T **  obj)
inlineexplicit

Given a pointer to a writable heap-allocated object, take over ownership of that object and set the original pointer to null.

template<class T>
SimTK::ClonePtr< T >::ClonePtr ( const T *  obj)
inlineexplicit

Given a pointer to a read-only object, create a new heap-allocated copy of that object via its clone() method and make this ClonePtr object the owner of the copy.

Ownership of the original object is not affected. If the supplied pointer is null, the resulting ClonePtr object is empty.

template<class T>
SimTK::ClonePtr< T >::ClonePtr ( const T &  obj)
inlineexplicit

Given a read-only reference to an object, create a new heap-allocated copy of that object via its clone() method and make this ClonePtr object the owner of the copy.

Ownership of the original object is not affected.

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

Copy constructor is deep; the new ClonePtr object contains a new copy of the object in the source, created via the source object's clone() method.

If the source container is empty this one will be empty also.

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

Destructor deletes the referenced object.

Member Function Documentation

template<class T>
ClonePtr& SimTK::ClonePtr< T >::operator= ( const ClonePtr< T > &  c)
inline

Copy assignment replaces the currently-held object by a heap-allocated copy of the object held in the source container.

The copy is created using the source object's clone() method. The currently-held object is deleted. If the source container is empty this one will be empty after the assignment.

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

This form of assignment replaces the currently-held object by a heap-allocated copy of the source object.

The copy is created using the source object's clone() method. The currently-held object is deleted.

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

This form of assignment replaces the currently-held object by the given source object and takes over ownership of the source object.

The currently-held object is deleted.

template<class T>
bool SimTK::ClonePtr< T >::operator== ( const ClonePtr< T > &  other) const
inline

Compare the contained objects for equality using the contained objects' operator==() operator.

If both containers are empty or both refer to the same object they will test equal; if only one is empty they will test not equal. Otherwise the objects are compared.

template<class T>
bool SimTK::ClonePtr< T >::operator!= ( const ClonePtr< T > &  other) const
inline

Compare the contained objects for inequality using operator==().

template<class T>
bool SimTK::ClonePtr< T >::operator< ( const ClonePtr< T > &  other) const
inline

Provide an ordering for use in sorted containers using the contained objects' operator<().

If both containers are empty or both refer to the same object they will test equal; if only one is empty that one is considered less than the other one. Otherwise the objects are compared using T::operator<().

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

Dereference a const pointer to the contained object.

This will fail if the container is empty.

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

Dereference a writable pointer to the contained object.

This will fail if the container is empty.

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

This "dereference" operator returns a const reference to the contained object.

This will fail if the container is empty.

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

This "dereference" operator returns a writable reference to the contained object.

This will fail if the container is empty.

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

This "address of" operator returns a const pointer to the contained object (or null if none).

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

This "address of" operator returns a writable pointer to the contained object (or null if none).

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

This is an implicit conversion from ClonePtr<T> to a const reference to the contained object.

This will fail if the container is empty.

template<class T>
SimTK::ClonePtr< T >::operator T & ( )
inline

This is an implicit conversion from ClonePtr<T> to a writable reference to the contained object.

template<class T>
SimTK::ClonePtr< 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::ClonePtr< T >::updPtr ( )
inline

Return a writable pointer to the contained object if any, or null.

You can use the "address of" operator&() instead if you prefer.

template<class T>
const T* SimTK::ClonePtr< T >::getPtr ( ) const
inline

Return a const pointer to the contained object if any, or null.

You can use the "address of" operator&() instead if you prefer.

template<class T>
T& SimTK::ClonePtr< T >::updRef ( )
inline

Return a writable reference to the contained object.

Don't call this if this container is empty. There is also an implicit conversion to reference that allows ClonePtr<T> to be used as though it were a T&.

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

Return a const reference to the contained object.

Don't call this if this container is empty. There is also an implicit conversion to reference that allows ClonePtr<T> to be used as though it were a T&.

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

Return true if this container is empty.

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

Make this container empty, deleting the currently contained object if there is one.

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

Extract the object from this container, leaving the container empty and transferring ownership to the caller.

A pointer to the object is returned. No destruction occurs.

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

Replace the contents of this container with the supplied heap-allocated object, taking over ownership of that object and deleting the current one first if necessary.

Nothing happens if the supplied pointer is the same as the one already stored.

template<class T>
void SimTK::ClonePtr< T >::reset ( T **  tpp)
inline

Replace the contents of this container with the supplied heap-allocated object, taking over ownership of that object, deleting the current one first if necessary, and setting the caller's supplied pointer to null.

If the supplied pointer is the same as the one we're already storing, then we just set the caller's pointer to null and return.

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

Swap the contents of this ClonePtr with another one, with ownership changing hands but no copying performed.

Friends And Related Function Documentation

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

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


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