ROSE  0.9.9.139
Public Types | Public Member Functions | Friends | List of all members
Sawyer::SharedPointer< T > Class Template Reference

Description

template<class T>
class Sawyer::SharedPointer< T >

Reference-counting smart pointer.

This class is a reference-counting pointer to an object that inherits from SharedObject. Usage is similar to boost::shared_ptr.

See also
SharedObject, SharedFromThis

Definition at line 34 of file SharedPointer.h.

#include <SharedPointer.h>

Inheritance diagram for Sawyer::SharedPointer< T >:
Inheritance graph
[legend]

Public Types

typedef T Pointee
 

Public Member Functions

 SharedPointer ()
 Constructs an empty shared pointer. More...
 
template<class Y >
 SharedPointer (Y *rawPtr)
 Constructs a shared pointer for an object. More...
 
 ~SharedPointer ()
 Conditionally deletes the pointed-to object. More...
 
SharedPointeroperator= (const Sawyer::Nothing &)
 Assignment. More...
 
T & operator* () const
 Reference to the pointed-to object. More...
 
T * operator-> () const
 Dereference pointed-to object. More...
 
template<class U >
SharedPointer< U > dynamicCast () const
 Dynamic cast. More...
 
bool operator! () const
 Boolean complement. More...
 
 operator unspecified_bool () const
 Type for Boolean context. More...
 
 SharedPointer (const SharedPointer &other)
 Constructs a new pointer that shares ownership of the pointed-to object with the other pointer. More...
 
template<class Y >
 SharedPointer (const SharedPointer< Y > &other)
 Constructs a new pointer that shares ownership of the pointed-to object with the other pointer. More...
 
SharedPointeroperator= (const SharedPointer &other)
 Assignment. More...
 
template<class Y >
SharedPointeroperator= (const SharedPointer< Y > &other)
 Assignment. More...
 
template<class U >
bool operator== (const SharedPointer< U > &other) const
 Comparison of two pointers. More...
 
template<class U >
bool operator!= (const SharedPointer< U > &other) const
 Comparison of two pointers. More...
 
template<class U >
bool operator< (const SharedPointer< U > &other) const
 Comparison of two pointers. More...
 
template<class U >
bool operator<= (const SharedPointer< U > &other) const
 Comparison of two pointers. More...
 
template<class U >
bool operator> (const SharedPointer< U > &other) const
 Comparison of two pointers. More...
 
template<class U >
bool operator>= (const SharedPointer< U > &other) const
 Comparison of two pointers. More...
 
template<class U >
bool operator== (const U *ptr) const
 Comparison of two pointers. More...
 
template<class U >
bool operator!= (const U *ptr) const
 Comparison of two pointers. More...
 
template<class U >
bool operator< (const U *ptr) const
 Comparison of two pointers. More...
 
template<class U >
bool operator<= (const U *ptr) const
 Comparison of two pointers. More...
 
template<class U >
bool operator> (const U *ptr) const
 Comparison of two pointers. More...
 
template<class U >
bool operator>= (const U *ptr) const
 Comparison of two pointers. More...
 

Friends

std::ostream & operator<< (std::ostream &out, const SharedPointer &ptr)
 Print a shared pointer. More...
 
Pointee * getRawPointer (const SharedPointer &ptr)
 Obtain the pointed-to object. More...
 
size_t ownershipCount (const SharedPointer &ptr)
 Returns the pointed-to object's reference count. More...
 

Constructor & Destructor Documentation

template<class T>
Sawyer::SharedPointer< T >::SharedPointer ( )
inline

Constructs an empty shared pointer.

Definition at line 66 of file SharedPointer.h.

template<class T>
Sawyer::SharedPointer< T >::SharedPointer ( const SharedPointer< T > &  other)
inline

Constructs a new pointer that shares ownership of the pointed-to object with the other pointer.

The pointed-to object will only be deleted after both pointers are deleted.

Definition at line 71 of file SharedPointer.h.

template<class T>
template<class Y >
Sawyer::SharedPointer< T >::SharedPointer ( const SharedPointer< Y > &  other)
inline

Constructs a new pointer that shares ownership of the pointed-to object with the other pointer.

The pointed-to object will only be deleted after both pointers are deleted.

Definition at line 75 of file SharedPointer.h.

template<class T>
template<class Y >
Sawyer::SharedPointer< T >::SharedPointer ( Y *  rawPtr)
inlineexplicit

Constructs a shared pointer for an object.

If obj is non-null then its reference count is incremented. It is possible to create any number of shared pointers to the same object using this constructor. The expression "delete obj" must be well formed and must not invoke undefined behavior.

Definition at line 86 of file SharedPointer.h.

template<class T>
Sawyer::SharedPointer< T >::~SharedPointer ( )
inline

Conditionally deletes the pointed-to object.

The object is deleted when its reference count reaches zero.

Definition at line 92 of file SharedPointer.h.

Member Function Documentation

template<class T>
SharedPointer& Sawyer::SharedPointer< T >::operator= ( const SharedPointer< T > &  other)
inline

Assignment.

This pointer is caused to point to the same object as other, decrementing the reference count for the object originally pointed to by this pointer and incrementing the reference count for the object pointed by other.

Definition at line 100 of file SharedPointer.h.

template<class T>
template<class Y >
SharedPointer& Sawyer::SharedPointer< T >::operator= ( const SharedPointer< Y > &  other)
inline

Assignment.

This pointer is caused to point to the same object as other, decrementing the reference count for the object originally pointed to by this pointer and incrementing the reference count for the object pointed by other.

Definition at line 104 of file SharedPointer.h.

template<class T>
SharedPointer& Sawyer::SharedPointer< T >::operator= ( const Sawyer::Nothing )
inline

Assignment.

This pointer is caused to point to nothing.

Definition at line 116 of file SharedPointer.h.

template<class T>
T& Sawyer::SharedPointer< T >::operator* ( ) const
inline

Reference to the pointed-to object.

An assertion will fail if assertions are enabled and this method is invoked on an empty pointer.

Definition at line 125 of file SharedPointer.h.

template<class T>
T* Sawyer::SharedPointer< T >::operator-> ( ) const
inline

Dereference pointed-to object.

The pointed-to object is returned. Returns null for empty pointers.

Definition at line 132 of file SharedPointer.h.

template<class T>
template<class U >
SharedPointer<U> Sawyer::SharedPointer< T >::dynamicCast ( ) const
inline
template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator== ( const SharedPointer< U > &  other) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 153 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator!= ( const SharedPointer< U > &  other) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 157 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator< ( const SharedPointer< U > &  other) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 161 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator<= ( const SharedPointer< U > &  other) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 165 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator> ( const SharedPointer< U > &  other) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 169 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator>= ( const SharedPointer< U > &  other) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 173 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator== ( const U *  ptr) const
inline

Comparison of two pointers.

Compares the underlying pointer with the specified pointer.

Definition at line 183 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator!= ( const U *  ptr) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 187 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator< ( const U *  ptr) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 191 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator<= ( const U *  ptr) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 195 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator> ( const U *  ptr) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 199 of file SharedPointer.h.

template<class T>
template<class U >
bool Sawyer::SharedPointer< T >::operator>= ( const U *  ptr) const
inline

Comparison of two pointers.

Comparisons operators compare the underlying pointers to objects.

Definition at line 203 of file SharedPointer.h.

template<class T>
bool Sawyer::SharedPointer< T >::operator! ( ) const
inline

Boolean complement.

This operator allows shared pointers to be used in situations like this:

SharedPointer<Object> obj = ...;
if (!obj) ...

Definition at line 216 of file SharedPointer.h.

template<class T>
Sawyer::SharedPointer< T >::operator unspecified_bool ( ) const
inline

Type for Boolean context.

Implicit conversion to a type that can be used in a boolean context such as an if or while statement. For instance:

SharedPointer<Object> x = ...;
if (x) {
//this is reached
}

Definition at line 243 of file SharedPointer.h.

Friends And Related Function Documentation

template<class T>
std::ostream& operator<< ( std::ostream &  out,
const SharedPointer< T > &  ptr 
)
friend

Print a shared pointer.

Printing a shared pointer is the same as printing the pointee's address.

Definition at line 221 of file SharedPointer.h.

template<class T>
Pointee* getRawPointer ( const SharedPointer< T > &  ptr)
friend

Obtain the pointed-to object.

The pointed-to object is returned. Returns null for empty pointers. An idiom for getting a raw pointer for a shared pointer that's known to be non-null is to dereference the shared pointer and then take the address:

SharedPointer<MyType> ptr = ...;
MyType *obj = &*ptr;

Definition at line 266 of file SharedPointer.h.

Referenced by Sawyer::SharedPointer< ValueSaver >::operator!=(), Sawyer::SharedPointer< ValueSaver >::operator<(), Sawyer::SharedPointer< ValueSaver >::operator<=(), Sawyer::SharedPointer< ValueSaver >::operator=(), Sawyer::SharedPointer< ValueSaver >::operator==(), Sawyer::SharedPointer< ValueSaver >::operator>(), and Sawyer::SharedPointer< ValueSaver >::operator>=().

template<class T>
size_t ownershipCount ( const SharedPointer< T > &  ptr)
friend

Returns the pointed-to object's reference count.

Returns zero for empty pointers.

Definition at line 271 of file SharedPointer.h.

Referenced by Sawyer::SharedPointer< ValueSaver >::operator*(), and Sawyer::SharedPointer< ValueSaver >::operator->().


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