X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/1cdf6cb021fcaa6548b90ba7b6765cf1e8b8b37b..5861c3c104ddcb2f1dfe9eb8c660b9792e59f270:/lib/base/smartptr.h diff --git a/lib/base/smartptr.h b/lib/base/smartptr.h index 159eeb2c..814d7457 100644 --- a/lib/base/smartptr.h +++ b/lib/base/smartptr.h @@ -3,14 +3,7 @@ #include "object.h" #include - -#ifdef SWIG -#define TEMPLATE_TYPEDEF(x, y) \ -%template(y) x; \ -typedef x y -#else -#define TEMPLATE_TYPEDEF(x, y) typedef x y -#endif +#include template class ePtr @@ -63,6 +56,83 @@ public: T* &ptrref() { assert(!ptr); return ptr; } T* operator->() const { assert(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 { assert(ptr); return ptr; } + operator T*() const { return this->ptr; } };