]> git.saurik.com Git - wxWidgets.git/blob - src/common/object.cpp
Add a debug helper to show information about a GtkWidget under gdb.
[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 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/object.h"
22 #include "wx/hash.h"
23 #include "wx/memory.h"
24 #include "wx/crt.h"
25 #endif
26
27 #include <string.h>
28
29 #if wxUSE_DEBUG_CONTEXT
30 #if defined(__VISAGECPP__)
31 #define DEBUG_PRINTF(NAME) { static int raz=0; \
32 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
33 #else
34 #define DEBUG_PRINTF(NAME)
35 #endif
36 #endif // wxUSE_DEBUG_CONTEXT
37
38 // we must disable optimizations for VC.NET because otherwise its too eager
39 // linker discards wxClassInfo objects in release build thus breaking many,
40 // many things
41 #if defined __VISUALC__ && __VISUALC__ >= 1300
42 #pragma optimize("", off)
43 #endif
44
45 #if wxUSE_EXTENDED_RTTI
46 const wxClassInfo* wxObject::ms_classParents[] = { NULL } ;
47 wxObject* wxVariantOfPtrToObjectConverterwxObject ( const wxAny &data )
48 { return wxANY_AS(data, wxObject*); }
49 wxAny wxObjectToVariantConverterwxObject ( wxObject *data )
50 { return wxAny( dynamic_cast<wxObject*> (data) ) ; }
51
52 wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , wxT("wxObject"),
53 (int) sizeof(wxObject), \
54 (wxObjectConstructorFn) 0 ,
55 NULL,NULL,0 , 0 ,
56 0 , wxVariantOfPtrToObjectConverterwxObject , 0 , wxObjectToVariantConverterwxObject);
57
58 template<> void wxStringWriteValue(wxString & , wxObject* const & ){ wxFAIL_MSG("unreachable"); }
59 template<> void wxStringWriteValue(wxString & , wxObject const & ){ wxFAIL_MSG("unreachable"); }
60
61 wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject*).name() ) ;
62 wxClassTypeInfo s_typeInfowxObject(wxT_OBJECT , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject).name() ) ;
63 #else
64 wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
65 (int) sizeof(wxObject),
66 (wxObjectConstructorFn) 0 );
67 #endif
68
69 // restore optimizations
70 #if defined __VISUALC__ && __VISUALC__ >= 1300
71 #pragma optimize("", on)
72 #endif
73
74 wxClassInfo* wxClassInfo::sm_first = NULL;
75 wxHashTable* wxClassInfo::sm_classTable = NULL;
76
77 // when using XTI, this method is already implemented inline inside
78 // DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function
79 // non-inline because this allows us to have a non-inline virtual function in
80 // all wx classes and this solves linking problems for HP-UX native toolchain
81 // and possibly others (we could make dtor non-inline as well but it's more
82 // useful to keep it inline than this function)
83 #if !wxUSE_EXTENDED_RTTI
84
85 wxClassInfo *wxObject::GetClassInfo() const
86 {
87 return &wxObject::ms_classInfo;
88 }
89
90 #endif // wxUSE_EXTENDED_RTTI
91
92 // this variable exists only so that we can avoid 'always true/false' warnings
93 const bool wxFalse = false;
94
95 // Is this object a kind of (a subclass of) 'info'?
96 // E.g. is wxWindow a kind of wxObject?
97 // Go from this class to superclass, taking into account
98 // two possible base classes.
99 bool wxObject::IsKindOf(const wxClassInfo *info) const
100 {
101 const wxClassInfo *thisInfo = GetClassInfo();
102 return (thisInfo) ? thisInfo->IsKindOf(info) : false ;
103 }
104
105 #if wxUSE_MEMORY_TRACING && defined( new )
106 #undef new
107 #endif
108
109
110 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
111 void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
112 {
113 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true);
114 }
115 #endif
116
117 #ifdef _WX_WANT_DELETE_VOID
118 void wxObject::operator delete ( void *buf )
119 {
120 wxDebugFree(buf);
121 }
122 #endif
123
124 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
125 void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
126 {
127 wxDebugFree(buf);
128 }
129 #endif
130
131 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
132 void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
133 {
134 wxDebugFree(buf);
135 }
136 #endif
137
138 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
139 void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
140 {
141 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true);
142 }
143 #endif
144
145 #ifdef _WX_WANT_ARRAY_DELETE_VOID
146 void wxObject::operator delete[] ( void *buf )
147 {
148 wxDebugFree(buf, true);
149 }
150 #endif
151
152 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
153 void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
154 {
155 wxDebugFree(buf, true);
156 }
157 #endif
158
159
160 // ----------------------------------------------------------------------------
161 // wxClassInfo
162 // ----------------------------------------------------------------------------
163
164 wxClassInfo::~wxClassInfo()
165 {
166 // remove this object from the linked list of all class infos: if we don't
167 // do it, loading/unloading a DLL containing static wxClassInfo objects is
168 // not going to work
169 if ( this == sm_first )
170 {
171 sm_first = m_next;
172 }
173 else
174 {
175 wxClassInfo *info = sm_first;
176 while (info)
177 {
178 if ( info->m_next == this )
179 {
180 info->m_next = m_next;
181 break;
182 }
183
184 info = info->m_next;
185 }
186 }
187 Unregister();
188 }
189
190 wxClassInfo *wxClassInfo::FindClass(const wxString& className)
191 {
192 if ( sm_classTable )
193 {
194 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
195 }
196 else
197 {
198 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
199 {
200 if ( className == info->GetClassName() )
201 return info;
202 }
203
204 return NULL;
205 }
206 }
207
208 // Reentrance can occur on some platforms (Solaris for one), as the use of hash
209 // and string objects can cause other modules to load and register classes
210 // before the original call returns. This is handled by keeping the hash table
211 // local when it is first created and only assigning it to the global variable
212 // when the function is ready to return.
213 //
214 // That does make the assumption that after the function has completed the
215 // first time the problem will no longer happen; all the modules it depends on
216 // will have been loaded. The assumption is checked using the 'entry' variable
217 // as a reentrance guard, it checks that once the hash table is global it is
218 // not accessed multiple times simulateously.
219
220 void wxClassInfo::Register()
221 {
222 #if wxDEBUG_LEVEL
223 // reentrance guard - see note above
224 static int entry = 0;
225 #endif // wxDEBUG_LEVEL
226
227 wxHashTable *classTable;
228
229 if ( !sm_classTable )
230 {
231 // keep the hash local initially, reentrance is possible
232 classTable = new wxHashTable(wxKEY_STRING);
233 }
234 else
235 {
236 // guard againt reentrance once the global has been created
237 wxASSERT_MSG(++entry == 1, wxT("wxClassInfo::Register() reentrance"));
238 classTable = sm_classTable;
239 }
240
241 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
242 // link any object module twice mistakenly, or link twice against wx shared
243 // library) will break this function because it will enter an infinite loop
244 // and eventually die with "out of memory" - as this is quite hard to
245 // detect if you're unaware of this, try to do some checks here.
246 wxASSERT_MSG( classTable->Get(m_className) == NULL,
247 wxString::Format
248 (
249 wxT("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
250 m_className
251 )
252 );
253
254 classTable->Put(m_className, (wxObject *)this);
255
256 // if we're using a local hash we need to try to make it global
257 if ( sm_classTable != classTable )
258 {
259 if ( !sm_classTable )
260 {
261 // make the hash global
262 sm_classTable = classTable;
263 }
264 else
265 {
266 // the gobal hash has already been created by a reentrant call,
267 // so delete the local hash and try again
268 delete classTable;
269 Register();
270 }
271 }
272
273 #if wxDEBUG_LEVEL
274 entry = 0;
275 #endif // wxDEBUG_LEVEL
276 }
277
278 void wxClassInfo::Unregister()
279 {
280 if ( sm_classTable )
281 {
282 sm_classTable->Delete(m_className);
283 if ( sm_classTable->GetCount() == 0 )
284 {
285 wxDELETE(sm_classTable);
286 }
287 }
288 }
289
290 wxObject *wxCreateDynamicObject(const wxString& name)
291 {
292 #if wxUSE_DEBUG_CONTEXT
293 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
294 #endif
295
296 if ( wxClassInfo::sm_classTable )
297 {
298 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
299 return info ? info->CreateObject() : NULL;
300 }
301 else // no sm_classTable yet
302 {
303 for ( wxClassInfo *info = wxClassInfo::sm_first;
304 info;
305 info = info->m_next )
306 {
307 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
308 return info->CreateObject();
309 }
310
311 return NULL;
312 }
313 }
314
315 // iterator interface
316 wxClassInfo::const_iterator::value_type
317 wxClassInfo::const_iterator::operator*() const
318 {
319 return (wxClassInfo*)m_node->GetData();
320 }
321
322 wxClassInfo::const_iterator& wxClassInfo::const_iterator::operator++()
323 {
324 m_node = m_table->Next();
325 return *this;
326 }
327
328 const wxClassInfo::const_iterator wxClassInfo::const_iterator::operator++(int)
329 {
330 wxClassInfo::const_iterator tmp = *this;
331 m_node = m_table->Next();
332 return tmp;
333 }
334
335 wxClassInfo::const_iterator wxClassInfo::begin_classinfo()
336 {
337 sm_classTable->BeginFind();
338
339 return const_iterator(sm_classTable->Next(), sm_classTable);
340 }
341
342 wxClassInfo::const_iterator wxClassInfo::end_classinfo()
343 {
344 return const_iterator(NULL, NULL);
345 }
346
347 // ----------------------------------------------------------------------------
348 // wxObjectRefData
349 // ----------------------------------------------------------------------------
350
351 void wxRefCounter::DecRef()
352 {
353 wxASSERT_MSG( m_count > 0, "invalid ref data count" );
354
355 if ( --m_count == 0 )
356 delete this;
357 }
358
359
360 // ----------------------------------------------------------------------------
361 // wxObject
362 // ----------------------------------------------------------------------------
363
364 void wxObject::Ref(const wxObject& clone)
365 {
366 #if wxUSE_DEBUG_CONTEXT
367 DEBUG_PRINTF(wxObject::Ref)
368 #endif
369
370 // nothing to be done
371 if (m_refData == clone.m_refData)
372 return;
373
374 // delete reference to old data
375 UnRef();
376
377 // reference new data
378 if ( clone.m_refData )
379 {
380 m_refData = clone.m_refData;
381 m_refData->IncRef();
382 }
383 }
384
385 void wxObject::UnRef()
386 {
387 if ( m_refData )
388 {
389 m_refData->DecRef();
390 m_refData = NULL;
391 }
392 }
393
394 void wxObject::AllocExclusive()
395 {
396 if ( !m_refData )
397 {
398 m_refData = CreateRefData();
399 }
400 else if ( m_refData->GetRefCount() > 1 )
401 {
402 // note that ref is not going to be destroyed in this case
403 const wxObjectRefData* ref = m_refData;
404 UnRef();
405
406 // ... so we can still access it
407 m_refData = CloneRefData(ref);
408 }
409 //else: ref count is 1, we are exclusive owners of m_refData anyhow
410
411 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
412 wxT("wxObject::AllocExclusive() failed.") );
413 }
414
415 wxObjectRefData *wxObject::CreateRefData() const
416 {
417 // if you use AllocExclusive() you must override this method
418 wxFAIL_MSG( wxT("CreateRefData() must be overridden if called!") );
419
420 return NULL;
421 }
422
423 wxObjectRefData *
424 wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
425 {
426 // if you use AllocExclusive() you must override this method
427 wxFAIL_MSG( wxT("CloneRefData() must be overridden if called!") );
428
429 return NULL;
430 }