#include <ptr.h>
Public Types | |
typedef T * | PointerType |
typedef T & | ReferenceType |
typedef BasePtrTrackerInternal::RefValue | RefValue |
Public Member Functions | |
SharedPtr (std::nullptr_t) | |
template<class T2 > | |
SharedPtr (T2 *p) | |
template<class T2 , class DL > | |
SharedPtr (T2 *p, DL d) | |
SharedPtr (const SharedPtr< T > &r) | |
template<class T2 > | |
SharedPtr (const SharedPtr< T2 > &r) | |
template<class T2 > | |
SharedPtr (const WeakPtr< T2 > &r) | |
SharedPtr & | operator= (const SharedPtr &r) |
template<class T2 > | |
SharedPtr & | operator= (const SharedPtr< T2 > &r) |
T & | operator* () const |
T * | operator-> () const |
PointerType | get () const |
template<class T2 > | |
bool | operator== (const SharedPtr< T2 > &r) const |
template<class T2 > | |
bool | operator!= (const SharedPtr< T2 > &r) const |
bool | operator== (std::nullptr_t) const |
bool | operator!= (std::nullptr_t) const |
bool | operator_bool () const |
int | refCount () const |
bool | unique () const |
void | reset () |
template<class T2 > | |
void | reset (const SharedPtr< T2 > &r) |
template<class T2 > | |
void | reset (const WeakPtr< T2 > &r) |
void | reset (T *ptr) |
template<class T2 > | |
SharedPtr< T2 > | staticCast () const |
template<class T2 > | |
SharedPtr< T2 > | dynamicCast () const |
template<class T2 > | |
SharedPtr< T2 > | constCast () const |
template<class T2 > | |
SharedPtr< T2 > | reinterpretCast () const |
![]() | |
operator bool_type () const | |
operator bool_type () | |
A simple shared pointer implementation modelled after boost.
This object keeps track of the assigned pointer and automatically frees it when no more SharedPtr references to it exist.
To achieve that the object implements an internal reference counting. Thus you should try to avoid using the plain pointer after assigning it to a SharedPtr object for the first time. If you still use the plain pointer be sure you do not delete it on your own. You may also not use the plain pointer to create a new SharedPtr object, since that would result in a double deletion of the pointer sooner or later.
Example creation: Common::SharedPtr<int> pointer(new int(1)); would create a pointer to int. Later on usage via *pointer is the same as for a normal pointer. If you need to access the plain pointer value itself later on use the get method. The class also supplies a operator ->, which does the same as the -> operator on a normal pointer.
Be sure you are using new to initialize the pointer you want to manage. If you do not use new for allocating, you have to supply a deleter as second parameter when creating a SharedPtr object. The deleter has to implement operator() which takes the pointer it should free as argument.
Note that you have to specify the type itself not the pointer type as template parameter.
When creating a SharedPtr object from a normal pointer you need a real definition of the type you want SharedPtr to manage, a simple forward definition is not enough.
The class has implicit upcast support, so if you got a class B derived from class A, you can assign a pointer to B without any problems to a SharedPtr object with template parameter A. The very same applies to assignment of a SharedPtr object to a SharedPtr object.
There are also operators != and == to compare two SharedPtr objects with compatible pointers. Comparison between a SharedPtr object and a plain pointer is only possible via SharedPtr::get.
|
inline |
Returns the plain pointer value. Be sure you know what you do if you are continuing to use that pointer.
|
inline |
Implicit conversion operator to bool for convenience, to make checks like "if (sharedPtr) ..." possible.
|
inline |
Returns the number of strong references to the object.
|
inline |
Checks if the object is the only object referring to the assigned pointer. This should just be used for debugging purposes.
|
inline |
Resets the object to a NULL pointer.
|
inline |
Resets the object to the specified shared pointer
|
inline |
Resets the object to the specified weak pointer
|
inline |
Resets the object to the specified pointer
|
inline |
Performs the equivalent of static_cast to a new pointer type
|
inline |
Performs the equivalent of dynamic_cast to a new pointer type
|
inline |
Performs the equivalent of const_cast to a new pointer type
|
inline |
Performs the equivalent of const_cast to a new pointer type