]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
gcc signed/unsigned warning 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"
24eb81cb
DW
24#endif
25
c801d85f 26#include <string.h>
c801d85f 27
ea57084d 28#if defined(__WXDEBUG__) || 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
35#endif // __WXDEBUG__ || 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 } ;
431346ff 46 wxObject* wxVariantToObjectConverterwxObject ( wxxVariant &data )
2c4c3987 47{ return data.wxTEMPLATED_MEMBER_CALL(Get , wxObject*) ; }
431346ff 48 wxObject* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant &data )
2c4c3987 49{ return &data.wxTEMPLATED_MEMBER_CALL(Get , wxObject) ; }
431346ff 50 wxxVariant wxObjectToVariantConverterwxObject ( wxObject *data )
a095505c 51 { return wxxVariant( dynamic_cast<wxObject*> (data) ) ; }
525d8583 52 wxClassInfo wxObject::ms_classInfo(ms_classParents , wxEmptyString , wxT("wxObject"),
a095505c 53 (int) sizeof(wxObject), \
431346ff 54 (wxObjectConstructorFn) 0 ,
7e548f6b
WS
55 (wxPropertyInfo*) NULL,(wxHandlerInfo*) NULL,0 , 0 ,
56 0 , wxVariantOfPtrToObjectConverterwxObject , wxVariantToObjectConverterwxObject , wxObjectToVariantConverterwxObject);
a095505c
SC
57 template<> void wxStringReadValue(const wxString & , wxObject * & ){assert(0) ;}
58 template<> void wxStringWriteValue(wxString & , wxObject* const & ){assert(0) ;}
431346ff
SC
59 template<> void wxStringReadValue(const wxString & , wxObject & ){assert(0) ;}
60 template<> void wxStringWriteValue(wxString & , wxObject const & ){assert(0) ;}
7e548f6b
WS
61 wxClassTypeInfo s_typeInfo(wxT_OBJECT_PTR , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject*).name() ) ;
62 wxClassTypeInfo s_typeInfowxObject(wxT_OBJECT , &wxObject::ms_classInfo , NULL , NULL , typeid(wxObject).name() ) ;
a095505c 63#else
c0db9626 64wxClassInfo wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
0b9ab0bd
RL
65 (int) sizeof(wxObject),
66 (wxObjectConstructorFn) 0 );
a095505c 67#endif
c801d85f 68
e1720942 69// restore optimizations
fbae49ee 70#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
71 #pragma optimize("", on)
72#endif
73
b2edef6f
VZ
74wxClassInfo* wxClassInfo::sm_first = NULL;
75wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 76
66d2fa18
VZ
77// when using XTI, this method is already implemented inline inside
78// DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function
79// non-inline because this allows us to have a non-inline virtual function in
80// all wx classes and this solves linking problems for HP-UX native toolchain
81// and possibly others (we could make dtor non-inline as well but it's more
82// useful to keep it inline than this function)
83#if !wxUSE_EXTENDED_RTTI
84
14ca93a0
VZ
85wxClassInfo *wxObject::GetClassInfo() const
86{
87 return &wxObject::ms_classInfo;
88}
89
66d2fa18
VZ
90#endif // wxUSE_EXTENDED_RTTI
91
258e7b25 92// this variable exists only so that we can avoid 'always true/false' warnings
7e548f6b 93const bool wxFalse = false;
c801d85f 94
b2edef6f
VZ
95// Is this object a kind of (a subclass of) 'info'?
96// E.g. is wxWindow a kind of wxObject?
97// Go from this class to superclass, taking into account
98// two possible base classes.
341287bf 99bool wxObject::IsKindOf(wxClassInfo *info) const
c801d85f 100{
afb74891 101 wxClassInfo *thisInfo = GetClassInfo();
7e548f6b 102 return (thisInfo) ? thisInfo->IsKindOf(info) : false ;
c801d85f
KB
103}
104
cf760e4c 105#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
7e548f6b 106 #undef new
cf760e4c
VZ
107#endif
108
109
b2edef6f 110#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 111void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 112{
7e548f6b 113 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true);
c801d85f 114}
b2edef6f 115#endif
c801d85f 116
b2edef6f
VZ
117#ifdef _WX_WANT_DELETE_VOID
118void wxObject::operator delete ( void *buf )
415ca026
DW
119{
120 wxDebugFree(buf);
121}
b2edef6f
VZ
122#endif
123
124#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
125void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 126{
afb74891 127 wxDebugFree(buf);
c801d85f 128}
0b9ab0bd
RL
129#endif
130
b2edef6f 131#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 132void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 133{
b2edef6f 134 wxDebugFree(buf);
76626af2
JS
135}
136#endif
137
b2edef6f 138#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 139void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 140{
7e548f6b 141 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, true, true);
c801d85f 142}
b2edef6f 143#endif
c801d85f 144
b2edef6f
VZ
145#ifdef _WX_WANT_ARRAY_DELETE_VOID
146void wxObject::operator delete[] ( void *buf )
c801d85f 147{
7e548f6b 148 wxDebugFree(buf, true);
c801d85f
KB
149}
150#endif
151
b2edef6f 152#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 153void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f 154{
7e548f6b 155 wxDebugFree(buf, true);
b2edef6f
VZ
156}
157#endif
afb74891 158
afb74891 159
0b9ab0bd
RL
160// ----------------------------------------------------------------------------
161// wxClassInfo
162// ----------------------------------------------------------------------------
c801d85f 163
aa4b7ef9
VZ
164wxClassInfo::~wxClassInfo()
165{
166 // remove this object from the linked list of all class infos: if we don't
167 // do it, loading/unloading a DLL containing static wxClassInfo objects is
168 // not going to work
169 if ( this == sm_first )
170 {
171 sm_first = m_next;
172 }
173 else
174 {
175 wxClassInfo *info = sm_first;
176 while (info)
177 {
178 if ( info->m_next == this )
179 {
180 info->m_next = m_next;
181 break;
182 }
183
184 info = info->m_next;
185 }
186 }
7e548f6b 187 Unregister();
aa4b7ef9
VZ
188}
189
0b9ab0bd 190wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
c801d85f 191{
80e8062f
VZ
192 if ( sm_classTable )
193 {
194 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
195 }
196 else
197 {
198 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
199 {
200 if ( wxStrcmp(info->GetClassName(), className) == 0 )
201 return info;
202 }
203
204 return NULL;
205 }
c801d85f
KB
206}
207
f5256c59
MW
208// This function wasn't written to be reentrant but there is a possiblity of
209// reentrance if something it does causes a shared lib to load and register
210// classes. On Solaris this happens when the wxHashTable is newed, so the first
211// part of the function has been modified to handle it, and a wxASSERT checks
212// against reentrance in the remainder of the function.
213
d1d738f1
VS
214void wxClassInfo::Register()
215{
cafc76a4 216 if ( !sm_classTable )
afb74891 217 {
f5256c59
MW
218 wxHashTable *classTable = new wxHashTable(wxKEY_STRING);
219
220 // check for reentrance
221 if ( sm_classTable )
222 delete classTable;
223 else
224 sm_classTable = classTable;
afb74891 225 }
cafc76a4 226
0b850f25 227#ifdef __WXDEBUG__
f5256c59
MW
228 // reentrance guard - see note above
229 static int entry = 0;
230 wxASSERT_MSG(++entry == 1, _T("wxClassInfo::Register() reentrance"));
0b850f25 231#endif
f5256c59 232
d9f0e932
VS
233 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
234 // link any object module twice mistakenly, or link twice against wx shared
235 // library) will break this function because it will enter an infinite loop
236 // and eventually die with "out of memory" - as this is quite hard to
237 // detect if you're unaware of this, try to do some checks here.
cafc76a4 238 wxASSERT_MSG( sm_classTable->Get(m_className) == NULL,
47547fbe
VZ
239 wxString::Format
240 (
f730d32b 241 _T("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
47547fbe
VZ
242 m_className
243 )
244 );
7e548f6b 245
cafc76a4 246 sm_classTable->Put(m_className, (wxObject *)this);
f5256c59 247
0b850f25 248#ifdef __WXDEBUG__
f5256c59 249 --entry;
0b850f25 250#endif
c801d85f
KB
251}
252
d1d738f1 253void wxClassInfo::Unregister()
c801d85f 254{
d1d738f1
VS
255 if ( sm_classTable )
256 {
257 sm_classTable->Delete(m_className);
cafc76a4
VS
258 if ( sm_classTable->GetCount() == 0 )
259 {
260 delete sm_classTable;
261 sm_classTable = NULL;
262 }
d1d738f1 263 }
0c32066b 264}
f4a8c29f 265
cf2f341a 266wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 267{
bbee61e5 268#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 269 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 270#endif
61cca9d2 271
b2edef6f 272 if ( wxClassInfo::sm_classTable )
0c32066b
JS
273 {
274 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 275 return info ? info->CreateObject() : NULL;
0c32066b 276 }
b2edef6f 277 else // no sm_classTable yet
0c32066b 278 {
b2edef6f
VZ
279 for ( wxClassInfo *info = wxClassInfo::sm_first;
280 info;
281 info = info->m_next )
282 {
cf2f341a 283 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 284 return info->CreateObject();
b2edef6f
VZ
285 }
286
287 return NULL;
0c32066b 288 }
c801d85f
KB
289}
290
7a4b9130 291
4a11340a
RR
292// ----------------------------------------------------------------------------
293// wxObjectRefData
294// ----------------------------------------------------------------------------
295
296void wxObjectRefData::DecRef()
297{
298 if ( --m_count == 0 )
299 delete this;
300}
301
302
0b9ab0bd 303// ----------------------------------------------------------------------------
a6391d30 304// wxObject
0b9ab0bd 305// ----------------------------------------------------------------------------
c801d85f
KB
306
307void wxObject::Ref(const wxObject& clone)
308{
bbee61e5 309#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 310 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 311#endif
0b9ab0bd 312
c89f5c02
RR
313 // nothing to be done
314 if (m_refData == clone.m_refData)
315 return;
316
0b9ab0bd 317 // delete reference to old data
c801d85f 318 UnRef();
0b9ab0bd 319
c801d85f 320 // reference new data
807d8487 321 if ( clone.m_refData )
0b9ab0bd 322 {
c801d85f 323 m_refData = clone.m_refData;
4a11340a 324 m_refData->IncRef();
c801d85f
KB
325 }
326}
327
afb74891 328void wxObject::UnRef()
c801d85f 329{
807d8487 330 if ( m_refData )
4fe5383d
VZ
331 {
332 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
333
4a11340a 334 m_refData->DecRef();
807d8487 335 m_refData = NULL;
c801d85f 336 }
c801d85f
KB
337}
338
807d8487
VZ
339void wxObject::AllocExclusive()
340{
341 if ( !m_refData )
342 {
343 m_refData = CreateRefData();
344 }
345 else if ( m_refData->GetRefCount() > 1 )
346 {
347 // note that ref is not going to be destroyed in this case
a9bf8315 348 const wxObjectRefData* ref = m_refData;
807d8487
VZ
349 UnRef();
350
351 // ... so we can still access it
352 m_refData = CloneRefData(ref);
353 }
354 //else: ref count is 1, we are exclusive owners of m_refData anyhow
355
356 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
357 _T("wxObject::AllocExclusive() failed.") );
358}
359
360wxObjectRefData *wxObject::CreateRefData() const
361{
d1870078
VZ
362 // if you use AllocExclusive() you must override this method
363 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
364
807d8487
VZ
365 return NULL;
366}
367
a9bf8315
VZ
368wxObjectRefData *
369wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 370{
d1870078
VZ
371 // if you use AllocExclusive() you must override this method
372 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
373
807d8487
VZ
374 return NULL;
375}