clean up of memory debugging macros and chanegs to compile with CW7 (patch 548408)
[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 and Markus Holzem
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows license
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 #endif
27
28 #include <string.h>
29
30 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
31 #include "wx/memory.h"
32 #endif
33
34 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
35 // for wxObject::Dump
36 #include "wx/ioswrap.h"
37
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
45
46
47 wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
48 (int) sizeof(wxObject),
49 (wxObjectConstructorFn) 0 );
50
51 wxClassInfo* wxClassInfo::sm_first = NULL;
52 wxHashTable* wxClassInfo::sm_classTable = NULL;
53
54 // These are here so we can avoid 'always true/false' warnings
55 // by referring to these instead of TRUE/FALSE
56 const bool wxTrue = TRUE;
57 const bool wxFalse = FALSE;
58
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.
63 bool wxObject::IsKindOf(wxClassInfo *info) const
64 {
65 wxClassInfo *thisInfo = GetClassInfo();
66 return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ;
67 }
68
69 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
70 void wxObject::Dump(wxSTD ostream& str)
71 {
72 if (GetClassInfo() && GetClassInfo()->GetClassName())
73 str << GetClassInfo()->GetClassName();
74 else
75 str << _T("unknown object class");
76 }
77 #endif
78
79
80
81 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
82 void *wxObject::operator new ( size_t size, wxChar *fileName, int lineNum )
83 {
84 return wxDebugAlloc(size, fileName, lineNum, TRUE);
85 }
86 #endif
87
88 #ifdef _WX_WANT_DELETE_VOID
89 void wxObject::operator delete ( void *buf )
90 {
91 wxDebugFree(buf);
92 }
93 #endif
94
95 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
96 void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
97 {
98 wxDebugFree(buf);
99 }
100 #endif
101
102 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
103 void wxObject::operator delete ( void *buf, wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
104 {
105 wxDebugFree(buf);
106 }
107 #endif
108
109 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
110 void *wxObject::operator new[] ( size_t size, wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
111 {
112 return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE);
113 }
114 #endif
115
116 #ifdef _WX_WANT_ARRAY_DELETE_VOID
117 void wxObject::operator delete[] ( void *buf )
118 {
119 wxDebugFree(buf, TRUE);
120 }
121 #endif
122
123 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
124 void wxObject::operator delete[] (void * buf, wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
125 {
126 wxDebugFree(buf, TRUE);
127 }
128 #endif
129
130
131 // ----------------------------------------------------------------------------
132 // wxClassInfo
133 // ----------------------------------------------------------------------------
134
135 wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
136 {
137 if ( sm_classTable )
138 {
139 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
140 }
141 else
142 {
143 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
144 {
145 if ( wxStrcmp(info->GetClassName(), className) == 0 )
146 return info;
147 }
148
149 return NULL;
150 }
151 }
152
153 // a tiny InitializeClasses() helper
154 /* static */
155 inline wxClassInfo *wxClassInfo::GetBaseByName(const wxChar *name)
156 {
157 if ( !name )
158 return NULL;
159
160 wxClassInfo *classInfo = (wxClassInfo *)sm_classTable->Get(name);
161
162 // this must be fixed, other things risk work wrongly later if you get this
163 wxASSERT_MSG( classInfo,
164 wxString::Format
165 (
166 _T("base class '%s' is unknown to wxWindows RTTI"),
167 name
168 ) );
169
170 return classInfo;
171 }
172
173 // Set pointers to base class(es) to speed up IsKindOf
174 void wxClassInfo::InitializeClasses()
175 {
176 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
177 // link any object module twice mistakenly) will break this function
178 // because it will enter an infinite loop and eventually die with "out of
179 // memory" - as this is quite hard to detect if you're unaware of this,
180 // try to do some checks here
181
182 #ifdef __WXDEBUG__
183 static const size_t nMaxClasses = 10000; // more than we'll ever have
184 size_t nClass = 0;
185 #endif
186
187 wxClassInfo::sm_classTable = new wxHashTable(wxKEY_STRING);
188
189 // Index all class infos by their class name
190
191 wxClassInfo *info;
192 for(info = sm_first; info; info = info->m_next)
193 {
194 if (info->m_className)
195 {
196 wxASSERT_MSG( ++nClass < nMaxClasses,
197 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
198 sm_classTable->Put(info->m_className, (wxObject *)info);
199 }
200 }
201
202 // Set base pointers for each wxClassInfo
203
204 for(info = sm_first; info; info = info->m_next)
205 {
206 info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1());
207 info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2());
208 }
209 }
210
211 void wxClassInfo::CleanUpClasses()
212 {
213 delete wxClassInfo::sm_classTable;
214 wxClassInfo::sm_classTable = NULL;
215 }
216
217
218 wxObject *wxCreateDynamicObject(const wxChar *name)
219 {
220 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
221 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
222 #endif
223
224 if ( wxClassInfo::sm_classTable )
225 {
226 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
227 return info ? info->CreateObject() : NULL;
228 }
229 else // no sm_classTable yet
230 {
231 for ( wxClassInfo *info = wxClassInfo::sm_first;
232 info;
233 info = info->m_next )
234 {
235 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
236 return info->CreateObject();
237 }
238
239 return NULL;
240 }
241 }
242
243
244 // ----------------------------------------------------------------------------
245 // wxClassInfo
246 // ----------------------------------------------------------------------------
247
248 void wxObject::Ref(const wxObject& clone)
249 {
250 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
251 DEBUG_PRINTF(wxObject::Ref)
252 #endif
253
254 // nothing to be done
255 if (m_refData == clone.m_refData)
256 return;
257
258 // delete reference to old data
259 UnRef();
260
261 // reference new data
262 if ( clone.m_refData )
263 {
264 m_refData = clone.m_refData;
265 ++(m_refData->m_count);
266 }
267 }
268
269 void wxObject::UnRef()
270 {
271 if ( m_refData )
272 {
273 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
274
275 if ( !--m_refData->m_count )
276 delete m_refData;
277 m_refData = NULL;
278 }
279 }
280
281 void wxObject::AllocExclusive()
282 {
283 if ( !m_refData )
284 {
285 m_refData = CreateRefData();
286 }
287 else if ( m_refData->GetRefCount() > 1 )
288 {
289 // note that ref is not going to be destroyed in this case
290 const wxObjectRefData* ref = m_refData;
291 UnRef();
292
293 // ... so we can still access it
294 m_refData = CloneRefData(ref);
295 }
296 //else: ref count is 1, we are exclusive owners of m_refData anyhow
297
298 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
299 _T("wxObject::AllocExclusive() failed.") );
300 }
301
302 wxObjectRefData *wxObject::CreateRefData() const
303 {
304 // if you use AllocExclusive() you must override this method
305 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
306
307 return NULL;
308 }
309
310 wxObjectRefData *
311 wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
312 {
313 // if you use AllocExclusive() you must override this method
314 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
315
316 return NULL;
317 }
318
319 // ----------------------------------------------------------------------------
320 // misc
321 // ----------------------------------------------------------------------------
322
323 #if defined(__DARWIN__) && defined(DYLIB_INIT)
324
325 extern "C" {
326 void __initialize_Cplusplus(void);
327 void wxWindowsDylibInit(void);
328 };
329
330 // Dynamic shared library (dylib) initialization routine
331 // required to initialize static C++ objects bacause of lazy dynamic linking
332 // http://developer.apple.com/techpubs/macosx/Essentials/
333 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
334
335 void wxWindowsDylibInit()
336 {
337 // The function __initialize_Cplusplus() must be called from the shared
338 // library initialization routine to cause the static C++ objects in
339 // the library to be initialized (reference number 2441683).
340
341 __initialize_Cplusplus();
342 }
343
344 #endif
345
346 // vi:sts=4:sw=4:et