]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
A slightly modified version of Patch #1197468. Keeps track of pending
[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
14f355c2 13#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
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
b2edef6f
VZ
36 #if defined(__VISAGECPP__)
37 #define DEBUG_PRINTF(NAME) { static int raz=0; \
38 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
39 #else
40 #define DEBUG_PRINTF(NAME)
41 #endif
42#endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
c801d85f 43
e1720942
VZ
44// we must disable optimizations for VC.NET because otherwise its too eager
45// linker discards wxClassInfo objects in release build thus breaking many,
46// many things
fbae49ee 47#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
48 #pragma optimize("", off)
49#endif
f6bcfd97 50
30bfc425 51#if wxUSE_EXTENDED_RTTI
05fa251a 52const wxClassInfo* wxObject::ms_classParents[] = { NULL } ;
431346ff 53 wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data )
2c4c3987 54{ return data.wxTEMPLATED_MEMBER_CALL(Get , wxObject*) ; }
431346ff 55 wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data )
2c4c3987 56{ return &data.wxTEMPLATED_MEMBER_CALL(Get , wxObject) ; }
431346ff 57 wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data )
a095505c 58 { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; }
525d8583 59 wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , wxT("wxObject"),
a095505c 60 (int) sizeof(wxObject), \
431346ff 61 (wxObjectConstructorFn) 0 ,
7e548f6b
WS
62 (wxPropertyInfo*) NULL,(wxHandlerInfo*) NULL,0 , 0 ,
63 0 , wxVariantOfPtrToObjectConverterwxObject , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject);
a095505c
SC
64 template<> void wxStringReadValue(const wxString & , wxObject * & ){assert(0) ;}
65 template<> void wxStringWriteValue(wxString & , wxObject* const & ){assert(0) ;}
431346ff
SC
66 template<> void wxStringReadValue(const wxString & , wxObject & ){assert(0) ;}
67 template<> void wxStringWriteValue(wxString & , wxObject const & ){assert(0) ;}
7e548f6b
WS
68 wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject*).name() ) ;
69 wxClassTypeInfo s_typeInfowxObject(wxT_OBJECT , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject).name() ) ;
a095505c 70#else
c0db9626 71wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
0b9ab0bd
RL
72 (int) sizeof(wxObject),
73 (wxObjectConstructorFn) 0 );
a095505c 74#endif
c801d85f 75
e1720942 76// restore optimizations
fbae49ee 77#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
78 #pragma optimize("", on)
79#endif
80
b2edef6f
VZ
81wxClassInfo* wxClassInfo::sm_first = NULL;
82wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 83
66d2fa18
VZ
84// when using XTI, this method is already implemented inline inside
85// DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function
86// non-inline because this allows us to have a non-inline virtual function in
87// all wx classes and this solves linking problems for HP-UX native toolchain
88// and possibly others (we could make dtor non-inline as well but it's more
89// useful to keep it inline than this function)
90#if !wxUSE_EXTENDED_RTTI
91
14ca93a0
VZ
92wxClassInfo *wxObject::GetClassInfo() const
93{
94 return &wxObject::ms_classInfo;
95}
96
66d2fa18
VZ
97#endif // wxUSE_EXTENDED_RTTI
98
b2edef6f 99// These are here so we can avoid 'always true/false' warnings
7e548f6b
WS
100// by referring to these instead of true/false
101const bool wxTrue = true;
102const bool wxFalse = false;
c801d85f 103
b2edef6f
VZ
104// Is this object a kind of (a subclass of) 'info'?
105// E.g. is wxWindow a kind of wxObject?
106// Go from this class to superclass, taking into account
107// two possible base classes.
341287bf 108bool wxObject::IsKindOf(wxClassInfo *info) const
c801d85f 109{
afb74891 110 wxClassInfo *thisInfo = GetClassInfo();
7e548f6b 111 return (thisInfo) ? thisInfo->IsKindOf(info) : false ;
c801d85f
KB
112}
113
cf760e4c 114#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
7e548f6b 115 #undef new
cf760e4c
VZ
116#endif
117
118
b2edef6f 119#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 120void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 121{
7e548f6b 122 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true);
c801d85f 123}
b2edef6f 124#endif
c801d85f 125
b2edef6f
VZ
126#ifdef _WX_WANT_DELETE_VOID
127void wxObject::operator delete ( void *buf )
415ca026
DW
128{
129 wxDebugFree(buf);
130}
b2edef6f
VZ
131#endif
132
133#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
134void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 135{
afb74891 136 wxDebugFree(buf);
c801d85f 137}
0b9ab0bd
RL
138#endif
139
b2edef6f 140#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 141void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 142{
b2edef6f 143 wxDebugFree(buf);
76626af2
JS
144}
145#endif
146
b2edef6f 147#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 148void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 149{
7e548f6b 150 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true);
c801d85f 151}
b2edef6f 152#endif
c801d85f 153
b2edef6f
VZ
154#ifdef _WX_WANT_ARRAY_DELETE_VOID
155void wxObject::operator delete[] ( void *buf )
c801d85f 156{
7e548f6b 157 wxDebugFree(buf, true);
c801d85f
KB
158}
159#endif
160
b2edef6f 161#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 162void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f 163{
7e548f6b 164 wxDebugFree(buf, true);
b2edef6f
VZ
165}
166#endif
afb74891 167
afb74891 168
0b9ab0bd
RL
169// ----------------------------------------------------------------------------
170// wxClassInfo
171// ----------------------------------------------------------------------------
c801d85f 172
aa4b7ef9
VZ
173wxClassInfo::~wxClassInfo()
174{
175 // remove this object from the linked list of all class infos: if we don't
176 // do it, loading/unloading a DLL containing static wxClassInfo objects is
177 // not going to work
178 if ( this == sm_first )
179 {
180 sm_first = m_next;
181 }
182 else
183 {
184 wxClassInfo *info = sm_first;
185 while (info)
186 {
187 if ( info->m_next == this )
188 {
189 info->m_next = m_next;
190 break;
191 }
192
193 info = info->m_next;
194 }
195 }
7e548f6b 196 Unregister();
aa4b7ef9
VZ
197}
198
0b9ab0bd 199wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
c801d85f 200{
80e8062f
VZ
201 if ( sm_classTable )
202 {
203 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
204 }
205 else
206 {
207 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
208 {
209 if ( wxStrcmp(info->GetClassName(), className) == 0 )
210 return info;
211 }
212
213 return NULL;
214 }
c801d85f
KB
215}
216
cafc76a4 217void wxClassInfo::CleanUp()
c801d85f 218{
cafc76a4 219 if ( sm_classTable )
afb74891 220 {
cafc76a4
VS
221 delete sm_classTable;
222 sm_classTable = NULL;
afb74891 223 }
d1d738f1 224}
afb74891 225
d1d738f1
VS
226void wxClassInfo::Register()
227{
cafc76a4 228 if ( !sm_classTable )
afb74891 229 {
cafc76a4 230 sm_classTable = new wxHashTable(wxKEY_STRING);
afb74891 231 }
cafc76a4
VS
232
233 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
234 // link any object module twice mistakenly) will break this function
235 // because it will enter an infinite loop and eventually die with "out of
236 // memory" - as this is quite hard to detect if you're unaware of this,
237 // try to do some checks here
238 wxASSERT_MSG( sm_classTable->Get(m_className) == NULL,
239 _T("class already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
7e548f6b 240
cafc76a4 241 sm_classTable->Put(m_className, (wxObject *)this);
c801d85f
KB
242}
243
d1d738f1 244void wxClassInfo::Unregister()
c801d85f 245{
d1d738f1
VS
246 if ( sm_classTable )
247 {
248 sm_classTable->Delete(m_className);
cafc76a4
VS
249 if ( sm_classTable->GetCount() == 0 )
250 {
251 delete sm_classTable;
252 sm_classTable = NULL;
253 }
d1d738f1 254 }
0c32066b 255}
f4a8c29f 256
cf2f341a 257wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 258{
bbee61e5 259#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 260 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 261#endif
61cca9d2 262
b2edef6f 263 if ( wxClassInfo::sm_classTable )
0c32066b
JS
264 {
265 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 266 return info ? info->CreateObject() : NULL;
0c32066b 267 }
b2edef6f 268 else // no sm_classTable yet
0c32066b 269 {
b2edef6f
VZ
270 for ( wxClassInfo *info = wxClassInfo::sm_first;
271 info;
272 info = info->m_next )
273 {
cf2f341a 274 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 275 return info->CreateObject();
b2edef6f
VZ
276 }
277
278 return NULL;
0c32066b 279 }
c801d85f
KB
280}
281
7a4b9130 282
0b9ab0bd 283// ----------------------------------------------------------------------------
a6391d30 284// wxObject
0b9ab0bd 285// ----------------------------------------------------------------------------
c801d85f 286
a6391d30
GD
287// Initialize ref data from another object (needed for copy constructor and
288// assignment operator)
289void wxObject::InitFrom(const wxObject& other)
290{
291 m_refData = other.m_refData;
292 if ( m_refData )
293 m_refData->m_count++;
294}
295
c801d85f
KB
296void wxObject::Ref(const wxObject& clone)
297{
bbee61e5 298#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 299 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 300#endif
0b9ab0bd 301
c89f5c02
RR
302 // nothing to be done
303 if (m_refData == clone.m_refData)
304 return;
305
0b9ab0bd 306 // delete reference to old data
c801d85f 307 UnRef();
0b9ab0bd 308
c801d85f 309 // reference new data
807d8487 310 if ( clone.m_refData )
0b9ab0bd 311 {
c801d85f
KB
312 m_refData = clone.m_refData;
313 ++(m_refData->m_count);
314 }
315}
316
afb74891 317void wxObject::UnRef()
c801d85f 318{
807d8487 319 if ( m_refData )
4fe5383d
VZ
320 {
321 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
322
323 if ( !--m_refData->m_count )
c801d85f 324 delete m_refData;
807d8487 325 m_refData = NULL;
c801d85f 326 }
c801d85f
KB
327}
328
807d8487
VZ
329void wxObject::AllocExclusive()
330{
331 if ( !m_refData )
332 {
333 m_refData = CreateRefData();
334 }
335 else if ( m_refData->GetRefCount() > 1 )
336 {
337 // note that ref is not going to be destroyed in this case
a9bf8315 338 const wxObjectRefData* ref = m_refData;
807d8487
VZ
339 UnRef();
340
341 // ... so we can still access it
342 m_refData = CloneRefData(ref);
343 }
344 //else: ref count is 1, we are exclusive owners of m_refData anyhow
345
346 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
347 _T("wxObject::AllocExclusive() failed.") );
348}
349
350wxObjectRefData *wxObject::CreateRefData() const
351{
d1870078
VZ
352 // if you use AllocExclusive() you must override this method
353 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
354
807d8487
VZ
355 return NULL;
356}
357
a9bf8315
VZ
358wxObjectRefData *
359wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 360{
d1870078
VZ
361 // if you use AllocExclusive() you must override this method
362 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
363
807d8487
VZ
364 return NULL;
365}