]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
added rtti info (python problem)
[wxWidgets.git] / src / common / object.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
b2edef6f 2// Name: src/common/object.cpp
c801d85f
KB
3// Purpose: wxObject implementation
4// Author: Julian Smart
0b9ab0bd 5// Modified by: Ron Lee
c801d85f
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
0b9ab0bd
RL
8// Copyright: (c) 1998 Julian Smart and Markus Holzem
9// (c) 2001 Ron Lee <ron@debian.org>
3f4a0c5b 10// Licence: wxWindows license
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
13#ifdef __GNUG__
14#pragma implementation "object.h"
15#endif
16
b2edef6f 17// For compilers that support precompilation, includes "wx.h".
c801d85f
KB
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
1678ad78 24#ifndef WX_PRECOMP
0b9ab0bd 25#include "wx/hash.h"
24eb81cb
DW
26#endif
27
c801d85f 28#include <string.h>
c801d85f 29
ea57084d 30#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
c801d85f
KB
31#include "wx/memory.h"
32#endif
33
ea57084d 34#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
3f4a0c5b 35 // for wxObject::Dump
b2edef6f 36 #include "wx/ioswrap.h"
c801d85f 37
b2edef6f
VZ
38 #if defined(__VISAGECPP__)
39 #define DEBUG_PRINTF(NAME) { static int raz=0; \
40 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
41 #else
42 #define DEBUG_PRINTF(NAME)
43 #endif
44#endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
c801d85f 45
f6bcfd97 46
0b9ab0bd
RL
47wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
48 (int) sizeof(wxObject),
49 (wxObjectConstructorFn) 0 );
c801d85f 50
b2edef6f
VZ
51wxClassInfo* wxClassInfo::sm_first = NULL;
52wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 53
b2edef6f
VZ
54// These are here so we can avoid 'always true/false' warnings
55// by referring to these instead of TRUE/FALSE
0b9ab0bd
RL
56const bool wxTrue = TRUE;
57const bool wxFalse = FALSE;
c801d85f 58
b2edef6f
VZ
59// Is this object a kind of (a subclass of) 'info'?
60// E.g. is wxWindow a kind of wxObject?
61// Go from this class to superclass, taking into account
62// two possible base classes.
341287bf 63bool wxObject::IsKindOf(wxClassInfo *info) const
c801d85f 64{
afb74891 65 wxClassInfo *thisInfo = GetClassInfo();
0b9ab0bd 66 return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ;
c801d85f
KB
67}
68
38830220 69#if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
dd107c50 70void wxObject::Dump(wxSTD ostream& str)
c801d85f 71{
afb74891
VZ
72 if (GetClassInfo() && GetClassInfo()->GetClassName())
73 str << GetClassInfo()->GetClassName();
74 else
b2edef6f 75 str << _T("unknown object class");
c801d85f
KB
76}
77#endif
78
c801d85f 79
cf760e4c
VZ
80#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
81 #undef new
82#endif
83
84
c801d85f 85
b2edef6f 86#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 87void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 88{
cf760e4c 89 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE);
c801d85f 90}
b2edef6f 91#endif
c801d85f 92
b2edef6f
VZ
93#ifdef _WX_WANT_DELETE_VOID
94void wxObject::operator delete ( void *buf )
415ca026
DW
95{
96 wxDebugFree(buf);
97}
b2edef6f
VZ
98#endif
99
100#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
101void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 102{
afb74891 103 wxDebugFree(buf);
c801d85f 104}
0b9ab0bd
RL
105#endif
106
b2edef6f 107#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 108void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 109{
b2edef6f 110 wxDebugFree(buf);
76626af2
JS
111}
112#endif
113
b2edef6f 114#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 115void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 116{
cf760e4c 117 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE);
c801d85f 118}
b2edef6f 119#endif
c801d85f 120
b2edef6f
VZ
121#ifdef _WX_WANT_ARRAY_DELETE_VOID
122void wxObject::operator delete[] ( void *buf )
c801d85f 123{
afb74891 124 wxDebugFree(buf, TRUE);
c801d85f
KB
125}
126#endif
127
b2edef6f 128#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 129void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f
VZ
130{
131 wxDebugFree(buf, TRUE);
132}
133#endif
afb74891 134
afb74891 135
0b9ab0bd
RL
136// ----------------------------------------------------------------------------
137// wxClassInfo
138// ----------------------------------------------------------------------------
c801d85f 139
0b9ab0bd 140wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
c801d85f 141{
80e8062f
VZ
142 if ( sm_classTable )
143 {
144 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
145 }
146 else
147 {
148 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
149 {
150 if ( wxStrcmp(info->GetClassName(), className) == 0 )
151 return info;
152 }
153
154 return NULL;
155 }
c801d85f
KB
156}
157
1f428942
VZ
158// a tiny InitializeClasses() helper
159/* static */
160inline wxClassInfo *wxClassInfo::GetBaseByName(const wxChar *name)
161{
162 if ( !name )
163 return NULL;
164
165 wxClassInfo *classInfo = (wxClassInfo *)sm_classTable->Get(name);
166
167 // this must be fixed, other things risk work wrongly later if you get this
21b52985
VZ
168 wxASSERT_MSG( classInfo,
169 wxString::Format
170 (
171 _T("base class '%s' is unknown to wxWindows RTTI"),
172 name
173 ) );
1f428942
VZ
174
175 return classInfo;
176}
c801d85f 177
1f428942 178// Set pointers to base class(es) to speed up IsKindOf
afb74891 179void wxClassInfo::InitializeClasses()
c801d85f 180{
309689b2
VZ
181 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
182 // link any object module twice mistakenly) will break this function
183 // because it will enter an infinite loop and eventually die with "out of
184 // memory" - as this is quite hard to detect if you're unaware of this,
185 // try to do some checks here
0b9ab0bd 186
309689b2 187#ifdef __WXDEBUG__
0b9ab0bd 188 static const size_t nMaxClasses = 10000; // more than we'll ever have
309689b2 189 size_t nClass = 0;
0b9ab0bd 190#endif
309689b2 191
edf0993c 192 sm_classTable = new wxHashTable(wxKEY_STRING);
afb74891 193
0b9ab0bd
RL
194 // Index all class infos by their class name
195
196 wxClassInfo *info;
197 for(info = sm_first; info; info = info->m_next)
afb74891
VZ
198 {
199 if (info->m_className)
309689b2
VZ
200 {
201 wxASSERT_MSG( ++nClass < nMaxClasses,
f6bcfd97 202 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
afb74891 203 sm_classTable->Put(info->m_className, (wxObject *)info);
309689b2 204 }
afb74891
VZ
205 }
206
0b9ab0bd
RL
207 // Set base pointers for each wxClassInfo
208
209 for(info = sm_first; info; info = info->m_next)
afb74891 210 {
1f428942
VZ
211 info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1());
212 info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2());
afb74891 213 }
c801d85f
KB
214}
215
afb74891 216void wxClassInfo::CleanUpClasses()
c801d85f 217{
0c32066b 218 delete wxClassInfo::sm_classTable;
b2edef6f 219 wxClassInfo::sm_classTable = NULL;
0c32066b 220}
f4a8c29f 221
0b9ab0bd 222
cf2f341a 223wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 224{
bbee61e5 225#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 226 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 227#endif
61cca9d2 228
b2edef6f 229 if ( wxClassInfo::sm_classTable )
0c32066b
JS
230 {
231 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 232 return info ? info->CreateObject() : NULL;
0c32066b 233 }
b2edef6f 234 else // no sm_classTable yet
0c32066b 235 {
b2edef6f
VZ
236 for ( wxClassInfo *info = wxClassInfo::sm_first;
237 info;
238 info = info->m_next )
239 {
cf2f341a 240 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 241 return info->CreateObject();
b2edef6f
VZ
242 }
243
244 return NULL;
0c32066b 245 }
c801d85f
KB
246}
247
7a4b9130 248
0b9ab0bd
RL
249// ----------------------------------------------------------------------------
250// wxClassInfo
251// ----------------------------------------------------------------------------
c801d85f
KB
252
253void wxObject::Ref(const wxObject& clone)
254{
bbee61e5 255#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 256 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 257#endif
0b9ab0bd 258
c89f5c02
RR
259 // nothing to be done
260 if (m_refData == clone.m_refData)
261 return;
262
0b9ab0bd 263 // delete reference to old data
c801d85f 264 UnRef();
0b9ab0bd 265
c801d85f 266 // reference new data
807d8487 267 if ( clone.m_refData )
0b9ab0bd 268 {
c801d85f
KB
269 m_refData = clone.m_refData;
270 ++(m_refData->m_count);
271 }
272}
273
afb74891 274void wxObject::UnRef()
c801d85f 275{
807d8487 276 if ( m_refData )
4fe5383d
VZ
277 {
278 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
279
280 if ( !--m_refData->m_count )
c801d85f 281 delete m_refData;
807d8487 282 m_refData = NULL;
c801d85f 283 }
c801d85f
KB
284}
285
807d8487
VZ
286void wxObject::AllocExclusive()
287{
288 if ( !m_refData )
289 {
290 m_refData = CreateRefData();
291 }
292 else if ( m_refData->GetRefCount() > 1 )
293 {
294 // note that ref is not going to be destroyed in this case
a9bf8315 295 const wxObjectRefData* ref = m_refData;
807d8487
VZ
296 UnRef();
297
298 // ... so we can still access it
299 m_refData = CloneRefData(ref);
300 }
301 //else: ref count is 1, we are exclusive owners of m_refData anyhow
302
303 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
304 _T("wxObject::AllocExclusive() failed.") );
305}
306
307wxObjectRefData *wxObject::CreateRefData() const
308{
d1870078
VZ
309 // if you use AllocExclusive() you must override this method
310 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
311
807d8487
VZ
312 return NULL;
313}
314
a9bf8315
VZ
315wxObjectRefData *
316wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 317{
d1870078
VZ
318 // if you use AllocExclusive() you must override this method
319 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
320
807d8487
VZ
321 return NULL;
322}
323
324// ----------------------------------------------------------------------------
325// misc
326// ----------------------------------------------------------------------------
ce28b4d7 327
edf0993c 328#if defined(__DARWIN__) && defined(WXMAKINGDLL)
ce28b4d7
GD
329
330extern "C" {
331 void __initialize_Cplusplus(void);
332 void wxWindowsDylibInit(void);
333};
334
edf0993c
GD
335// Dynamic shared library (dylib) initialization routine
336// required to initialize static C++ objects bacause of lazy dynamic linking
337// http://developer.apple.com/techpubs/macosx/Essentials/
338// SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
0b9ab0bd 339
ce28b4d7
GD
340void wxWindowsDylibInit()
341{
342 // The function __initialize_Cplusplus() must be called from the shared
343 // library initialization routine to cause the static C++ objects in
344 // the library to be initialized (reference number 2441683).
345
edf0993c
GD
346 // This only seems to be necessary if the library initialization routine
347 // needs to use the static C++ objects
ce28b4d7
GD
348 __initialize_Cplusplus();
349}
350
351#endif
0b9ab0bd
RL
352
353// vi:sts=4:sw=4:et