fix possible crash on weird audio streams
[enigma2.git] / lib / base / smartptr.h
index 7e441ab2976ab67e48e67b89064a72c22fe9ec7f..a2c6691f09097f2116b02cc4ef6e0b1957d7073d 100644 (file)
@@ -3,14 +3,9 @@
 
 #include "object.h"
 #include <stdio.h>
+#include <lib/python/swig.h>
 
-#ifdef SWIG
-#define TEMPLATE_TYPEDEF(x, y) \
-%template(y) x; \
-typedef x y
-#else
-#define TEMPLATE_TYPEDEF(x, y) typedef x y
-#endif
+inline void ptrAssert(void *p) { if (!p) *(unsigned long*)0=0; }
 
 template<class T>
 class ePtr
@@ -59,9 +54,12 @@ 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; }
+       operator bool() const { return !!this->ptr; }
+#endif
+       T* operator->() const { ptrAssert(ptr); return ptr; }
        operator T*() const { return this->ptr; }
 };
 
@@ -134,9 +132,11 @@ public:
                }
        }
        
+#ifndef SWIG
        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; }
+       T* &ptrref() { ASSERT(!ptr); return ptr; }
+#endif
+       T* operator->() const { ptrAssert(ptr); return ptr; }
        operator T*() const { return this->ptr; }
 };
 
@@ -184,13 +184,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