X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/ddc3964ed95d01e72229dc9af968a327cd84e56c..633c730e5feed206333c5174d53c6384c08047b2:/lib/base/smartptr.h diff --git a/lib/base/smartptr.h b/lib/base/smartptr.h index 906bba69..6c3dbc2f 100644 --- a/lib/base/smartptr.h +++ b/lib/base/smartptr.h @@ -3,6 +3,9 @@ #include "object.h" #include +#include + +inline void ptrAssert(void *p) { if (!p) *(unsigned long*)0=0; } template class ePtr @@ -53,7 +56,84 @@ public: T* grabRef() { if (!ptr) return 0; ptr->AddRef(); return ptr; } T* &ptrref() { assert(!ptr); return ptr; } - T* operator->() const { assert(ptr); return ptr; } + T* operator->() const { ptrAssert(ptr); return ptr; } + operator T*() const { return this->ptr; } + + operator bool() const { return !!this->ptr; } +}; + + +template +class eUsePtr +{ +protected: + T *ptr; +public: + T &operator*() { return *ptr; } + eUsePtr(): ptr(0) + { + } + eUsePtr(T *c): ptr(c) + { + if (c) + { + c->AddRef(); + c->AddUse(); + } + } + eUsePtr(const eUsePtr &c) + { + ptr=c.ptr; + if (ptr) + { + ptr->AddRef(); + ptr->AddUse(); + } + } + eUsePtr &operator=(T *c) + { + if (c) + { + c->AddRef(); + c->AddUse(); + } + if (ptr) + { + ptr->ReleaseUse(); + ptr->Release(); + } + ptr=c; + return *this; + } + + eUsePtr &operator=(eUsePtr &c) + { + if (c.ptr) + { + c.ptr->AddRef(); + c.ptr->AddUse(); + } + if (ptr) + { + ptr->ReleaseUse(); + ptr->Release(); + } + ptr=c.ptr; + return *this; + } + + ~eUsePtr() + { + if (ptr) + { + ptr->ReleaseUse(); + ptr->Release(); + } + } + + T* grabRef() { if (!ptr) return 0; ptr->AddRef(); ptr->AddUse(); return ptr; } + T* &ptrref() { assert(!ptr); return ptr; } + T* operator->() const { ptrAssert(ptr); return ptr; } operator T*() const { return this->ptr; } }; @@ -101,13 +181,13 @@ public: } - ePtrHelper operator->() { assert(ptr); return ePtrHelper(ptr); } + ePtrHelper operator->() { ptrAssert(ptr); return ePtrHelper(ptr); } /* for const objects, we don't need the helper, as they can't */ /* be changed outside the program flow. at least this is */ /* what the compiler assumes, so in case you're using const */ /* eMutablePtrs note that they have to be const. */ - const T* operator->() const { assert(ptr); return ptr; } + const T* operator->() const { ptrAssert(ptr); return ptr; } }; #endif