Nugget
|
#include <shared_ptr.h>
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_ptr & | operator= (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_type & | operator= (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_sp * | mpRefCount |
Friends | |
template<typename U > | |
class | weak_ptr |
template<typename U > | |
void | allocate_shared_helper (shared_ptr< U > &, ref_count_sp *, U *) |
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
|
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
|
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
|
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
|
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
|
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.
|
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.
|
inline |
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.
|
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.
|
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.
|
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();
|
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;
|
inline |
operator* Returns the owner pointer dereferenced. Example usage: shared_ptr<int> ptr(new int(3)); int x = *ptr;
|
inline |
operator-> Allows access to the owned pointer via operator->() Example usage: struct X{ void DoSomething(); }; shared_ptr<int> ptr(new X); ptr->DoSomething();
|
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.
|
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.
|
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.
|
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.
|
inline |
owner_before C++11 function for ordering.
|
inline |
reset Releases the owned pointer.
|
inline |
reset Releases the owned pointer and takes ownership of the passed in pointer.
|
inline |
reset Releases the owned pointer and takes ownership of the passed in pointer.
|
inline |
reset Resets the shared_ptr
|
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.
|
inline |
unique Returns: use_count() == 1.
|
inline |
use_count Returns: the number of shared_ptr objects, *this included, that share ownership with *this, or 0 when *this is empty.