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