]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
fixing osx_carbon
[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>
65571936 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
b2edef6f 13// For compilers that support precompilation, includes "wx.h".
c801d85f
KB
14#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
8e3f3880 17 #pragma hdrstop
c801d85f
KB
18#endif
19
1678ad78 20#ifndef WX_PRECOMP
df5168c4 21 #include "wx/object.h"
8e3f3880 22 #include "wx/hash.h"
5b56bffb 23 #include "wx/memory.h"
0bf751e7 24 #include "wx/crt.h"
24eb81cb
DW
25#endif
26
c801d85f 27#include <string.h>
c801d85f 28
4b6a582b 29#if wxUSE_DEBUG_CONTEXT
b2edef6f
VZ
30 #if defined(__VISAGECPP__)
31 #define DEBUG_PRINTF(NAME) { static int raz=0; \
32 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
33 #else
34 #define DEBUG_PRINTF(NAME)
35 #endif
4b6a582b 36#endif // wxUSE_DEBUG_CONTEXT
c801d85f 37
e1720942
VZ
38// we must disable optimizations for VC.NET because otherwise its too eager
39// linker discards wxClassInfo objects in release build thus breaking many,
40// many things
fbae49ee 41#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
42 #pragma optimize("", off)
43#endif
f6bcfd97 44
30bfc425 45#if wxUSE_EXTENDED_RTTI
05fa251a 46const wxClassInfo* wxObject::ms_classParents[] = { NULL } ;
e765d7ee
SC
47wxObject* wxVariantOfPtrToObjectConverterwxObject ( const wxAny &data )
48{ return wxANY_AS(data, wxObject*); }
49 wxAny wxObjectToVariantConverterwxObject ( wxObject *data )
50 { return wxAny( dynamic_cast<wxObject*> (data) ) ; }
51
525d8583 52 wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , wxT("wxObject"),
a095505c 53 (int) sizeof(wxObject), \
431346ff 54 (wxObjectConstructorFn) 0 ,
d3b9f782 55 NULL,NULL,0 , 0 ,
e765d7ee
SC
56 0 , wxVariantOfPtrToObjectConverterwxObject , 0 , wxObjectToVariantConverterwxObject);
57
2e587bb9 58 template<> void wxStringWriteValue(wxString & , wxObject* const & ){ wxFAIL_MSG("unreachable"); }
2e587bb9 59 template<> void wxStringWriteValue(wxString & , wxObject const & ){ wxFAIL_MSG("unreachable"); }
e765d7ee 60
7e548f6b
WS
61 wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject*).name() ) ;
62 wxClassTypeInfo s_typeInfowxObject(wxT_OBJECT , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject).name() ) ;
a095505c 63#else
c0db9626 64wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
0b9ab0bd
RL
65 (int) sizeof(wxObject),
66 (wxObjectConstructorFn) 0 );
a095505c 67#endif
c801d85f 68
e1720942 69// restore optimizations
fbae49ee 70#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
71 #pragma optimize("", on)
72#endif
73
b2edef6f
VZ
74wxClassInfo* wxClassInfo::sm_first = NULL;
75wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 76
66d2fa18
VZ
77// when using XTI, this method is already implemented inline inside
78// DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function
79// non-inline because this allows us to have a non-inline virtual function in
80// all wx classes and this solves linking problems for HP-UX native toolchain
81// and possibly others (we could make dtor non-inline as well but it's more
82// useful to keep it inline than this function)
83#if !wxUSE_EXTENDED_RTTI
84
14ca93a0
VZ
85wxClassInfo *wxObject::GetClassInfo() const
86{
87 return &wxObject::ms_classInfo;
88}
89
66d2fa18
VZ
90#endif // wxUSE_EXTENDED_RTTI
91
258e7b25 92// this variable exists only so that we can avoid 'always true/false' warnings
7e548f6b 93const bool wxFalse = false;
c801d85f 94
b2edef6f
VZ
95// Is this object a kind of (a subclass of) 'info'?
96// E.g. is wxWindow a kind of wxObject?
97// Go from this class to superclass, taking into account
98// two possible base classes.
303c6f20 99bool wxObject::IsKindOf(const wxClassInfo *info) const
c801d85f 100{
303c6f20 101 const wxClassInfo *thisInfo = GetClassInfo();
7e548f6b 102 return (thisInfo) ? thisInfo->IsKindOf(info) : false ;
c801d85f
KB
103}
104
4b6a582b 105#if wxUSE_MEMORY_TRACING && defined( new )
7e548f6b 106 #undef new
cf760e4c
VZ
107#endif
108
109
b2edef6f 110#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 111void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 112{
7e548f6b 113 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true);
c801d85f 114}
b2edef6f 115#endif
c801d85f 116
b2edef6f
VZ
117#ifdef _WX_WANT_DELETE_VOID
118void wxObject::operator delete ( void *buf )
415ca026
DW
119{
120 wxDebugFree(buf);
121}
b2edef6f
VZ
122#endif
123
124#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
125void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 126{
afb74891 127 wxDebugFree(buf);
c801d85f 128}
0b9ab0bd
RL
129#endif
130
b2edef6f 131#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 132void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 133{
b2edef6f 134 wxDebugFree(buf);
76626af2
JS
135}
136#endif
137
b2edef6f 138#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 139void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 140{
7e548f6b 141 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true);
c801d85f 142}
b2edef6f 143#endif
c801d85f 144
b2edef6f
VZ
145#ifdef _WX_WANT_ARRAY_DELETE_VOID
146void wxObject::operator delete[] ( void *buf )
c801d85f 147{
7e548f6b 148 wxDebugFree(buf, true);
c801d85f
KB
149}
150#endif
151
b2edef6f 152#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 153void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f 154{
7e548f6b 155 wxDebugFree(buf, true);
b2edef6f
VZ
156}
157#endif
afb74891 158
afb74891 159
0b9ab0bd
RL
160// ----------------------------------------------------------------------------
161// wxClassInfo
162// ----------------------------------------------------------------------------
c801d85f 163
aa4b7ef9
VZ
164wxClassInfo::~wxClassInfo()
165{
166 // remove this object from the linked list of all class infos: if we don't
167 // do it, loading/unloading a DLL containing static wxClassInfo objects is
168 // not going to work
169 if ( this == sm_first )
170 {
171 sm_first = m_next;
172 }
173 else
174 {
175 wxClassInfo *info = sm_first;
176 while (info)
177 {
178 if ( info->m_next == this )
179 {
180 info->m_next = m_next;
181 break;
182 }
183
184 info = info->m_next;
185 }
186 }
7e548f6b 187 Unregister();
aa4b7ef9
VZ
188}
189
c1e10a22 190wxClassInfo *wxClassInfo::FindClass(const wxString& className)
c801d85f 191{
80e8062f
VZ
192 if ( sm_classTable )
193 {
194 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
195 }
196 else
197 {
198 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
199 {
c1e10a22 200 if ( className == info->GetClassName() )
80e8062f
VZ
201 return info;
202 }
203
204 return NULL;
205 }
c801d85f
KB
206}
207
52798331
MW
208// Reentrance can occur on some platforms (Solaris for one), as the use of hash
209// and string objects can cause other modules to load and register classes
210// before the original call returns. This is handled by keeping the hash table
211// local when it is first created and only assigning it to the global variable
212// when the function is ready to return.
213//
214// That does make the assumption that after the function has completed the
215// first time the problem will no longer happen; all the modules it depends on
216// will have been loaded. The assumption is checked using the 'entry' variable
217// as a reentrance guard, it checks that once the hash table is global it is
218// not accessed multiple times simulateously.
f5256c59 219
d1d738f1
VS
220void wxClassInfo::Register()
221{
4b6a582b 222#if wxDEBUG_LEVEL
f5256c59
MW
223 // reentrance guard - see note above
224 static int entry = 0;
4b6a582b 225#endif // wxDEBUG_LEVEL
f5256c59 226
52798331
MW
227 wxHashTable *classTable;
228
229 if ( !sm_classTable )
230 {
231 // keep the hash local initially, reentrance is possible
232 classTable = new wxHashTable(wxKEY_STRING);
233 }
234 else
235 {
236 // guard againt reentrance once the global has been created
9a83f860 237 wxASSERT_MSG(++entry == 1, wxT("wxClassInfo::Register() reentrance"));
52798331
MW
238 classTable = sm_classTable;
239 }
240
d9f0e932
VS
241 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
242 // link any object module twice mistakenly, or link twice against wx shared
243 // library) will break this function because it will enter an infinite loop
244 // and eventually die with "out of memory" - as this is quite hard to
245 // detect if you're unaware of this, try to do some checks here.
52798331 246 wxASSERT_MSG( classTable->Get(m_className) == NULL,
47547fbe
VZ
247 wxString::Format
248 (
9a83f860 249 wxT("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
47547fbe
VZ
250 m_className
251 )
252 );
7e548f6b 253
52798331
MW
254 classTable->Put(m_className, (wxObject *)this);
255
256 // if we're using a local hash we need to try to make it global
257 if ( sm_classTable != classTable )
258 {
259 if ( !sm_classTable )
260 {
261 // make the hash global
262 sm_classTable = classTable;
263 }
264 else
265 {
266 // the gobal hash has already been created by a reentrant call,
267 // so delete the local hash and try again
268 delete classTable;
269 Register();
270 }
271 }
f5256c59 272
4b6a582b 273#if wxDEBUG_LEVEL
52798331 274 entry = 0;
4b6a582b 275#endif // wxDEBUG_LEVEL
c801d85f
KB
276}
277
d1d738f1 278void wxClassInfo::Unregister()
c801d85f 279{
d1d738f1
VS
280 if ( sm_classTable )
281 {
282 sm_classTable->Delete(m_className);
cafc76a4
VS
283 if ( sm_classTable->GetCount() == 0 )
284 {
5276b0a5 285 wxDELETE(sm_classTable);
cafc76a4 286 }
d1d738f1 287 }
0c32066b 288}
f4a8c29f 289
c1e10a22 290wxObject *wxCreateDynamicObject(const wxString& name)
0c32066b 291{
4b6a582b 292#if wxUSE_DEBUG_CONTEXT
b2edef6f 293 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 294#endif
61cca9d2 295
b2edef6f 296 if ( wxClassInfo::sm_classTable )
0c32066b
JS
297 {
298 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 299 return info ? info->CreateObject() : NULL;
0c32066b 300 }
b2edef6f 301 else // no sm_classTable yet
0c32066b 302 {
b2edef6f
VZ
303 for ( wxClassInfo *info = wxClassInfo::sm_first;
304 info;
305 info = info->m_next )
306 {
cf2f341a 307 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 308 return info->CreateObject();
b2edef6f
VZ
309 }
310
311 return NULL;
0c32066b 312 }
c801d85f
KB
313}
314
644cb537
MB
315// iterator interface
316wxClassInfo::const_iterator::value_type
317wxClassInfo::const_iterator::operator*() const
318{
319 return (wxClassInfo*)m_node->GetData();
320}
321
322wxClassInfo::const_iterator& wxClassInfo::const_iterator::operator++()
323{
324 m_node = m_table->Next();
325 return *this;
326}
327
328const wxClassInfo::const_iterator wxClassInfo::const_iterator::operator++(int)
329{
330 wxClassInfo::const_iterator tmp = *this;
331 m_node = m_table->Next();
332 return tmp;
333}
334
335wxClassInfo::const_iterator wxClassInfo::begin_classinfo()
336{
337 sm_classTable->BeginFind();
338
339 return const_iterator(sm_classTable->Next(), sm_classTable);
340}
341
342wxClassInfo::const_iterator wxClassInfo::end_classinfo()
343{
344 return const_iterator(NULL, NULL);
345}
7a4b9130 346
4a11340a
RR
347// ----------------------------------------------------------------------------
348// wxObjectRefData
349// ----------------------------------------------------------------------------
350
deff6d72 351void wxRefCounter::DecRef()
4a11340a 352{
deff6d72
VZ
353 wxASSERT_MSG( m_count > 0, "invalid ref data count" );
354
4a11340a
RR
355 if ( --m_count == 0 )
356 delete this;
357}
358
359
0b9ab0bd 360// ----------------------------------------------------------------------------
a6391d30 361// wxObject
0b9ab0bd 362// ----------------------------------------------------------------------------
c801d85f
KB
363
364void wxObject::Ref(const wxObject& clone)
365{
4b6a582b 366#if wxUSE_DEBUG_CONTEXT
807d8487 367 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 368#endif
0b9ab0bd 369
c89f5c02
RR
370 // nothing to be done
371 if (m_refData == clone.m_refData)
372 return;
373
0b9ab0bd 374 // delete reference to old data
c801d85f 375 UnRef();
0b9ab0bd 376
c801d85f 377 // reference new data
807d8487 378 if ( clone.m_refData )
0b9ab0bd 379 {
c801d85f 380 m_refData = clone.m_refData;
4a11340a 381 m_refData->IncRef();
c801d85f
KB
382 }
383}
384
afb74891 385void wxObject::UnRef()
c801d85f 386{
807d8487 387 if ( m_refData )
4fe5383d 388 {
4a11340a 389 m_refData->DecRef();
807d8487 390 m_refData = NULL;
c801d85f 391 }
c801d85f
KB
392}
393
807d8487
VZ
394void wxObject::AllocExclusive()
395{
396 if ( !m_refData )
397 {
398 m_refData = CreateRefData();
399 }
400 else if ( m_refData->GetRefCount() > 1 )
401 {
402 // note that ref is not going to be destroyed in this case
a9bf8315 403 const wxObjectRefData* ref = m_refData;
807d8487
VZ
404 UnRef();
405
406 // ... so we can still access it
407 m_refData = CloneRefData(ref);
408 }
409 //else: ref count is 1, we are exclusive owners of m_refData anyhow
410
411 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
9a83f860 412 wxT("wxObject::AllocExclusive() failed.") );
807d8487
VZ
413}
414
415wxObjectRefData *wxObject::CreateRefData() const
416{
d1870078 417 // if you use AllocExclusive() you must override this method
9a83f860 418 wxFAIL_MSG( wxT("CreateRefData() must be overridden if called!") );
d1870078 419
807d8487
VZ
420 return NULL;
421}
422
a9bf8315
VZ
423wxObjectRefData *
424wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 425{
d1870078 426 // if you use AllocExclusive() you must override this method
9a83f860 427 wxFAIL_MSG( wxT("CloneRefData() must be overridden if called!") );
d1870078 428
807d8487
VZ
429 return NULL;
430}