forked from LemonHaze420/DCPopulous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.cpp
258 lines (227 loc) · 4.72 KB
/
object.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "Object.h"
#include "SafeProgramming.h"
#include <windows.h>
// Class variable
#ifndef ULTRA_FAST_OBJECT
DWORD CObject::ObjectCount = 0;
DWORD CObject::MaxObjectCount = 0;
CObject* CObject::FirstObject = NULL;
#endif
// ----
CObject::CObject()
{
ErrorCode = 0;
#ifndef ULTRA_FAST_OBJECT
StringRepresentation = NULL;
CreationInfo = NULL;
#endif
#if (!defined(FAST_OBJECT) && !defined(ULTRA_FAST_OBJECT))
NextObject = NULL;
if (ObjectCount == 0)
{
FirstObject = this;
}
else
{
// Add to end of object chain.
CObject* i = FirstObject;
while (i->NextObject)
{
// Check that we're not already in the list.
if (this == i)
{
// Panic!!!
OutputDebugString(TEXT("PANIC: Loop in object chain possible!\n"));
}
else
{
i = i->NextObject;
}
}
i->NextObject = this;
}
#endif
#ifndef ULTRA_FAST_OBJECT
// Count object in...
if (MaxObjectCount < (2 << 30))
ObjectID = MaxObjectCount++;
else
ObjectID = 0;
if (ObjectCount < (2 << 30))
ObjectCount++;
#endif
}
// ----
CObject::~CObject()
{
#ifndef ULTRA_FAST_OBJECT
// Release any memory used for string representation.
SAFELY_DELETE_ARRAY(StringRepresentation);
SAFELY_DELETE_ARRAY(CreationInfo);
#endif
#if (!defined(FAST_OBJECT) && !defined(ULTRA_FAST_OBJECT))
if (FirstObject)
{
if (FirstObject == this)
{
FirstObject = FirstObject->NextObject;
}
else
{
CObject* i = FirstObject;
while ((i->NextObject) && (i->NextObject != this))
{
i = i->NextObject;
}
if (i->NextObject)
i->NextObject = NextObject;
else
{
// Panic!!!
OutputDebugString(TEXT("PANIC: Object not found in object chain!\n"));
}
}
}
else
{
// Somethings gone wrong here!
OutputDebugString(TEXT("ERROR: No objects in chain, but object still exist!\n"));
}
#endif
#ifndef ULTRA_FAST_OBJECT
// Count the object out...
ObjectCount--;
#endif
}
// ----
// Return the object count.
int CObject::GetObjectCount()
{
#ifndef ULTRA_FAST_OBJECT
return ObjectCount;
#else
return 0;
#endif
}
// ----
TCHAR* CObject::GetCreationInfo()
{
#ifndef ULTRA_FAST_OBJECT
return CreationInfo;
#else
return NULL;
#endif
}
// ----
void CObject::SetCreation(char* _VarName, char* _Constructor, char* _File, int _Line)
{
#ifndef ULTRA_FAST_OBJECT
SAFELY_DELETE_ARRAY(CreationInfo);
#define OBJECT_STR "Object #"
#define CREATED_STR " created at line "
#define INFILE_STR " in file "
// Build string.
CreationInfo = new TCHAR[ strlen(OBJECT_STR)
+ 10
+ 1
+ strlen(_VarName)
+ 1
+ strlen(_Constructor)
+ 1
+ strlen(CREATED_STR)
+ 10
+ strlen(INFILE_STR)
+ strlen(_File)
+ 3 // . + \n + \0
];
if (CreationInfo)
{
#ifdef _UNICODE
swprintf( CreationInfo,
TEXT("%hs%d %hs %hs %hs%d%hs%hs.\n"),
OBJECT_STR,
ObjectID,
_VarName,
_Constructor,
CREATED_STR,
_Line,
INFILE_STR,
_File);
#else
sprintf(CreationInfo,
"%s%d %s %s %s%d%s%s.\n",
OBJECT_STR,
ObjectID,
_VarName,
_Constructor,
CREATED_STR,
_Line,
INFILE_STR,
_File);
#endif
}
#endif
}
// ----
void CObject::OutputObjectList()
{
#ifndef ULTRA_FAST_OBJECT
OutputDebugString(TEXT("\nStarting object list...\n"));
CObject* i = FirstObject;
while (i)
{
if (i->CreationInfo)
OutputDebugString(i->CreationInfo);
else
{
TCHAR buffer[256];
#ifdef _UNICODE
swprintf(buffer, TEXT("Object #%d is of unknown origin (check how the object was created).\n"), i->ObjectID);
#else
sprintf(buffer, TEXT("Object #%d is of unknown origin (check how the object was created).\n"), i->ObjectID);
#endif
OutputDebugString(buffer);
}
i = i->NextObject;
}
OutputDebugString(TEXT("End of list.\n"));
#else
OutputDebugString(TEXT("This build does not support object information.\nUndefine ULTRA_FAST_OBJECT, and optionally FAST_OBJECT.\n"));
#endif
}
// ----
// Returns an internally stored error code.
// The error code is particular to a given class.
DWORD CObject::GetErrorCode()
{
DWORD ret = ErrorCode;
ErrorCode = 0;
return ret;
}
// ----
// Returns a string representation for the object's data.
// WARNING: This is only intended for debug use as it is gonna be slow!!!
TCHAR* CObject::ToString()
{
#ifndef ULTRA_FAST_OBJECT
SAFELY_DELETE_ARRAY(StringRepresentation);
// Build string.
char* DataRep = new char[sizeof(*this) + 2];
if (DataRep)
{
memcpy((void*)DataRep, (void*)this, sizeof(*this));
DataRep[sizeof(*this)] = 0;
#ifdef _UNICODE
TCHAR* LDataRep = new TCHAR[sizeof(*this) + 2];
swprintf(LDataRep, L"%hs\n", DataRep);
delete[] DataRep;
StringRepresentation = LDataRep;
#else
StringRepresentation = DataRep;
#endif
}
return StringRepresentation;
#else
return NULL;
#endif
}