Nugget
|
#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_ptr & | operator= (const scoped_ptr &) |
scoped_ptr & | operator= (T *pValue) |
Protected Attributes | |
T * | mpValue |
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.
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).
|
protected |
this_type This is an alias for scoped_ptr<T>, this class.
|
protected |
scoped_ptr This function is private in order to prevent copying, for safety.
|
inlineexplicit |
scoped_ptr Construct a scoped_ptr from a pointer allocated via new. Example usage: scoped_ptr<int> ptr(new int(3));
|
inline |
~scoped_ptr Destroys the owned pointer. The destructor for the object referred to by the owned pointer will be called.
|
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;
|
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();
|
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);
|
inline |
operator* Returns the owner pointer dereferenced. Example usage: scoped_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(); }; scoped_ptr<int> ptr(new X); ptr->DoSomething();
|
protected |
scoped_ptr This function is private in order to prevent copying, for safety.
|
protected |
scoped_ptr This function is private in order to prevent copying, for safety.
|
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)
|
inline |
swap Exchanges the owned pointer beween two scoped_ptr objects.
|
protected |
mpValue The owned pointer.