initialize wxClassInfo::sm_classTable automatically
[wxWidgets.git] / src / common / object.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/object.cpp
3 // Purpose: wxObject implementation
4 // Author: Julian Smart
5 // Modified by: Ron Lee
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifdef __GNUG__
14 #pragma implementation "object.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/hash.h"
26 #include "wx/object.h"
27 #endif
28
29 #include <string.h>
30
31 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
32 #include "wx/memory.h"
33 #endif
34
35 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
36 // for wxObject::Dump
37 #include "wx/ioswrap.h"
38
39 #if defined(__VISAGECPP__)
40 #define DEBUG_PRINTF(NAME) { static int raz=0; \
41 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
42 #else
43 #define DEBUG_PRINTF(NAME)
44 #endif
45 #endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
46
47 // we must disable optimizations for VC.NET because otherwise its too eager
48 // linker discards wxClassInfo objects in release build thus breaking many,
49 // many things
50 #if defined __VISUALC__ && __VISUALC__ >= 1300
51 #pragma optimize("", off)
52 #endif
53
54 #if wxUSE_EXTENDED_RTTI
55 const wxClassInfo* wxObject::sm_classParentswxObject[] = { NULL } ;
56 wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data )
57 { return data.Get<wxObject*>() ; }
58 wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data )
59 { return &data.Get<wxObject>() ; }
60 wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data )
61 { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; }
62 wxClassInfo wxObject::sm_classwxObject(sm_classParentswxObject , wxT("") , wxT("wxObject"),
63 (int) sizeof(wxObject), \
64 (wxObjectConstructorFn) 0 ,
65 (wxPropertyInfo*) NULL,(wxHandlerInfo*) NULL,0 , 0 ,
66 0 , wxVariantOfPtrToObjectConverterwxObject , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject);
67 template<> void wxStringReadValue(const wxString & , wxObject * & ){assert(0) ;}
68 template<> void wxStringWriteValue(wxString & , wxObject* const & ){assert(0) ;}
69 template<> void wxStringReadValue(const wxString & , wxObject & ){assert(0) ;}
70 template<> void wxStringWriteValue(wxString & , wxObject const & ){assert(0) ;}
71 template<> const wxTypeInfo* wxGetTypeInfo( wxObject ** )
72 { static wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::sm_classwxObject) ; return &s_typeInfo ; }
73 template<> const wxTypeInfo* wxGetTypeInfo( wxObject * )
74 { static wxClassTypeInfo s_typeInfo(wxT_OBJECT , &wxObject::sm_classwxObject) ; return &s_typeInfo ; }
75 #else
76 wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
77 (int) sizeof(wxObject),
78 (wxObjectConstructorFn) 0 );
79 #endif
80
81 // restore optimizations
82 #if defined __VISUALC__ && __VISUALC__ >= 1300
83 #pragma optimize("", on)
84 #endif
85
86 wxClassInfo* wxClassInfo::sm_first = NULL;
87 wxHashTable* wxClassInfo::sm_classTable = NULL;
88
89 // These are here so we can avoid 'always true/false' warnings
90 // by referring to these instead of TRUE/FALSE
91 const bool wxTrue = TRUE;
92 const bool wxFalse = FALSE;
93
94 // Is this object a kind of (a subclass of) 'info'?
95 // E.g. is wxWindow a kind of wxObject?
96 // Go from this class to superclass, taking into account
97 // two possible base classes.
98 bool wxObject::IsKindOf(wxClassInfo *info) const
99 {
100 wxClassInfo *thisInfo = GetClassInfo();
101 return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ;
102 }
103
104 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
105 void wxObject::Dump(wxSTD ostream& str)
106 {
107 if (GetClassInfo() && GetClassInfo()->GetClassName())
108 str << GetClassInfo()->GetClassName();
109 else
110 str << _T("unknown object class");
111 }
112 #endif
113
114
115 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
116 #undef new
117 #endif
118
119
120 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
121 void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
122 {
123 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE);
124 }
125 #endif
126
127 #ifdef _WX_WANT_DELETE_VOID
128 void wxObject::operator delete ( void *buf )
129 {
130 wxDebugFree(buf);
131 }
132 #endif
133
134 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
135 void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
136 {
137 wxDebugFree(buf);
138 }
139 #endif
140
141 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
142 void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
143 {
144 wxDebugFree(buf);
145 }
146 #endif
147
148 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
149 void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
150 {
151 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE);
152 }
153 #endif
154
155 #ifdef _WX_WANT_ARRAY_DELETE_VOID
156 void wxObject::operator delete[] ( void *buf )
157 {
158 wxDebugFree(buf, TRUE);
159 }
160 #endif
161
162 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
163 void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
164 {
165 wxDebugFree(buf, TRUE);
166 }
167 #endif
168
169
170 // ----------------------------------------------------------------------------
171 // wxClassInfo
172 // ----------------------------------------------------------------------------
173
174 wxClassInfo::~wxClassInfo()
175 {
176 // remove this object from the linked list of all class infos: if we don't
177 // do it, loading/unloading a DLL containing static wxClassInfo objects is
178 // not going to work
179 if ( this == sm_first )
180 {
181 sm_first = m_next;
182 }
183 else
184 {
185 wxClassInfo *info = sm_first;
186 while (info)
187 {
188 if ( info->m_next == this )
189 {
190 info->m_next = m_next;
191 break;
192 }
193
194 info = info->m_next;
195 }
196 }
197 Unregister();
198 }
199
200 wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
201 {
202 if ( sm_classTable )
203 {
204 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
205 }
206 else
207 {
208 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
209 {
210 if ( wxStrcmp(info->GetClassName(), className) == 0 )
211 return info;
212 }
213
214 return NULL;
215 }
216 }
217
218 // Set pointers to base class(es) to speed up IsKindOf
219 void wxClassInfo::InitializeClasses()
220 {
221 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
222 // link any object module twice mistakenly) will break this function
223 // because it will enter an infinite loop and eventually die with "out of
224 // memory" - as this is quite hard to detect if you're unaware of this,
225 // try to do some checks here
226
227 #ifdef __WXDEBUG__
228 static const size_t nMaxClasses = 10000; // more than we'll ever have
229 size_t nClass = 0;
230 #endif
231
232 // Do this initialization only once, because classes are added
233 // automatically if
234 if ( sm_classTable == NULL )
235 {
236 sm_classTable = new wxHashTable(wxKEY_STRING);
237
238 // Index all class infos by their class name:
239 wxClassInfo *info;
240 for(info = sm_first; info; info = info->m_next)
241 {
242 if (info->m_className)
243 {
244 wxASSERT_MSG( ++nClass < nMaxClasses,
245 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
246 sm_classTable->Put(info->m_className, (wxObject *)info);
247 }
248 }
249 }
250 }
251
252 void wxClassInfo::CleanUpClasses()
253 {
254 delete wxClassInfo::sm_classTable;
255 wxClassInfo::sm_classTable = NULL;
256 }
257
258 void wxClassInfo::Register()
259 {
260 if ( sm_classTable )
261 {
262 sm_classTable->Put(m_className, (wxObject *)this);
263 }
264 }
265
266 void wxClassInfo::Unregister()
267 {
268 if ( sm_classTable )
269 {
270 sm_classTable->Delete(m_className);
271 }
272 }
273
274 wxObject *wxCreateDynamicObject(const wxChar *name)
275 {
276 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
277 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
278 #endif
279
280 if ( wxClassInfo::sm_classTable )
281 {
282 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
283 return info ? info->CreateObject() : NULL;
284 }
285 else // no sm_classTable yet
286 {
287 for ( wxClassInfo *info = wxClassInfo::sm_first;
288 info;
289 info = info->m_next )
290 {
291 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
292 return info->CreateObject();
293 }
294
295 return NULL;
296 }
297 }
298
299
300 // ----------------------------------------------------------------------------
301 // wxObject
302 // ----------------------------------------------------------------------------
303
304 // Initialize ref data from another object (needed for copy constructor and
305 // assignment operator)
306 void wxObject::InitFrom(const wxObject& other)
307 {
308 m_refData = other.m_refData;
309 if ( m_refData )
310 m_refData->m_count++;
311 }
312
313 void wxObject::Ref(const wxObject& clone)
314 {
315 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
316 DEBUG_PRINTF(wxObject::Ref)
317 #endif
318
319 // nothing to be done
320 if (m_refData == clone.m_refData)
321 return;
322
323 // delete reference to old data
324 UnRef();
325
326 // reference new data
327 if ( clone.m_refData )
328 {
329 m_refData = clone.m_refData;
330 ++(m_refData->m_count);
331 }
332 }
333
334 void wxObject::UnRef()
335 {
336 if ( m_refData )
337 {
338 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
339
340 if ( !--m_refData->m_count )
341 delete m_refData;
342 m_refData = NULL;
343 }
344 }
345
346 void wxObject::AllocExclusive()
347 {
348 if ( !m_refData )
349 {
350 m_refData = CreateRefData();
351 }
352 else if ( m_refData->GetRefCount() > 1 )
353 {
354 // note that ref is not going to be destroyed in this case
355 const wxObjectRefData* ref = m_refData;
356 UnRef();
357
358 // ... so we can still access it
359 m_refData = CloneRefData(ref);
360 }
361 //else: ref count is 1, we are exclusive owners of m_refData anyhow
362
363 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
364 _T("wxObject::AllocExclusive() failed.") );
365 }
366
367 wxObjectRefData *wxObject::CreateRefData() const
368 {
369 // if you use AllocExclusive() you must override this method
370 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
371
372 return NULL;
373 }
374
375 wxObjectRefData *
376 wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
377 {
378 // if you use AllocExclusive() you must override this method
379 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
380
381 return NULL;
382 }
383
384 // ----------------------------------------------------------------------------
385 // misc
386 // ----------------------------------------------------------------------------
387
388 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
389
390 extern "C" {
391 void __initialize_Cplusplus(void);
392 void wxWindowsDylibInit(void);
393 };
394
395 // Dynamic shared library (dylib) initialization routine
396 // required to initialize static C++ objects bacause of lazy dynamic linking
397 // http://developer.apple.com/techpubs/macosx/Essentials/
398 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
399
400 void wxWindowsDylibInit()
401 {
402 // The function __initialize_Cplusplus() must be called from the shared
403 // library initialization routine to cause the static C++ objects in
404 // the library to be initialized (reference number 2441683).
405
406 // This only seems to be necessary if the library initialization routine
407 // needs to use the static C++ objects
408 __initialize_Cplusplus();
409 }
410
411 #endif
412
413 // vi:sts=4:sw=4:et