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