fixed refcount of eListboxPythonStringContent
[enigma2.git] / lib / base / smartptr.h
1 #ifndef __smartptr_h
2 #define __smartptr_h
3
4 #include "object.h"
5 #include <stdio.h>
6
7 template<class T>
8 class ePtr
9 {
10 protected:
11         T *ptr;
12 public:
13         T &operator*() { return *ptr; }
14         ePtr(): ptr(0)
15         {
16         }
17         ePtr(T *c): ptr(c)
18         {
19                 if (c)
20                         c->AddRef();
21         }
22         ePtr(const ePtr &c)
23         {
24                 ptr=c.ptr;
25                 if (ptr)
26                         ptr->AddRef();
27         }
28         ePtr &operator=(T *c)
29         {
30                 if (c)
31                         c->AddRef();
32                 if (ptr)
33                         ptr->Release();
34                 ptr=c;
35                 return *this;
36         }
37         
38         ePtr &operator=(ePtr<T> &c)
39         {
40                 if (c.ptr)
41                         c.ptr->AddRef();
42                 if (ptr)
43                         ptr->Release();
44                 ptr=c.ptr;
45                 return *this;
46         }
47         
48         ~ePtr()
49         {
50                 if (ptr)
51                         ptr->Release();
52         }
53         
54         T* grabRef() { if (!ptr) return 0; ptr->AddRef(); return ptr; }
55         T* &ptrref() { assert(!ptr); return ptr; }
56         T* operator->() const { assert(ptr); return ptr; }
57         operator T*() const { return this->ptr; }
58 };
59
60
61
62 #ifndef SWIG
63 template<class T>
64 class eMutablePtr: public ePtr<T>
65 {
66                 /* read doc/iObject about the ePtrHelper */
67         template<class T1>
68         class ePtrHelper
69         {
70                 T1 *m_obj;
71         public:
72                 inline ePtrHelper(T1 *obj): m_obj(obj)
73                 {
74                         m_obj->AddRef();
75                 }
76                 inline ~ePtrHelper()
77                 {
78                         m_obj->Release();
79                 }
80                 inline T1* operator->() { return m_obj; }
81         };
82 protected:
83         T *ptr;
84 public:
85         eMutablePtr(): ePtr<T>(0)
86         {
87         }
88         
89         eMutablePtr(T *c): ePtr<T>(c)
90         {
91         }
92
93         eMutablePtr(const eMutablePtr &c): ePtr<T>(c)
94         {
95         }
96         
97         eMutablePtr &operator=(T *c)
98         {
99                 ePtr<T>::operator=(c);
100                 return *this;
101         }
102         
103         
104         ePtrHelper<T> operator->() { assert(ptr); return ePtrHelper<T>(ptr); }
105
106                         /* for const objects, we don't need the helper, as they can't */
107                         /* be changed outside the program flow. at least this is */
108                         /* what the compiler assumes, so in case you're using const */
109                         /* eMutablePtrs note that they have to be const. */
110         const T* operator->() const { assert(ptr); return ptr; }
111 };
112 #endif
113
114 #endif