xti extensions
[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<> const wxTypeInfo* wxGetTypeInfo( wxObject ** )
70 { static wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::sm_classwxObject) ; return &s_typeInfo ; }
71 #else
72 wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
73 (int) sizeof(wxObject),
74 (wxObjectConstructorFn) 0 );
75 #endif
76
77 // restore optimizations
78 #if defined __VISUALC__ && __VISUALC__ >= 1300
79 #pragma optimize("", on)
80 #endif
81
82 wxClassInfo* wxClassInfo::sm_first = NULL;
83 wxHashTable* wxClassInfo::sm_classTable = NULL;
84
85 // These are here so we can avoid 'always true/false' warnings
86 // by referring to these instead of TRUE/FALSE
87 const bool wxTrue = TRUE;
88 const bool wxFalse = FALSE;
89
90 // Is this object a kind of (a subclass of) 'info'?
91 // E.g. is wxWindow a kind of wxObject?
92 // Go from this class to superclass, taking into account
93 // two possible base classes.
94 bool wxObject::IsKindOf(wxClassInfo *info) const
95 {
96 wxClassInfo *thisInfo = GetClassInfo();
97 return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ;
98 }
99
100 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
101 void wxObject::Dump(wxSTD ostream& str)
102 {
103 if (GetClassInfo() && GetClassInfo()->GetClassName())
104 str << GetClassInfo()->GetClassName();
105 else
106 str << _T("unknown object class");
107 }
108 #endif
109
110
111 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
112 #undef new
113 #endif
114
115
116 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
117 void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
118 {
119 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE);
120 }
121 #endif
122
123 #ifdef _WX_WANT_DELETE_VOID
124 void wxObject::operator delete ( void *buf )
125 {
126 wxDebugFree(buf);
127 }
128 #endif
129
130 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
131 void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
132 {
133 wxDebugFree(buf);
134 }
135 #endif
136
137 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
138 void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
139 {
140 wxDebugFree(buf);
141 }
142 #endif
143
144 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
145 void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
146 {
147 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE);
148 }
149 #endif
150
151 #ifdef _WX_WANT_ARRAY_DELETE_VOID
152 void wxObject::operator delete[] ( void *buf )
153 {
154 wxDebugFree(buf, TRUE);
155 }
156 #endif
157
158 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
159 void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
160 {
161 wxDebugFree(buf, TRUE);
162 }
163 #endif
164
165
166 // ----------------------------------------------------------------------------
167 // wxClassInfo
168 // ----------------------------------------------------------------------------
169
170 wxClassInfo::~wxClassInfo()
171 {
172 // remove this object from the linked list of all class infos: if we don't
173 // do it, loading/unloading a DLL containing static wxClassInfo objects is
174 // not going to work
175 if ( this == sm_first )
176 {
177 sm_first = m_next;
178 }
179 else
180 {
181 wxClassInfo *info = sm_first;
182 while (info)
183 {
184 if ( info->m_next == this )
185 {
186 info->m_next = m_next;
187 break;
188 }
189
190 info = info->m_next;
191 }
192 }
193 #if wxUSE_EXTENDED_RTTI
194 Unregister( m_className ) ;
195 #endif
196 }
197
198 wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
199 {
200 if ( sm_classTable )
201 {
202 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
203 }
204 else
205 {
206 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
207 {
208 if ( wxStrcmp(info->GetClassName(), className) == 0 )
209 return info;
210 }
211
212 return NULL;
213 }
214 }
215
216 // a tiny InitializeClasses() helper
217 /* static */
218 inline wxClassInfo *wxClassInfo::GetBaseByName(const wxChar *name)
219 {
220 if ( !name )
221 return NULL;
222
223 wxClassInfo *classInfo = (wxClassInfo *)sm_classTable->Get(name);
224
225 #ifdef __WXDEBUG__
226 // this must be fixed, other things will work wrongly later if you get this
227 if ( !classInfo )
228 {
229 wxFAIL_MSG( wxString::Format
230 (
231 _T("base class '%s' is unknown to wxWindows RTTI"),
232 name
233 ) );
234 }
235 #endif // __WXDEBUG__
236
237 return classInfo;
238 }
239
240 // Set pointers to base class(es) to speed up IsKindOf
241 void wxClassInfo::InitializeClasses()
242 {
243 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
244 // link any object module twice mistakenly) will break this function
245 // because it will enter an infinite loop and eventually die with "out of
246 // memory" - as this is quite hard to detect if you're unaware of this,
247 // try to do some checks here
248
249 #ifdef __WXDEBUG__
250 static const size_t nMaxClasses = 10000; // more than we'll ever have
251 size_t nClass = 0;
252 #endif
253
254 sm_classTable = new wxHashTable(wxKEY_STRING);
255
256 // Index all class infos by their class name
257
258 wxClassInfo *info;
259 for(info = sm_first; info; info = info->m_next)
260 {
261 if (info->m_className)
262 {
263 wxASSERT_MSG( ++nClass < nMaxClasses,
264 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
265 sm_classTable->Put(info->m_className, (wxObject *)info);
266 }
267 }
268
269 #if wxUSE_EXTENDED_RTTI == 0
270 // Set base pointers for each wxClassInfo
271
272 for(info = sm_first; info; info = info->m_next)
273 {
274 info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1());
275 info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2());
276 }
277 #endif
278 }
279
280 void wxClassInfo::CleanUpClasses()
281 {
282 delete wxClassInfo::sm_classTable;
283 wxClassInfo::sm_classTable = NULL;
284 }
285
286 wxObject *wxCreateDynamicObject(const wxChar *name)
287 {
288 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
289 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
290 #endif
291
292 if ( wxClassInfo::sm_classTable )
293 {
294 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
295 return info ? info->CreateObject() : NULL;
296 }
297 else // no sm_classTable yet
298 {
299 for ( wxClassInfo *info = wxClassInfo::sm_first;
300 info;
301 info = info->m_next )
302 {
303 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
304 return info->CreateObject();
305 }
306
307 return NULL;
308 }
309 }
310
311
312 // ----------------------------------------------------------------------------
313 // wxObject
314 // ----------------------------------------------------------------------------
315
316 // Initialize ref data from another object (needed for copy constructor and
317 // assignment operator)
318 void wxObject::InitFrom(const wxObject& other)
319 {
320 m_refData = other.m_refData;
321 if ( m_refData )
322 m_refData->m_count++;
323 }
324
325 void wxObject::Ref(const wxObject& clone)
326 {
327 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
328 DEBUG_PRINTF(wxObject::Ref)
329 #endif
330
331 // nothing to be done
332 if (m_refData == clone.m_refData)
333 return;
334
335 // delete reference to old data
336 UnRef();
337
338 // reference new data
339 if ( clone.m_refData )
340 {
341 m_refData = clone.m_refData;
342 ++(m_refData->m_count);
343 }
344 }
345
346 void wxObject::UnRef()
347 {
348 if ( m_refData )
349 {
350 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
351
352 if ( !--m_refData->m_count )
353 delete m_refData;
354 m_refData = NULL;
355 }
356 }
357
358 void wxObject::AllocExclusive()
359 {
360 if ( !m_refData )
361 {
362 m_refData = CreateRefData();
363 }
364 else if ( m_refData->GetRefCount() > 1 )
365 {
366 // note that ref is not going to be destroyed in this case
367 const wxObjectRefData* ref = m_refData;
368 UnRef();
369
370 // ... so we can still access it
371 m_refData = CloneRefData(ref);
372 }
373 //else: ref count is 1, we are exclusive owners of m_refData anyhow
374
375 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
376 _T("wxObject::AllocExclusive() failed.") );
377 }
378
379 wxObjectRefData *wxObject::CreateRefData() const
380 {
381 // if you use AllocExclusive() you must override this method
382 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
383
384 return NULL;
385 }
386
387 wxObjectRefData *
388 wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
389 {
390 // if you use AllocExclusive() you must override this method
391 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
392
393 return NULL;
394 }
395
396 // ----------------------------------------------------------------------------
397 // misc
398 // ----------------------------------------------------------------------------
399
400 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
401
402 extern "C" {
403 void __initialize_Cplusplus(void);
404 void wxWindowsDylibInit(void);
405 };
406
407 // Dynamic shared library (dylib) initialization routine
408 // required to initialize static C++ objects bacause of lazy dynamic linking
409 // http://developer.apple.com/techpubs/macosx/Essentials/
410 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
411
412 void wxWindowsDylibInit()
413 {
414 // The function __initialize_Cplusplus() must be called from the shared
415 // library initialization routine to cause the static C++ objects in
416 // the library to be initialized (reference number 2441683).
417
418 // This only seems to be necessary if the library initialization routine
419 // needs to use the static C++ objects
420 __initialize_Cplusplus();
421 }
422
423 #endif
424
425 // vi:sts=4:sw=4:et