OS/2 updates for easier VA debugging
[wxWidgets.git] / src / common / object.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: object.cpp
3 // Purpose: wxObject implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "object.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/hash.h"
25 #if wxUSE_SERIAL
26 #include "wx/objstrm.h"
27 #include "wx/serbase.h"
28
29 // for error messages
30 #include "wx/log.h"
31 #include "wx/intl.h"
32 #endif // wxUSE_SERIAL
33 #endif // WX_PRECOMP
34
35 #include <string.h>
36 #include <assert.h>
37
38 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
39 #include "wx/memory.h"
40 #endif
41
42 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
43 // for wxObject::Dump
44 #include "wx/ioswrap.h"
45 #if defined(__VISAGECPP__)
46 // help with VA debugging
47 #define DEBUG_PRINTF(NAME) { static int raz=0; \
48 printf( #NAME " %i\n",raz); fflush(stdout); \
49 raz++; \
50 }
51 #else
52 #define DEBUG_PRINTF(NAME)
53 #endif
54 #endif
55
56 wxClassInfo wxObject::sm_classwxObject((wxChar *) wxT("wxObject"), (wxChar *) NULL, (wxChar *) NULL, (int ) sizeof(wxObject), (wxObjectConstructorFn) NULL);
57 wxClassInfo* wxClassInfo::sm_first = (wxClassInfo *) NULL;
58 wxHashTable* wxClassInfo::sm_classTable = (wxHashTable*) NULL;
59
60 #if defined(__WXDEBUG__) && defined(__VISAGECPP__)
61 int wxObject::N = 0; // total number of objects
62 int wxObject::Nid = 0;// object serial counter
63 #endif // __WXDEBUG__
64
65 // These are here so we can avoid 'always true/false' warnings
66 // by referring to these instead of TRUE/FALSE
67 const bool wxTrue = TRUE;
68 const bool wxFalse = FALSE;
69
70 /*
71 * wxWindows root object.
72 */
73
74 wxObject::wxObject()
75 {
76 m_refData = (wxObjectRefData *) NULL;
77 #if wxUSE_SERIAL
78 m_serialObj = (wxObject_Serialize *)NULL;
79 #endif
80 #if defined(__WXDEBUG__) && defined(__VISAGECPP__)
81 id = Nid++;
82 N++;
83 // { printf("wxObject %i/%i \t",id,N);
84 // }
85 #endif
86 }
87
88 wxObject::~wxObject()
89 {
90 UnRef();
91 #if wxUSE_SERIAL
92 if (m_serialObj)
93 delete m_serialObj;
94 #endif
95 #if defined(__WXDEBUG__) && defined(__VISAGECPP__)
96 N--;
97 // { printf("~wxObject %i/%i \t",id,N);
98 // }
99 #endif //__WXDEBUG__
100 }
101
102 /*
103 * Is this object a kind of (a subclass of) 'info'?
104 * E.g. is wxWindow a kind of wxObject?
105 * Go from this class to superclass, taking into account
106 * two possible base classes.
107 */
108
109 bool wxObject::IsKindOf(wxClassInfo *info) const
110 {
111 wxClassInfo *thisInfo = GetClassInfo();
112 if (thisInfo)
113 return thisInfo->IsKindOf(info);
114 else
115 return FALSE;
116 }
117
118 wxObject *wxObject::Clone() const
119 {
120 wxObject *object = GetClassInfo()->CreateObject();
121 CopyObject(*object);
122 return object;
123 }
124
125 #ifdef __WXDEBUG__
126 void wxObject::CopyObject(wxObject& object_dest) const
127 #else // !Debug
128 void wxObject::CopyObject(wxObject& WXUNUSED(object_dest)) const
129 #endif // Debug/!Debug
130 {
131 wxASSERT(object_dest.GetClassInfo()->IsKindOf(GetClassInfo()));
132 }
133
134 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
135 void wxObject::Dump(ostream& str)
136 {
137 if (GetClassInfo() && GetClassInfo()->GetClassName())
138 str << GetClassInfo()->GetClassName();
139 else
140 str << "unknown object class";
141 }
142 #endif
143
144 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
145
146 #ifdef new
147 #undef new
148 #endif
149
150 void *wxObject::operator new (size_t size, wxChar * fileName, int lineNum)
151 {
152 return wxDebugAlloc(size, fileName, lineNum, TRUE);
153 }
154
155 #if defined(__VISAGECPP__)
156 # if __DEBUG_ALLOC__
157 void wxObject::operator delete (void * buf,const char * _fname, size_t _line)
158 {
159 wxDebugFree(buf);
160 }
161 # endif //__DEBUG_ALLOC__
162 #else
163 void wxObject::operator delete (void * buf)
164 {
165 wxDebugFree(buf);
166 }
167 #endif // __VISAGECPP__
168
169 // VC++ 6.0
170 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
171 void wxObject::operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */)
172 {
173 ::operator delete(pData);
174 }
175 #endif
176
177 // Cause problems for VC++ - crashes
178 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
179 void * wxObject::operator new[] (size_t size, wxChar * fileName, int lineNum)
180 {
181 return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE);
182 }
183
184 void wxObject::operator delete[] (void * buf)
185 {
186 wxDebugFree(buf, TRUE);
187 }
188 #endif
189
190 #endif
191
192 /*
193 * Class info: provides run-time class type information.
194 */
195
196 wxClassInfo::wxClassInfo(wxChar *cName, wxChar *baseName1, wxChar *baseName2, int sz, wxObjectConstructorFn constr)
197 {
198 m_className = cName;
199 m_baseClassName1 = baseName1;
200 m_baseClassName2 = baseName2;
201
202 m_objectSize = sz;
203 m_objectConstructor = constr;
204
205 m_next = sm_first;
206 sm_first = this;
207
208 m_baseInfo1 = (wxClassInfo *) NULL;
209 m_baseInfo2 = (wxClassInfo *) NULL;
210 }
211
212 wxObject *wxClassInfo::CreateObject()
213 {
214 if (m_objectConstructor)
215 return (wxObject *)(*m_objectConstructor)();
216 else
217 return (wxObject *) NULL;
218 }
219
220 wxClassInfo *wxClassInfo::FindClass(wxChar *c)
221 {
222 wxClassInfo *p = sm_first;
223 while (p)
224 {
225 if (p && p->GetClassName() && wxStrcmp(p->GetClassName(), c) == 0)
226 return p;
227 p = p->m_next;
228 }
229 return (wxClassInfo *) NULL;
230 }
231
232 // Climb upwards through inheritance hierarchy.
233 // Dual inheritance is catered for.
234 bool wxClassInfo::IsKindOf(wxClassInfo *info) const
235 {
236 if (info == NULL)
237 return FALSE;
238
239 if (this == info)
240 return TRUE;
241
242 if (m_baseInfo1)
243 if (m_baseInfo1->IsKindOf(info))
244 return TRUE;
245
246 if (m_baseInfo2)
247 return m_baseInfo2->IsKindOf(info);
248
249 return FALSE;
250 }
251
252 // Set pointers to base class(es) to speed up IsKindOf
253 void wxClassInfo::InitializeClasses()
254 {
255 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
256 // link any object module twice mistakenly) will break this function
257 // because it will enter an infinite loop and eventually die with "out of
258 // memory" - as this is quite hard to detect if you're unaware of this,
259 // try to do some checks here
260 #ifdef __WXDEBUG__
261 // more classes than we'll ever have
262 static const size_t nMaxClasses = 10000;
263 size_t nClass = 0;
264 #endif // Debug
265
266 wxClassInfo::sm_classTable = new wxHashTable(wxKEY_STRING);
267
268 // Index all class infos by their class name
269 wxClassInfo *info = sm_first;
270 while (info)
271 {
272 if (info->m_className)
273 {
274 wxASSERT_MSG( ++nClass < nMaxClasses,
275 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
276
277 sm_classTable->Put(info->m_className, (wxObject *)info);
278 }
279
280 info = info->m_next;
281 }
282
283 // Set base pointers for each wxClassInfo
284 info = sm_first;
285 while (info)
286 {
287 if (info->GetBaseClassName1())
288 info->m_baseInfo1 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName1());
289 if (info->GetBaseClassName2())
290 info->m_baseInfo2 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName2());
291 info = info->m_next;
292 }
293 }
294
295 void wxClassInfo::CleanUpClasses()
296 {
297 delete wxClassInfo::sm_classTable;
298 wxClassInfo::sm_classTable = NULL;
299 }
300
301 wxObject *wxCreateDynamicObject(const wxChar *name)
302 {
303 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
304
305 if (wxClassInfo::sm_classTable)
306 {
307 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
308 if (!info)
309 return (wxObject *)NULL;
310
311 return info->CreateObject();
312 }
313 else
314 {
315 wxClassInfo *info = wxClassInfo::sm_first;
316 while (info)
317 {
318 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
319 return info->CreateObject();
320 info = info->m_next;
321 }
322 return (wxObject*) NULL;
323 }
324 }
325
326 #if wxUSE_SERIAL
327
328 #include "wx/serbase.h"
329 #include "wx/dynlib.h"
330
331 wxObject* wxCreateStoredObject( wxInputStream &stream )
332 {
333 wxObjectInputStream obj_s(stream);
334 return obj_s.LoadObject();
335 };
336
337 void wxObject::StoreObject( wxObjectOutputStream& stream )
338 {
339 DEBUG_PRINTF(wxObject::StoreObject)
340
341 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
342 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
343
344 if (!lib) {
345 wxLogError(_("Can't load wxSerial dynamic library."));
346 return;
347 }
348 if (!m_serialObj) {
349 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
350
351 if (!m_serialObj) {
352 wxLogError(_("Can't find the serialization object '%s' "
353 "for the object '%s'."),
354 obj_name.c_str(),
355 GetClassInfo()->GetClassName());
356 return;
357 }
358 m_serialObj->SetObject(this);
359 }
360
361 m_serialObj->StoreObject(stream);
362 }
363
364 void wxObject::LoadObject( wxObjectInputStream& stream )
365 {
366 DEBUG_PRINTF(wxObject::LoadObject)
367
368 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
369 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
370
371 if (!m_serialObj) {
372 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
373
374 if (!m_serialObj) {
375 wxLogError(_("Can't find the serialization object '%s' "
376 "for the object '%s'."),
377 obj_name.c_str(),
378 GetClassInfo()->GetClassName());
379 return;
380 }
381 m_serialObj->SetObject(this);
382 }
383
384 m_serialObj->LoadObject(stream);
385 }
386
387 #endif // wxUSE_SERIAL
388
389 /*
390 * wxObject: cloning of objects
391 */
392
393 void wxObject::Ref(const wxObject& clone)
394 {
395 DEBUG_PRINTF(wxObject::Ref)
396 // delete reference to old data
397 UnRef();
398 // reference new data
399 if (clone.m_refData) {
400 m_refData = clone.m_refData;
401 ++(m_refData->m_count);
402 }
403 }
404
405 void wxObject::UnRef()
406 {
407 if ( m_refData )
408 {
409 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
410
411 if ( !--m_refData->m_count )
412 delete m_refData;
413 m_refData = (wxObjectRefData *) NULL;
414 }
415 }
416
417 /*
418 * wxObjectData
419 */
420
421 wxObjectRefData::wxObjectRefData(void) : m_count(1)
422 {
423 }
424
425 wxObjectRefData::~wxObjectRefData()
426 {
427 }