aboutsummaryrefslogtreecommitdiff
path: root/lib/base/smartptr.h
blob: be83528ff7b44b35a032fbe0b0abba53af64805c (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef __smartptr_h
#define __smartptr_h

#include "object.h"
#include <stdio.h>
#include <string.h>
#include <lib/python/swig.h>

inline void ptrAssert(void *p) { if (!p) *(unsigned long*)0=0; }

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)
	{
	}
	ePtr(T *c): ptr(c)
	{
		if (c)
			c->AddRef();
		updatePtrStr();
	}
	ePtr(const ePtr &c): ptr(c.ptr)
	{
		if (ptr)
			ptr->AddRef();
		updatePtrStr();
	}
	ePtr &operator=(T *c)
	{
		if (c)
			c->AddRef();
		if (ptr)
			ptr->Release();
		ptr=c;
		updatePtrStr();
		return *this;
	}
	ePtr &operator=(ePtr<T> &c)
	{
		if (c.ptr)
			c.ptr->AddRef();
		if (ptr)
			ptr->Release();
		ptr=c.ptr;
		updatePtrStr();
		return *this;
	}
	~ePtr()
	{
		if (ptr)
			ptr->Release();
	}
	char *getPtrString()
	{
		return m_ptrStr;
	}
#ifndef SWIG
	T* grabRef() { if (!ptr) return 0; ptr->AddRef(); 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; }
};



#ifndef SWIG
template<class T>
class eMutablePtr: public ePtr<T>
{
		/* read doc/iObject about the ePtrHelper */
	template<class T1>
	class ePtrHelper
	{
		T1 *m_obj;
	public:
		inline ePtrHelper(T1 *obj): m_obj(obj)
		{
			m_obj->AddRef();
		}
		inline ~ePtrHelper()
		{
			m_obj->Release();
		}
		inline T1* operator->() { return m_obj; }
	};
protected:
	T *ptr;
public:
	eMutablePtr(): ePtr<T>(0)
	{
	}
	eMutablePtr(T *c): ePtr<T>(c)
	{
	}
	eMutablePtr(const eMutablePtr &c): ePtr<T>(c)
	{
	}
	eMutablePtr &operator=(T *c)
	{
		ePtr<T>::operator=(c);
		return *this;
	}
	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. */
	const T* operator->() const { ptrAssert(ptr); return ptr; }
};
#endif

#endif