]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
no changes, just a typo fix
[wxWidgets.git] / src / common / object.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
b2edef6f 2// Name: src/common/object.cpp
c801d85f
KB
3// Purpose: wxObject implementation
4// Author: Julian Smart
0b9ab0bd 5// Modified by: Ron Lee
c801d85f
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
55d99c7a 8// Copyright: (c) 1998 Julian Smart
0b9ab0bd 9// (c) 2001 Ron Lee <ron@debian.org>
65571936 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
b2edef6f 13// For compilers that support precompilation, includes "wx.h".
c801d85f
KB
14#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
8e3f3880 17 #pragma hdrstop
c801d85f
KB
18#endif
19
1678ad78 20#ifndef WX_PRECOMP
df5168c4 21 #include "wx/object.h"
8e3f3880 22 #include "wx/hash.h"
5b56bffb 23 #include "wx/memory.h"
0bf751e7 24 #include "wx/crt.h"
24eb81cb
DW
25#endif
26
c801d85f 27#include <string.h>
c801d85f 28
ea57084d 29#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f
VZ
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 // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
c801d85f 37
e1720942
VZ
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
fbae49ee 41#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
42 #pragma optimize("", off)
43#endif
f6bcfd97 44
30bfc425 45#if wxUSE_EXTENDED_RTTI
05fa251a 46const wxClassInfo* wxObject::ms_classParents[] = { NULL } ;
431346ff 47 wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data )
2c4c3987 48{ return data.wxTEMPLATED_MEMBER_CALL(Get , wxObject*) ; }
431346ff 49 wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data )
2c4c3987 50{ return &data.wxTEMPLATED_MEMBER_CALL(Get , wxObject) ; }
431346ff 51 wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data )
a095505c 52 { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; }
525d8583 53 wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , wxT("wxObject"),
a095505c 54 (int) sizeof(wxObject), \
431346ff 55 (wxObjectConstructorFn) 0 ,
7e548f6b
WS
56 (wxPropertyInfo*) NULL,(wxHandlerInfo*) NULL,0 , 0 ,
57 0 , wxVariantOfPtrToObjectConverterwxObject , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject);
a095505c
SC
58 template<> void wxStringReadValue(const wxString & , wxObject * & ){assert(0) ;}
59 template<> void wxStringWriteValue(wxString & , wxObject* const & ){assert(0) ;}
431346ff
SC
60 template<> void wxStringReadValue(const wxString & , wxObject & ){assert(0) ;}
61 template<> void wxStringWriteValue(wxString & , wxObject const & ){assert(0) ;}
7e548f6b
WS
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() ) ;
a095505c 64#else
c0db9626 65wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
0b9ab0bd
RL
66 (int) sizeof(wxObject),
67 (wxObjectConstructorFn) 0 );
a095505c 68#endif
c801d85f 69
e1720942 70// restore optimizations
fbae49ee 71#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
72 #pragma optimize("", on)
73#endif
74
b2edef6f
VZ
75wxClassInfo* wxClassInfo::sm_first = NULL;
76wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 77
66d2fa18
VZ
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
14ca93a0
VZ
86wxClassInfo *wxObject::GetClassInfo() const
87{
88 return &wxObject::ms_classInfo;
89}
90
66d2fa18
VZ
91#endif // wxUSE_EXTENDED_RTTI
92
258e7b25 93// this variable exists only so that we can avoid 'always true/false' warnings
7e548f6b 94const bool wxFalse = false;
c801d85f 95
b2edef6f
VZ
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.
341287bf 100bool wxObject::IsKindOf(wxClassInfo *info) const
c801d85f 101{
afb74891 102 wxClassInfo *thisInfo = GetClassInfo();
7e548f6b 103 return (thisInfo) ? thisInfo->IsKindOf(info) : false ;
c801d85f
KB
104}
105
cf760e4c 106#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
7e548f6b 107 #undef new
cf760e4c
VZ
108#endif
109
110
b2edef6f 111#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 112void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 113{
7e548f6b 114 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true);
c801d85f 115}
b2edef6f 116#endif
c801d85f 117
b2edef6f
VZ
118#ifdef _WX_WANT_DELETE_VOID
119void wxObject::operator delete ( void *buf )
415ca026
DW
120{
121 wxDebugFree(buf);
122}
b2edef6f
VZ
123#endif
124
125#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
126void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 127{
afb74891 128 wxDebugFree(buf);
c801d85f 129}
0b9ab0bd
RL
130#endif
131
b2edef6f 132#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 133void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 134{
b2edef6f 135 wxDebugFree(buf);
76626af2
JS
136}
137#endif
138
b2edef6f 139#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 140void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 141{
7e548f6b 142 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true);
c801d85f 143}
b2edef6f 144#endif
c801d85f 145
b2edef6f
VZ
146#ifdef _WX_WANT_ARRAY_DELETE_VOID
147void wxObject::operator delete[] ( void *buf )
c801d85f 148{
7e548f6b 149 wxDebugFree(buf, true);
c801d85f
KB
150}
151#endif
152
b2edef6f 153#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 154void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f 155{
7e548f6b 156 wxDebugFree(buf, true);
b2edef6f
VZ
157}
158#endif
afb74891 159
afb74891 160
0b9ab0bd
RL
161// ----------------------------------------------------------------------------
162// wxClassInfo
163// ----------------------------------------------------------------------------
c801d85f 164
aa4b7ef9
VZ
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 }
7e548f6b 188 Unregister();
aa4b7ef9
VZ
189}
190
c1e10a22 191wxClassInfo *wxClassInfo::FindClass(const wxString& className)
c801d85f 192{
80e8062f
VZ
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 {
c1e10a22 201 if ( className == info->GetClassName() )
80e8062f
VZ
202 return info;
203 }
204
205 return NULL;
206 }
c801d85f
KB
207}
208
f5256c59
MW
209// This function wasn't written to be reentrant but there is a possiblity of
210// reentrance if something it does causes a shared lib to load and register
211// classes. On Solaris this happens when the wxHashTable is newed, so the first
212// part of the function has been modified to handle it, and a wxASSERT checks
213// against reentrance in the remainder of the function.
214
d1d738f1
VS
215void wxClassInfo::Register()
216{
cafc76a4 217 if ( !sm_classTable )
afb74891 218 {
f5256c59
MW
219 wxHashTable *classTable = new wxHashTable(wxKEY_STRING);
220
221 // check for reentrance
222 if ( sm_classTable )
223 delete classTable;
224 else
225 sm_classTable = classTable;
afb74891 226 }
cafc76a4 227
0b850f25 228#ifdef __WXDEBUG__
f5256c59
MW
229 // reentrance guard - see note above
230 static int entry = 0;
231 wxASSERT_MSG(++entry == 1, _T("wxClassInfo::Register() reentrance"));
0b850f25 232#endif
f5256c59 233
d9f0e932
VS
234 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
235 // link any object module twice mistakenly, or link twice against wx shared
236 // library) will break this function because it will enter an infinite loop
237 // and eventually die with "out of memory" - as this is quite hard to
238 // detect if you're unaware of this, try to do some checks here.
cafc76a4 239 wxASSERT_MSG( sm_classTable->Get(m_className) == NULL,
47547fbe
VZ
240 wxString::Format
241 (
f730d32b 242 _T("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
47547fbe
VZ
243 m_className
244 )
245 );
7e548f6b 246
cafc76a4 247 sm_classTable->Put(m_className, (wxObject *)this);
f5256c59 248
0b850f25 249#ifdef __WXDEBUG__
f5256c59 250 --entry;
0b850f25 251#endif
c801d85f
KB
252}
253
d1d738f1 254void wxClassInfo::Unregister()
c801d85f 255{
d1d738f1
VS
256 if ( sm_classTable )
257 {
258 sm_classTable->Delete(m_className);
cafc76a4
VS
259 if ( sm_classTable->GetCount() == 0 )
260 {
261 delete sm_classTable;
262 sm_classTable = NULL;
263 }
d1d738f1 264 }
0c32066b 265}
f4a8c29f 266
c1e10a22 267wxObject *wxCreateDynamicObject(const wxString& name)
0c32066b 268{
bbee61e5 269#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 270 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 271#endif
61cca9d2 272
b2edef6f 273 if ( wxClassInfo::sm_classTable )
0c32066b
JS
274 {
275 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 276 return info ? info->CreateObject() : NULL;
0c32066b 277 }
b2edef6f 278 else // no sm_classTable yet
0c32066b 279 {
b2edef6f
VZ
280 for ( wxClassInfo *info = wxClassInfo::sm_first;
281 info;
282 info = info->m_next )
283 {
cf2f341a 284 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 285 return info->CreateObject();
b2edef6f
VZ
286 }
287
288 return NULL;
0c32066b 289 }
c801d85f
KB
290}
291
644cb537
MB
292// iterator interface
293wxClassInfo::const_iterator::value_type
294wxClassInfo::const_iterator::operator*() const
295{
296 return (wxClassInfo*)m_node->GetData();
297}
298
299wxClassInfo::const_iterator& wxClassInfo::const_iterator::operator++()
300{
301 m_node = m_table->Next();
302 return *this;
303}
304
305const wxClassInfo::const_iterator wxClassInfo::const_iterator::operator++(int)
306{
307 wxClassInfo::const_iterator tmp = *this;
308 m_node = m_table->Next();
309 return tmp;
310}
311
312wxClassInfo::const_iterator wxClassInfo::begin_classinfo()
313{
314 sm_classTable->BeginFind();
315
316 return const_iterator(sm_classTable->Next(), sm_classTable);
317}
318
319wxClassInfo::const_iterator wxClassInfo::end_classinfo()
320{
321 return const_iterator(NULL, NULL);
322}
7a4b9130 323
4a11340a
RR
324// ----------------------------------------------------------------------------
325// wxObjectRefData
326// ----------------------------------------------------------------------------
327
328void wxObjectRefData::DecRef()
329{
330 if ( --m_count == 0 )
331 delete this;
332}
333
334
0b9ab0bd 335// ----------------------------------------------------------------------------
a6391d30 336// wxObject
0b9ab0bd 337// ----------------------------------------------------------------------------
c801d85f
KB
338
339void wxObject::Ref(const wxObject& clone)
340{
bbee61e5 341#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 342 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 343#endif
0b9ab0bd 344
c89f5c02
RR
345 // nothing to be done
346 if (m_refData == clone.m_refData)
347 return;
348
0b9ab0bd 349 // delete reference to old data
c801d85f 350 UnRef();
0b9ab0bd 351
c801d85f 352 // reference new data
807d8487 353 if ( clone.m_refData )
0b9ab0bd 354 {
c801d85f 355 m_refData = clone.m_refData;
4a11340a 356 m_refData->IncRef();
c801d85f
KB
357 }
358}
359
afb74891 360void wxObject::UnRef()
c801d85f 361{
807d8487 362 if ( m_refData )
4fe5383d
VZ
363 {
364 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
365
4a11340a 366 m_refData->DecRef();
807d8487 367 m_refData = NULL;
c801d85f 368 }
c801d85f
KB
369}
370
807d8487
VZ
371void wxObject::AllocExclusive()
372{
373 if ( !m_refData )
374 {
375 m_refData = CreateRefData();
376 }
377 else if ( m_refData->GetRefCount() > 1 )
378 {
379 // note that ref is not going to be destroyed in this case
a9bf8315 380 const wxObjectRefData* ref = m_refData;
807d8487
VZ
381 UnRef();
382
383 // ... so we can still access it
384 m_refData = CloneRefData(ref);
385 }
386 //else: ref count is 1, we are exclusive owners of m_refData anyhow
387
388 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
389 _T("wxObject::AllocExclusive() failed.") );
390}
391
392wxObjectRefData *wxObject::CreateRefData() const
393{
d1870078
VZ
394 // if you use AllocExclusive() you must override this method
395 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
396
807d8487
VZ
397 return NULL;
398}
399
a9bf8315
VZ
400wxObjectRefData *
401wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 402{
d1870078
VZ
403 // if you use AllocExclusive() you must override this method
404 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
405
807d8487
VZ
406 return NULL;
407}