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