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