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