]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
fixed the width of the (week day as number) field, should be 1, not 2
[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$
0b9ab0bd
RL
8// Copyright: (c) 1998 Julian Smart and Markus Holzem
9// (c) 2001 Ron Lee <ron@debian.org>
3f4a0c5b 10// Licence: wxWindows license
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
191 // this must be fixed, other things risk work wrongly later if you get this
21b52985
VZ
192 wxASSERT_MSG( classInfo,
193 wxString::Format
194 (
195 _T("base class '%s' is unknown to wxWindows RTTI"),
196 name
197 ) );
1f428942
VZ
198
199 return classInfo;
200}
c801d85f 201
1f428942 202// Set pointers to base class(es) to speed up IsKindOf
afb74891 203void wxClassInfo::InitializeClasses()
c801d85f 204{
309689b2
VZ
205 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
206 // link any object module twice mistakenly) will break this function
207 // because it will enter an infinite loop and eventually die with "out of
208 // memory" - as this is quite hard to detect if you're unaware of this,
209 // try to do some checks here
0b9ab0bd 210
309689b2 211#ifdef __WXDEBUG__
0b9ab0bd 212 static const size_t nMaxClasses = 10000; // more than we'll ever have
309689b2 213 size_t nClass = 0;
0b9ab0bd 214#endif
309689b2 215
edf0993c 216 sm_classTable = new wxHashTable(wxKEY_STRING);
afb74891 217
0b9ab0bd
RL
218 // Index all class infos by their class name
219
220 wxClassInfo *info;
221 for(info = sm_first; info; info = info->m_next)
afb74891
VZ
222 {
223 if (info->m_className)
309689b2
VZ
224 {
225 wxASSERT_MSG( ++nClass < nMaxClasses,
f6bcfd97 226 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
afb74891 227 sm_classTable->Put(info->m_className, (wxObject *)info);
309689b2 228 }
afb74891
VZ
229 }
230
0b9ab0bd
RL
231 // Set base pointers for each wxClassInfo
232
233 for(info = sm_first; info; info = info->m_next)
afb74891 234 {
1f428942
VZ
235 info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1());
236 info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2());
afb74891 237 }
c801d85f
KB
238}
239
afb74891 240void wxClassInfo::CleanUpClasses()
c801d85f 241{
0c32066b 242 delete wxClassInfo::sm_classTable;
b2edef6f 243 wxClassInfo::sm_classTable = NULL;
0c32066b 244}
f4a8c29f 245
0b9ab0bd 246
cf2f341a 247wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 248{
bbee61e5 249#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 250 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 251#endif
61cca9d2 252
b2edef6f 253 if ( wxClassInfo::sm_classTable )
0c32066b
JS
254 {
255 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 256 return info ? info->CreateObject() : NULL;
0c32066b 257 }
b2edef6f 258 else // no sm_classTable yet
0c32066b 259 {
b2edef6f
VZ
260 for ( wxClassInfo *info = wxClassInfo::sm_first;
261 info;
262 info = info->m_next )
263 {
cf2f341a 264 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 265 return info->CreateObject();
b2edef6f
VZ
266 }
267
268 return NULL;
0c32066b 269 }
c801d85f
KB
270}
271
7a4b9130 272
0b9ab0bd 273// ----------------------------------------------------------------------------
a6391d30 274// wxObject
0b9ab0bd 275// ----------------------------------------------------------------------------
c801d85f 276
a6391d30
GD
277// Initialize ref data from another object (needed for copy constructor and
278// assignment operator)
279void wxObject::InitFrom(const wxObject& other)
280{
281 m_refData = other.m_refData;
282 if ( m_refData )
283 m_refData->m_count++;
284}
285
c801d85f
KB
286void wxObject::Ref(const wxObject& clone)
287{
bbee61e5 288#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 289 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 290#endif
0b9ab0bd 291
c89f5c02
RR
292 // nothing to be done
293 if (m_refData == clone.m_refData)
294 return;
295
0b9ab0bd 296 // delete reference to old data
c801d85f 297 UnRef();
0b9ab0bd 298
c801d85f 299 // reference new data
807d8487 300 if ( clone.m_refData )
0b9ab0bd 301 {
c801d85f
KB
302 m_refData = clone.m_refData;
303 ++(m_refData->m_count);
304 }
305}
306
afb74891 307void wxObject::UnRef()
c801d85f 308{
807d8487 309 if ( m_refData )
4fe5383d
VZ
310 {
311 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
312
313 if ( !--m_refData->m_count )
c801d85f 314 delete m_refData;
807d8487 315 m_refData = NULL;
c801d85f 316 }
c801d85f
KB
317}
318
807d8487
VZ
319void wxObject::AllocExclusive()
320{
321 if ( !m_refData )
322 {
323 m_refData = CreateRefData();
324 }
325 else if ( m_refData->GetRefCount() > 1 )
326 {
327 // note that ref is not going to be destroyed in this case
a9bf8315 328 const wxObjectRefData* ref = m_refData;
807d8487
VZ
329 UnRef();
330
331 // ... so we can still access it
332 m_refData = CloneRefData(ref);
333 }
334 //else: ref count is 1, we are exclusive owners of m_refData anyhow
335
336 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
337 _T("wxObject::AllocExclusive() failed.") );
338}
339
340wxObjectRefData *wxObject::CreateRefData() const
341{
d1870078
VZ
342 // if you use AllocExclusive() you must override this method
343 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
344
807d8487
VZ
345 return NULL;
346}
347
a9bf8315
VZ
348wxObjectRefData *
349wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 350{
d1870078
VZ
351 // if you use AllocExclusive() you must override this method
352 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
353
807d8487
VZ
354 return NULL;
355}
356
357// ----------------------------------------------------------------------------
358// misc
359// ----------------------------------------------------------------------------
ce28b4d7 360
edf0993c 361#if defined(__DARWIN__) && defined(WXMAKINGDLL)
ce28b4d7
GD
362
363extern "C" {
364 void __initialize_Cplusplus(void);
365 void wxWindowsDylibInit(void);
366};
367
edf0993c
GD
368// Dynamic shared library (dylib) initialization routine
369// required to initialize static C++ objects bacause of lazy dynamic linking
370// http://developer.apple.com/techpubs/macosx/Essentials/
371// SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
0b9ab0bd 372
ce28b4d7
GD
373void wxWindowsDylibInit()
374{
375 // The function __initialize_Cplusplus() must be called from the shared
376 // library initialization routine to cause the static C++ objects in
377 // the library to be initialized (reference number 2441683).
378
edf0993c
GD
379 // This only seems to be necessary if the library initialization routine
380 // needs to use the static C++ objects
ce28b4d7
GD
381 __initialize_Cplusplus();
382}
383
384#endif
0b9ab0bd
RL
385
386// vi:sts=4:sw=4:et