X-Git-Url: http://code.vuplus.com/gitweb/?p=vuplus_dvbapp;a=blobdiff_plain;f=lib%2Fbase%2Fsmartptr.h;h=159eeb2c0122ff43fa16786fb7dd787c20eaeb62;hp=029fd1dc721cd041a1dbfa511ee357f3d6a6d562;hb=1cdf6cb021fcaa6548b90ba7b6765cf1e8b8b37b;hpb=d63d2c3c6cbbd574dda4f8b00ebe6c661735edd5 diff --git a/lib/base/smartptr.h b/lib/base/smartptr.h index 029fd1d..159eeb2 100644 --- a/lib/base/smartptr.h +++ b/lib/base/smartptr.h @@ -4,6 +4,14 @@ #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 + template class ePtr { @@ -27,21 +35,21 @@ public: } ePtr &operator=(T *c) { + if (c) + c->AddRef(); if (ptr) ptr->Release(); ptr=c; - if (ptr) - ptr->AddRef(); return *this; } ePtr &operator=(ePtr &c) { + if (c.ptr) + c.ptr->AddRef(); if (ptr) ptr->Release(); ptr=c.ptr; - if (ptr) - ptr->AddRef(); return *this; } @@ -50,11 +58,65 @@ public: if (ptr) ptr->Release(); } + + T* grabRef() { if (!ptr) return 0; ptr->AddRef(); return ptr; } T* &ptrref() { assert(!ptr); return ptr; } - T* operator->() { assert(ptr); return ptr; } - const T* operator->() const { assert(ptr); return ptr; } + T* operator->() const { assert(ptr); return ptr; } operator T*() const { return this->ptr; } }; + +#ifndef SWIG +template +class eMutablePtr: public ePtr +{ + /* read doc/iObject about the ePtrHelper */ + template + class ePtrHelper + { + T1 *m_obj; + public: + inline ePtrHelper(T1 *obj): m_obj(obj) + { + m_obj->AddRef(); + } + inline ~ePtrHelper() + { + m_obj->Release(); + } + inline T1* operator->() { return m_obj; } + }; +protected: + T *ptr; +public: + eMutablePtr(): ePtr(0) + { + } + + eMutablePtr(T *c): ePtr(c) + { + } + + eMutablePtr(const eMutablePtr &c): ePtr(c) + { + } + + eMutablePtr &operator=(T *c) + { + ePtr::operator=(c); + return *this; + } + + + ePtrHelper operator->() { assert(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; } +}; +#endif + #endif