]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: object.cpp | |
3 | // Purpose: wxObject implementation | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
3f4a0c5b | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "object.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
1678ad78 | 23 | #ifndef WX_PRECOMP |
794df945 | 24 | #include "wx/hash.h" |
1ccbb61a | 25 | #if wxUSE_SERIAL |
794df945 VZ |
26 | #include "wx/objstrm.h" |
27 | #include "wx/serbase.h" | |
28 | ||
29 | // for error messages | |
30 | #include "wx/log.h" | |
31 | #include "wx/intl.h" | |
32 | #endif // wxUSE_SERIAL | |
33 | #endif // WX_PRECOMP | |
c801d85f | 34 | |
24eb81cb DW |
35 | #ifdef __VISAGECPP__ |
36 | #include "wx/objstrm.h" | |
37 | #include "wx/serbase.h" | |
38 | #endif | |
39 | ||
c801d85f KB |
40 | #include <string.h> |
41 | #include <assert.h> | |
42 | ||
ea57084d | 43 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
c801d85f KB |
44 | #include "wx/memory.h" |
45 | #endif | |
46 | ||
ea57084d | 47 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
3f4a0c5b VZ |
48 | // for wxObject::Dump |
49 | #include "wx/ioswrap.h" | |
415ca026 DW |
50 | #if defined(__VISAGECPP__) |
51 | // help with VA debugging | |
52 | #define DEBUG_PRINTF(NAME) { static int raz=0; \ | |
53 | printf( #NAME " %i\n",raz); fflush(stdout); \ | |
54 | raz++; \ | |
55 | } | |
56 | #else | |
57 | #define DEBUG_PRINTF(NAME) | |
58 | #endif | |
c801d85f KB |
59 | #endif |
60 | ||
223d09f6 | 61 | wxClassInfo wxObject::sm_classwxObject((wxChar *) wxT("wxObject"), (wxChar *) NULL, (wxChar *) NULL, (int ) sizeof(wxObject), (wxObjectConstructorFn) NULL); |
0c32066b JS |
62 | wxClassInfo* wxClassInfo::sm_first = (wxClassInfo *) NULL; |
63 | wxHashTable* wxClassInfo::sm_classTable = (wxHashTable*) NULL; | |
c801d85f | 64 | |
f6bcfd97 BP |
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 | ||
c801d85f KB |
70 | /* |
71 | * wxWindows root object. | |
72 | */ | |
73 | ||
afb74891 | 74 | wxObject::wxObject() |
c801d85f | 75 | { |
afb74891 | 76 | m_refData = (wxObjectRefData *) NULL; |
1ccbb61a | 77 | #if wxUSE_SERIAL |
afb74891 | 78 | m_serialObj = (wxObject_Serialize *)NULL; |
f4a8c29f | 79 | #endif |
c801d85f KB |
80 | } |
81 | ||
afb74891 | 82 | wxObject::~wxObject() |
c801d85f | 83 | { |
afb74891 | 84 | UnRef(); |
1ccbb61a | 85 | #if wxUSE_SERIAL |
afb74891 VZ |
86 | if (m_serialObj) |
87 | delete m_serialObj; | |
f4a8c29f | 88 | #endif |
c801d85f KB |
89 | } |
90 | ||
91 | /* | |
92 | * Is this object a kind of (a subclass of) 'info'? | |
93 | * E.g. is wxWindow a kind of wxObject? | |
94 | * Go from this class to superclass, taking into account | |
95 | * two possible base classes. | |
96 | */ | |
afb74891 | 97 | |
341287bf | 98 | bool wxObject::IsKindOf(wxClassInfo *info) const |
c801d85f | 99 | { |
afb74891 VZ |
100 | wxClassInfo *thisInfo = GetClassInfo(); |
101 | if (thisInfo) | |
102 | return thisInfo->IsKindOf(info); | |
103 | else | |
104 | return FALSE; | |
c801d85f KB |
105 | } |
106 | ||
38830220 | 107 | #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) |
dd107c50 | 108 | void wxObject::Dump(wxSTD ostream& str) |
c801d85f | 109 | { |
afb74891 VZ |
110 | if (GetClassInfo() && GetClassInfo()->GetClassName()) |
111 | str << GetClassInfo()->GetClassName(); | |
112 | else | |
113 | str << "unknown object class"; | |
c801d85f KB |
114 | } |
115 | #endif | |
116 | ||
ea57084d | 117 | #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING |
c801d85f KB |
118 | |
119 | #ifdef new | |
120 | #undef new | |
121 | #endif | |
122 | ||
4de6207a | 123 | void *wxObject::operator new (size_t size, wxChar * fileName, int lineNum) |
c801d85f | 124 | { |
afb74891 | 125 | return wxDebugAlloc(size, fileName, lineNum, TRUE); |
c801d85f KB |
126 | } |
127 | ||
415ca026 DW |
128 | #if defined(__VISAGECPP__) |
129 | # if __DEBUG_ALLOC__ | |
130 | void wxObject::operator delete (void * buf,const char * _fname, size_t _line) | |
131 | { | |
132 | wxDebugFree(buf); | |
133 | } | |
134 | # endif //__DEBUG_ALLOC__ | |
135 | #else | |
c801d85f KB |
136 | void wxObject::operator delete (void * buf) |
137 | { | |
afb74891 | 138 | wxDebugFree(buf); |
c801d85f | 139 | } |
415ca026 | 140 | #endif // __VISAGECPP__ |
c801d85f | 141 | |
76626af2 | 142 | // VC++ 6.0 |
3f4a0c5b | 143 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
f6bcfd97 | 144 | void wxObject::operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */) |
76626af2 | 145 | { |
afb74891 | 146 | ::operator delete(pData); |
76626af2 JS |
147 | } |
148 | #endif | |
149 | ||
c801d85f | 150 | // Cause problems for VC++ - crashes |
7c74e7fe | 151 | #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__) |
4de6207a | 152 | void * wxObject::operator new[] (size_t size, wxChar * fileName, int lineNum) |
c801d85f | 153 | { |
afb74891 | 154 | return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE); |
c801d85f KB |
155 | } |
156 | ||
157 | void wxObject::operator delete[] (void * buf) | |
158 | { | |
afb74891 | 159 | wxDebugFree(buf, TRUE); |
c801d85f KB |
160 | } |
161 | #endif | |
162 | ||
163 | #endif | |
164 | ||
165 | /* | |
166 | * Class info: provides run-time class type information. | |
167 | */ | |
168 | ||
6057972b VZ |
169 | wxClassInfo::wxClassInfo(const wxChar *cName, |
170 | const wxChar *baseName1, | |
171 | const wxChar *baseName2, | |
172 | int sz, | |
173 | wxObjectConstructorFn constr) | |
c801d85f | 174 | { |
afb74891 VZ |
175 | m_className = cName; |
176 | m_baseClassName1 = baseName1; | |
177 | m_baseClassName2 = baseName2; | |
178 | ||
179 | m_objectSize = sz; | |
180 | m_objectConstructor = constr; | |
181 | ||
182 | m_next = sm_first; | |
183 | sm_first = this; | |
184 | ||
185 | m_baseInfo1 = (wxClassInfo *) NULL; | |
186 | m_baseInfo2 = (wxClassInfo *) NULL; | |
c801d85f KB |
187 | } |
188 | ||
afb74891 | 189 | wxObject *wxClassInfo::CreateObject() |
c801d85f | 190 | { |
afb74891 VZ |
191 | if (m_objectConstructor) |
192 | return (wxObject *)(*m_objectConstructor)(); | |
193 | else | |
194 | return (wxObject *) NULL; | |
c801d85f KB |
195 | } |
196 | ||
6057972b | 197 | wxClassInfo *wxClassInfo::FindClass(const wxChar *c) |
c801d85f | 198 | { |
afb74891 VZ |
199 | wxClassInfo *p = sm_first; |
200 | while (p) | |
201 | { | |
6057972b VZ |
202 | if ( wxStrcmp(p->GetClassName(), c) == 0 ) |
203 | break; | |
204 | ||
afb74891 VZ |
205 | p = p->m_next; |
206 | } | |
6057972b VZ |
207 | |
208 | return p; | |
c801d85f KB |
209 | } |
210 | ||
211 | // Climb upwards through inheritance hierarchy. | |
212 | // Dual inheritance is catered for. | |
6057972b | 213 | bool wxClassInfo::IsKindOf(const wxClassInfo *info) const |
c801d85f | 214 | { |
afb74891 VZ |
215 | if (info == NULL) |
216 | return FALSE; | |
217 | ||
afb74891 VZ |
218 | if (this == info) |
219 | return TRUE; | |
c801d85f | 220 | |
afb74891 VZ |
221 | if (m_baseInfo1) |
222 | if (m_baseInfo1->IsKindOf(info)) | |
223 | return TRUE; | |
c801d85f | 224 | |
afb74891 VZ |
225 | if (m_baseInfo2) |
226 | return m_baseInfo2->IsKindOf(info); | |
c801d85f | 227 | |
afb74891 | 228 | return FALSE; |
c801d85f KB |
229 | } |
230 | ||
231 | // Set pointers to base class(es) to speed up IsKindOf | |
afb74891 | 232 | void wxClassInfo::InitializeClasses() |
c801d85f | 233 | { |
309689b2 VZ |
234 | // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you |
235 | // link any object module twice mistakenly) will break this function | |
236 | // because it will enter an infinite loop and eventually die with "out of | |
237 | // memory" - as this is quite hard to detect if you're unaware of this, | |
238 | // try to do some checks here | |
239 | #ifdef __WXDEBUG__ | |
240 | // more classes than we'll ever have | |
241 | static const size_t nMaxClasses = 10000; | |
242 | size_t nClass = 0; | |
243 | #endif // Debug | |
244 | ||
afb74891 VZ |
245 | wxClassInfo::sm_classTable = new wxHashTable(wxKEY_STRING); |
246 | ||
247 | // Index all class infos by their class name | |
248 | wxClassInfo *info = sm_first; | |
249 | while (info) | |
250 | { | |
251 | if (info->m_className) | |
309689b2 VZ |
252 | { |
253 | wxASSERT_MSG( ++nClass < nMaxClasses, | |
f6bcfd97 | 254 | _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") ); |
309689b2 | 255 | |
afb74891 | 256 | sm_classTable->Put(info->m_className, (wxObject *)info); |
309689b2 VZ |
257 | } |
258 | ||
afb74891 VZ |
259 | info = info->m_next; |
260 | } | |
261 | ||
262 | // Set base pointers for each wxClassInfo | |
263 | info = sm_first; | |
264 | while (info) | |
265 | { | |
266 | if (info->GetBaseClassName1()) | |
267 | info->m_baseInfo1 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName1()); | |
268 | if (info->GetBaseClassName2()) | |
269 | info->m_baseInfo2 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName2()); | |
270 | info = info->m_next; | |
271 | } | |
c801d85f KB |
272 | } |
273 | ||
afb74891 | 274 | void wxClassInfo::CleanUpClasses() |
c801d85f | 275 | { |
0c32066b JS |
276 | delete wxClassInfo::sm_classTable; |
277 | wxClassInfo::sm_classTable = NULL; | |
278 | } | |
f4a8c29f | 279 | |
cf2f341a | 280 | wxObject *wxCreateDynamicObject(const wxChar *name) |
0c32066b | 281 | { |
bbee61e5 | 282 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
415ca026 | 283 | DEBUG_PRINTF(wxObject *wxCreateDynamicObject) |
bbee61e5 | 284 | #endif |
61cca9d2 | 285 | |
0c32066b JS |
286 | if (wxClassInfo::sm_classTable) |
287 | { | |
288 | wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name); | |
289 | if (!info) | |
290 | return (wxObject *)NULL; | |
291 | ||
292 | return info->CreateObject(); | |
293 | } | |
294 | else | |
295 | { | |
296 | wxClassInfo *info = wxClassInfo::sm_first; | |
297 | while (info) | |
298 | { | |
cf2f341a | 299 | if (info->m_className && wxStrcmp(info->m_className, name) == 0) |
0c32066b JS |
300 | return info->CreateObject(); |
301 | info = info->m_next; | |
302 | } | |
303 | return (wxObject*) NULL; | |
304 | } | |
c801d85f KB |
305 | } |
306 | ||
1ccbb61a | 307 | #if wxUSE_SERIAL |
c801d85f | 308 | |
7a4b9130 GL |
309 | #include "wx/serbase.h" |
310 | #include "wx/dynlib.h" | |
7a4b9130 | 311 | |
1678ad78 | 312 | wxObject* wxCreateStoredObject( wxInputStream &stream ) |
c801d85f | 313 | { |
afb74891 VZ |
314 | wxObjectInputStream obj_s(stream); |
315 | return obj_s.LoadObject(); | |
c801d85f KB |
316 | }; |
317 | ||
7a4b9130 GL |
318 | void wxObject::StoreObject( wxObjectOutputStream& stream ) |
319 | { | |
bbee61e5 | 320 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
415ca026 | 321 | DEBUG_PRINTF(wxObject::StoreObject) |
bbee61e5 | 322 | #endif |
61cca9d2 | 323 | |
afb74891 VZ |
324 | wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize"; |
325 | wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial"); | |
7a4b9130 | 326 | |
afb74891 VZ |
327 | if (!lib) { |
328 | wxLogError(_("Can't load wxSerial dynamic library.")); | |
329 | return; | |
330 | } | |
f4a8c29f | 331 | if (!m_serialObj) { |
afb74891 VZ |
332 | m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name ); |
333 | ||
334 | if (!m_serialObj) { | |
335 | wxLogError(_("Can't find the serialization object '%s' " | |
336 | "for the object '%s'."), | |
337 | obj_name.c_str(), | |
338 | GetClassInfo()->GetClassName()); | |
339 | return; | |
340 | } | |
341 | m_serialObj->SetObject(this); | |
f4a8c29f | 342 | } |
7a4b9130 | 343 | |
afb74891 | 344 | m_serialObj->StoreObject(stream); |
7a4b9130 GL |
345 | } |
346 | ||
347 | void wxObject::LoadObject( wxObjectInputStream& stream ) | |
348 | { | |
bbee61e5 | 349 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
415ca026 | 350 | DEBUG_PRINTF(wxObject::LoadObject) |
bbee61e5 | 351 | #endif |
61cca9d2 | 352 | |
afb74891 VZ |
353 | wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize"; |
354 | wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial"); | |
7a4b9130 | 355 | |
f4a8c29f | 356 | if (!m_serialObj) { |
afb74891 VZ |
357 | m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name ); |
358 | ||
359 | if (!m_serialObj) { | |
360 | wxLogError(_("Can't find the serialization object '%s' " | |
361 | "for the object '%s'."), | |
362 | obj_name.c_str(), | |
363 | GetClassInfo()->GetClassName()); | |
364 | return; | |
365 | } | |
366 | m_serialObj->SetObject(this); | |
f4a8c29f | 367 | } |
6f34921d | 368 | |
afb74891 | 369 | m_serialObj->LoadObject(stream); |
7a4b9130 GL |
370 | } |
371 | ||
bfc6fde4 | 372 | #endif // wxUSE_SERIAL |
c801d85f KB |
373 | |
374 | /* | |
375 | * wxObject: cloning of objects | |
376 | */ | |
377 | ||
378 | void wxObject::Ref(const wxObject& clone) | |
379 | { | |
bbee61e5 | 380 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT |
415ca026 | 381 | DEBUG_PRINTF(wxObject::Ref) |
bbee61e5 JJ |
382 | #endif |
383 | // delete reference to old data | |
c801d85f KB |
384 | UnRef(); |
385 | // reference new data | |
386 | if (clone.m_refData) { | |
387 | m_refData = clone.m_refData; | |
388 | ++(m_refData->m_count); | |
389 | } | |
390 | } | |
391 | ||
afb74891 | 392 | void wxObject::UnRef() |
c801d85f | 393 | { |
4fe5383d VZ |
394 | if ( m_refData ) |
395 | { | |
396 | wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") ); | |
397 | ||
398 | if ( !--m_refData->m_count ) | |
c801d85f | 399 | delete m_refData; |
4fe5383d | 400 | m_refData = (wxObjectRefData *) NULL; |
c801d85f | 401 | } |
c801d85f KB |
402 | } |
403 | ||
404 | /* | |
405 | * wxObjectData | |
406 | */ | |
407 | ||
408 | wxObjectRefData::wxObjectRefData(void) : m_count(1) | |
409 | { | |
410 | } | |
411 | ||
afb74891 | 412 | wxObjectRefData::~wxObjectRefData() |
c801d85f KB |
413 | { |
414 | } | |
ce28b4d7 GD |
415 | |
416 | #if defined(__DARWIN__) && defined(DYLIB_INIT) | |
417 | ||
418 | extern "C" { | |
419 | void __initialize_Cplusplus(void); | |
420 | void wxWindowsDylibInit(void); | |
421 | }; | |
422 | ||
423 | // Dynamic shared library (dylib) initialization routine | |
424 | // required to initialize static C++ objects bacause of lazy dynamic linking | |
425 | // http://developer.apple.com/techpubs/macosx/Essentials/ | |
426 | // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html | |
427 | // | |
428 | void wxWindowsDylibInit() | |
429 | { | |
430 | // The function __initialize_Cplusplus() must be called from the shared | |
431 | // library initialization routine to cause the static C++ objects in | |
432 | // the library to be initialized (reference number 2441683). | |
433 | ||
434 | __initialize_Cplusplus(); | |
435 | } | |
436 | ||
437 | #endif |