Nugget
Public Types | Public Member Functions | Protected Types | Protected Attributes | List of all members
eastl::shared_array< T, Allocator, Deleter > Class Template Reference

#include <shared_array.h>

Public Types

typedef T element_type
 The allocator used to manage new/delete of mpRefCount.
 
typedef T *(this_type::* bool_) () const
 

Public Member Functions

 shared_array (T *pArray=NULL, const allocator_type &allocator=EASTL_SHARED_ARRAY_DEFAULT_ALLOCATOR)
 
 shared_array (const shared_array &sharedArray)
 
 ~shared_array ()
 
shared_arrayoperator= (const shared_array &sharedArray)
 
shared_arrayoperator= (T *pValue)
 
void reset (T *pArray=NULL)
 
void swap (this_type &sharedArray)
 
T & operator[] (ptrdiff_t i) const
 
T & operator* () const
 
T * operator-> () const EA_NOEXCEPT
 
T * get () const EA_NOEXCEPT
 
int use_count () const
 
bool unique () const
 
 operator bool_ () const EA_NOEXCEPT
 
bool operator! () const EA_NOEXCEPT
 
const allocator_typeget_allocator () const EA_NOEXCEPT
 
allocator_typeget_allocator () EA_NOEXCEPT
 
void set_allocator (const allocator_type &allocator)
 

Protected Types

typedef shared_array< T > this_type
 
typedef Allocator allocator_type
 allocator_type
 
typedef Deleter deleter_type
 deleter_type
 
typedef int ref_count
 

Protected Attributes

T * mpArray
 
ref_countmpRefCount
 The owned pointer. Points to an array of T.
 
allocator_type mAllocator
 Reference count for owned pointer.
 

Detailed Description

template<typename T, typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
class eastl::shared_array< T, Allocator, Deleter >

class shared_array A shared_array is the same as shared_ptr but for arrays.

Member Typedef Documentation

◆ bool_

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
typedef T*(this_type::* eastl::shared_array< T, Allocator, Deleter >::bool_) () const

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

Note that below we do not use operator bool(). The reason for this is that booleans automatically convert up to short, int, float, etc. The result is that this: if(sharedArray == 1) would yield true (bad).

◆ ref_count

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
typedef int eastl::shared_array< T, Allocator, Deleter >::ref_count
protected

ref_count An internal reference count type. Must be convertable to int so that the public use_count function can work.

◆ this_type

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
typedef shared_array<T> eastl::shared_array< T, Allocator, Deleter >::this_type
protected

this_type This is an alias for shared_array<T>, this class.

Constructor & Destructor Documentation

◆ shared_array() [1/2]

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
eastl::shared_array< T, Allocator, Deleter >::shared_array ( T *  pArray = NULL,
const allocator_type allocator = EASTL_SHARED_ARRAY_DEFAULT_ALLOCATOR 
)
inlineexplicit

shared_array 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 via operator new. If an exception occurs during the allocation of the shared reference count, the owned pointer is deleted and the exception is rethrown. A null pointer is given a reference count of 1.

◆ shared_array() [2/2]

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
eastl::shared_array< T, Allocator, Deleter >::shared_array ( const shared_array< T, Allocator, Deleter > &  sharedArray)
inline

shared_array Shares ownership of a pointer with another instance of shared_array. This function increments the shared reference count on the pointer. If we want a shared_array constructor that is templated on shared_array, then we need to make it in addition to this function, as otherwise the compiler will generate this function and things will go wrong.

◆ ~shared_array()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
eastl::shared_array< T, Allocator, Deleter >::~shared_array ( )
inline

~shared_array 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 , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
T* eastl::shared_array< T, Allocator, Deleter >::get ( ) const
inline

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_array<int> ptr = new X; X* pX = ptr.get(); pX->DoSomething();

◆ get_allocator()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
const allocator_type& eastl::shared_array< T, Allocator, Deleter >::get_allocator ( ) const
inline

get_allocator Returns the memory allocator associated with this class.

◆ operator!()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
bool eastl::shared_array< T, Allocator, Deleter >::operator! ( ) const
inline

operator! This returns the opposite of operator bool; it returns true if the owned pointer is null. Some compilers require this and some don't. shared_array<int> ptr = new int(3); if(!ptr) assert(false);

◆ operator*()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
T& eastl::shared_array< T, Allocator, Deleter >::operator* ( ) const
inline

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

◆ operator->()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
T* eastl::shared_array< T, Allocator, Deleter >::operator-> ( ) const
inline

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

◆ operator=() [1/2]

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
shared_array& eastl::shared_array< T, Allocator, Deleter >::operator= ( const shared_array< T, Allocator, Deleter > &  sharedArray)
inline

operator= Copies another shared_array 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. If we want a shared_array operator= that is templated on shared_array, 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/2]

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
shared_array& eastl::shared_array< T, Allocator, Deleter >::operator= ( T *  pValue)
inline

operator= Assigns a new pointer, while decrementing the reference count on the current pointer. The new pointer can be NULL and the current pointer can NULL. If the new pointer is equivalent to the current pointer, then nothing is done.

◆ operator[]()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
T& eastl::shared_array< T, Allocator, Deleter >::operator[] ( ptrdiff_t  i) const
inline

operator[] Returns a reference to the specified item in the owned pointer array. Example usage: shared_array<int> ptr = new int[6]; int x = ptr[2];

◆ reset()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
void eastl::shared_array< T, Allocator, Deleter >::reset ( T *  pArray = NULL)
inline

reset Releases the owned pointer and takes ownership of the passed in pointer. If the passed in pointer is the same as the owned pointer, nothing is done. The passed in pointer can be null, in which case the use count is set to 1.

◆ set_allocator()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
void eastl::shared_array< T, Allocator, Deleter >::set_allocator ( const allocator_type allocator)
inline

set_allocator Sets the memory allocator associated with this class.

◆ swap()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
void eastl::shared_array< T, Allocator, Deleter >::swap ( this_type sharedArray)
inline

swap Exchanges the owned pointer beween two shared_array objects.

◆ unique()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
bool eastl::shared_array< T, Allocator, Deleter >::unique ( ) const
inline

unique Returns true if the reference count on the owned pointer is one. The return value is true if the owned pointer is null.

◆ use_count()

template<typename T , typename Allocator = EASTLAllocatorType, typename Deleter = smart_array_deleter<T>>
int eastl::shared_array< T, Allocator, Deleter >::use_count ( ) const
inline

use_count Returns the reference count on the owned pointer. The return value is one if the owned pointer is null.


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