Nugget
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
eastl::shared_ptr< T > Class Template Reference

#include <shared_ptr.h>

Collaboration diagram for eastl::shared_ptr< T >:
Collaboration graph
[legend]

Public Types

typedef shared_ptr< T > this_type
 
typedef T element_type
 
typedef shared_ptr_traits< T >::reference_type reference_type
 
typedef EASTLAllocatorType default_allocator_type
 
typedef default_delete< T > default_deleter_type
 
typedef weak_ptr< T > weak_type
 

Public Member Functions

 shared_ptr () EA_NOEXCEPT
 Base pointer to Reference count for owned pointer and the owned pointer. More...
 
template<typename U >
 shared_ptr (U *pValue, typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *=0)
 
 shared_ptr (std::nullptr_t) EA_NOEXCEPT
 
template<typename U , typename Deleter >
 shared_ptr (U *pValue, Deleter deleter, typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *=0)
 
template<typename Deleter >
 shared_ptr (std::nullptr_t, Deleter deleter)
 
template<typename U , typename Deleter , typename Allocator >
 shared_ptr (U *pValue, Deleter deleter, const Allocator &allocator, typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *=0)
 
template<typename Deleter , typename Allocator >
 shared_ptr (std::nullptr_t, Deleter deleter, Allocator allocator)
 
 shared_ptr (const shared_ptr &sharedPtr) EA_NOEXCEPT
 
template<typename U >
 shared_ptr (const shared_ptr< U > &sharedPtr, typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *=0) EA_NOEXCEPT
 
template<typename U >
 shared_ptr (const shared_ptr< U > &sharedPtr, element_type *pValue) EA_NOEXCEPT
 
 shared_ptr (shared_ptr &&sharedPtr) EA_NOEXCEPT
 
template<typename U >
 shared_ptr (shared_ptr< U > &&sharedPtr, typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *=0) EA_NOEXCEPT
 
template<typename U , typename Deleter >
 shared_ptr (unique_ptr< U, Deleter > &&uniquePtr, typename eastl::enable_if<!eastl::is_array< U >::value &&!is_lvalue_reference< Deleter >::value &&eastl::is_convertible< U *, element_type * >::value >::type *=0)
 
template<typename U , typename Deleter , typename Allocator >
 shared_ptr (unique_ptr< U, Deleter > &&uniquePtr, const Allocator &allocator, typename eastl::enable_if<!eastl::is_array< U >::value &&!is_lvalue_reference< Deleter >::value &&eastl::is_convertible< U *, element_type * >::value >::type *=0)
 
template<typename U >
 shared_ptr (const weak_ptr< U > &weakPtr, typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *=0)
 
 ~shared_ptr ()
 
shared_ptroperator= (const shared_ptr &sharedPtr) EA_NOEXCEPT
 
template<typename U >
eastl::enable_if< eastl::is_convertible< U *, element_type * >::value, this_type & >::type operator= (const shared_ptr< U > &sharedPtr) EA_NOEXCEPT
 
this_typeoperator= (shared_ptr &&sharedPtr) EA_NOEXCEPT
 
template<typename U >
eastl::enable_if< eastl::is_convertible< U *, element_type * >::value, this_type & >::type operator= (shared_ptr< U > &&sharedPtr) EA_NOEXCEPT
 
template<typename U , typename Deleter >
eastl::enable_if<!eastl::is_array< U >::value &&eastl::is_convertible< U *, element_type * >::value, this_type & >::type operator= (unique_ptr< U, Deleter > &&uniquePtr)
 
void reset () EA_NOEXCEPT
 
template<typename U >
eastl::enable_if< eastl::is_convertible< U *, element_type * >::value, void >::type reset (U *pValue)
 
template<typename U , typename Deleter >
eastl::enable_if< eastl::is_convertible< U *, element_type * >::value, void >::type reset (U *pValue, Deleter deleter)
 
template<typename U , typename Deleter , typename Allocator >
eastl::enable_if< eastl::is_convertible< U *, element_type * >::value, void >::type reset (U *pValue, Deleter deleter, const Allocator &allocator)
 
void swap (this_type &sharedPtr) EA_NOEXCEPT
 
reference_type operator* () const EA_NOEXCEPT
 
element_type * operator-> () const EA_NOEXCEPT
 
element_type * get () const EA_NOEXCEPT
 
int use_count () const EA_NOEXCEPT
 
bool unique () const EA_NOEXCEPT
 
template<typename U >
bool owner_before (const shared_ptr< U > &sharedPtr) const EA_NOEXCEPT
 
template<typename U >
bool owner_before (const weak_ptr< U > &weakPtr) const EA_NOEXCEPT
 
template<typename Deleter >
Deleter * get_deleter () const EA_NOEXCEPT
 
 operator bool () const EA_NOEXCEPT
 
template<typename U >
bool equivalent_ownership (const shared_ptr< U > &sharedPtr) const
 Returns true if the given shared_ptr ows the same T pointer that we do.
 

Protected Member Functions

template<typename U , typename Allocator , typename Deleter >
void alloc_internal (U pValue, Allocator allocator, Deleter deleter)
 

Protected Attributes

element_type * mpValue
 
ref_count_spmpRefCount
 

Friends

template<typename U >
class weak_ptr
 
template<typename U >
void allocate_shared_helper (shared_ptr< U > &, ref_count_sp *, U *)
 

Detailed Description

template<typename T>
class eastl::shared_ptr< T >

shared_ptr

This class implements the C++11 shared_ptr template. A shared_ptr is like the C++ Standard Library unique_ptr except that it allows sharing of pointers between instances via reference counting. shared_ptr objects can safely be copied and can safely be used in C++ Standard Library containers such as std::vector or std::list.

This class is not thread safe in that you cannot use an instance of it from two threads at the same time and cannot use two separate instances of it, which own the same pointer, at the same time. Use standard multithread mutex techniques to address the former problems and use shared_ptr_mt to address the latter. Note that this is contrary to the C++11 standard.

As of this writing, arrays aren't supported, but they are planned in the future based on the C++17 proposal: http://isocpp.org/files/papers/N3920.html

Constructor & Destructor Documentation

◆ shared_ptr() [1/8]

template<typename T >
eastl::shared_ptr< T >::shared_ptr ( )
inline

Base pointer to Reference count for owned pointer and the owned pointer.

Initializes and "empty" shared_ptr. Postcondition: use_count() == zero and get() == 0

◆ shared_ptr() [2/8]

template<typename T >
template<typename U >
eastl::shared_ptr< T >::shared_ptr ( U *  pValue,
typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *  = 0 
)
inlineexplicit

Takes ownership of the pointer and sets the reference count to the pointer to 1. It is OK if the input pointer is null. The shared reference count is allocated on the heap using the default eastl allocator. Throws: bad_alloc, or an implementation-defined exception when a resource other than memory could not be obtained. Exception safety: If an exception is thrown, delete p is called. Postcondition in the event of no exception: use_count() == 1 && get() == p

◆ shared_ptr() [3/8]

template<typename T >
template<typename U , typename Deleter >
eastl::shared_ptr< T >::shared_ptr ( U *  pValue,
Deleter  deleter,
typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *  = 0 
)
inline

Takes ownership of the pointer and sets the reference count to the pointer to 1. It is OK if the input pointer is null. The shared reference count is allocated on the heap using the default eastl allocator. The pointer will be disposed using the provided deleter. If an exception occurs during the allocation of the shared reference count, the owned pointer is deleted and the exception is rethrown. Postcondition: use_count() == 1 && get() == p

◆ shared_ptr() [4/8]

template<typename T >
template<typename U , typename Deleter , typename Allocator >
eastl::shared_ptr< T >::shared_ptr ( U *  pValue,
Deleter  deleter,
const Allocator &  allocator,
typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *  = 0 
)
inlineexplicit

Takes ownership of the pointer and sets the reference count to the pointer to 1. It is OK if the input pointer is null. The shared reference count is allocated on the heap using the supplied allocator. The pointer will be disposed using the provided deleter. If an exception occurs during the allocation of the shared reference count, the owned pointer is deleted and the exception is rethrown. Postcondition: use_count() == 1 && get() == p

◆ shared_ptr() [5/8]

template<typename T >
eastl::shared_ptr< T >::shared_ptr ( const shared_ptr< T > &  sharedPtr)
inline

shared_ptr construction with self type. If we want a shared_ptr constructor that is templated on shared_ptr, then we need to make it in addition to this function, as otherwise the compiler will generate this function and things will go wrong. To accomplish this in a thread-safe way requires use of shared_ptr atomic_store.

◆ shared_ptr() [6/8]

template<typename T >
template<typename U >
eastl::shared_ptr< T >::shared_ptr ( const shared_ptr< U > &  sharedPtr,
typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *  = 0 
)
inline

shared_ptr Shares ownership of a pointer with another instance of shared_ptr. This function increments the shared reference count on the pointer. To accomplish this in a thread-safe way requires use of shared_ptr atomic_store.

◆ shared_ptr() [7/8]

template<typename T >
template<typename U >
eastl::shared_ptr< T >::shared_ptr ( const shared_ptr< U > &  sharedPtr,
element_type *  pValue 
)
inline

shared_ptr

20.7.2.2.1p13: Constructs a shared_ptr instance that stores p and shares ownership with r. Postconditions: get() == pValue && use_count() == sharedPtr.use_count(). To avoid the possibility of a dangling pointer, the user of this constructor must ensure that pValue remains valid at least until the ownership group of sharedPtr is destroyed. This constructor allows creation of an empty shared_ptr instance with a non-NULL stored pointer.

Shares ownership of a pointer with another instance of shared_ptr while storing a potentially different pointer. This function increments the shared reference count on the sharedPtr if it exists. If sharedPtr has no shared reference then a shared reference is not created an pValue is not deleted in our destructor and effectively the pointer is not actually shared.

To accomplish this in a thread-safe way requires the user to maintain the lifetime of sharedPtr as described above.

◆ shared_ptr() [8/8]

template<typename T >
template<typename U >
eastl::shared_ptr< T >::shared_ptr ( const weak_ptr< U > &  weakPtr,
typename eastl::enable_if< eastl::is_convertible< U *, element_type * >::value >::type *  = 0 
)
inlineexplicit

shared_ptr(weak_ptr) Shares ownership of a pointer with an instance of weak_ptr. This function increments the shared reference count on the pointer.

◆ ~shared_ptr()

template<typename T >
eastl::shared_ptr< T >::~shared_ptr ( )
inline

~shared_ptr Decrements the reference count for the owned pointer. If the reference count goes to zero, the owned pointer is deleted and the shared reference count is deleted.

Member Function Documentation

◆ get()

template<typename T >
element_type* eastl::shared_ptr< T >::get ( ) const
inline

operator[] Index into the array pointed to by the owned pointer. The behaviour is undefined if the owned pointer is nullptr, if the user specified index is negative, or if the index is outside the referred array bounds.

When T is not an array type, it is unspecified whether this function is declared. If the function is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function is guaranteed to be legal. get Returns the owned pointer. Note that this class does not provide an operator T() function. This is because such a thing (automatic conversion) is deemed unsafe. Example usage: struct X{ void DoSomething(); }; shared_ptr<int> ptr(new X); X* pX = ptr.get(); pX->DoSomething();

◆ operator bool()

template<typename T >
eastl::shared_ptr< T >::operator bool ( ) const
inlineexplicit

Explicit operator bool Allows for using a shared_ptr as a boolean. Example usage: shared_ptr<int> ptr(new int(3)); if(ptr) ++*ptr;

◆ operator*()

template<typename T >
reference_type eastl::shared_ptr< T >::operator* ( ) const
inline

operator* Returns the owner pointer dereferenced. Example usage: shared_ptr<int> ptr(new int(3)); int x = *ptr;

◆ operator->()

template<typename T >
element_type* eastl::shared_ptr< T >::operator-> ( ) const
inline

operator-> Allows access to the owned pointer via operator->() Example usage: struct X{ void DoSomething(); }; shared_ptr<int> ptr(new X); ptr->DoSomething();

◆ operator=() [1/4]

template<typename T >
shared_ptr& eastl::shared_ptr< T >::operator= ( const shared_ptr< T > &  sharedPtr)
inline

operator= Assignment to self type. If we want a shared_ptr operator= that is templated on shared_ptr, then we need to make it in addition to this function, as otherwise the compiler will generate this function and things will go wrong.

◆ operator=() [2/4]

template<typename T >
template<typename U >
eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, this_type&>::type eastl::shared_ptr< T >::operator= ( const shared_ptr< U > &  sharedPtr)
inline

operator= Copies another shared_ptr to this object. Note that this object may already own a shared pointer with another different pointer (but still of the same type) before this call. In that case, this function releases the old pointer, decrementing its reference count and deleting it if zero, takes shared ownership of the new pointer and increments its reference count.

◆ operator=() [3/4]

template<typename T >
this_type& eastl::shared_ptr< T >::operator= ( shared_ptr< T > &&  sharedPtr)
inline

operator= Assignment to self type. If we want a shared_ptr operator= that is templated on shared_ptr, then we need to make it in addition to this function, as otherwise the compiler will generate this function and things will go wrong.

◆ operator=() [4/4]

template<typename T >
template<typename U >
eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, this_type&>::type eastl::shared_ptr< T >::operator= ( shared_ptr< U > &&  sharedPtr)
inline

operator= Moves another shared_ptr to this object. Note that this object may already own a shared pointer with another different pointer (but still of the same type) before this call. In that case, this function releases the old pointer, decrementing its reference count and deleting it if zero, takes shared ownership of the new pointer and increments its reference count.

◆ owner_before()

template<typename T >
template<typename U >
bool eastl::shared_ptr< T >::owner_before ( const shared_ptr< U > &  sharedPtr) const
inline

owner_before C++11 function for ordering.

◆ reset() [1/4]

template<typename T >
void eastl::shared_ptr< T >::reset ( )
inline

reset Releases the owned pointer.

◆ reset() [2/4]

template<typename T >
template<typename U >
eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, void>::type eastl::shared_ptr< T >::reset ( U *  pValue)
inline

reset Releases the owned pointer and takes ownership of the passed in pointer.

◆ reset() [3/4]

template<typename T >
template<typename U , typename Deleter >
eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, void>::type eastl::shared_ptr< T >::reset ( U *  pValue,
Deleter  deleter 
)
inline

reset Releases the owned pointer and takes ownership of the passed in pointer.

◆ reset() [4/4]

template<typename T >
template<typename U , typename Deleter , typename Allocator >
eastl::enable_if<eastl::is_convertible<U*, element_type*>::value, void>::type eastl::shared_ptr< T >::reset ( U *  pValue,
Deleter  deleter,
const Allocator &  allocator 
)
inline

reset Resets the shared_ptr

◆ swap()

template<typename T >
void eastl::shared_ptr< T >::swap ( this_type sharedPtr)
inline

swap Exchanges the owned pointer between two shared_ptr objects. This function is not intrinsically thread-safe. You must use atomic_exchange(shared_ptr<T>*, shared_ptr<T>) or manually coordinate the swap.

◆ unique()

template<typename T >
bool eastl::shared_ptr< T >::unique ( ) const
inline

unique Returns: use_count() == 1.

◆ use_count()

template<typename T >
int eastl::shared_ptr< T >::use_count ( ) const
inline

use_count Returns: the number of shared_ptr objects, *this included, that share ownership with *this, or 0 when *this is empty.


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