]> git.saurik.com Git - wxWidgets.git/blob - src/common/object.cpp
attempt to fix crash in tree ctrl
[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 #endif
46
47 wxClassInfo wxObject::sm_classwxObject((wxChar *) wxT("wxObject"), (wxChar *) NULL, (wxChar *) NULL, (int ) sizeof(wxObject), (wxObjectConstructorFn) NULL);
48 wxClassInfo* wxClassInfo::sm_first = (wxClassInfo *) NULL;
49 wxHashTable* wxClassInfo::sm_classTable = (wxHashTable*) NULL;
50
51 // These are here so we can avoid 'always true/false' warnings
52 // by referring to these instead of TRUE/FALSE
53 const bool wxTrue = TRUE;
54 const bool wxFalse = FALSE;
55
56 /*
57 * wxWindows root object.
58 */
59
60 wxObject::wxObject()
61 {
62 m_refData = (wxObjectRefData *) NULL;
63 #if wxUSE_SERIAL
64 m_serialObj = (wxObject_Serialize *)NULL;
65 #endif
66 }
67
68 wxObject::~wxObject()
69 {
70 UnRef();
71 #if wxUSE_SERIAL
72 if (m_serialObj)
73 delete m_serialObj;
74 #endif
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 */
83
84 bool wxObject::IsKindOf(wxClassInfo *info) const
85 {
86 wxClassInfo *thisInfo = GetClassInfo();
87 if (thisInfo)
88 return thisInfo->IsKindOf(info);
89 else
90 return FALSE;
91 }
92
93 wxObject *wxObject::Clone() const
94 {
95 wxObject *object = GetClassInfo()->CreateObject();
96 CopyObject(*object);
97 return object;
98 }
99
100 #ifdef __WXDEBUG__
101 void wxObject::CopyObject(wxObject& object_dest) const
102 #else // !Debug
103 void wxObject::CopyObject(wxObject& WXUNUSED(object_dest)) const
104 #endif // Debug/!Debug
105 {
106 wxASSERT(object_dest.GetClassInfo()->IsKindOf(GetClassInfo()));
107 }
108
109 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
110 void wxObject::Dump(ostream& str)
111 {
112 if (GetClassInfo() && GetClassInfo()->GetClassName())
113 str << GetClassInfo()->GetClassName();
114 else
115 str << "unknown object class";
116 }
117 #endif
118
119 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
120
121 #ifdef new
122 #undef new
123 #endif
124
125 void *wxObject::operator new (size_t size, wxChar * fileName, int lineNum)
126 {
127 return wxDebugAlloc(size, fileName, lineNum, TRUE);
128 }
129
130 void wxObject::operator delete (void * buf)
131 {
132 wxDebugFree(buf);
133 }
134
135 // VC++ 6.0
136 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
137 void wxObject::operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */)
138 {
139 ::operator delete(pData);
140 }
141 #endif
142
143 // Cause problems for VC++ - crashes
144 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
145 void * wxObject::operator new[] (size_t size, wxChar * fileName, int lineNum)
146 {
147 return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE);
148 }
149
150 void wxObject::operator delete[] (void * buf)
151 {
152 wxDebugFree(buf, TRUE);
153 }
154 #endif
155
156 #endif
157
158 /*
159 * Class info: provides run-time class type information.
160 */
161
162 wxClassInfo::wxClassInfo(wxChar *cName, wxChar *baseName1, wxChar *baseName2, int sz, wxObjectConstructorFn constr)
163 {
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;
176 }
177
178 wxObject *wxClassInfo::CreateObject()
179 {
180 if (m_objectConstructor)
181 return (wxObject *)(*m_objectConstructor)();
182 else
183 return (wxObject *) NULL;
184 }
185
186 wxClassInfo *wxClassInfo::FindClass(wxChar *c)
187 {
188 wxClassInfo *p = sm_first;
189 while (p)
190 {
191 if (p && p->GetClassName() && wxStrcmp(p->GetClassName(), c) == 0)
192 return p;
193 p = p->m_next;
194 }
195 return (wxClassInfo *) NULL;
196 }
197
198 // Climb upwards through inheritance hierarchy.
199 // Dual inheritance is catered for.
200 bool wxClassInfo::IsKindOf(wxClassInfo *info) const
201 {
202 if (info == NULL)
203 return FALSE;
204
205 if (this == info)
206 return TRUE;
207
208 if (m_baseInfo1)
209 if (m_baseInfo1->IsKindOf(info))
210 return TRUE;
211
212 if (m_baseInfo2)
213 return m_baseInfo2->IsKindOf(info);
214
215 return FALSE;
216 }
217
218 // Set pointers to base class(es) to speed up IsKindOf
219 void wxClassInfo::InitializeClasses()
220 {
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
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)
239 {
240 wxASSERT_MSG( ++nClass < nMaxClasses,
241 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
242
243 sm_classTable->Put(info->m_className, (wxObject *)info);
244 }
245
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 }
259 }
260
261 void wxClassInfo::CleanUpClasses()
262 {
263 delete wxClassInfo::sm_classTable;
264 wxClassInfo::sm_classTable = NULL;
265 }
266
267 wxObject *wxCreateDynamicObject(const wxChar *name)
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 {
282 if (info->m_className && wxStrcmp(info->m_className, name) == 0)
283 return info->CreateObject();
284 info = info->m_next;
285 }
286 return (wxObject*) NULL;
287 }
288 }
289
290 #if wxUSE_SERIAL
291
292 #include "wx/serbase.h"
293 #include "wx/dynlib.h"
294
295 wxObject* wxCreateStoredObject( wxInputStream &stream )
296 {
297 wxObjectInputStream obj_s(stream);
298 return obj_s.LoadObject();
299 };
300
301 void wxObject::StoreObject( wxObjectOutputStream& stream )
302 {
303 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
304 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
305
306 if (!lib) {
307 wxLogError(_("Can't load wxSerial dynamic library."));
308 return;
309 }
310 if (!m_serialObj) {
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);
321 }
322
323 m_serialObj->StoreObject(stream);
324 }
325
326 void wxObject::LoadObject( wxObjectInputStream& stream )
327 {
328 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
329 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
330
331 if (!m_serialObj) {
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);
342 }
343
344 m_serialObj->LoadObject(stream);
345 }
346
347 #endif // wxUSE_SERIAL
348
349 /*
350 * wxObject: cloning of objects
351 */
352
353 void 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
364 void wxObject::UnRef()
365 {
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 )
371 delete m_refData;
372 m_refData = (wxObjectRefData *) NULL;
373 }
374 }
375
376 /*
377 * wxObjectData
378 */
379
380 wxObjectRefData::wxObjectRefData(void) : m_count(1)
381 {
382 }
383
384 wxObjectRefData::~wxObjectRefData()
385 {
386 }