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