]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/object.cpp
Allow user code to override key events in generic wxListCtrl.
[wxWidgets.git] / src / common / object.cpp
... / ...
CommitLineData
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
46const 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
65wxClassInfo 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
75wxClassInfo* wxClassInfo::sm_first = NULL;
76wxHashTable* 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
86wxClassInfo *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
94const 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.
100bool 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
112void *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
119void wxObject::operator delete ( void *buf )
120{
121 wxDebugFree(buf);
122}
123#endif
124
125#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
126void 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
133void 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
140void *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
147void wxObject::operator delete[] ( void *buf )
148{
149 wxDebugFree(buf, true);
150}
151#endif
152
153#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
154void 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
165wxClassInfo::~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
191wxClassInfo *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
221void 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
279void wxClassInfo::Unregister()
280{
281 if ( sm_classTable )
282 {
283 sm_classTable->Delete(m_className);
284 if ( sm_classTable->GetCount() == 0 )
285 {
286 wxDELETE(sm_classTable);
287 }
288 }
289}
290
291wxObject *wxCreateDynamicObject(const wxString& name)
292{
293#if wxUSE_DEBUG_CONTEXT
294 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
295#endif
296
297 if ( wxClassInfo::sm_classTable )
298 {
299 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
300 return info ? info->CreateObject() : NULL;
301 }
302 else // no sm_classTable yet
303 {
304 for ( wxClassInfo *info = wxClassInfo::sm_first;
305 info;
306 info = info->m_next )
307 {
308 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
309 return info->CreateObject();
310 }
311
312 return NULL;
313 }
314}
315
316// iterator interface
317wxClassInfo::const_iterator::value_type
318wxClassInfo::const_iterator::operator*() const
319{
320 return (wxClassInfo*)m_node->GetData();
321}
322
323wxClassInfo::const_iterator& wxClassInfo::const_iterator::operator++()
324{
325 m_node = m_table->Next();
326 return *this;
327}
328
329const wxClassInfo::const_iterator wxClassInfo::const_iterator::operator++(int)
330{
331 wxClassInfo::const_iterator tmp = *this;
332 m_node = m_table->Next();
333 return tmp;
334}
335
336wxClassInfo::const_iterator wxClassInfo::begin_classinfo()
337{
338 sm_classTable->BeginFind();
339
340 return const_iterator(sm_classTable->Next(), sm_classTable);
341}
342
343wxClassInfo::const_iterator wxClassInfo::end_classinfo()
344{
345 return const_iterator(NULL, NULL);
346}
347
348// ----------------------------------------------------------------------------
349// wxObjectRefData
350// ----------------------------------------------------------------------------
351
352void wxRefCounter::DecRef()
353{
354 wxASSERT_MSG( m_count > 0, "invalid ref data count" );
355
356 if ( --m_count == 0 )
357 delete this;
358}
359
360
361// ----------------------------------------------------------------------------
362// wxObject
363// ----------------------------------------------------------------------------
364
365void wxObject::Ref(const wxObject& clone)
366{
367#if wxUSE_DEBUG_CONTEXT
368 DEBUG_PRINTF(wxObject::Ref)
369#endif
370
371 // nothing to be done
372 if (m_refData == clone.m_refData)
373 return;
374
375 // delete reference to old data
376 UnRef();
377
378 // reference new data
379 if ( clone.m_refData )
380 {
381 m_refData = clone.m_refData;
382 m_refData->IncRef();
383 }
384}
385
386void wxObject::UnRef()
387{
388 if ( m_refData )
389 {
390 m_refData->DecRef();
391 m_refData = NULL;
392 }
393}
394
395void wxObject::AllocExclusive()
396{
397 if ( !m_refData )
398 {
399 m_refData = CreateRefData();
400 }
401 else if ( m_refData->GetRefCount() > 1 )
402 {
403 // note that ref is not going to be destroyed in this case
404 const wxObjectRefData* ref = m_refData;
405 UnRef();
406
407 // ... so we can still access it
408 m_refData = CloneRefData(ref);
409 }
410 //else: ref count is 1, we are exclusive owners of m_refData anyhow
411
412 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
413 wxT("wxObject::AllocExclusive() failed.") );
414}
415
416wxObjectRefData *wxObject::CreateRefData() const
417{
418 // if you use AllocExclusive() you must override this method
419 wxFAIL_MSG( wxT("CreateRefData() must be overridden if called!") );
420
421 return NULL;
422}
423
424wxObjectRefData *
425wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
426{
427 // if you use AllocExclusive() you must override this method
428 wxFAIL_MSG( wxT("CloneRefData() must be overridden if called!") );
429
430 return NULL;
431}