]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
borland fix
[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$
55d99c7a 8// Copyright: (c) 1998 Julian Smart
0b9ab0bd 9// (c) 2001 Ron Lee <ron@debian.org>
55d99c7a 10// Licence: wxWindows licence
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
df5168c4
MB
25 #include "wx/hash.h"
26 #include "wx/object.h"
24eb81cb
DW
27#endif
28
c801d85f 29#include <string.h>
c801d85f 30
ea57084d 31#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
c801d85f
KB
32#include "wx/memory.h"
33#endif
34
ea57084d 35#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
3f4a0c5b 36 // for wxObject::Dump
b2edef6f 37 #include "wx/ioswrap.h"
c801d85f 38
b2edef6f
VZ
39 #if defined(__VISAGECPP__)
40 #define DEBUG_PRINTF(NAME) { static int raz=0; \
41 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
42 #else
43 #define DEBUG_PRINTF(NAME)
44 #endif
45#endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
c801d85f 46
e1720942
VZ
47// we must disable optimizations for VC.NET because otherwise its too eager
48// linker discards wxClassInfo objects in release build thus breaking many,
49// many things
fbae49ee 50#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
51 #pragma optimize("", off)
52#endif
f6bcfd97 53
30bfc425 54#if wxUSE_EXTENDED_RTTI
a095505c 55const wxClassInfo* wxObject::sm_classParentswxObject[] = { NULL } ;
066f1b7a 56 wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data )
a095505c 57{ return data.Get<wxObject*>() ; }
066f1b7a
SC
58 wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data )
59{ return &data.Get<wxObject>() ; }
a095505c
SC
60 wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data )
61 { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; }
62 wxClassInfo wxObject::sm_classwxObject(sm_classParentswxObject , wxT("") , wxT("wxObject"),
63 (int) sizeof(wxObject), \
64 (wxObjectConstructorFn) 0 ,
fbbdc52c 65 (wxPropertyInfo*) NULL,(wxHandlerInfo*) NULL,0 , 0 ,
066f1b7a 66 0 , wxVariantOfPtrToObjectConverterwxObject , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject);
a095505c
SC
67 template<> void wxStringReadValue(const wxString & , wxObject * & ){assert(0) ;}
68 template<> void wxStringWriteValue(wxString & , wxObject* const & ){assert(0) ;}
69 template<> const wxTypeInfo* wxGetTypeInfo( wxObject ** )
066f1b7a 70 { static wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::sm_classwxObject) ; return &s_typeInfo ; }
a095505c 71#else
0b9ab0bd
RL
72wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
73 (int) sizeof(wxObject),
74 (wxObjectConstructorFn) 0 );
a095505c 75#endif
c801d85f 76
e1720942 77// restore optimizations
fbae49ee 78#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
79 #pragma optimize("", on)
80#endif
81
b2edef6f
VZ
82wxClassInfo* wxClassInfo::sm_first = NULL;
83wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 84
b2edef6f
VZ
85// These are here so we can avoid 'always true/false' warnings
86// by referring to these instead of TRUE/FALSE
0b9ab0bd
RL
87const bool wxTrue = TRUE;
88const bool wxFalse = FALSE;
c801d85f 89
b2edef6f
VZ
90// Is this object a kind of (a subclass of) 'info'?
91// E.g. is wxWindow a kind of wxObject?
92// Go from this class to superclass, taking into account
93// two possible base classes.
341287bf 94bool wxObject::IsKindOf(wxClassInfo *info) const
c801d85f 95{
afb74891 96 wxClassInfo *thisInfo = GetClassInfo();
0b9ab0bd 97 return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ;
c801d85f
KB
98}
99
38830220 100#if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
dd107c50 101void wxObject::Dump(wxSTD ostream& str)
c801d85f 102{
afb74891
VZ
103 if (GetClassInfo() && GetClassInfo()->GetClassName())
104 str << GetClassInfo()->GetClassName();
105 else
b2edef6f 106 str << _T("unknown object class");
c801d85f
KB
107}
108#endif
109
c801d85f 110
cf760e4c
VZ
111#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
112 #undef new
113#endif
114
115
b2edef6f 116#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 117void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 118{
cf760e4c 119 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE);
c801d85f 120}
b2edef6f 121#endif
c801d85f 122
b2edef6f
VZ
123#ifdef _WX_WANT_DELETE_VOID
124void wxObject::operator delete ( void *buf )
415ca026
DW
125{
126 wxDebugFree(buf);
127}
b2edef6f
VZ
128#endif
129
130#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
131void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 132{
afb74891 133 wxDebugFree(buf);
c801d85f 134}
0b9ab0bd
RL
135#endif
136
b2edef6f 137#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 138void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 139{
b2edef6f 140 wxDebugFree(buf);
76626af2
JS
141}
142#endif
143
b2edef6f 144#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 145void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 146{
cf760e4c 147 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE);
c801d85f 148}
b2edef6f 149#endif
c801d85f 150
b2edef6f
VZ
151#ifdef _WX_WANT_ARRAY_DELETE_VOID
152void wxObject::operator delete[] ( void *buf )
c801d85f 153{
afb74891 154 wxDebugFree(buf, TRUE);
c801d85f
KB
155}
156#endif
157
b2edef6f 158#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 159void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f
VZ
160{
161 wxDebugFree(buf, TRUE);
162}
163#endif
afb74891 164
afb74891 165
0b9ab0bd
RL
166// ----------------------------------------------------------------------------
167// wxClassInfo
168// ----------------------------------------------------------------------------
c801d85f 169
aa4b7ef9
VZ
170wxClassInfo::~wxClassInfo()
171{
172 // remove this object from the linked list of all class infos: if we don't
173 // do it, loading/unloading a DLL containing static wxClassInfo objects is
174 // not going to work
175 if ( this == sm_first )
176 {
177 sm_first = m_next;
178 }
179 else
180 {
181 wxClassInfo *info = sm_first;
182 while (info)
183 {
184 if ( info->m_next == this )
185 {
186 info->m_next = m_next;
187 break;
188 }
189
190 info = info->m_next;
191 }
192 }
30bfc425 193#if wxUSE_EXTENDED_RTTI
a095505c
SC
194 Unregister( m_className ) ;
195#endif
aa4b7ef9
VZ
196}
197
0b9ab0bd 198wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
c801d85f 199{
80e8062f
VZ
200 if ( sm_classTable )
201 {
202 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
203 }
204 else
205 {
206 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
207 {
208 if ( wxStrcmp(info->GetClassName(), className) == 0 )
209 return info;
210 }
211
212 return NULL;
213 }
c801d85f
KB
214}
215
1f428942
VZ
216// a tiny InitializeClasses() helper
217/* static */
218inline wxClassInfo *wxClassInfo::GetBaseByName(const wxChar *name)
219{
220 if ( !name )
221 return NULL;
222
223 wxClassInfo *classInfo = (wxClassInfo *)sm_classTable->Get(name);
224
27db95f0
VZ
225#ifdef __WXDEBUG__
226 // this must be fixed, other things will work wrongly later if you get this
227 if ( !classInfo )
228 {
229 wxFAIL_MSG( wxString::Format
230 (
231 _T("base class '%s' is unknown to wxWindows RTTI"),
232 name
233 ) );
234 }
235#endif // __WXDEBUG__
1f428942
VZ
236
237 return classInfo;
238}
c801d85f 239
1f428942 240// Set pointers to base class(es) to speed up IsKindOf
afb74891 241void wxClassInfo::InitializeClasses()
c801d85f 242{
309689b2
VZ
243 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
244 // link any object module twice mistakenly) will break this function
245 // because it will enter an infinite loop and eventually die with "out of
246 // memory" - as this is quite hard to detect if you're unaware of this,
247 // try to do some checks here
0b9ab0bd 248
309689b2 249#ifdef __WXDEBUG__
0b9ab0bd 250 static const size_t nMaxClasses = 10000; // more than we'll ever have
309689b2 251 size_t nClass = 0;
0b9ab0bd 252#endif
309689b2 253
edf0993c 254 sm_classTable = new wxHashTable(wxKEY_STRING);
afb74891 255
0b9ab0bd
RL
256 // Index all class infos by their class name
257
258 wxClassInfo *info;
259 for(info = sm_first; info; info = info->m_next)
afb74891
VZ
260 {
261 if (info->m_className)
309689b2
VZ
262 {
263 wxASSERT_MSG( ++nClass < nMaxClasses,
f6bcfd97 264 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
afb74891 265 sm_classTable->Put(info->m_className, (wxObject *)info);
309689b2 266 }
afb74891
VZ
267 }
268
30bfc425 269#if wxUSE_EXTENDED_RTTI == 0
0b9ab0bd
RL
270 // Set base pointers for each wxClassInfo
271
272 for(info = sm_first; info; info = info->m_next)
afb74891 273 {
1f428942
VZ
274 info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1());
275 info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2());
afb74891 276 }
a095505c 277#endif
c801d85f
KB
278}
279
afb74891 280void wxClassInfo::CleanUpClasses()
c801d85f 281{
0c32066b 282 delete wxClassInfo::sm_classTable;
b2edef6f 283 wxClassInfo::sm_classTable = NULL;
0c32066b 284}
f4a8c29f 285
cf2f341a 286wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 287{
bbee61e5 288#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 289 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 290#endif
61cca9d2 291
b2edef6f 292 if ( wxClassInfo::sm_classTable )
0c32066b
JS
293 {
294 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 295 return info ? info->CreateObject() : NULL;
0c32066b 296 }
b2edef6f 297 else // no sm_classTable yet
0c32066b 298 {
b2edef6f
VZ
299 for ( wxClassInfo *info = wxClassInfo::sm_first;
300 info;
301 info = info->m_next )
302 {
cf2f341a 303 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 304 return info->CreateObject();
b2edef6f
VZ
305 }
306
307 return NULL;
0c32066b 308 }
c801d85f
KB
309}
310
7a4b9130 311
0b9ab0bd 312// ----------------------------------------------------------------------------
a6391d30 313// wxObject
0b9ab0bd 314// ----------------------------------------------------------------------------
c801d85f 315
a6391d30
GD
316// Initialize ref data from another object (needed for copy constructor and
317// assignment operator)
318void wxObject::InitFrom(const wxObject& other)
319{
320 m_refData = other.m_refData;
321 if ( m_refData )
322 m_refData->m_count++;
323}
324
c801d85f
KB
325void wxObject::Ref(const wxObject& clone)
326{
bbee61e5 327#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 328 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 329#endif
0b9ab0bd 330
c89f5c02
RR
331 // nothing to be done
332 if (m_refData == clone.m_refData)
333 return;
334
0b9ab0bd 335 // delete reference to old data
c801d85f 336 UnRef();
0b9ab0bd 337
c801d85f 338 // reference new data
807d8487 339 if ( clone.m_refData )
0b9ab0bd 340 {
c801d85f
KB
341 m_refData = clone.m_refData;
342 ++(m_refData->m_count);
343 }
344}
345
afb74891 346void wxObject::UnRef()
c801d85f 347{
807d8487 348 if ( m_refData )
4fe5383d
VZ
349 {
350 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
351
352 if ( !--m_refData->m_count )
c801d85f 353 delete m_refData;
807d8487 354 m_refData = NULL;
c801d85f 355 }
c801d85f
KB
356}
357
807d8487
VZ
358void wxObject::AllocExclusive()
359{
360 if ( !m_refData )
361 {
362 m_refData = CreateRefData();
363 }
364 else if ( m_refData->GetRefCount() > 1 )
365 {
366 // note that ref is not going to be destroyed in this case
a9bf8315 367 const wxObjectRefData* ref = m_refData;
807d8487
VZ
368 UnRef();
369
370 // ... so we can still access it
371 m_refData = CloneRefData(ref);
372 }
373 //else: ref count is 1, we are exclusive owners of m_refData anyhow
374
375 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
376 _T("wxObject::AllocExclusive() failed.") );
377}
378
379wxObjectRefData *wxObject::CreateRefData() const
380{
d1870078
VZ
381 // if you use AllocExclusive() you must override this method
382 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
383
807d8487
VZ
384 return NULL;
385}
386
a9bf8315
VZ
387wxObjectRefData *
388wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 389{
d1870078
VZ
390 // if you use AllocExclusive() you must override this method
391 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
392
807d8487
VZ
393 return NULL;
394}
395
396// ----------------------------------------------------------------------------
397// misc
398// ----------------------------------------------------------------------------
ce28b4d7 399
edf0993c 400#if defined(__DARWIN__) && defined(WXMAKINGDLL)
ce28b4d7
GD
401
402extern "C" {
403 void __initialize_Cplusplus(void);
404 void wxWindowsDylibInit(void);
405};
406
edf0993c
GD
407// Dynamic shared library (dylib) initialization routine
408// required to initialize static C++ objects bacause of lazy dynamic linking
409// http://developer.apple.com/techpubs/macosx/Essentials/
410// SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
0b9ab0bd 411
ce28b4d7
GD
412void wxWindowsDylibInit()
413{
414 // The function __initialize_Cplusplus() must be called from the shared
415 // library initialization routine to cause the static C++ objects in
416 // the library to be initialized (reference number 2441683).
417
edf0993c
GD
418 // This only seems to be necessary if the library initialization routine
419 // needs to use the static C++ objects
ce28b4d7
GD
420 __initialize_Cplusplus();
421}
422
423#endif
0b9ab0bd
RL
424
425// vi:sts=4:sw=4:et