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