]> git.saurik.com Git - wxWidgets.git/blob - src/common/object.cpp
some more src code reformatting
[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 wxObject *wxObject::Clone() const
103 {
104 wxObject *object = GetClassInfo()->CreateObject();
105 CopyObject(*object);
106 return object;
107 }
108
109 #ifdef __WXDEBUG__
110 void wxObject::CopyObject(wxObject& object_dest) const
111 #else // !Debug
112 void wxObject::CopyObject(wxObject& WXUNUSED(object_dest)) const
113 #endif // Debug/!Debug
114 {
115 wxASSERT(object_dest.GetClassInfo()->IsKindOf(GetClassInfo()));
116 }
117
118 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
119 void wxObject::Dump(ostream& str)
120 {
121 if (GetClassInfo() && GetClassInfo()->GetClassName())
122 str << GetClassInfo()->GetClassName();
123 else
124 str << "unknown object class";
125 }
126 #endif
127
128 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
129
130 #ifdef new
131 #undef new
132 #endif
133
134 void *wxObject::operator new (size_t size, wxChar * fileName, int lineNum)
135 {
136 return wxDebugAlloc(size, fileName, lineNum, TRUE);
137 }
138
139 #if defined(__VISAGECPP__)
140 # if __DEBUG_ALLOC__
141 void wxObject::operator delete (void * buf,const char * _fname, size_t _line)
142 {
143 wxDebugFree(buf);
144 }
145 # endif //__DEBUG_ALLOC__
146 #else
147 void wxObject::operator delete (void * buf)
148 {
149 wxDebugFree(buf);
150 }
151 #endif // __VISAGECPP__
152
153 // VC++ 6.0
154 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
155 void wxObject::operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */)
156 {
157 ::operator delete(pData);
158 }
159 #endif
160
161 // Cause problems for VC++ - crashes
162 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
163 void * wxObject::operator new[] (size_t size, wxChar * fileName, int lineNum)
164 {
165 return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE);
166 }
167
168 void wxObject::operator delete[] (void * buf)
169 {
170 wxDebugFree(buf, TRUE);
171 }
172 #endif
173
174 #endif
175
176 /*
177 * Class info: provides run-time class type information.
178 */
179
180 wxClassInfo::wxClassInfo(const wxChar *cName,
181 const wxChar *baseName1,
182 const wxChar *baseName2,
183 int sz,
184 wxObjectConstructorFn constr)
185 {
186 m_className = cName;
187 m_baseClassName1 = baseName1;
188 m_baseClassName2 = baseName2;
189
190 m_objectSize = sz;
191 m_objectConstructor = constr;
192
193 m_next = sm_first;
194 sm_first = this;
195
196 m_baseInfo1 = (wxClassInfo *) NULL;
197 m_baseInfo2 = (wxClassInfo *) NULL;
198 }
199
200 wxObject *wxClassInfo::CreateObject()
201 {
202 if (m_objectConstructor)
203 return (wxObject *)(*m_objectConstructor)();
204 else
205 return (wxObject *) NULL;
206 }
207
208 wxClassInfo *wxClassInfo::FindClass(const wxChar *c)
209 {
210 wxClassInfo *p = sm_first;
211 while (p)
212 {
213 if ( wxStrcmp(p->GetClassName(), c) == 0 )
214 break;
215
216 p = p->m_next;
217 }
218
219 return p;
220 }
221
222 // Climb upwards through inheritance hierarchy.
223 // Dual inheritance is catered for.
224 bool wxClassInfo::IsKindOf(const wxClassInfo *info) const
225 {
226 if (info == NULL)
227 return FALSE;
228
229 if (this == info)
230 return TRUE;
231
232 if (m_baseInfo1)
233 if (m_baseInfo1->IsKindOf(info))
234 return TRUE;
235
236 if (m_baseInfo2)
237 return m_baseInfo2->IsKindOf(info);
238
239 return FALSE;
240 }
241
242 // Set pointers to base class(es) to speed up IsKindOf
243 void wxClassInfo::InitializeClasses()
244 {
245 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
246 // link any object module twice mistakenly) will break this function
247 // because it will enter an infinite loop and eventually die with "out of
248 // memory" - as this is quite hard to detect if you're unaware of this,
249 // try to do some checks here
250 #ifdef __WXDEBUG__
251 // more classes than we'll ever have
252 static const size_t nMaxClasses = 10000;
253 size_t nClass = 0;
254 #endif // Debug
255
256 wxClassInfo::sm_classTable = new wxHashTable(wxKEY_STRING);
257
258 // Index all class infos by their class name
259 wxClassInfo *info = sm_first;
260 while (info)
261 {
262 if (info->m_className)
263 {
264 wxASSERT_MSG( ++nClass < nMaxClasses,
265 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
266
267 sm_classTable->Put(info->m_className, (wxObject *)info);
268 }
269
270 info = info->m_next;
271 }
272
273 // Set base pointers for each wxClassInfo
274 info = sm_first;
275 while (info)
276 {
277 if (info->GetBaseClassName1())
278 info->m_baseInfo1 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName1());
279 if (info->GetBaseClassName2())
280 info->m_baseInfo2 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName2());
281 info = info->m_next;
282 }
283 }
284
285 void wxClassInfo::CleanUpClasses()
286 {
287 delete wxClassInfo::sm_classTable;
288 wxClassInfo::sm_classTable = NULL;
289 }
290
291 wxObject *wxCreateDynamicObject(const wxChar *name)
292 {
293 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
294 DEBUG_PRINTF(wxObject *wxCreateDynamicObject)
295 #endif
296
297 if (wxClassInfo::sm_classTable)
298 {
299 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
300 if (!info)
301 return (wxObject *)NULL;
302
303 return info->CreateObject();
304 }
305 else
306 {
307 wxClassInfo *info = wxClassInfo::sm_first;
308 while (info)
309 {
310 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
311 return info->CreateObject();
312 info = info->m_next;
313 }
314 return (wxObject*) NULL;
315 }
316 }
317
318 #if wxUSE_SERIAL
319
320 #include "wx/serbase.h"
321 #include "wx/dynlib.h"
322
323 wxObject* wxCreateStoredObject( wxInputStream &stream )
324 {
325 wxObjectInputStream obj_s(stream);
326 return obj_s.LoadObject();
327 };
328
329 void wxObject::StoreObject( wxObjectOutputStream& stream )
330 {
331 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
332 DEBUG_PRINTF(wxObject::StoreObject)
333 #endif
334
335 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
336 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
337
338 if (!lib) {
339 wxLogError(_("Can't load wxSerial dynamic library."));
340 return;
341 }
342 if (!m_serialObj) {
343 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
344
345 if (!m_serialObj) {
346 wxLogError(_("Can't find the serialization object '%s' "
347 "for the object '%s'."),
348 obj_name.c_str(),
349 GetClassInfo()->GetClassName());
350 return;
351 }
352 m_serialObj->SetObject(this);
353 }
354
355 m_serialObj->StoreObject(stream);
356 }
357
358 void wxObject::LoadObject( wxObjectInputStream& stream )
359 {
360 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
361 DEBUG_PRINTF(wxObject::LoadObject)
362 #endif
363
364 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
365 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
366
367 if (!m_serialObj) {
368 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
369
370 if (!m_serialObj) {
371 wxLogError(_("Can't find the serialization object '%s' "
372 "for the object '%s'."),
373 obj_name.c_str(),
374 GetClassInfo()->GetClassName());
375 return;
376 }
377 m_serialObj->SetObject(this);
378 }
379
380 m_serialObj->LoadObject(stream);
381 }
382
383 #endif // wxUSE_SERIAL
384
385 /*
386 * wxObject: cloning of objects
387 */
388
389 void wxObject::Ref(const wxObject& clone)
390 {
391 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
392 DEBUG_PRINTF(wxObject::Ref)
393 #endif
394 // delete reference to old data
395 UnRef();
396 // reference new data
397 if (clone.m_refData) {
398 m_refData = clone.m_refData;
399 ++(m_refData->m_count);
400 }
401 }
402
403 void wxObject::UnRef()
404 {
405 if ( m_refData )
406 {
407 wxASSERT_MSG( m_refData->m_count > 0, _T("invalid ref data count") );
408
409 if ( !--m_refData->m_count )
410 delete m_refData;
411 m_refData = (wxObjectRefData *) NULL;
412 }
413 }
414
415 /*
416 * wxObjectData
417 */
418
419 wxObjectRefData::wxObjectRefData(void) : m_count(1)
420 {
421 }
422
423 wxObjectRefData::~wxObjectRefData()
424 {
425 }