OS/2 compile fixes.
[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 #ifdef __VISAGECPP__
36 #include "wx/objstrm.h"
37 #include "wx/serbase.h"
38 #endif
39
40 #include <string.h>
41 #include <assert.h>
42
43 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
44 #include "wx/memory.h"
45 #endif
46
47 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
48 // for wxObject::Dump
49 #include "wx/ioswrap.h"
50 #if defined(__VISAGECPP__)
51 // help with VA debugging
52 #define DEBUG_PRINTF(NAME) { static int raz=0; \
53 printf( #NAME " %i\n",raz); fflush(stdout); \
54 raz++; \
55 }
56 #else
57 #define DEBUG_PRINTF(NAME)
58 #endif
59 #endif
60
61 wxClassInfo wxObject::sm_classwxObject((wxChar *) wxT("wxObject"), (wxChar *) NULL, (wxChar *) NULL, (int ) sizeof(wxObject), (wxObjectConstructorFn) NULL);
62 wxClassInfo* wxClassInfo::sm_first = (wxClassInfo *) NULL;
63 wxHashTable* wxClassInfo::sm_classTable = (wxHashTable*) NULL;
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 }
81
82 wxObject::~wxObject()
83 {
84 UnRef();
85 #if wxUSE_SERIAL
86 if (m_serialObj)
87 delete m_serialObj;
88 #endif
89 }
90
91 /*
92 * Is this object a kind of (a subclass of) 'info'?
93 * E.g. is wxWindow a kind of wxObject?
94 * Go from this class to superclass, taking into account
95 * two possible base classes.
96 */
97
98 bool wxObject::IsKindOf(wxClassInfo *info) const
99 {
100 wxClassInfo *thisInfo = GetClassInfo();
101 if (thisInfo)
102 return thisInfo->IsKindOf(info);
103 else
104 return FALSE;
105 }
106
107 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
108 void wxObject::Dump(wxSTD ostream& str)
109 {
110 if (GetClassInfo() && GetClassInfo()->GetClassName())
111 str << GetClassInfo()->GetClassName();
112 else
113 str << "unknown object class";
114 }
115 #endif
116
117 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
118
119 #ifdef new
120 #undef new
121 #endif
122
123 void *wxObject::operator new (size_t size, wxChar * fileName, int lineNum)
124 {
125 return wxDebugAlloc(size, fileName, lineNum, TRUE);
126 }
127
128 #if defined(__VISAGECPP__)
129 # if __DEBUG_ALLOC__
130 void wxObject::operator delete (void * buf,const char * _fname, size_t _line)
131 {
132 wxDebugFree(buf);
133 }
134 # endif //__DEBUG_ALLOC__
135 #else
136 void wxObject::operator delete (void * buf)
137 {
138 wxDebugFree(buf);
139 }
140 #endif // __VISAGECPP__
141
142 // VC++ 6.0
143 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
144 void wxObject::operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */)
145 {
146 ::operator delete(pData);
147 }
148 #endif
149
150 // Cause problems for VC++ - crashes
151 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
152 void * wxObject::operator new[] (size_t size, wxChar * fileName, int lineNum)
153 {
154 return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE);
155 }
156
157 void wxObject::operator delete[] (void * buf)
158 {
159 wxDebugFree(buf, TRUE);
160 }
161 #endif
162
163 #endif
164
165 /*
166 * Class info: provides run-time class type information.
167 */
168
169 wxClassInfo::wxClassInfo(const wxChar *cName,
170 const wxChar *baseName1,
171 const wxChar *baseName2,
172 int sz,
173 wxObjectConstructorFn constr)
174 {
175 m_className = cName;
176 m_baseClassName1 = baseName1;
177 m_baseClassName2 = baseName2;
178
179 m_objectSize = sz;
180 m_objectConstructor = constr;
181
182 m_next = sm_first;
183 sm_first = this;
184
185 m_baseInfo1 = (wxClassInfo *) NULL;
186 m_baseInfo2 = (wxClassInfo *) NULL;
187 }
188
189 wxObject *wxClassInfo::CreateObject()
190 {
191 if (m_objectConstructor)
192 return (wxObject *)(*m_objectConstructor)();
193 else
194 return (wxObject *) NULL;
195 }
196
197 wxClassInfo *wxClassInfo::FindClass(const wxChar *c)
198 {
199 wxClassInfo *p = sm_first;
200 while (p)
201 {
202 if ( wxStrcmp(p->GetClassName(), c) == 0 )
203 break;
204
205 p = p->m_next;
206 }
207
208 return p;
209 }
210
211 // Climb upwards through inheritance hierarchy.
212 // Dual inheritance is catered for.
213 bool wxClassInfo::IsKindOf(const wxClassInfo *info) const
214 {
215 if (info == NULL)
216 return FALSE;
217
218 if (this == info)
219 return TRUE;
220
221 if (m_baseInfo1)
222 if (m_baseInfo1->IsKindOf(info))
223 return TRUE;
224
225 if (m_baseInfo2)
226 return m_baseInfo2->IsKindOf(info);
227
228 return FALSE;
229 }
230
231 // Set pointers to base class(es) to speed up IsKindOf
232 void wxClassInfo::InitializeClasses()
233 {
234 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
235 // link any object module twice mistakenly) will break this function
236 // because it will enter an infinite loop and eventually die with "out of
237 // memory" - as this is quite hard to detect if you're unaware of this,
238 // try to do some checks here
239 #ifdef __WXDEBUG__
240 // more classes than we'll ever have
241 static const size_t nMaxClasses = 10000;
242 size_t nClass = 0;
243 #endif // Debug
244
245 wxClassInfo::sm_classTable = new wxHashTable(wxKEY_STRING);
246
247 // Index all class infos by their class name
248 wxClassInfo *info = sm_first;
249 while (info)
250 {
251 if (info->m_className)
252 {
253 wxASSERT_MSG( ++nClass < nMaxClasses,
254 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
255
256 sm_classTable->Put(info->m_className, (wxObject *)info);
257 }
258
259 info = info->m_next;
260 }
261
262 // Set base pointers for each wxClassInfo
263 info = sm_first;
264 while (info)
265 {
266 if (info->GetBaseClassName1())
267 info->m_baseInfo1 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName1());
268 if (info->GetBaseClassName2())
269 info->m_baseInfo2 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName2());
270 info = info->m_next;
271 }
272 }
273
274 void wxClassInfo::CleanUpClasses()
275 {
276 delete wxClassInfo::sm_classTable;
277 wxClassInfo::sm_classTable = NULL;
278 }
279
280 wxObject *wxCreateDynamicObject(const wxChar *name)
281 {
282 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
283 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
284 #endif
285
286 if (wxClassInfo::sm_classTable)
287 {
288 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
289 if (!info)
290 return (wxObject *)NULL;
291
292 return info->CreateObject();
293 }
294 else
295 {
296 wxClassInfo *info = wxClassInfo::sm_first;
297 while (info)
298 {
299 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
300 return info->CreateObject();
301 info = info->m_next;
302 }
303 return (wxObject*) NULL;
304 }
305 }
306
307 #if wxUSE_SERIAL
308
309 #include "wx/serbase.h"
310 #include "wx/dynlib.h"
311
312 wxObject* wxCreateStoredObject( wxInputStream &stream )
313 {
314 wxObjectInputStream obj_s(stream);
315 return obj_s.LoadObject();
316 };
317
318 void wxObject::StoreObject( wxObjectOutputStream& stream )
319 {
320 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
321 DEBUG_PRINTF(wxObject::StoreObject)
322 #endif
323
324 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
325 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
326
327 if (!lib) {
328 wxLogError(_("Can't load wxSerial dynamic library."));
329 return;
330 }
331 if (!m_serialObj) {
332 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
333
334 if (!m_serialObj) {
335 wxLogError(_("Can't find the serialization object '%s' "
336 "for the object '%s'."),
337 obj_name.c_str(),
338 GetClassInfo()->GetClassName());
339 return;
340 }
341 m_serialObj->SetObject(this);
342 }
343
344 m_serialObj->StoreObject(stream);
345 }
346
347 void wxObject::LoadObject( wxObjectInputStream& stream )
348 {
349 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
350 DEBUG_PRINTF(wxObject::LoadObject)
351 #endif
352
353 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
354 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
355
356 if (!m_serialObj) {
357 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
358
359 if (!m_serialObj) {
360 wxLogError(_("Can't find the serialization object '%s' "
361 "for the object '%s'."),
362 obj_name.c_str(),
363 GetClassInfo()->GetClassName());
364 return;
365 }
366 m_serialObj->SetObject(this);
367 }
368
369 m_serialObj->LoadObject(stream);
370 }
371
372 #endif // wxUSE_SERIAL
373
374 /*
375 * wxObject: cloning of objects
376 */
377
378 void wxObject::Ref(const wxObject& clone)
379 {
380 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
381 DEBUG_PRINTF(wxObject::Ref)
382 #endif
383 // delete reference to old data
384 UnRef();
385 // reference new data
386 if (clone.m_refData) {
387 m_refData = clone.m_refData;
388 ++(m_refData->m_count);
389 }
390 }
391
392 void wxObject::UnRef()
393 {
394 if ( m_refData )
395 {
396 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
397
398 if ( !--m_refData->m_count )
399 delete m_refData;
400 m_refData = (wxObjectRefData *) NULL;
401 }
402 }
403
404 /*
405 * wxObjectData
406 */
407
408 wxObjectRefData::wxObjectRefData(void) : m_count(1)
409 {
410 }
411
412 wxObjectRefData::~wxObjectRefData()
413 {
414 }
415
416 #if defined(__DARWIN__) && defined(DYLIB_INIT)
417
418 extern "C" {
419 void __initialize_Cplusplus(void);
420 void wxWindowsDylibInit(void);
421 };
422
423 // Dynamic shared library (dylib) initialization routine
424 // required to initialize static C++ objects bacause of lazy dynamic linking
425 // http://developer.apple.com/techpubs/macosx/Essentials/
426 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
427 //
428 void wxWindowsDylibInit()
429 {
430 // The function __initialize_Cplusplus() must be called from the shared
431 // library initialization routine to cause the static C++ objects in
432 // the library to be initialized (reference number 2441683).
433
434 __initialize_Cplusplus();
435 }
436
437 #endif