* Add link target to the Makefile.
[matthijs/ABM2.git] / ABM2 / Engine / mmanager.h
1 #ifndef MMANAGER_H_INCLUDED\r
2 #define MMANAGER_H_INCLUDED\r
3 \r
4 #include "engine.h"\r
5 \r
6 class IMMObject\r
7 {\r
8         private:\r
9                 static IMMObject * liveObjects;\r
10                 static IMMObject * deadObjects;\r
11                 IMMObject *nextObject;\r
12                 IMMObject *prevObject;\r
13                 long refCount;\r
14                 bool bIsStackAllocated;\r
15                 static std::list<IMMObject*> heapObjects;\r
16         protected:\r
17                 IMMObject();\r
18                 virtual ~IMMObject();\r
19         public:\r
20                 void AddRef();\r
21                 void Release();\r
22                 static void CollectGarbage();\r
23                 static void CollectRemainingObjects(bool bEmitWarnings=false);\r
24                 virtual unsigned long size()=0;\r
25                 void *operator new(size_t size);\r
26                 void operator delete(void* obj);\r
27 };\r
28 \r
29 #define AUTO_SIZE unsigned long size(){return sizeof(*this);}\r
30 \r
31 template<class T>\r
32 class CMMPointer\r
33 {\r
34 protected:\r
35         T* obj;\r
36 public:\r
37         CMMPointer(){obj=0;}\r
38         CMMPointer(T *o){obj=0; *this=o;}\r
39         CMMPointer(const CMMPointer<T> &p){obj=0; *this=p;}\r
40 \r
41         ~CMMPointer(){if(obj)obj->Release();}\r
42 \r
43         inline void operator =(const CMMPointer<T> &p)\r
44         {\r
45                 if(obj)obj->Release();\r
46                 obj=p.obj;\r
47                 if(obj)obj->AddRef();\r
48         }\r
49         inline void operator =(T* o)\r
50         {\r
51                 if(obj)obj->Release();\r
52                 obj=o;\r
53                 if(obj)obj->AddRef();\r
54         }\r
55 \r
56         inline T& operator *() const\r
57         {\r
58                 assert(obj!=0 && "Tried to * a NULL smart pointer");\r
59                 return *obj;\r
60         }\r
61 \r
62         inline T* operator ->() const\r
63         {\r
64                 assert(obj!=0 && "Tried to -> on a NULL smart pointer");\r
65                 return obj;\r
66         }\r
67 \r
68         inline operator T*() const\r
69         {\r
70                 return obj;\r
71         }\r
72 \r
73         inline bool operator !(){return !(obj);}\r
74 \r
75         inline bool isValid() const\r
76         {return (obj!=0);}\r
77 \r
78         inline bool operator ==(const CMMPointer<T> &p) const \r
79         {\r
80                 return (obj==p.obj);\r
81         }\r
82         inline bool operator ==(const T* o) const\r
83         {\r
84                 return (obj==o);\r
85         }\r
86 };\r
87 \r
88 template<class T, int i>\r
89 class CMMBlob : public IMMObject\r
90 {\r
91         protected:\r
92         T buffer[i];\r
93         public:\r
94         inline T& operator [](int index){assert(index<i); return buffer[index];}\r
95         inline operator T*(){return buffer;}\r
96         AUTO_SIZE;\r
97 };\r
98 \r
99 template<class T>\r
100 class CMMDynamicBlob : public IMMObject\r
101 {\r
102         protected:\r
103         unsigned long dataSize;\r
104         T *buffer;\r
105         public:\r
106         inline T& operator [](int index){assert(index<dataSize); return buffer[index];}\r
107         inline operator T*(){return buffer;}\r
108         CMMDynamicBlob(unsigned long size){dataSize=size; buffer=new T[size]; }\r
109         ~CMMDynamicBlob(){if(buffer)delete[] buffer;}\r
110         unsigned long size(){return dataSize*sizeof(T) + sizeof(this); }\r
111         unsigned long blobsize(){return dataSize;}\r
112 };\r
113 \r
114 #endif\r