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