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