]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
don't crash on weird line endings like \r\r\n
[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
0b9ab0bd 292// ----------------------------------------------------------------------------
a6391d30 293// wxObject
0b9ab0bd 294// ----------------------------------------------------------------------------
c801d85f
KB
295
296void wxObject::Ref(const wxObject& clone)
297{
bbee61e5 298#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 299 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 300#endif
0b9ab0bd 301
c89f5c02
RR
302 // nothing to be done
303 if (m_refData == clone.m_refData)
304 return;
305
0b9ab0bd 306 // delete reference to old data
c801d85f 307 UnRef();
0b9ab0bd 308
c801d85f 309 // reference new data
807d8487 310 if ( clone.m_refData )
0b9ab0bd 311 {
c801d85f
KB
312 m_refData = clone.m_refData;
313 ++(m_refData->m_count);
314 }
315}
316
afb74891 317void wxObject::UnRef()
c801d85f 318{
807d8487 319 if ( m_refData )
4fe5383d
VZ
320 {
321 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
322
8d7eaf91 323 if ( --m_refData->m_count == 0 )
c801d85f 324 delete m_refData;
807d8487 325 m_refData = NULL;
c801d85f 326 }
c801d85f
KB
327}
328
807d8487
VZ
329void wxObject::AllocExclusive()
330{
331 if ( !m_refData )
332 {
333 m_refData = CreateRefData();
334 }
335 else if ( m_refData->GetRefCount() > 1 )
336 {
337 // note that ref is not going to be destroyed in this case
a9bf8315 338 const wxObjectRefData* ref = m_refData;
807d8487
VZ
339 UnRef();
340
341 // ... so we can still access it
342 m_refData = CloneRefData(ref);
343 }
344 //else: ref count is 1, we are exclusive owners of m_refData anyhow
345
346 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
347 _T("wxObject::AllocExclusive() failed.") );
348}
349
350wxObjectRefData *wxObject::CreateRefData() const
351{
d1870078
VZ
352 // if you use AllocExclusive() you must override this method
353 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
354
807d8487
VZ
355 return NULL;
356}
357
a9bf8315
VZ
358wxObjectRefData *
359wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 360{
d1870078
VZ
361 // if you use AllocExclusive() you must override this method
362 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
363
807d8487
VZ
364 return NULL;
365}