-
Notifications
You must be signed in to change notification settings - Fork 2
/
mmanager.h
114 lines (99 loc) · 2.26 KB
/
mmanager.h
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
#ifndef MMANAGER_H_INCLUDED
#define MMANAGER_H_INCLUDED
#include "engine.h"
class IMMObject
{
private:
static IMMObject * liveObjects;
static IMMObject * deadObjects;
IMMObject *nextObject;
IMMObject *prevObject;
long refCount;
bool bIsStackAllocated;
static std::list<IMMObject*> heapObjects;
protected:
IMMObject();
virtual ~IMMObject();
public:
void AddRef();
void Release();
static void CollectGarbage();
static void CollectRemainingObjects(bool bEmitWarnings=false);
virtual unsigned long size()=0;
void *operator new(size_t size);
void operator delete(void* obj);
};
#define AUTO_SIZE unsigned long size(){return sizeof(*this);}
template<class T>
class CMMPointer
{
protected:
T* obj;
public:
CMMPointer(){obj=0;}
CMMPointer(T *o){obj=0; *this=o;}
CMMPointer(const CMMPointer<T> &p){obj=0; *this=p;}
~CMMPointer(){if(obj)obj->Release();}
inline bool operator =(const CMMPointer<T> &p)
{
if(obj)obj->Release();
obj=p.obj;
if(obj)obj->AddRef();
}
inline bool operator =(T* o)
{
if(obj)obj->Release();
obj=o;
if(obj)obj->AddRef();
}
inline T& operator *() const
{
assert(obj!=0 && "Tried to * a NULL smart pointer");
return *obj;
}
inline T* operator ->() const
{
assert(obj!=0 && "Tried to -> on a NULL smart pointer");
return obj;
}
inline operator T*() const
{
return obj;
}
inline bool operator !(){return !(obj);}
inline bool isValid() const
{return (obj!=0);}
inline bool operator ==(const CMMPointer<T> &p) const
{
return (obj==p.obj);
}
inline bool operator ==(const T* o) const
{
return (obj==o);
}
};
template<class T, int i>
class CMMBlob : public IMMObject
{
protected:
T buffer[i];
public:
inline T& operator [](int index){assert(index<i); return buffer[index];}
inline operator T*(){return buffer;}
AUTO_SIZE;
};
template<class T>
class CMMDynamicBlob : public IMMObject
{
protected:
unsigned long dataSize;
T *buffer;
public:
inline T& operator [](int index){assert(index<dataSize); return buffer[index];}
inline operator T*(){return buffer;}
CMMDynamicBlob(unsigned long size){dataSize=size; buffer=new T[size]; }
~CMMDynamicBlob(){if(buffer)delete[] buffer;}
unsigned long size(){return dataSize*sizeof(T) + sizeof(this); }
unsigned long blobsize(){return dataSize;}
};
#endif