]> git.saurik.com Git - wxWidgets.git/blob - src/common/object.cpp
restore the stream position in wxImageHandler itself instead of forcing all
[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 and Markus Holzem
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows license
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::FindClass(const wxChar *className)
140 {
141 if ( sm_classTable )
142 {
143 return (wxClassInfo *)wxClassInfo::sm_classTable->Get(className);
144 }
145 else
146 {
147 for ( wxClassInfo *info = sm_first; info ; info = info->m_next )
148 {
149 if ( wxStrcmp(info->GetClassName(), className) == 0 )
150 return info;
151 }
152
153 return NULL;
154 }
155 }
156
157 // a tiny InitializeClasses() helper
158 /* static */
159 inline wxClassInfo *wxClassInfo::GetBaseByName(const wxChar *name)
160 {
161 if ( !name )
162 return NULL;
163
164 wxClassInfo *classInfo = (wxClassInfo *)sm_classTable->Get(name);
165
166 // this must be fixed, other things risk work wrongly later if you get this
167 wxASSERT_MSG( classInfo,
168 wxString::Format
169 (
170 _T("base class '%s' is unknown to wxWindows RTTI"),
171 name
172 ) );
173
174 return classInfo;
175 }
176
177 // Set pointers to base class(es) to speed up IsKindOf
178 void wxClassInfo::InitializeClasses()
179 {
180 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
181 // link any object module twice mistakenly) will break this function
182 // because it will enter an infinite loop and eventually die with "out of
183 // memory" - as this is quite hard to detect if you're unaware of this,
184 // try to do some checks here
185
186 #ifdef __WXDEBUG__
187 static const size_t nMaxClasses = 10000; // more than we'll ever have
188 size_t nClass = 0;
189 #endif
190
191 sm_classTable = new wxHashTable(wxKEY_STRING);
192
193 // Index all class infos by their class name
194
195 wxClassInfo *info;
196 for(info = sm_first; info; info = info->m_next)
197 {
198 if (info->m_className)
199 {
200 wxASSERT_MSG( ++nClass < nMaxClasses,
201 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
202 sm_classTable->Put(info->m_className, (wxObject *)info);
203 }
204 }
205
206 // Set base pointers for each wxClassInfo
207
208 for(info = sm_first; info; info = info->m_next)
209 {
210 info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1());
211 info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2());
212 }
213 }
214
215 void wxClassInfo::CleanUpClasses()
216 {
217 delete wxClassInfo::sm_classTable;
218 wxClassInfo::sm_classTable = NULL;
219 }
220
221
222 wxObject *wxCreateDynamicObject(const wxChar *name)
223 {
224 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
225 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
226 #endif
227
228 if ( wxClassInfo::sm_classTable )
229 {
230 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
231 return info ? info->CreateObject() : NULL;
232 }
233 else // no sm_classTable yet
234 {
235 for ( wxClassInfo *info = wxClassInfo::sm_first;
236 info;
237 info = info->m_next )
238 {
239 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
240 return info->CreateObject();
241 }
242
243 return NULL;
244 }
245 }
246
247
248 // ----------------------------------------------------------------------------
249 // wxObject
250 // ----------------------------------------------------------------------------
251
252 // Initialize ref data from another object (needed for copy constructor and
253 // assignment operator)
254 void wxObject::InitFrom(const wxObject& other)
255 {
256 m_refData = other.m_refData;
257 if ( m_refData )
258 m_refData->m_count++;
259 }
260
261 void wxObject::Ref(const wxObject& clone)
262 {
263 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
264 DEBUG_PRINTF(wxObject::Ref)
265 #endif
266
267 // nothing to be done
268 if (m_refData == clone.m_refData)
269 return;
270
271 // delete reference to old data
272 UnRef();
273
274 // reference new data
275 if ( clone.m_refData )
276 {
277 m_refData = clone.m_refData;
278 ++(m_refData->m_count);
279 }
280 }
281
282 void wxObject::UnRef()
283 {
284 if ( m_refData )
285 {
286 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
287
288 if ( !--m_refData->m_count )
289 delete m_refData;
290 m_refData = NULL;
291 }
292 }
293
294 void wxObject::AllocExclusive()
295 {
296 if ( !m_refData )
297 {
298 m_refData = CreateRefData();
299 }
300 else if ( m_refData->GetRefCount() > 1 )
301 {
302 // note that ref is not going to be destroyed in this case
303 const wxObjectRefData* ref = m_refData;
304 UnRef();
305
306 // ... so we can still access it
307 m_refData = CloneRefData(ref);
308 }
309 //else: ref count is 1, we are exclusive owners of m_refData anyhow
310
311 wxASSERT_MSG( m_refData && m_refData->GetRefCount() == 1,
312 _T("wxObject::AllocExclusive() failed.") );
313 }
314
315 wxObjectRefData *wxObject::CreateRefData() const
316 {
317 // if you use AllocExclusive() you must override this method
318 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
319
320 return NULL;
321 }
322
323 wxObjectRefData *
324 wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const
325 {
326 // if you use AllocExclusive() you must override this method
327 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
328
329 return NULL;
330 }
331
332 // ----------------------------------------------------------------------------
333 // misc
334 // ----------------------------------------------------------------------------
335
336 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
337
338 extern "C" {
339 void __initialize_Cplusplus(void);
340 void wxWindowsDylibInit(void);
341 };
342
343 // Dynamic shared library (dylib) initialization routine
344 // required to initialize static C++ objects bacause of lazy dynamic linking
345 // http://developer.apple.com/techpubs/macosx/Essentials/
346 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
347
348 void wxWindowsDylibInit()
349 {
350 // The function __initialize_Cplusplus() must be called from the shared
351 // library initialization routine to cause the static C++ objects in
352 // the library to be initialized (reference number 2441683).
353
354 // This only seems to be necessary if the library initialization routine
355 // needs to use the static C++ objects
356 __initialize_Cplusplus();
357 }
358
359 #endif
360
361 // vi:sts=4:sw=4:et