]> git.saurik.com Git - wxWidgets.git/blob - src/common/object.cpp
wxStringList::copy ctor and assignment operator added (very inefficient
[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 #ifdef wxUSE_SERIAL
26 #include "wx/objstrm.h"
27 #include "wx/serbase.h"
28 #endif
29 #endif
30
31 #include <string.h>
32 #include <assert.h>
33
34 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
35 #include "wx/memory.h"
36 #endif
37
38 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
39 // for wxObject::Dump
40 #if wxUSE_IOSTREAMH
41 # include <iostream.h>
42 #else
43 # include <iostream>
44 # ifdef _MSC_VER
45 using namespace std;
46 # endif
47 #endif
48 #endif
49
50 #if !USE_SHARED_LIBRARY
51 wxClassInfo wxObject::sm_classwxObject((char *) "wxObject", (char *) NULL, (char *) NULL, (int ) sizeof(wxObject), (wxObjectConstructorFn) NULL);
52 wxClassInfo* wxClassInfo::sm_first = (wxClassInfo *) NULL;
53 wxHashTable* wxClassInfo::sm_classTable = (wxHashTable*) NULL;
54 #endif
55
56 /*
57 * wxWindows root object.
58 */
59
60 wxObject::wxObject(void)
61 {
62 m_refData = (wxObjectRefData *) NULL;
63 #ifdef wxUSE_SERIAL
64 m_serialObj = (wxObject_Serialize *)NULL;
65 #endif
66 }
67
68 wxObject::~wxObject(void)
69 {
70 UnRef();
71 #ifdef 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 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
94 void wxObject::Dump(ostream& str)
95 {
96 if (GetClassInfo() && GetClassInfo()->GetClassName())
97 str << GetClassInfo()->GetClassName();
98 else
99 str << "unknown object class";
100 }
101 #endif
102
103 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
104
105 #ifdef new
106 #undef new
107 #endif
108
109 void * wxObject::operator new (size_t size, char * fileName, int lineNum)
110 {
111 return wxDebugAlloc(size, fileName, lineNum, TRUE);
112 }
113
114 void wxObject::operator delete (void * buf)
115 {
116 wxDebugFree(buf);
117 }
118
119 // VC++ 6.0
120 #if _MSC_VER >= 1200
121 void wxObject::operator delete(void* pData, char* /* fileName */, int /* lineNum */)
122 {
123 ::operator delete(pData);
124 }
125 #endif
126
127 // Cause problems for VC++ - crashes
128 #ifndef _MSC_VER
129 void * wxObject::operator new[] (size_t size, char * fileName, int lineNum)
130 {
131 return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE);
132 }
133
134 void wxObject::operator delete[] (void * buf)
135 {
136 wxDebugFree(buf, TRUE);
137 }
138 #endif
139
140 #endif
141
142 /*
143 * Class info: provides run-time class type information.
144 */
145
146 wxClassInfo::wxClassInfo(char *cName, char *baseName1, char *baseName2, int sz, wxObjectConstructorFn constr)
147 {
148 m_className = cName;
149 m_baseClassName1 = baseName1;
150 m_baseClassName2 = baseName2;
151
152 m_objectSize = sz;
153 m_objectConstructor = constr;
154
155 m_next = sm_first;
156 sm_first = this;
157
158 m_baseInfo1 = (wxClassInfo *) NULL;
159 m_baseInfo2 = (wxClassInfo *) NULL;
160 }
161
162 wxObject *wxClassInfo::CreateObject(void)
163 {
164 if (m_objectConstructor)
165 return (wxObject *)(*m_objectConstructor)();
166 else
167 return (wxObject *) NULL;
168 }
169
170 wxClassInfo *wxClassInfo::FindClass(char *c)
171 {
172 wxClassInfo *p = sm_first;
173 while (p)
174 {
175 if (p && p->GetClassName() && strcmp(p->GetClassName(), c) == 0)
176 return p;
177 p = p->m_next;
178 }
179 return (wxClassInfo *) NULL;
180 }
181
182 // Climb upwards through inheritance hierarchy.
183 // Dual inheritance is catered for.
184 bool wxClassInfo::IsKindOf(wxClassInfo *info) const
185 {
186 if (info == NULL)
187 return FALSE;
188
189 // For some reason, when making/using a DLL, static data has to be included
190 // in both the DLL and the application. This can lead to duplicate
191 // wxClassInfo objects, so we have to test the name instead of the pointers.
192 // PROBABLY NO LONGER TRUE now I've done DLL creation right.
193 /*
194 #if WXMAKINGDLL
195 if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0))
196 return TRUE;
197 #else
198 */
199 if (this == info)
200 return TRUE;
201
202 if (m_baseInfo1)
203 if (m_baseInfo1->IsKindOf(info))
204 return TRUE;
205
206 if (m_baseInfo2)
207 return m_baseInfo2->IsKindOf(info);
208
209 return FALSE;
210 }
211
212 // Set pointers to base class(es) to speed up IsKindOf
213 void wxClassInfo::InitializeClasses(void)
214 {
215 wxClassInfo::sm_classTable = new wxHashTable(wxKEY_STRING);
216
217 // Index all class infos by their class name
218 wxClassInfo *info = sm_first;
219 while (info)
220 {
221 if (info->m_className)
222 sm_classTable->Put(info->m_className, (wxObject *)info);
223 info = info->m_next;
224 }
225
226 // Set base pointers for each wxClassInfo
227 info = sm_first;
228 while (info)
229 {
230 if (info->GetBaseClassName1())
231 info->m_baseInfo1 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName1());
232 if (info->GetBaseClassName2())
233 info->m_baseInfo2 = (wxClassInfo *)sm_classTable->Get(info->GetBaseClassName2());
234 info = info->m_next;
235 }
236 }
237
238 void wxClassInfo::CleanUpClasses(void)
239 {
240 delete wxClassInfo::sm_classTable;
241 wxClassInfo::sm_classTable = NULL;
242 }
243
244 wxObject *wxCreateDynamicObject(const char *name)
245 {
246 if (wxClassInfo::sm_classTable)
247 {
248 wxClassInfo *info = (wxClassInfo *)wxClassInfo::sm_classTable->Get(name);
249 if (!info)
250 return (wxObject *)NULL;
251
252 return info->CreateObject();
253 }
254 else
255 {
256 wxClassInfo *info = wxClassInfo::sm_first;
257 while (info)
258 {
259 if (info->m_className && strcmp(info->m_className, name) == 0)
260 return info->CreateObject();
261 info = info->m_next;
262 }
263 return (wxObject*) NULL;
264 }
265 return (wxObject*) NULL;
266 }
267
268 #ifdef wxUSE_SERIAL
269
270 #include "wx/serbase.h"
271 #include "wx/dynlib.h"
272 #include "wx/msgdlg.h"
273
274 wxObject* wxCreateStoredObject( wxInputStream &stream )
275 {
276 wxObjectInputStream obj_s(stream);
277 return obj_s.LoadObject();
278 };
279
280 void wxObject::StoreObject( wxObjectOutputStream& stream )
281 {
282 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
283 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
284
285 if (!lib) {
286 wxMessageBox("Can't load wxSerial dynamic library.", "Alert !");
287 return;
288 }
289 if (!m_serialObj) {
290 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
291
292 if (!m_serialObj) {
293 wxString message;
294
295 message.Printf("Can't find the serialization object (%s) for the object %s",
296 WXSTRINGCAST obj_name,
297 WXSTRINGCAST GetClassInfo()->GetClassName());
298 wxMessageBox(message, "Alert !");
299 return;
300 }
301 m_serialObj->SetObject(this);
302 }
303
304 m_serialObj->StoreObject(stream);
305 }
306
307 void wxObject::LoadObject( wxObjectInputStream& stream )
308 {
309 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
310 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
311
312 if (!m_serialObj) {
313 m_serialObj = (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
314
315 if (!m_serialObj) {
316 wxString message;
317
318 message.Printf("Can't find the serialization object (%s) for the object %s",
319 WXSTRINGCAST obj_name,
320 WXSTRINGCAST GetClassInfo()->GetClassName());
321 wxMessageBox(message, "Alert !");
322 return;
323 }
324 m_serialObj->SetObject(this);
325 }
326
327 m_serialObj->LoadObject(stream);
328 }
329
330 #endif
331
332 /*
333 * wxObject: cloning of objects
334 */
335
336 void wxObject::Ref(const wxObject& clone)
337 {
338 // delete reference to old data
339 UnRef();
340 // reference new data
341 if (clone.m_refData) {
342 m_refData = clone.m_refData;
343 ++(m_refData->m_count);
344 }
345 }
346
347 void wxObject::UnRef(void)
348 {
349 if (m_refData) {
350 assert(m_refData->m_count > 0);
351 --(m_refData->m_count);
352 if (m_refData->m_count == 0)
353 delete m_refData;
354 }
355 m_refData = (wxObjectRefData *) NULL;
356 }
357
358 /*
359 * wxObjectData
360 */
361
362 wxObjectRefData::wxObjectRefData(void) : m_count(1)
363 {
364 }
365
366 wxObjectRefData::~wxObjectRefData(void)
367 {
368 }
369
370 // These are here so we can avoid 'always true/false' warnings
371 // by referring to these instead of TRUE/FALSE
372 const bool wxTrue = TRUE;
373 const bool wxFalse = FALSE;