Nugget
|
#include <safe_ptr.h>
Public Types | |
typedef T | value_type |
typedef safe_ptr< T > | this_type |
typedef T *(this_type::* | bool_) () const |
Boolean negation operator. | |
Public Member Functions | |
safe_ptr (T *pObject) | |
Default constructor. | |
safe_ptr (const this_type &safePtr) | |
Construct a safeptr from a naked pointer. | |
this_type & | operator= (const this_type &safePtr) |
Copy constructor. | |
this_type & | operator= (T *const pObject) |
Assignment operator. | |
bool | operator== (const this_type &safePtr) const |
Assign this to a naked pointer. | |
T * | get () const |
Returns true if safePtr points to the same object as this. | |
operator T* () const | |
Get the naked pointer from this safe ptr. | |
T * | operator-> () const |
Implicit safe_ptr<T> -> T* conversion operator. | |
T & | operator* () const |
Member operator. | |
bool | operator! () const |
Dereference operator. | |
operator bool_ () const | |
Allows for a more portable version of testing an instance of this class as a bool. | |
Public Member Functions inherited from eastl::safe_ptr_base | |
bool | unique () const |
bool | empty () const |
Returns true if there are no other smart pointers pointing to our object except us. True if mpObject is NUll. | |
void | reset (const safe_object *pObject) |
Returns true if mpObject is NULL. | |
void | reset () |
Make this point to pObject and enlist. | |
Additional Inherited Members | |
Protected Member Functions inherited from eastl::safe_ptr_base | |
safe_ptr_base () | |
Make this point to NULL and delist. | |
safe_ptr_base (const safe_object *pObject) | |
safe_ptr_base (const safe_ptr_base &safePtrBase) | |
Protected Attributes inherited from eastl::safe_ptr_base | |
const safe_object * | mpObject |
safe_ptr is an automatic, lightweight solution to the dangling pointer problem. This class is an alternative to weak_ptr which has the primary benefit of not allocating memory at the primary cost of being a tad slower and thread-unsafe.
During normal usage, safe_ptr<T> behaves exactly as a T*. When the raw pointer referenced by the safe_ptr is deleted, all of the SafePtrs for the raw pointer are set to NULL.
This works by making the raw objects derive from the class safe_object, which maintains a linked-list of the Safe pointers that reference it. When a safe_object is destroyed, it walks its linked list, setting the object reference for each of its SafePtrs to NULL.
The overhead for this is light - a single pointer is added to the size of the pointed to object, and a safePtr is the size of a raw pointer plus one list pointer.
This class is not thread-safe. In particular, manipulation of safe_ptr objects that refer to the same underlying object cannot be done safely from multiple threads. safe_ptr objects that are unrelated can be used safely from multiple threads.
Example usage: class RandomLifetimeObject : public safe_object { public: RandomLifetimeObject(); Method(); ... };
safe_ptr<RandomLifetimeObject> pSafePtr(new RandomLifetimeObject); safe_ptr<RandomLifetimeObject> pSafePtrCopy = pSafePtr;
pSafePtr->Method(); delete pSafePtr; At this point, pSafePtrCopy evaluates to NULL.