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