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

#include <scoped_ptr.h>

Public Types

typedef T element_type
 
typedef T *(this_type::* bool_) () const
 

Public Member Functions

 scoped_ptr (T *pValue=NULL)
 
 ~scoped_ptr ()
 
void reset (T *pValue=NULL)
 
T * detach ()
 
void swap (this_type &scopedPtr)
 
add_lvalue_reference< T >::type operator* () const
 
T * operator-> () const
 
T * get () const
 
 operator bool_ () const
 
bool operator! () const
 

Protected Types

typedef scoped_ptr< T > this_type
 
typedef Deleter deleter_type
 deleter_type
 

Protected Member Functions

 scoped_ptr (const scoped_ptr &)
 
scoped_ptroperator= (const scoped_ptr &)
 
scoped_ptroperator= (T *pValue)
 

Protected Attributes

T * mpValue
 

Detailed Description

template<typename T, typename Deleter = smart_ptr_deleter<T>>
class eastl::scoped_ptr< T, Deleter >

class scoped_ptr

This class is intended to be the same as the C++11 unique_ptr class, but was created before there was such a thing.

This class implements a scoped_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 scoped_ptr class template stores a pointer to a dynamically allocated object. The object pointed to is automatically deleted on destructor of scoped_ptr or can be manually deleted via the scopted_ptr::reset function.

scoped_ptr cannot be used in C++ Standard Library containers; you'll need to use the shared_ptr template if you want to do this. The reason you can't use scoped_ptr is that it prohibits copying. You can't (safely) use auto_ptr in C++ Standard Library containers because copying of an auto_ptr will create a situation whereby objects are multiply freed.

scoped_ptr cannot be used with arrays of objects. The reason for this is that it calls delete on the owned pointer and not delete[]. The latter allows for the calling of the destructors for the objects of the owned pointer. If you want to use scoped_ptr with a dynamically allocated array, use the scoped_array function instead.

Member Typedef Documentation

◆ bool_

template<typename T , typename Deleter = smart_ptr_deleter<T>>
typedef T*(this_type::* eastl::scoped_ptr< T, Deleter >::bool_) () const

Implicit operator bool Allows for using a scoped_ptr as a boolean. Example usage: scoped_ptr<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(scopedPtr == 1) would yield true (bad).

◆ this_type

template<typename T , typename Deleter = smart_ptr_deleter<T>>
typedef scoped_ptr<T> eastl::scoped_ptr< T, Deleter >::this_type
protected

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

Constructor & Destructor Documentation

◆ scoped_ptr() [1/2]

template<typename T , typename Deleter = smart_ptr_deleter<T>>
eastl::scoped_ptr< T, Deleter >::scoped_ptr ( const scoped_ptr< T, Deleter > &  )
protected

scoped_ptr This function is private in order to prevent copying, for safety.

◆ scoped_ptr() [2/2]

template<typename T , typename Deleter = smart_ptr_deleter<T>>
eastl::scoped_ptr< T, Deleter >::scoped_ptr ( T *  pValue = NULL)
inlineexplicit

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

◆ ~scoped_ptr()

template<typename T , typename Deleter = smart_ptr_deleter<T>>
eastl::scoped_ptr< T, Deleter >::~scoped_ptr ( )
inline

~scoped_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 = smart_ptr_deleter<T>>
T* eastl::scoped_ptr< T, Deleter >::detach ( )
inline

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

◆ get()

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

◆ operator!()

template<typename T , typename Deleter = smart_ptr_deleter<T>>
bool eastl::scoped_ptr< T, 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. scoped_ptr<int> ptr(new int(3)); if(!ptr) assert(false);

◆ operator*()

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

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

◆ operator->()

template<typename T , typename Deleter = smart_ptr_deleter<T>>
T* eastl::scoped_ptr< T, Deleter >::operator-> ( ) const
inline

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

◆ operator=() [1/2]

template<typename T , typename Deleter = smart_ptr_deleter<T>>
scoped_ptr& eastl::scoped_ptr< T, Deleter >::operator= ( const scoped_ptr< T, Deleter > &  )
protected

scoped_ptr This function is private in order to prevent copying, for safety.

◆ operator=() [2/2]

template<typename T , typename Deleter = smart_ptr_deleter<T>>
scoped_ptr& eastl::scoped_ptr< T, Deleter >::operator= ( T *  pValue)
protected

scoped_ptr This function is private in order to prevent copying, for safety.

◆ reset()

template<typename T , typename Deleter = smart_ptr_deleter<T>>
void eastl::scoped_ptr< T, Deleter >::reset ( T *  pValue = NULL)
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: scoped_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 = smart_ptr_deleter<T>>
void eastl::scoped_ptr< T, Deleter >::swap ( this_type scopedPtr)
inline

swap Exchanges the owned pointer beween two scoped_ptr objects.

Member Data Documentation

◆ mpValue

template<typename T , typename Deleter = smart_ptr_deleter<T>>
T* eastl::scoped_ptr< T, Deleter >::mpValue
protected

mpValue The owned pointer.


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