aboutsummaryrefslogtreecommitdiff
path: root/lib/base/smartptr.h
blob: 029fd1dc721cd041a1dbfa511ee357f3d6a6d562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef __smartptr_h
#define __smartptr_h

#include "object.h"
#include <stdio.h>

template<class T>
class ePtr
{
protected:
	T *ptr;
public:
	T &operator*() { return *ptr; }
	ePtr(): ptr(0)
	{
	}
	ePtr(T *c): ptr(c)
	{
		if (c)
			c->AddRef();
	}
	ePtr(const ePtr &c)
	{
		ptr=c.ptr;
		if (ptr)
			ptr->AddRef();
	}
	ePtr &operator=(T *c)
	{
		if (ptr)
			ptr->Release();
		ptr=c;
		if (ptr)
			ptr->AddRef();
		return *this;
	}
	
	ePtr &operator=(ePtr<T> &c)
	{
		if (ptr)
			ptr->Release();
		ptr=c.ptr;
		if (ptr)
			ptr->AddRef();
		return *this;
	}
	
	~ePtr()
	{
		if (ptr)
			ptr->Release();
	}
	T* &ptrref() { assert(!ptr); return ptr; }
	T* operator->() { assert(ptr); return ptr; }
	const T* operator->() const { assert(ptr); return ptr; }
	operator T*() const { return this->ptr; }
};


#endif