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