servicemp3.cpp: more simple/flexible streaming detection
[enigma2.git] / lib / base / smartptr.h
index 159eeb2c0122ff43fa16786fb7dd787c20eaeb62..be83528ff7b44b35a032fbe0b0abba53af64805c 100644 (file)
@@ -3,20 +3,28 @@
 
 #include "object.h"
 #include <stdio.h>
 
 #include "object.h"
 #include <stdio.h>
+#include <string.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
 {
 protected:
        T *ptr;
 
 template<class T>
 class ePtr
 {
 protected:
        T *ptr;
+       char m_ptrStr[sizeof(void*)*2+1];
+       void updatePtrStr()
+       {
+               if (ptr) {
+                       if (sizeof(void*) > 4)
+                               sprintf(m_ptrStr, "%llx", (unsigned long long)ptr);
+                       else
+                               sprintf(m_ptrStr, "%lx", (unsigned long)ptr);
+               }
+               else
+                       strcpy(m_ptrStr, "NIL");
+       }
 public:
        T &operator*() { return *ptr; }
        ePtr(): ptr(0)
 public:
        T &operator*() { return *ptr; }
        ePtr(): ptr(0)
@@ -26,12 +34,13 @@ public:
        {
                if (c)
                        c->AddRef();
        {
                if (c)
                        c->AddRef();
+               updatePtrStr();
        }
        }
-       ePtr(const ePtr &c)
+       ePtr(const ePtr &c): ptr(c.ptr)
        {
        {
-               ptr=c.ptr;
                if (ptr)
                        ptr->AddRef();
                if (ptr)
                        ptr->AddRef();
+               updatePtrStr();
        }
        ePtr &operator=(T *c)
        {
        }
        ePtr &operator=(T *c)
        {
@@ -40,9 +49,9 @@ public:
                if (ptr)
                        ptr->Release();
                ptr=c;
                if (ptr)
                        ptr->Release();
                ptr=c;
+               updatePtrStr();
                return *this;
        }
                return *this;
        }
-       
        ePtr &operator=(ePtr<T> &c)
        {
                if (c.ptr)
        ePtr &operator=(ePtr<T> &c)
        {
                if (c.ptr)
@@ -50,18 +59,98 @@ public:
                if (ptr)
                        ptr->Release();
                ptr=c.ptr;
                if (ptr)
                        ptr->Release();
                ptr=c.ptr;
+               updatePtrStr();
                return *this;
        }
                return *this;
        }
-       
        ~ePtr()
        {
                if (ptr)
                        ptr->Release();
        }
        ~ePtr()
        {
                if (ptr)
                        ptr->Release();
        }
-       
+       char *getPtrString()
+       {
+               return m_ptrStr;
+       }
+#ifndef SWIG
        T* grabRef() { if (!ptr) return 0; ptr->AddRef(); return ptr; }
        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; }
+};
+
+
+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; }
 };
 
        operator T*() const { return this->ptr; }
 };
 
@@ -93,29 +182,23 @@ public:
        eMutablePtr(): ePtr<T>(0)
        {
        }
        eMutablePtr(): ePtr<T>(0)
        {
        }
-       
        eMutablePtr(T *c): ePtr<T>(c)
        {
        }
        eMutablePtr(T *c): ePtr<T>(c)
        {
        }
-
        eMutablePtr(const eMutablePtr &c): ePtr<T>(c)
        {
        }
        eMutablePtr(const eMutablePtr &c): ePtr<T>(c)
        {
        }
-       
        eMutablePtr &operator=(T *c)
        {
                ePtr<T>::operator=(c);
                return *this;
        }
        eMutablePtr &operator=(T *c)
        {
                ePtr<T>::operator=(c);
                return *this;
        }
-       
-       
-       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. */
                        /* 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
 
 };
 #endif