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