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