]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
added support for POST method and alternate ports (part of patch 649438)
[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
13#ifdef __GNUG__
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
3f4a0c5b 36 // for wxObject::Dump
b2edef6f 37 #include "wx/ioswrap.h"
c801d85f 38
b2edef6f
VZ
39 #if defined(__VISAGECPP__)
40 #define DEBUG_PRINTF(NAME) { static int raz=0; \
41 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
42 #else
43 #define DEBUG_PRINTF(NAME)
44 #endif
45#endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
c801d85f 46
e1720942
VZ
47// we must disable optimizations for VC.NET because otherwise its too eager
48// linker discards wxClassInfo objects in release build thus breaking many,
49// many things
fbae49ee 50#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
51 #pragma optimize("", off)
52#endif
f6bcfd97 53
0b9ab0bd
RL
54wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
55 (int) sizeof(wxObject),
56 (wxObjectConstructorFn) 0 );
c801d85f 57
e1720942 58// restore optimizations
fbae49ee 59#if defined __VISUALC__ && __VISUALC__ >= 1300
e1720942
VZ
60 #pragma optimize("", on)
61#endif
62
b2edef6f
VZ
63wxClassInfo* wxClassInfo::sm_first = NULL;
64wxHashTable* wxClassInfo::sm_classTable = NULL;
c801d85f 65
b2edef6f
VZ
66// These are here so we can avoid 'always true/false' warnings
67// by referring to these instead of TRUE/FALSE
0b9ab0bd
RL
68const bool wxTrue = TRUE;
69const bool wxFalse = FALSE;
c801d85f 70
b2edef6f
VZ
71// Is this object a kind of (a subclass of) 'info'?
72// E.g. is wxWindow a kind of wxObject?
73// Go from this class to superclass, taking into account
74// two possible base classes.
341287bf 75bool wxObject::IsKindOf(wxClassInfo *info) const
c801d85f 76{
afb74891 77 wxClassInfo *thisInfo = GetClassInfo();
0b9ab0bd 78 return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ;
c801d85f
KB
79}
80
38830220 81#if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
dd107c50 82void wxObject::Dump(wxSTD ostream& str)
c801d85f 83{
afb74891
VZ
84 if (GetClassInfo() && GetClassInfo()->GetClassName())
85 str << GetClassInfo()->GetClassName();
86 else
b2edef6f 87 str << _T("unknown object class");
c801d85f
KB
88}
89#endif
90
c801d85f 91
cf760e4c
VZ
92#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
93 #undef new
94#endif
95
96
b2edef6f 97#ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
cf760e4c 98void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum )
c801d85f 99{
cf760e4c 100 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE);
c801d85f 101}
b2edef6f 102#endif
c801d85f 103
b2edef6f
VZ
104#ifdef _WX_WANT_DELETE_VOID
105void wxObject::operator delete ( void *buf )
415ca026
DW
106{
107 wxDebugFree(buf);
108}
b2edef6f
VZ
109#endif
110
111#ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
112void wxObject::operator delete ( void *buf, const char *_fname, size_t _line )
c801d85f 113{
afb74891 114 wxDebugFree(buf);
c801d85f 115}
0b9ab0bd
RL
116#endif
117
b2edef6f 118#ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
cf760e4c 119void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) )
76626af2 120{
b2edef6f 121 wxDebugFree(buf);
76626af2
JS
122}
123#endif
124
b2edef6f 125#ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
cf760e4c 126void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum )
c801d85f 127{
cf760e4c 128 return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE);
c801d85f 129}
b2edef6f 130#endif
c801d85f 131
b2edef6f
VZ
132#ifdef _WX_WANT_ARRAY_DELETE_VOID
133void wxObject::operator delete[] ( void *buf )
c801d85f 134{
afb74891 135 wxDebugFree(buf, TRUE);
c801d85f
KB
136}
137#endif
138
b2edef6f 139#ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
cf760e4c 140void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) )
b2edef6f
VZ
141{
142 wxDebugFree(buf, TRUE);
143}
144#endif
afb74891 145
afb74891 146
0b9ab0bd
RL
147// ----------------------------------------------------------------------------
148// wxClassInfo
149// ----------------------------------------------------------------------------
c801d85f 150
aa4b7ef9
VZ
151wxClassInfo::~wxClassInfo()
152{
153 // remove this object from the linked list of all class infos: if we don't
154 // do it, loading/unloading a DLL containing static wxClassInfo objects is
155 // not going to work
156 if ( this == sm_first )
157 {
158 sm_first = m_next;
159 }
160 else
161 {
162 wxClassInfo *info = sm_first;
163 while (info)
164 {
165 if ( info->m_next == this )
166 {
167 info->m_next = m_next;
168 break;
169 }
170
171 info = info->m_next;
172 }
173 }
174}
175
0b9ab0bd 176wxClassInfo *wxClassInfo::FindClass(const wxChar *className)
c801d85f 177{
80e8062f
VZ
178 if ( sm_classTable )
179 {
180 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
181 }
182 else
183 {
184 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
185 {
186 if ( wxStrcmp(info->GetClassName(), className) == 0 )
187 return info;
188 }
189
190 return NULL;
191 }
c801d85f
KB
192}
193
1f428942
VZ
194// a tiny InitializeClasses() helper
195/* static */
196inline wxClassInfo *wxClassInfo::GetBaseByName(const wxChar *name)
197{
198 if ( !name )
199 return NULL;
200
201 wxClassInfo *classInfo = (wxClassInfo *)sm_classTable->Get(name);
202
27db95f0
VZ
203#ifdef __WXDEBUG__
204 // this must be fixed, other things will work wrongly later if you get this
205 if ( !classInfo )
206 {
207 wxFAIL_MSG( wxString::Format
208 (
209 _T("base class '%s' is unknown to wxWindows RTTI"),
210 name
211 ) );
212 }
213#endif // __WXDEBUG__
1f428942
VZ
214
215 return classInfo;
216}
c801d85f 217
1f428942 218// Set pointers to base class(es) to speed up IsKindOf
afb74891 219void wxClassInfo::InitializeClasses()
c801d85f 220{
309689b2
VZ
221 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
222 // link any object module twice mistakenly) will break this function
223 // because it will enter an infinite loop and eventually die with "out of
224 // memory" - as this is quite hard to detect if you're unaware of this,
225 // try to do some checks here
0b9ab0bd 226
309689b2 227#ifdef __WXDEBUG__
0b9ab0bd 228 static const size_t nMaxClasses = 10000; // more than we'll ever have
309689b2 229 size_t nClass = 0;
0b9ab0bd 230#endif
309689b2 231
edf0993c 232 sm_classTable = new wxHashTable(wxKEY_STRING);
afb74891 233
0b9ab0bd
RL
234 // Index all class infos by their class name
235
236 wxClassInfo *info;
237 for(info = sm_first; info; info = info->m_next)
afb74891
VZ
238 {
239 if (info->m_className)
309689b2
VZ
240 {
241 wxASSERT_MSG( ++nClass < nMaxClasses,
f6bcfd97 242 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
afb74891 243 sm_classTable->Put(info->m_className, (wxObject *)info);
309689b2 244 }
afb74891
VZ
245 }
246
0b9ab0bd
RL
247 // Set base pointers for each wxClassInfo
248
249 for(info = sm_first; info; info = info->m_next)
afb74891 250 {
1f428942
VZ
251 info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1());
252 info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2());
afb74891 253 }
c801d85f
KB
254}
255
afb74891 256void wxClassInfo::CleanUpClasses()
c801d85f 257{
0c32066b 258 delete wxClassInfo::sm_classTable;
b2edef6f 259 wxClassInfo::sm_classTable = NULL;
0c32066b 260}
f4a8c29f 261
0b9ab0bd 262
cf2f341a 263wxObject *wxCreateDynamicObject(const wxChar *name)
0c32066b 264{
bbee61e5 265#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
b2edef6f 266 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
bbee61e5 267#endif
61cca9d2 268
b2edef6f 269 if ( wxClassInfo::sm_classTable )
0c32066b
JS
270 {
271 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
b2edef6f 272 return info ? info->CreateObject() : NULL;
0c32066b 273 }
b2edef6f 274 else // no sm_classTable yet
0c32066b 275 {
b2edef6f
VZ
276 for ( wxClassInfo *info = wxClassInfo::sm_first;
277 info;
278 info = info->m_next )
279 {
cf2f341a 280 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
0c32066b 281 return info->CreateObject();
b2edef6f
VZ
282 }
283
284 return NULL;
0c32066b 285 }
c801d85f
KB
286}
287
7a4b9130 288
0b9ab0bd 289// ----------------------------------------------------------------------------
a6391d30 290// wxObject
0b9ab0bd 291// ----------------------------------------------------------------------------
c801d85f 292
a6391d30
GD
293// Initialize ref data from another object (needed for copy constructor and
294// assignment operator)
295void wxObject::InitFrom(const wxObject& other)
296{
297 m_refData = other.m_refData;
298 if ( m_refData )
299 m_refData->m_count++;
300}
301
c801d85f
KB
302void wxObject::Ref(const wxObject& clone)
303{
bbee61e5 304#if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
807d8487 305 DEBUG_PRINTF(wxObject::Ref)
bbee61e5 306#endif
0b9ab0bd 307
c89f5c02
RR
308 // nothing to be done
309 if (m_refData == clone.m_refData)
310 return;
311
0b9ab0bd 312 // delete reference to old data
c801d85f 313 UnRef();
0b9ab0bd 314
c801d85f 315 // reference new data
807d8487 316 if ( clone.m_refData )
0b9ab0bd 317 {
c801d85f
KB
318 m_refData = clone.m_refData;
319 ++(m_refData->m_count);
320 }
321}
322
afb74891 323void wxObject::UnRef()
c801d85f 324{
807d8487 325 if ( m_refData )
4fe5383d
VZ
326 {
327 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
328
329 if ( !--m_refData->m_count )
c801d85f 330 delete m_refData;
807d8487 331 m_refData = NULL;
c801d85f 332 }
c801d85f
KB
333}
334
807d8487
VZ
335void wxObject::AllocExclusive()
336{
337 if ( !m_refData )
338 {
339 m_refData = CreateRefData();
340 }
341 else if ( m_refData->GetRefCount() > 1 )
342 {
343 // note that ref is not going to be destroyed in this case
a9bf8315 344 const wxObjectRefData* ref = m_refData;
807d8487
VZ
345 UnRef();
346
347 // ... so we can still access it
348 m_refData = CloneRefData(ref);
349 }
350 //else: ref count is 1, we are exclusive owners of m_refData anyhow
351
352 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
353 _T("wxObject::AllocExclusive() failed.") );
354}
355
356wxObjectRefData *wxObject::CreateRefData() const
357{
d1870078
VZ
358 // if you use AllocExclusive() you must override this method
359 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
360
807d8487
VZ
361 return NULL;
362}
363
a9bf8315
VZ
364wxObjectRefData *
365wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
807d8487 366{
d1870078
VZ
367 // if you use AllocExclusive() you must override this method
368 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
369
807d8487
VZ
370 return NULL;
371}
372
373// ----------------------------------------------------------------------------
374// misc
375// ----------------------------------------------------------------------------
ce28b4d7 376
edf0993c 377#if defined(__DARWIN__) && defined(WXMAKINGDLL)
ce28b4d7
GD
378
379extern "C" {
380 void __initialize_Cplusplus(void);
381 void wxWindowsDylibInit(void);
382};
383
edf0993c
GD
384// Dynamic shared library (dylib) initialization routine
385// required to initialize static C++ objects bacause of lazy dynamic linking
386// http://developer.apple.com/techpubs/macosx/Essentials/
387// SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
0b9ab0bd 388
ce28b4d7
GD
389void wxWindowsDylibInit()
390{
391 // The function __initialize_Cplusplus() must be called from the shared
392 // library initialization routine to cause the static C++ objects in
393 // the library to be initialized (reference number 2441683).
394
edf0993c
GD
395 // This only seems to be necessary if the library initialization routine
396 // needs to use the static C++ objects
ce28b4d7
GD
397 __initialize_Cplusplus();
398}
399
400#endif
0b9ab0bd
RL
401
402// vi:sts=4:sw=4:et