]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
Rich text lib separation.
[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
d1d738f1
VS
208void wxClassInfo::Register()
209{
cafc76a4 210 if ( !sm_classTable )
afb74891 211 {
cafc76a4 212 sm_classTable = new wxHashTable(wxKEY_STRING);
afb74891 213 }
cafc76a4 214
d9f0e932
VS
215 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
216 // link any object module twice mistakenly, or link twice against wx shared
217 // library) will break this function because it will enter an infinite loop
218 // and eventually die with "out of memory" - as this is quite hard to
219 // detect if you're unaware of this, try to do some checks here.
cafc76a4 220 wxASSERT_MSG( sm_classTable->Get(m_className) == NULL,
47547fbe
VZ
221 wxString::Format
222 (
f730d32b 223 _T("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
47547fbe
VZ
224 m_className
225 )
226 );
7e548f6b 227
cafc76a4 228 sm_classTable->Put(m_className, (wxObject *)this);
c801d85f
KB
229}
230
d1d738f1 231void wxClassInfo::Unregister()
c801d85f 232{
d1d738f1
VS
233 if ( sm_classTable )
234 {
235 sm_classTable->Delete(m_className);
cafc76a4
VS
236 if ( sm_classTable->GetCount() == 0 )
237 {
238 delete sm_classTable;
239 sm_classTable = NULL;
240 }
d1d738f1 241 }
0c32066b 242}
f4a8c29f 243
cf2f341a 244wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 245{
bbee61e5 246#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 247 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 248#endif
61cca9d2 249
b2edef6f 250 if ( wxClassInfo::sm_classTable )
0c32066b
JS
251 {
252 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 253 return info ? info->CreateObject() : NULL;
0c32066b 254 }
b2edef6f 255 else // no sm_classTable yet
0c32066b 256 {
b2edef6f
VZ
257 for ( wxClassInfo *info = wxClassInfo::sm_first;
258 info;
259 info = info->m_next )
260 {
cf2f341a 261 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 262 return info->CreateObject();
b2edef6f
VZ
263 }
264
265 return NULL;
0c32066b 266 }
c801d85f
KB
267}
268
7a4b9130 269
0b9ab0bd 270// ----------------------------------------------------------------------------
a6391d30 271// wxObject
0b9ab0bd 272// ----------------------------------------------------------------------------
c801d85f
KB
273
274void wxObject::Ref(const wxObject& clone)
275{
bbee61e5 276#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 277 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 278#endif
0b9ab0bd 279
c89f5c02
RR
280 // nothing to be done
281 if (m_refData == clone.m_refData)
282 return;
283
0b9ab0bd 284 // delete reference to old data
c801d85f 285 UnRef();
0b9ab0bd 286
c801d85f 287 // reference new data
807d8487 288 if ( clone.m_refData )
0b9ab0bd 289 {
c801d85f
KB
290 m_refData = clone.m_refData;
291 ++(m_refData->m_count);
292 }
293}
294
afb74891 295void wxObject::UnRef()
c801d85f 296{
807d8487 297 if ( m_refData )
4fe5383d
VZ
298 {
299 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
300
8d7eaf91 301 if ( --m_refData->m_count == 0 )
c801d85f 302 delete m_refData;
807d8487 303 m_refData = NULL;
c801d85f 304 }
c801d85f
KB
305}
306
807d8487
VZ
307void wxObject::AllocExclusive()
308{
309 if ( !m_refData )
310 {
311 m_refData = CreateRefData();
312 }
313 else if ( m_refData->GetRefCount() > 1 )
314 {
315 // note that ref is not going to be destroyed in this case
a9bf8315 316 const wxObjectRefData* ref = m_refData;
807d8487
VZ
317 UnRef();
318
319 // ... so we can still access it
320 m_refData = CloneRefData(ref);
321 }
322 //else: ref count is 1, we are exclusive owners of m_refData anyhow
323
324 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
325 _T("wxObject::AllocExclusive() failed.") );
326}
327
328wxObjectRefData *wxObject::CreateRefData() const
329{
d1870078
VZ
330 // if you use AllocExclusive() you must override this method
331 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
332
807d8487
VZ
333 return NULL;
334}
335
a9bf8315
VZ
336wxObjectRefData *
337wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 338{
d1870078
VZ
339 // if you use AllocExclusive() you must override this method
340 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
341
807d8487
VZ
342 return NULL;
343}