]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
Fix wxHtmlHelpData::SetTempDir() to behave correctly without trailing slash.
[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 6// Created: 04/01/98
55d99c7a 7// Copyright: (c) 1998 Julian Smart
0b9ab0bd 8// (c) 2001 Ron Lee <ron@debian.org>
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
b2edef6f 12// For compilers that support precompilation, includes "wx.h".
c801d85f
KB
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
8e3f3880 16 #pragma hdrstop
c801d85f
KB
17#endif
18
1678ad78 19#ifndef WX_PRECOMP
df5168c4 20 #include "wx/object.h"
8e3f3880 21 #include "wx/hash.h"
5b56bffb 22 #include "wx/memory.h"
0bf751e7 23 #include "wx/crt.h"
24eb81cb
DW
24#endif
25
c801d85f 26#include <string.h>
c801d85f 27
4b6a582b 28#if wxUSE_DEBUG_CONTEXT
b2edef6f
VZ
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
4b6a582b 35#endif // wxUSE_DEBUG_CONTEXT
c801d85f 36
e1720942
VZ
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
fbae49ee 40#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
41 #pragma optimize("", off)
42#endif
f6bcfd97 43
30bfc425 44#if wxUSE_EXTENDED_RTTI
05fa251a 45const wxClassInfo* wxObject::ms_classParents[] = { NULL } ;
e765d7ee
SC
46wxObject* wxVariantOfPtrToObjectConverterwxObject ( const wxAny &data )
47{ return wxANY_AS(data, wxObject*); }
48 wxAny wxObjectToVariantConverterwxObject ( wxObject *data )
49 { return wxAny( dynamic_cast<wxObject*> (data) ) ; }
50
525d8583 51 wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , wxT("wxObject"),
a095505c 52 (int) sizeof(wxObject), \
431346ff 53 (wxObjectConstructorFn) 0 ,
d3b9f782 54 NULL,NULL,0 , 0 ,
e765d7ee
SC
55 0 , wxVariantOfPtrToObjectConverterwxObject , 0 , wxObjectToVariantConverterwxObject);
56
2e587bb9 57 template<> void wxStringWriteValue(wxString & , wxObject* const & ){ wxFAIL_MSG("unreachable"); }
2e587bb9 58 template<> void wxStringWriteValue(wxString & , wxObject const & ){ wxFAIL_MSG("unreachable"); }
e765d7ee 59
7e548f6b
WS
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() ) ;
a095505c 62#else
c0db9626 63wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
0b9ab0bd
RL
64 (int) sizeof(wxObject),
65 (wxObjectConstructorFn) 0 );
a095505c 66#endif
c801d85f 67
e1720942 68// restore optimizations
fbae49ee 69#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
70 #pragma optimize("", on)
71#endif
72
b2edef6f
VZ
73wxClassInfo* wxClassInfo::sm_first = NULL;
74wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 75
66d2fa18
VZ
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
14ca93a0
VZ
84wxClassInfo *wxObject::GetClassInfo() const
85{
86 return &wxObject::ms_classInfo;
87}
88
66d2fa18
VZ
89#endif // wxUSE_EXTENDED_RTTI
90
258e7b25 91// this variable exists only so that we can avoid 'always true/false' warnings
7e548f6b 92const bool wxFalse = false;
c801d85f 93
b2edef6f
VZ
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.
303c6f20 98bool wxObject::IsKindOf(const wxClassInfo *info) const
c801d85f 99{
303c6f20 100 const wxClassInfo *thisInfo = GetClassInfo();
7e548f6b 101 return (thisInfo) ? thisInfo->IsKindOf(info) : false ;
c801d85f
KB
102}
103
4b6a582b 104#if wxUSE_MEMORY_TRACING && defined( new )
7e548f6b 105 #undef new
cf760e4c
VZ
106#endif
107
108
b2edef6f 109#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 110void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 111{
7e548f6b 112 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true);
c801d85f 113}
b2edef6f 114#endif
c801d85f 115
b2edef6f
VZ
116#ifdef _WX_WANT_DELETE_VOID
117void wxObject::operator delete ( void *buf )
415ca026
DW
118{
119 wxDebugFree(buf);
120}
b2edef6f
VZ
121#endif
122
123#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
124void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 125{
afb74891 126 wxDebugFree(buf);
c801d85f 127}
0b9ab0bd
RL
128#endif
129
b2edef6f 130#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 131void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 132{
b2edef6f 133 wxDebugFree(buf);
76626af2
JS
134}
135#endif
136
b2edef6f 137#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 138void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 139{
7e548f6b 140 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true);
c801d85f 141}
b2edef6f 142#endif
c801d85f 143
b2edef6f
VZ
144#ifdef _WX_WANT_ARRAY_DELETE_VOID
145void wxObject::operator delete[] ( void *buf )
c801d85f 146{
7e548f6b 147 wxDebugFree(buf, true);
c801d85f
KB
148}
149#endif
150
b2edef6f 151#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 152void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f 153{
7e548f6b 154 wxDebugFree(buf, true);
b2edef6f
VZ
155}
156#endif
afb74891 157
afb74891 158
0b9ab0bd
RL
159// ----------------------------------------------------------------------------
160// wxClassInfo
161// ----------------------------------------------------------------------------
c801d85f 162
aa4b7ef9
VZ
163wxClassInfo::~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 }
7e548f6b 186 Unregister();
aa4b7ef9
VZ
187}
188
c1e10a22 189wxClassInfo *wxClassInfo::FindClass(const wxString& className)
c801d85f 190{
80e8062f
VZ
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 {
c1e10a22 199 if ( className == info->GetClassName() )
80e8062f
VZ
200 return info;
201 }
202
203 return NULL;
204 }
c801d85f
KB
205}
206
52798331
MW
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.
f5256c59 218
d1d738f1
VS
219void wxClassInfo::Register()
220{
4b6a582b 221#if wxDEBUG_LEVEL
f5256c59
MW
222 // reentrance guard - see note above
223 static int entry = 0;
4b6a582b 224#endif // wxDEBUG_LEVEL
f5256c59 225
52798331
MW
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
9a83f860 236 wxASSERT_MSG(++entry == 1, wxT("wxClassInfo::Register() reentrance"));
52798331
MW
237 classTable = sm_classTable;
238 }
239
d9f0e932
VS
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.
52798331 245 wxASSERT_MSG( classTable->Get(m_className) == NULL,
47547fbe
VZ
246 wxString::Format
247 (
9a83f860 248 wxT("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
47547fbe
VZ
249 m_className
250 )
251 );
7e548f6b 252
52798331
MW
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 }
f5256c59 271
4b6a582b 272#if wxDEBUG_LEVEL
52798331 273 entry = 0;
4b6a582b 274#endif // wxDEBUG_LEVEL
c801d85f
KB
275}
276
d1d738f1 277void wxClassInfo::Unregister()
c801d85f 278{
d1d738f1
VS
279 if ( sm_classTable )
280 {
281 sm_classTable->Delete(m_className);
cafc76a4
VS
282 if ( sm_classTable->GetCount() == 0 )
283 {
5276b0a5 284 wxDELETE(sm_classTable);
cafc76a4 285 }
d1d738f1 286 }
0c32066b 287}
f4a8c29f 288
c1e10a22 289wxObject *wxCreateDynamicObject(const wxString& name)
0c32066b 290{
4b6a582b 291#if wxUSE_DEBUG_CONTEXT
b2edef6f 292 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 293#endif
61cca9d2 294
b2edef6f 295 if ( wxClassInfo::sm_classTable )
0c32066b
JS
296 {
297 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 298 return info ? info->CreateObject() : NULL;
0c32066b 299 }
b2edef6f 300 else // no sm_classTable yet
0c32066b 301 {
b2edef6f
VZ
302 for ( wxClassInfo *info = wxClassInfo::sm_first;
303 info;
304 info = info->m_next )
305 {
cf2f341a 306 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 307 return info->CreateObject();
b2edef6f
VZ
308 }
309
310 return NULL;
0c32066b 311 }
c801d85f
KB
312}
313
644cb537
MB
314// iterator interface
315wxClassInfo::const_iterator::value_type
316wxClassInfo::const_iterator::operator*() const
317{
318 return (wxClassInfo*)m_node->GetData();
319}
320
321wxClassInfo::const_iterator& wxClassInfo::const_iterator::operator++()
322{
323 m_node = m_table->Next();
324 return *this;
325}
326
327const 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
334wxClassInfo::const_iterator wxClassInfo::begin_classinfo()
335{
336 sm_classTable->BeginFind();
337
338 return const_iterator(sm_classTable->Next(), sm_classTable);
339}
340
341wxClassInfo::const_iterator wxClassInfo::end_classinfo()
342{
343 return const_iterator(NULL, NULL);
344}
7a4b9130 345
4a11340a
RR
346// ----------------------------------------------------------------------------
347// wxObjectRefData
348// ----------------------------------------------------------------------------
349
deff6d72 350void wxRefCounter::DecRef()
4a11340a 351{
deff6d72
VZ
352 wxASSERT_MSG( m_count > 0, "invalid ref data count" );
353
4a11340a
RR
354 if ( --m_count == 0 )
355 delete this;
356}
357
358
0b9ab0bd 359// ----------------------------------------------------------------------------
a6391d30 360// wxObject
0b9ab0bd 361// ----------------------------------------------------------------------------
c801d85f
KB
362
363void wxObject::Ref(const wxObject& clone)
364{
4b6a582b 365#if wxUSE_DEBUG_CONTEXT
807d8487 366 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 367#endif
0b9ab0bd 368
c89f5c02
RR
369 // nothing to be done
370 if (m_refData == clone.m_refData)
371 return;
372
0b9ab0bd 373 // delete reference to old data
c801d85f 374 UnRef();
0b9ab0bd 375
c801d85f 376 // reference new data
807d8487 377 if ( clone.m_refData )
0b9ab0bd 378 {
c801d85f 379 m_refData = clone.m_refData;
4a11340a 380 m_refData->IncRef();
c801d85f
KB
381 }
382}
383
afb74891 384void wxObject::UnRef()
c801d85f 385{
807d8487 386 if ( m_refData )
4fe5383d 387 {
4a11340a 388 m_refData->DecRef();
807d8487 389 m_refData = NULL;
c801d85f 390 }
c801d85f
KB
391}
392
807d8487
VZ
393void 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
a9bf8315 402 const wxObjectRefData* ref = m_refData;
807d8487
VZ
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,
9a83f860 411 wxT("wxObject::AllocExclusive() failed.") );
807d8487
VZ
412}
413
414wxObjectRefData *wxObject::CreateRefData() const
415{
d1870078 416 // if you use AllocExclusive() you must override this method
9a83f860 417 wxFAIL_MSG( wxT("CreateRefData() must be overridden if called!") );
d1870078 418
807d8487
VZ
419 return NULL;
420}
421
a9bf8315
VZ
422wxObjectRefData *
423wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 424{
d1870078 425 // if you use AllocExclusive() you must override this method
9a83f860 426 wxFAIL_MSG( wxT("CloneRefData() must be overridden if called!") );
d1870078 427
807d8487
VZ
428 return NULL;
429}