replace more assertions to get proper log messages
[enigma2.git] / lib / base / smartptr.h
index 906bba69e8fb49922f033977a7278b15b6c6c72d..ac6b9eb0767fe8d8854e43043c63bc67e4c368f9 100644 (file)
@@ -3,6 +3,9 @@
 
 #include "object.h"
 #include <stdio.h>
+#include <lib/python/swig.h>
+
+inline void ptrAssert(void *p) { if (!p) *(unsigned long*)0=0; }
 
 template<class T>
 class ePtr
@@ -51,9 +54,90 @@ public:
                        ptr->Release();
        }
        
+#ifndef SWIG
        T* grabRef() { if (!ptr) return 0; ptr->AddRef(); return ptr; }
-       T* &ptrref() { assert(!ptr); return ptr; }
-       T* operator->() const { assert(ptr); return ptr; }
+       T* &ptrref() { ASSERT(!ptr); return ptr; }
+#endif
+       T* operator->() const { ptrAssert(ptr); return ptr; }
+       operator T*() const { return this->ptr; }
+       
+       operator bool() const { return !!this->ptr; }
+};
+
+
+template<class T>
+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<T> &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();
+               }
+       }
+       
+#ifndef SWIG
+       T* grabRef() { if (!ptr) return 0; ptr->AddRef(); ptr->AddUse(); return ptr; }
+       T* &ptrref() { ASSERT(!ptr); return ptr; }
+#endif
+       T* operator->() const { ptrAssert(ptr); return ptr; }
        operator T*() const { return this->ptr; }
 };
 
@@ -101,13 +185,13 @@ public:
        }
        
        
-       ePtrHelper<T> operator->() { assert(ptr); return ePtrHelper<T>(ptr); }
+       ePtrHelper<T> operator->() { ptrAssert(ptr); return ePtrHelper<T>(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