#include <smartptr.h>
Inheritance diagram for gutz::SmartPtr< T >:
Smart Pointers are FUN FUN FUN.
For all intents an purposes, SmartPtrs work just like regular pointers, for instance you can:
gutz::SmartPtr<MyClass> mcsp(); // create a "0" (Null) pointer by default mcsp = new MyClass(); // assign a pointer if( mcsp ) // check if the pointer is non-zero if( !mcsp ) // check if the pointer is zero if( mcsp == mcp ) // compare against another SmartPtr or raw pointer mcsp->myFunction(); // call a member function (*mcsp) = blah; // de-refrence // declaration of some function taking a pointer to MyClass void someFunc(MyClass *mcp); // call that function, but let the SmartPtr auto-cast to a pointer someFunc( mcsp ); // works fine too
Const SmartPtrs work just like const ptrs.
const SmartPtr<T> ~= T const*
mySP.getPtr();
There is one major caveaut: Any class that can have a SmartPtr to it MUST be a subclass of gutz::Counted. This insures that we only have one (1) reference counter to any pointer, since it is a member of the class rather than a "proxy-class". One day I will implement an "AutoDeletPtr" which allows you to do the same thing with non-Counted classes. Forcing a Counted subclass, however, has advantages:
SmartPtr is itself a subclass of counted so that you can have SmartPtrs to SmartPtrs and SmartPtrRef s.
Notes: Having issues? You shouldn't create a Counted object on the stack. If you do, and somebody else gets a smart pointer to that object, the smartptr will call delete on stack memory when the refcount drops to 0, which it might if the function doesn't delete the object first, both cases are bad. Here is an example that would cause an error.
SmartPtr<T> newObjSP; T newObj; newObjSP = &newObj;
SmartPtr<T> newObjSP = new T;
Another caveaut of smartptrs is that if an object is using a smartptr to itself (why? who knows?), when the smartptr is deleted it might just delete the object that is using it. This happens especially when the constructor uses the smartptr, badness! If you have to do this, you need to increment your own reference counter first, then decrement when the ptr is gone (to avoid memory leaks). Again, be carefull, if the smartptr is created on the stack, you're pretty much screwed, because you can't possibly decrement after the smartptr is deleted. This reference to self situation can come up when iterators use smartptrs to the object they are iterating through. If the iterator is used internally, then when it goes away, the object itself might get deleted. Like say, when you need to iterate through yourself in the constructor :) You know you are getting into the danger zone when you do this (no pun intended):
// somewhere in a member function of "MyObj" SmartPtr<MyObj> s(this); /// this!!!
// somewhere in a member function of "MyObj" _incCount(); SmartPtr<MyObj> *sp = new SmartPtr<MyObj>(this); <... do what you gota do ...> delete sp; _decCount();
// somewhere in a member function of "MyObj" _incCount(); someFunctionUsingSPs(SmartPtr<MyObj>(this)); _decCount();
Definition at line 180 of file smartptr.h.
get the contained pointer | |
T * | getPtr () |
T *const | getPtr () const |
assign another smart pointer, or a raw pointer | |
SmartPtr & | operator= (const SmartPtr &sp) |
T * | operator= (T *const ptr) |
access members of T | |
ex mySmartPtr->myFunction() | |
T * | operator-> () |
T *const | operator-> () const |
De-reference like a regular pointer | |
ex (*mySmartPtr).myFunction() | |
T & | operator * () |
T & | operator * () const |
Implicit conversion to T* | |
good for function calls that take a regular pointer, for example: ///declaration of some function, taking a pointer void someFunc(someType *sc); ///calling "someFunc" using a smartptr as the parameter someFunc( mySP2someType ); /// converts SP to a raw pointer implicitly if( mySP2someType ) /// test if ptr != 0 | |
operator T *const () const | |
Casting for convenience | |
Handles dynamic cast for you. | |
template<class CT> SmartPtr< CT > | cast () const |
mySubClassSP = myBaseClassSP.down_cast<MySubClassType>(); | |
utilities, comparison ops | |
just like raw ptrs, if(SP != 0)... if(SP == NULL) | |
bool | operator! () const |
bool | isNull () const |
just for convienence. | |
Public Types | |
typedef T | type |
Public Member Functions | |
SmartPtr () | |
SmartPtr (T *const ptr) | |
SmartPtr (const SmartPtr &sp) | |
virtual | ~SmartPtr () |
Protected Member Functions | |
void | assign (T *const ref) |
virtual void | _incCount () |
gutz::Counted interface, increment reference count by one. | |
virtual void | _decCount () |
gutz::Counted interface, decrement reference count by one. | |
virtual int | _getCount () const |
gutz::Counted interface, get the current reference count. | |
Protected Attributes | |
T * | _ref |
The only data we have is this pointer, so we have the same memory foot print of a regular pointer. | |
Friends | |
class | SmartPtr |
class | SmartPtrRef |
|
Definition at line 183 of file smartptr.h. |
|
Definition at line 185 of file smartptr.h. |
|
Definition at line 186 of file smartptr.h. |
|
Definition at line 187 of file smartptr.h. |
|
Definition at line 188 of file smartptr.h. |
|
gutz::Counted interface, decrement reference count by one. Not generaly used by subclasses, mostly for collaboration with gutz::SmartPtr. Sometimes you need to call this though, see the documentation for gutz::SmartPtr Definition at line 54 of file smartptr.h. Referenced by TFImage::clear(), NrroImage::fBlendOverRGBA(), and Nrro::updateMinMax(). |
|
gutz::Counted interface, get the current reference count. Not generaly used by subclasses, mostly for collaboration with gutz::SmartPtr. Definition at line 58 of file smartptr.h. |
|
gutz::Counted interface, increment reference count by one. Not generaly used by subclasses, mostly for collaboration with gutz::SmartPtr. Sometimes you need to call this though, see the documentation for gutz::SmartPtr Definition at line 48 of file smartptr.h. Referenced by TFImage::clear(), NrroImage::fBlendOverRGBA(), and Nrro::updateMinMax(). |
|
inc ref if non-zero save off old reference assign _ref = ref delete the old reference just a dec most of the time... but if we hold the last reference, nuke it Definition at line 285 of file smartptr.h. Referenced by gutz::SmartPtr< TFElementList >::operator=(), gutz::SmartPtr< TFElementList >::SmartPtr(), and gutz::SmartPtr< TFElementList >::~SmartPtr(). |
|
mySubClassSP = myBaseClassSP.down_cast<MySubClassType>();
Definition at line 249 of file smartptr.h. |
|
Definition at line 194 of file smartptr.h. |
|
|
just for convienence. access via, mySp.isNull() Definition at line 276 of file smartptr.h. Referenced by TFItemVec::addItem(), Crank::checkKind(), and GrinderKeys::pconstID(). |
|
Definition at line 218 of file smartptr.h. |
|
Definition at line 217 of file smartptr.h. |
|
Definition at line 234 of file smartptr.h. |
|
Definition at line 261 of file smartptr.h. |
|
Definition at line 210 of file smartptr.h. |
|
Definition at line 209 of file smartptr.h. |
|
Definition at line 202 of file smartptr.h. |
|
Definition at line 200 of file smartptr.h. |
|
Definition at line 40 of file smartptr.h. Referenced by Nrro::NrroIter< T >::NrroIter(). |
|
Definition at line 41 of file smartptr.h. |
|
The only data we have is this pointer, so we have the same memory foot print of a regular pointer.
Definition at line 283 of file smartptr.h. Referenced by gutz::SmartPtr< TFElementList >::assign(), gutz::SmartPtr< TFElementList >::cast(), gutz::SmartPtr< TFElementList >::getPtr(), gutz::SmartPtr< TFElementList >::operator=(), and gutz::SmartPtr< TFElementList >::SmartPtr(). |