]>
Commit | Line | Data |
---|---|---|
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$ | |
0b9ab0bd RL |
8 | // Copyright: (c) 1998 Julian Smart and Markus Holzem |
9 | // (c) 2001 Ron Lee <ron@debian.org> | |
3f4a0c5b | 10 | // Licence: wxWindows license |
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 |
0b9ab0bd | 25 | #include "wx/hash.h" |
24eb81cb DW |
26 | #endif |
27 | ||
c801d85f | 28 | #include <string.h> |
c801d85f | 29 | |
ea57084d | 30 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
c801d85f KB |
31 | #include "wx/memory.h" |
32 | #endif | |
33 | ||
ea57084d | 34 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
3f4a0c5b | 35 | // for wxObject::Dump |
b2edef6f | 36 | #include "wx/ioswrap.h" |
c801d85f | 37 | |
b2edef6f VZ |
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 | |
c801d85f | 45 | |
f6bcfd97 | 46 | |
0b9ab0bd RL |
47 | wxClassInfo wxObject::sm_classwxObject( wxT("wxObject"), 0, 0, |
48 | (int) sizeof(wxObject), | |
49 | (wxObjectConstructorFn) 0 ); | |
c801d85f | 50 | |
b2edef6f VZ |
51 | wxClassInfo* wxClassInfo::sm_first = NULL; |
52 | wxHashTable* wxClassInfo::sm_classTable = NULL; | |
c801d85f | 53 | |
b2edef6f VZ |
54 | // These are here so we can avoid 'always true/false' warnings |
55 | // by referring to these instead of TRUE/FALSE | |
0b9ab0bd RL |
56 | const bool wxTrue = TRUE; |
57 | const bool wxFalse = FALSE; | |
c801d85f | 58 | |
b2edef6f VZ |
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. | |
341287bf | 63 | bool wxObject::IsKindOf(wxClassInfo *info) const |
c801d85f | 64 | { |
afb74891 | 65 | wxClassInfo *thisInfo = GetClassInfo(); |
0b9ab0bd | 66 | return (thisInfo) ? thisInfo->IsKindOf(info) : FALSE ; |
c801d85f KB |
67 | } |
68 | ||
38830220 | 69 | #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) |
dd107c50 | 70 | void wxObject::Dump(wxSTD ostream& str) |
c801d85f | 71 | { |
afb74891 VZ |
72 | if (GetClassInfo() && GetClassInfo()->GetClassName()) |
73 | str << GetClassInfo()->GetClassName(); | |
74 | else | |
b2edef6f | 75 | str << _T("unknown object class"); |
c801d85f KB |
76 | } |
77 | #endif | |
78 | ||
c801d85f | 79 | |
cf760e4c VZ |
80 | #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new ) |
81 | #undef new | |
82 | #endif | |
83 | ||
84 | ||
b2edef6f | 85 | #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT |
cf760e4c | 86 | void *wxObject::operator new ( size_t size, const wxChar *fileName, int lineNum ) |
c801d85f | 87 | { |
cf760e4c | 88 | return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE); |
c801d85f | 89 | } |
b2edef6f | 90 | #endif |
c801d85f | 91 | |
b2edef6f VZ |
92 | #ifdef _WX_WANT_DELETE_VOID |
93 | void wxObject::operator delete ( void *buf ) | |
415ca026 DW |
94 | { |
95 | wxDebugFree(buf); | |
96 | } | |
b2edef6f VZ |
97 | #endif |
98 | ||
99 | #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET | |
100 | void wxObject::operator delete ( void *buf, const char *_fname, size_t _line ) | |
c801d85f | 101 | { |
afb74891 | 102 | wxDebugFree(buf); |
c801d85f | 103 | } |
0b9ab0bd RL |
104 | #endif |
105 | ||
b2edef6f | 106 | #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT |
cf760e4c | 107 | void wxObject::operator delete ( void *buf, const wxChar *WXUNUSED(fileName), int WXUNUSED(lineNum) ) |
76626af2 | 108 | { |
b2edef6f | 109 | wxDebugFree(buf); |
76626af2 JS |
110 | } |
111 | #endif | |
112 | ||
b2edef6f | 113 | #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT |
cf760e4c | 114 | void *wxObject::operator new[] ( size_t size, const wxChar* fileName, int lineNum ) |
c801d85f | 115 | { |
cf760e4c | 116 | return wxDebugAlloc(size, (wxChar*) fileName, lineNum, TRUE, TRUE); |
c801d85f | 117 | } |
b2edef6f | 118 | #endif |
c801d85f | 119 | |
b2edef6f VZ |
120 | #ifdef _WX_WANT_ARRAY_DELETE_VOID |
121 | void wxObject::operator delete[] ( void *buf ) | |
c801d85f | 122 | { |
afb74891 | 123 | wxDebugFree(buf, TRUE); |
c801d85f KB |
124 | } |
125 | #endif | |
126 | ||
b2edef6f | 127 | #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT |
cf760e4c | 128 | void wxObject::operator delete[] (void * buf, const wxChar* WXUNUSED(fileName), int WXUNUSED(lineNum) ) |
b2edef6f VZ |
129 | { |
130 | wxDebugFree(buf, TRUE); | |
131 | } | |
132 | #endif | |
afb74891 | 133 | |
afb74891 | 134 | |
0b9ab0bd RL |
135 | // ---------------------------------------------------------------------------- |
136 | // wxClassInfo | |
137 | // ---------------------------------------------------------------------------- | |
c801d85f | 138 | |
0b9ab0bd | 139 | wxClassInfo *wxClassInfo::FindClass(const wxChar *className) |
c801d85f | 140 | { |
80e8062f VZ |
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 | } | |
c801d85f KB |
155 | } |
156 | ||
1f428942 VZ |
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 | |
21b52985 VZ |
167 | wxASSERT_MSG( classInfo, |
168 | wxString::Format | |
169 | ( | |
170 | _T("base class '%s' is unknown to wxWindows RTTI"), | |
171 | name | |
172 | ) ); | |
1f428942 VZ |
173 | |
174 | return classInfo; | |
175 | } | |
c801d85f | 176 | |
1f428942 | 177 | // Set pointers to base class(es) to speed up IsKindOf |
afb74891 | 178 | void wxClassInfo::InitializeClasses() |
c801d85f | 179 | { |
309689b2 VZ |
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 | |
0b9ab0bd | 185 | |
309689b2 | 186 | #ifdef __WXDEBUG__ |
0b9ab0bd | 187 | static const size_t nMaxClasses = 10000; // more than we'll ever have |
309689b2 | 188 | size_t nClass = 0; |
0b9ab0bd | 189 | #endif |
309689b2 | 190 | |
edf0993c | 191 | sm_classTable = new wxHashTable(wxKEY_STRING); |
afb74891 | 192 | |
0b9ab0bd RL |
193 | // Index all class infos by their class name |
194 | ||
195 | wxClassInfo *info; | |
196 | for(info = sm_first; info; info = info->m_next) | |
afb74891 VZ |
197 | { |
198 | if (info->m_className) | |
309689b2 VZ |
199 | { |
200 | wxASSERT_MSG( ++nClass < nMaxClasses, | |
f6bcfd97 | 201 | _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") ); |
afb74891 | 202 | sm_classTable->Put(info->m_className, (wxObject *)info); |
309689b2 | 203 | } |
afb74891 VZ |
204 | } |
205 | ||
0b9ab0bd RL |
206 | // Set base pointers for each wxClassInfo |
207 | ||
208 | for(info = sm_first; info; info = info->m_next) | |
afb74891 | 209 | { |
1f428942 VZ |
210 | info->m_baseInfo1 = GetBaseByName(info->GetBaseClassName1()); |
211 | info->m_baseInfo2 = GetBaseByName(info->GetBaseClassName2()); | |
afb74891 | 212 | } |
c801d85f KB |
213 | } |
214 | ||
afb74891 | 215 | void wxClassInfo::CleanUpClasses() |
c801d85f | 216 | { |
0c32066b | 217 | delete wxClassInfo::sm_classTable; |
b2edef6f | 218 | wxClassInfo::sm_classTable = NULL; |
0c32066b | 219 | } |
f4a8c29f | 220 | |
0b9ab0bd | 221 | |
cf2f341a | 222 | wxObject *wxCreateDynamicObject(const wxChar *name) |
0c32066b | 223 | { |
bbee61e5 | 224 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
b2edef6f | 225 | DEBUG_PRINTF(wxObject *wxCreateDynamicObject) |
bbee61e5 | 226 | #endif |
61cca9d2 | 227 | |
b2edef6f | 228 | if ( wxClassInfo::sm_classTable ) |
0c32066b JS |
229 | { |
230 | wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name); | |
b2edef6f | 231 | return info ? info->CreateObject() : NULL; |
0c32066b | 232 | } |
b2edef6f | 233 | else // no sm_classTable yet |
0c32066b | 234 | { |
b2edef6f VZ |
235 | for ( wxClassInfo *info = wxClassInfo::sm_first; |
236 | info; | |
237 | info = info->m_next ) | |
238 | { | |
cf2f341a | 239 | if (info->m_className && wxStrcmp(info->m_className, name) == 0) |
0c32066b | 240 | return info->CreateObject(); |
b2edef6f VZ |
241 | } |
242 | ||
243 | return NULL; | |
0c32066b | 244 | } |
c801d85f KB |
245 | } |
246 | ||
7a4b9130 | 247 | |
0b9ab0bd | 248 | // ---------------------------------------------------------------------------- |
a6391d30 | 249 | // wxObject |
0b9ab0bd | 250 | // ---------------------------------------------------------------------------- |
c801d85f | 251 | |
a6391d30 GD |
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 | ||
c801d85f KB |
261 | void wxObject::Ref(const wxObject& clone) |
262 | { | |
bbee61e5 | 263 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
807d8487 | 264 | DEBUG_PRINTF(wxObject::Ref) |
bbee61e5 | 265 | #endif |
0b9ab0bd | 266 | |
c89f5c02 RR |
267 | // nothing to be done |
268 | if (m_refData == clone.m_refData) | |
269 | return; | |
270 | ||
0b9ab0bd | 271 | // delete reference to old data |
c801d85f | 272 | UnRef(); |
0b9ab0bd | 273 | |
c801d85f | 274 | // reference new data |
807d8487 | 275 | if ( clone.m_refData ) |
0b9ab0bd | 276 | { |
c801d85f KB |
277 | m_refData = clone.m_refData; |
278 | ++(m_refData->m_count); | |
279 | } | |
280 | } | |
281 | ||
afb74891 | 282 | void wxObject::UnRef() |
c801d85f | 283 | { |
807d8487 | 284 | if ( m_refData ) |
4fe5383d VZ |
285 | { |
286 | wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") ); | |
287 | ||
288 | if ( !--m_refData->m_count ) | |
c801d85f | 289 | delete m_refData; |
807d8487 | 290 | m_refData = NULL; |
c801d85f | 291 | } |
c801d85f KB |
292 | } |
293 | ||
807d8487 VZ |
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 | |
a9bf8315 | 303 | const wxObjectRefData* ref = m_refData; |
807d8487 VZ |
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 | { | |
d1870078 VZ |
317 | // if you use AllocExclusive() you must override this method |
318 | wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") ); | |
319 | ||
807d8487 VZ |
320 | return NULL; |
321 | } | |
322 | ||
a9bf8315 VZ |
323 | wxObjectRefData * |
324 | wxObject::CloneRefData(const wxObjectRefData * WXUNUSED(data)) const | |
807d8487 | 325 | { |
d1870078 VZ |
326 | // if you use AllocExclusive() you must override this method |
327 | wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") ); | |
328 | ||
807d8487 VZ |
329 | return NULL; |
330 | } | |
331 | ||
332 | // ---------------------------------------------------------------------------- | |
333 | // misc | |
334 | // ---------------------------------------------------------------------------- | |
ce28b4d7 | 335 | |
edf0993c | 336 | #if defined(__DARWIN__) && defined(WXMAKINGDLL) |
ce28b4d7 GD |
337 | |
338 | extern "C" { | |
339 | void __initialize_Cplusplus(void); | |
340 | void wxWindowsDylibInit(void); | |
341 | }; | |
342 | ||
edf0993c GD |
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 | |
0b9ab0bd | 347 | |
ce28b4d7 GD |
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 | ||
edf0993c GD |
354 | // This only seems to be necessary if the library initialization routine |
355 | // needs to use the static C++ objects | |
ce28b4d7 GD |
356 | __initialize_Cplusplus(); |
357 | } | |
358 | ||
359 | #endif | |
0b9ab0bd RL |
360 | |
361 | // vi:sts=4:sw=4:et |