openATV enigma2
openATV is an open source SetTopBox Graphical user interface.
smartptr.h
Go to the documentation of this file.
1 #ifndef __smartptr_h
2 #define __smartptr_h
3 
4 #include "object.h"
5 #include <stdio.h>
6 #include <string.h>
7 #include <lib/python/swig.h>
8 
9 template<class T>
10 class ePtr
11 {
12 protected:
13  T *ptr;
14 public:
15  T &operator*() { return *ptr; }
16  ePtr(): ptr(0)
17  {
18  }
19  ePtr(T *c): ptr(c)
20  {
21  if (c)
22  c->AddRef();
23  }
24  ePtr(const ePtr &c): ptr(c.ptr)
25  {
26  if (ptr)
27  ptr->AddRef();
28  }
29  ePtr &operator=(T *c)
30  {
31  if (c)
32  c->AddRef();
33  if (ptr)
34  ptr->Release();
35  ptr=c;
36  return *this;
37  }
39  {
40  if (c.ptr)
41  c.ptr->AddRef();
42  if (ptr)
43  ptr->Release();
44  ptr=c.ptr;
45  return *this;
46  }
48  {
49  if (ptr)
50  ptr->Release();
51  }
52  /*
53  * Horribly misnamed now, but ServiceEventTracker does not
54  * care about the returned type.
55  */
56  void *getPtrString() const
57  {
58  return ptr;
59  }
60 #ifndef SWIG
61  T* grabRef() { if (!ptr) return 0; ptr->AddRef(); return ptr; }
62  T* &ptrref() { return ptr; }
63  operator bool() const { return !!this->ptr; }
64 #endif
65  T* operator->() const { return ptr; }
66  operator T*() const { return this->ptr; }
67 };
68 
69 
70 template<class T>
71 class eUsePtr
72 {
73 protected:
74  T *ptr;
75 public:
76  T &operator*() { return *ptr; }
77  eUsePtr(): ptr(0)
78  {
79  }
80  eUsePtr(T *c): ptr(c)
81  {
82  if (c)
83  {
84  c->AddRef();
85  c->AddUse();
86  }
87  }
88  eUsePtr(const eUsePtr &c)
89  {
90  ptr=c.ptr;
91  if (ptr)
92  {
93  ptr->AddRef();
94  ptr->AddUse();
95  }
96  }
98  {
99  if (c)
100  {
101  c->AddRef();
102  c->AddUse();
103  }
104  if (ptr)
105  {
106  ptr->ReleaseUse();
107  ptr->Release();
108  }
109  ptr=c;
110  return *this;
111  }
113  {
114  if (c.ptr)
115  {
116  c.ptr->AddRef();
117  c.ptr->AddUse();
118  }
119  if (ptr)
120  {
121  ptr->ReleaseUse();
122  ptr->Release();
123  }
124  ptr=c.ptr;
125  return *this;
126  }
128  {
129  if (ptr)
130  {
131  ptr->ReleaseUse();
132  ptr->Release();
133  }
134  }
135 #ifndef SWIG
136  T* grabRef() { if (!ptr) return 0; ptr->AddRef(); ptr->AddUse(); return ptr; }
137  T* &ptrref() { return ptr; }
138 #endif
139  T* operator->() const { return ptr; }
140  operator T*() const { return this->ptr; }
141 };
142 
143 
144 
145 #ifndef SWIG
146 template<class T>
147 class eMutablePtr: public ePtr<T>
148 {
149  /* read doc/iObject about the ePtrHelper */
150  template<class T1>
151  class ePtrHelper
152  {
153  T1 *m_obj;
154  public:
155  inline ePtrHelper(T1 *obj): m_obj(obj)
156  {
157  m_obj->AddRef();
158  }
159  inline ~ePtrHelper()
160  {
161  m_obj->Release();
162  }
163  inline T1* operator->() { return m_obj; }
164  };
165 protected:
166  T *ptr;
167 public:
168  eMutablePtr(): ePtr<T>(0)
169  {
170  }
171  eMutablePtr(T *c): ePtr<T>(c)
172  {
173  }
174  eMutablePtr(const eMutablePtr &c): ePtr<T>(c)
175  {
176  }
178  {
180  return *this;
181  }
182  ePtrHelper<T> operator->() { return ePtrHelper<T>(ptr); }
183  /* for const objects, we don't need the helper, as they can't */
184  /* be changed outside the program flow. at least this is */
185  /* what the compiler assumes, so in case you're using const */
186  /* eMutablePtrs note that they have to be const. */
187  const T* operator->() const { return ptr; }
188 };
189 #endif
190 
191 #endif
Definition: smartptr.h:148
eMutablePtr & operator=(T *c)
Definition: smartptr.h:177
T * ptr
Definition: smartptr.h:166
const T * operator->() const
Definition: smartptr.h:187
eMutablePtr()
Definition: smartptr.h:168
eMutablePtr(const eMutablePtr &c)
Definition: smartptr.h:174
eMutablePtr(T *c)
Definition: smartptr.h:171
ePtrHelper< T > operator->()
Definition: smartptr.h:182
Definition: smartptr.h:11
T *& ptrref()
Definition: smartptr.h:62
T * ptr
Definition: smartptr.h:13
void * getPtrString() const
Definition: smartptr.h:56
ePtr(T *c)
Definition: smartptr.h:19
ePtr & operator=(T *c)
Definition: smartptr.h:29
ePtr()
Definition: smartptr.h:16
ePtr & operator=(ePtr< T > &c)
Definition: smartptr.h:38
T * operator->() const
Definition: smartptr.h:65
~ePtr()
Definition: smartptr.h:47
T * grabRef()
Definition: smartptr.h:61
ePtr(const ePtr &c)
Definition: smartptr.h:24
T & operator*()
Definition: smartptr.h:15
Definition: smartptr.h:72
~eUsePtr()
Definition: smartptr.h:127
eUsePtr & operator=(eUsePtr< T > &c)
Definition: smartptr.h:112
eUsePtr()
Definition: smartptr.h:77
T * grabRef()
Definition: smartptr.h:136
eUsePtr & operator=(T *c)
Definition: smartptr.h:97
T *& ptrref()
Definition: smartptr.h:137
eUsePtr(T *c)
Definition: smartptr.h:80
T & operator*()
Definition: smartptr.h:76
T * operator->() const
Definition: smartptr.h:139
T * ptr
Definition: smartptr.h:74
eUsePtr(const eUsePtr &c)
Definition: smartptr.h:88