]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
Added 'Go' button for quickly saving setup or configure file
[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
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
b2edef6f 85#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 86void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 87{
cf760e4c 88 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE);
c801d85f 89}
b2edef6f 90#endif
c801d85f 91
b2edef6f
VZ
92#ifdef _WX_WANT_DELETE_VOID
93void wxObject::operator delete ( void *buf )
415ca026
DW
94{
95 wxDebugFree(buf);
96}
b2edef6f
VZ
97#endif
98
99#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
100void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 101{
afb74891 102 wxDebugFree(buf);
c801d85f 103}
0b9ab0bd
RL
104#endif
105
b2edef6f 106#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 107void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 108{
b2edef6f 109 wxDebugFree(buf);
76626af2
JS
110}
111#endif
112
b2edef6f 113#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 114void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 115{
cf760e4c 116 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE);
c801d85f 117}
b2edef6f 118#endif
c801d85f 119
b2edef6f
VZ
120#ifdef _WX_WANT_ARRAY_DELETE_VOID
121void wxObject::operator delete[] ( void *buf )
c801d85f 122{
afb74891 123 wxDebugFree(buf, TRUE);
c801d85f
KB
124}
125#endif
126
b2edef6f 127#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 128void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f
VZ
129{
130 wxDebugFree(buf, TRUE);
131}
132#endif
afb74891 133
afb74891 134
0b9ab0bd
RL
135// ----------------------------------------------------------------------------
136// wxClassInfo
137// ----------------------------------------------------------------------------
c801d85f 138
aa4b7ef9
VZ
139wxClassInfo::~wxClassInfo()
140{
141 // remove this object from the linked list of all class infos: if we don't
142 // do it, loading/unloading a DLL containing static wxClassInfo objects is
143 // not going to work
144 if ( this == sm_first )
145 {
146 sm_first = m_next;
147 }
148 else
149 {
150 wxClassInfo *info = sm_first;
151 while (info)
152 {
153 if ( info->m_next == this )
154 {
155 info->m_next = m_next;
156 break;
157 }
158
159 info = info->m_next;
160 }
161 }
162}
163
0b9ab0bd 164wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
c801d85f 165{
80e8062f
VZ
166 if ( sm_classTable )
167 {
168 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
169 }
170 else
171 {
172 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
173 {
174 if ( wxStrcmp(info->GetClassName(), className) == 0 )
175 return info;
176 }
177
178 return NULL;
179 }
c801d85f
KB
180}
181
1f428942
VZ
182// a tiny InitializeClasses() helper
183/* static */
184inline wxClassInfo *wxClassInfo::GetBaseByName(const wxChar *name)
185{
186 if ( !name )
187 return NULL;
188
189 wxClassInfo *classInfo = (wxClassInfo *)sm_classTable->Get(name);
190
27db95f0
VZ
191#ifdef __WXDEBUG__
192 // this must be fixed, other things will work wrongly later if you get this
193 if ( !classInfo )
194 {
195 wxFAIL_MSG( wxString::Format
196 (
197 _T("base class '%s' is unknown to wxWindows RTTI"),
198 name
199 ) );
200 }
201#endif // __WXDEBUG__
1f428942
VZ
202
203 return classInfo;
204}
c801d85f 205
1f428942 206// Set pointers to base class(es) to speed up IsKindOf
afb74891 207void wxClassInfo::InitializeClasses()
c801d85f 208{
309689b2
VZ
209 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
210 // link any object module twice mistakenly) will break this function
211 // because it will enter an infinite loop and eventually die with "out of
212 // memory" - as this is quite hard to detect if you're unaware of this,
213 // try to do some checks here
0b9ab0bd 214
309689b2 215#ifdef __WXDEBUG__
0b9ab0bd 216 static const size_t nMaxClasses = 10000; // more than we'll ever have
309689b2 217 size_t nClass = 0;
0b9ab0bd 218#endif
309689b2 219
edf0993c 220 sm_classTable = new wxHashTable(wxKEY_STRING);
afb74891 221
0b9ab0bd
RL
222 // Index all class infos by their class name
223
224 wxClassInfo *info;
225 for(info = sm_first; info; info = info->m_next)
afb74891
VZ
226 {
227 if (info->m_className)
309689b2
VZ
228 {
229 wxASSERT_MSG( ++nClass < nMaxClasses,
f6bcfd97 230 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
afb74891 231 sm_classTable->Put(info->m_className, (wxObject *)info);
309689b2 232 }
afb74891
VZ
233 }
234
0b9ab0bd
RL
235 // Set base pointers for each wxClassInfo
236
237 for(info = sm_first; info; info = info->m_next)
afb74891 238 {
1f428942
VZ
239 info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1());
240 info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2());
afb74891 241 }
c801d85f
KB
242}
243
afb74891 244void wxClassInfo::CleanUpClasses()
c801d85f 245{
0c32066b 246 delete wxClassInfo::sm_classTable;
b2edef6f 247 wxClassInfo::sm_classTable = NULL;
0c32066b 248}
f4a8c29f 249
0b9ab0bd 250
cf2f341a 251wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 252{
bbee61e5 253#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 254 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 255#endif
61cca9d2 256
b2edef6f 257 if ( wxClassInfo::sm_classTable )
0c32066b
JS
258 {
259 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 260 return info ? info->CreateObject() : NULL;
0c32066b 261 }
b2edef6f 262 else // no sm_classTable yet
0c32066b 263 {
b2edef6f
VZ
264 for ( wxClassInfo *info = wxClassInfo::sm_first;
265 info;
266 info = info->m_next )
267 {
cf2f341a 268 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 269 return info->CreateObject();
b2edef6f
VZ
270 }
271
272 return NULL;
0c32066b 273 }
c801d85f
KB
274}
275
7a4b9130 276
0b9ab0bd 277// ----------------------------------------------------------------------------
a6391d30 278// wxObject
0b9ab0bd 279// ----------------------------------------------------------------------------
c801d85f 280
a6391d30
GD
281// Initialize ref data from another object (needed for copy constructor and
282// assignment operator)
283void wxObject::InitFrom(const wxObject& other)
284{
285 m_refData = other.m_refData;
286 if ( m_refData )
287 m_refData->m_count++;
288}
289
c801d85f
KB
290void wxObject::Ref(const wxObject& clone)
291{
bbee61e5 292#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 293 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 294#endif
0b9ab0bd 295
c89f5c02
RR
296 // nothing to be done
297 if (m_refData == clone.m_refData)
298 return;
299
0b9ab0bd 300 // delete reference to old data
c801d85f 301 UnRef();
0b9ab0bd 302
c801d85f 303 // reference new data
807d8487 304 if ( clone.m_refData )
0b9ab0bd 305 {
c801d85f
KB
306 m_refData = clone.m_refData;
307 ++(m_refData->m_count);
308 }
309}
310
afb74891 311void wxObject::UnRef()
c801d85f 312{
807d8487 313 if ( m_refData )
4fe5383d
VZ
314 {
315 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
316
317 if ( !--m_refData->m_count )
c801d85f 318 delete m_refData;
807d8487 319 m_refData = NULL;
c801d85f 320 }
c801d85f
KB
321}
322
807d8487
VZ
323void wxObject::AllocExclusive()
324{
325 if ( !m_refData )
326 {
327 m_refData = CreateRefData();
328 }
329 else if ( m_refData->GetRefCount() > 1 )
330 {
331 // note that ref is not going to be destroyed in this case
a9bf8315 332 const wxObjectRefData* ref = m_refData;
807d8487
VZ
333 UnRef();
334
335 // ... so we can still access it
336 m_refData = CloneRefData(ref);
337 }
338 //else: ref count is 1, we are exclusive owners of m_refData anyhow
339
340 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
341 _T("wxObject::AllocExclusive() failed.") );
342}
343
344wxObjectRefData *wxObject::CreateRefData() const
345{
d1870078
VZ
346 // if you use AllocExclusive() you must override this method
347 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
348
807d8487
VZ
349 return NULL;
350}
351
a9bf8315
VZ
352wxObjectRefData *
353wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 354{
d1870078
VZ
355 // if you use AllocExclusive() you must override this method
356 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
357
807d8487
VZ
358 return NULL;
359}
360
361// ----------------------------------------------------------------------------
362// misc
363// ----------------------------------------------------------------------------
ce28b4d7 364
edf0993c 365#if defined(__DARWIN__) && defined(WXMAKINGDLL)
ce28b4d7
GD
366
367extern "C" {
368 void __initialize_Cplusplus(void);
369 void wxWindowsDylibInit(void);
370};
371
edf0993c
GD
372// Dynamic shared library (dylib) initialization routine
373// required to initialize static C++ objects bacause of lazy dynamic linking
374// http://developer.apple.com/techpubs/macosx/Essentials/
375// SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
0b9ab0bd 376
ce28b4d7
GD
377void wxWindowsDylibInit()
378{
379 // The function __initialize_Cplusplus() must be called from the shared
380 // library initialization routine to cause the static C++ objects in
381 // the library to be initialized (reference number 2441683).
382
edf0993c
GD
383 // This only seems to be necessary if the library initialization routine
384 // needs to use the static C++ objects
ce28b4d7
GD
385 __initialize_Cplusplus();
386}
387
388#endif
0b9ab0bd
RL
389
390// vi:sts=4:sw=4:et