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

#include <unique_ptr.h>

Collaboration diagram for eastl::unique_ptr< T, Deleter >:
Collaboration graph
[legend]

Public Types

typedef Deleter deleter_type
 
typedef T element_type
 
typedef unique_ptr< element_type, deleter_type > this_type
 
typedef Internal::unique_pointer_type< element_type, deleter_type >::type pointer
 

Public Member Functions

EA_CPP14_CONSTEXPR unique_ptr () EA_NOEXCEPT
 
EA_CPP14_CONSTEXPR unique_ptr (std::nullptr_t) EA_NOEXCEPT
 
 unique_ptr (pointer pValue) EA_NOEXCEPT
 
 unique_ptr (pointer pValue, typename eastl::conditional< eastl::is_lvalue_reference< deleter_type >::value, deleter_type, typename eastl::add_lvalue_reference< const deleter_type >::type >::type deleter) EA_NOEXCEPT
 
 unique_ptr (pointer pValue, typename eastl::remove_reference< deleter_type >::type &&deleter) EA_NOEXCEPT
 
 unique_ptr (this_type &&x) EA_NOEXCEPT
 
template<typename U , typename E >
 unique_ptr (unique_ptr< U, E > &&u, typename enable_if<!is_array< U >::value &&is_convertible< typename unique_ptr< U, E >::pointer, pointer >::value &&is_convertible< E, deleter_type >::value &&(is_same< deleter_type, E >::value||!is_lvalue_reference< deleter_type >::value)>::type *=0) EA_NOEXCEPT
 
this_typeoperator= (this_type &&x) EA_NOEXCEPT
 
template<typename U , typename E >
enable_if<!is_array< U >::value &&is_convertible< typename unique_ptr< U, E >::pointer, pointer >::value &&is_assignable< deleter_type &, E && >::value, this_type & >::type operator= (unique_ptr< U, E > &&u) EA_NOEXCEPT
 
this_typeoperator= (std::nullptr_t) EA_NOEXCEPT
 operator=(nullptr_t)
 
 ~unique_ptr () EA_NOEXCEPT
 
void reset (pointer pValue=pointer()) EA_NOEXCEPT
 
pointer release () EA_NOEXCEPT
 
pointer detach () EA_NOEXCEPT
 
void swap (this_type &x) EA_NOEXCEPT
 
add_lvalue_reference< T >::type operator* () const
 
pointer operator-> () const EA_NOEXCEPT
 
pointer get () const EA_NOEXCEPT
 
deleter_type & get_deleter () EA_NOEXCEPT
 
const deleter_type & get_deleter () const EA_NOEXCEPT
 
 operator bool () const EA_NOEXCEPT
 
 unique_ptr (const this_type &)=delete
 These functions are deleted in order to prevent copying, for safety.
 
unique_ptroperator= (const this_type &)=delete
 
unique_ptroperator= (pointer pValue)=delete
 

Protected Attributes

eastl::compressed_pair< pointer, deleter_type > mPair
 

Detailed Description

template<typename T, typename Deleter = eastl::default_delete<T>>
class eastl::unique_ptr< T, Deleter >

class unique_ptr

This class implements a unique_ptr template. This is a class which is similar to the C++ auto_ptr template, except that it prohibits copying of itself, for safety.

More specifically, the unique_ptr class template stores a pointer to a dynamically allocated object. The object pointed to is automatically deleted on destructor of unique_ptr or can be manually deleted via the unique_ptr::reset function.

Memory allocation notes: unique_ptr doesn't allocate memory; all allocated pointers are externally derived. unique_ptr does deallocate memory, though always through the user-provided deleter. You need to make sure you are consistent in providing a deleter which frees memory in a way that matches how it was originally allocated. Deleters have instance information and are moved between containers the same way the allocated pointers are. Thus you can allocate memory via some heap and provide a deleter which contains a pointer to that same heap, and regardless of what you do with the unique_ptr, including moving it to another unique_ptr, the deletion will use the originally provided heap.

Example usage: unique_ptr<int> p(new int); *p = 4;

unique_ptr<int[]> pArray(new int[4]); p[0] = 4;

Type completeness requirements http://stackoverflow.com/questions/6012157/is-stdunique-ptrt-required-to-know-the-full-definition-of-t/6089065#6089065 Here is a table which documents several members of shared_ptr and unique_ptr with respect to completeness requirements. If the member requires a complete type, the entry has a "C", otherwise the table entry is filled with "I".

                            unique_ptr       shared_ptr
+------------------------+---------------+---------------+
|          P()           |      I        |      I        |
|  default constructor   |               |               |
+------------------------+---------------+---------------+
|      P(const P&)       |     N/A       |      I        |
|    copy constructor    |               |               |
+------------------------+---------------+---------------+
|         P(P&&)         |      I        |      I        |
|    move constructor    |               |               |
+------------------------+---------------+---------------+
|         ~P()           |      C        |      I        |
|       destructor       |               |               |
+------------------------+---------------+---------------+
|         P(A*)          |      I        |      C        |
+------------------------+---------------+---------------+
|  operator=(const P&)   |     N/A       |      I        |
|    copy assignment     |               |               |
+------------------------+---------------+---------------+
|    operator=(P&&)      |      C        |      I        |
|    move assignment     |               |               |
+------------------------+---------------+---------------+
|        reset()         |      C        |      I        |
+------------------------+---------------+---------------+
|       reset(A*)        |      C        |      C        |
+------------------------+---------------+---------------+

Constructor & Destructor Documentation

◆ unique_ptr() [1/7]

template<typename T , typename Deleter = eastl::default_delete<T>>
EA_CPP14_CONSTEXPR eastl::unique_ptr< T, Deleter >::unique_ptr ( )
inline

unique_ptr Construct a unique_ptr from a pointer allocated via new. Example usage: unique_ptr<int> ptr;

◆ unique_ptr() [2/7]

template<typename T , typename Deleter = eastl::default_delete<T>>
EA_CPP14_CONSTEXPR eastl::unique_ptr< T, Deleter >::unique_ptr ( std::nullptr_t  )
inline

unique_ptr Construct a unique_ptr from a null pointer. Example usage: unique_ptr<int> ptr(nullptr);

◆ unique_ptr() [3/7]

template<typename T , typename Deleter = eastl::default_delete<T>>
eastl::unique_ptr< T, Deleter >::unique_ptr ( pointer  pValue)
inlineexplicit

unique_ptr Construct a unique_ptr from a pointer allocated via new. Example usage: unique_ptr<int> ptr(new int(3));

◆ unique_ptr() [4/7]

template<typename T , typename Deleter = eastl::default_delete<T>>
eastl::unique_ptr< T, Deleter >::unique_ptr ( pointer  pValue,
typename eastl::conditional< eastl::is_lvalue_reference< deleter_type >::value, deleter_type, typename eastl::add_lvalue_reference< const deleter_type >::type >::type  deleter 
)
inline

unique_ptr Constructs a unique_ptr with the owner pointer and deleter specified Example usage: eastl::smart_ptr_deleter<int> del; unique_ptr<int> ptr(new int(3), del);

◆ unique_ptr() [5/7]

template<typename T , typename Deleter = eastl::default_delete<T>>
eastl::unique_ptr< T, Deleter >::unique_ptr ( pointer  pValue,
typename eastl::remove_reference< deleter_type >::type &&  deleter 
)
inline

unique_ptr Constructs a unique_ptr with the owned pointer and deleter specified (rvalue) Example usage: unique_ptr<int> ptr(new int(3), eastl::smart_ptr_deleter<int>());

◆ unique_ptr() [6/7]

template<typename T , typename Deleter = eastl::default_delete<T>>
eastl::unique_ptr< T, Deleter >::unique_ptr ( this_type &&  x)
inline

unique_ptr Move constructor Example usage: unique_ptr<int> ptr(new int(3)); unique_ptr<int> newPtr = eastl::move(ptr);

◆ unique_ptr() [7/7]

template<typename T , typename Deleter = eastl::default_delete<T>>
template<typename U , typename E >
eastl::unique_ptr< T, Deleter >::unique_ptr ( unique_ptr< U, E > &&  u,
typename enable_if<!is_array< U >::value &&is_convertible< typename unique_ptr< U, E >::pointer, pointer >::value &&is_convertible< E, deleter_type >::value &&(is_same< deleter_type, E >::value||!is_lvalue_reference< deleter_type >::value)>::type *  = 0 
)
inline

unique_ptr Move constructor Example usage: unique_ptr<int> ptr(new int(3)); unique_ptr<int> newPtr = eastl::move(ptr);

◆ ~unique_ptr()

template<typename T , typename Deleter = eastl::default_delete<T>>
eastl::unique_ptr< T, Deleter >::~unique_ptr ( )
inline

~unique_ptr Destroys the owned pointer. The destructor for the object referred to by the owned pointer will be called.

Member Function Documentation

◆ detach()

template<typename T , typename Deleter = eastl::default_delete<T>>
pointer eastl::unique_ptr< T, Deleter >::detach ( )
inline

detach For backwards-compatibility with pre-C++11 code.

◆ get()

template<typename T , typename Deleter = eastl::default_delete<T>>
pointer eastl::unique_ptr< T, 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(); }; unique_ptr<int> ptr(new X); X* pX = ptr.get(); pX->DoSomething();

◆ get_deleter() [1/2]

template<typename T , typename Deleter = eastl::default_delete<T>>
const deleter_type& eastl::unique_ptr< T, Deleter >::get_deleter ( ) const
inline

get_deleter Const version for getting the deleter

◆ get_deleter() [2/2]

template<typename T , typename Deleter = eastl::default_delete<T>>
deleter_type& eastl::unique_ptr< T, Deleter >::get_deleter ( )
inline

get_deleter Returns the deleter used to delete the owned pointer Example usage: unique_ptr<int> ptr(new int(3)); eastl::smart_ptr_deleter<int>& del = ptr.get_deleter();

◆ operator bool()

template<typename T , typename Deleter = eastl::default_delete<T>>
eastl::unique_ptr< T, Deleter >::operator bool ( ) const
inlineexplicit

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

◆ operator*()

template<typename T , typename Deleter = eastl::default_delete<T>>
add_lvalue_reference<T>::type eastl::unique_ptr< T, Deleter >::operator* ( ) const
inline

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

◆ operator->()

template<typename T , typename Deleter = eastl::default_delete<T>>
pointer eastl::unique_ptr< T, Deleter >::operator-> ( ) const
inline

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

◆ operator=() [1/2]

template<typename T , typename Deleter = eastl::default_delete<T>>
this_type& eastl::unique_ptr< T, Deleter >::operator= ( this_type &&  x)
inline

unique_ptr Move assignment Example usage: unique_ptr<int> ptr(new int(3)); unique_ptr<int> newPtr(new int(4)); ptr = eastl::move(newPtr); // Deletes int(3) and assigns mpValue to int(4)

◆ operator=() [2/2]

template<typename T , typename Deleter = eastl::default_delete<T>>
template<typename U , typename E >
enable_if<!is_array<U>::value && is_convertible<typename unique_ptr<U, E>::pointer, pointer>::value && is_assignable<deleter_type&, E&&>::value, this_type&>::type eastl::unique_ptr< T, Deleter >::operator= ( unique_ptr< U, E > &&  u)
inline

unique_ptr Move assignment

◆ release()

template<typename T , typename Deleter = eastl::default_delete<T>>
pointer eastl::unique_ptr< T, Deleter >::release ( )
inline

release This simply forgets the owned pointer. It doesn't free it but rather assumes that the user does. Example usage: unique_ptr<int> ptr(new int(3)); int* pInt = ptr.release(); delete pInt;

◆ reset()

template<typename T , typename Deleter = eastl::default_delete<T>>
void eastl::unique_ptr< T, Deleter >::reset ( pointer  pValue = pointer())
inline

reset Deletes 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. Example usage: unique_ptr<int> ptr(new int(3)); ptr.reset(new int(4)); // deletes int(3) ptr.reset(NULL); // deletes int(4)

◆ swap()

template<typename T , typename Deleter = eastl::default_delete<T>>
void eastl::unique_ptr< T, Deleter >::swap ( this_type x)
inline

swap Exchanges the owned pointer beween two unique_ptr objects.


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