import of enigma2
[vuplus_dvbapp] / 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 (ptr)
31                         ptr->Release();
32                 ptr=c;
33                 if (ptr)
34                         ptr->AddRef();
35                 return *this;
36         }
37         
38         ePtr &operator=(ePtr<T> &c)
39         {
40                 if (ptr)
41                         ptr->Release();
42                 ptr=c.ptr;
43                 if (ptr)
44                         ptr->AddRef();
45                 return *this;
46         }
47         
48         ~ePtr()
49         {
50                 if (ptr)
51                         ptr->Release();
52         }
53         T* &ptrref() { assert(!ptr); return ptr; }
54         T* operator->() { assert(ptr); return ptr; }
55         const T* operator->() const { assert(ptr); return ptr; }
56         operator T*() const { return this->ptr; }
57 };
58
59
60 #endif