From: Felix Domke Date: Thu, 27 May 2004 14:28:44 +0000 (+0000) Subject: add helper object for eSmartPtr, resolving a race condition. X-Git-Tag: 2.6.0~5960 X-Git-Url: https://git.cweiske.de/enigma2.git/commitdiff_plain/ae9b6fba0b02b5990fd1635a2154336c5043df02?ds=sidebyside add helper object for eSmartPtr, resolving a race condition. --- diff --git a/lib/base/smartptr.h b/lib/base/smartptr.h index 029fd1dc..c89763e3 100644 --- a/lib/base/smartptr.h +++ b/lib/base/smartptr.h @@ -7,6 +7,22 @@ template class 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: @@ -51,7 +67,12 @@ public: ptr->Release(); } T* &ptrref() { assert(!ptr); return ptr; } - T* operator->() { assert(ptr); return ptr; } + 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 */ + /* ePtrs note that they have to be const. */ const T* operator->() const { assert(ptr); return ptr; } operator T*() const { return this->ptr; } };