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