]> git.saurik.com Git - wxWidgets.git/blame - src/common/object.cpp
Various Dialog Editor-related mods; Dlg Ed. getting usable now under MSW.
[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
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
1678ad78 23#ifndef WX_PRECOMP
c801d85f 24#include "wx/hash.h"
1678ad78
GL
25#include "wx/objstrm.h"
26#endif
c801d85f
KB
27
28#include <string.h>
29#include <assert.h>
30
b2aef89b 31#if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
c801d85f
KB
32#include "wx/memory.h"
33#endif
34
b2aef89b 35#if WXDEBUG || USE_DEBUG_CONTEXT
c801d85f
KB
36 // for wxObject::Dump
37 #include <iostream.h>
38#endif
39
40#if !USE_SHARED_LIBRARY
41wxClassInfo wxObject::classwxObject("wxObject", NULL, NULL, sizeof(wxObject), NULL);
42wxClassInfo *wxClassInfo::first = NULL;
43#endif
44
45/*
46 * wxWindows root object.
47 */
48
49wxObject::wxObject(void)
50{
51 m_refData = NULL;
52}
53
54wxObject::~wxObject(void)
55{
56 UnRef();
57}
58
59/*
60 * Is this object a kind of (a subclass of) 'info'?
61 * E.g. is wxWindow a kind of wxObject?
62 * Go from this class to superclass, taking into account
63 * two possible base classes.
64 */
65
66bool wxObject::IsKindOf(wxClassInfo *info)
67{
68 wxClassInfo *thisInfo = GetClassInfo();
69 if (thisInfo)
70 return thisInfo->IsKindOf(info);
71 else
72 return FALSE;
73}
74
b2aef89b 75#if WXDEBUG || USE_DEBUG_CONTEXT
c801d85f
KB
76void wxObject::Dump(ostream& str)
77{
78 if (GetClassInfo() && GetClassInfo()->GetClassName())
79 str << GetClassInfo()->GetClassName();
80 else
81 str << "unknown object class";
82}
83#endif
84
b2aef89b 85#if WXDEBUG && USE_MEMORY_TRACING
c801d85f
KB
86
87#ifdef new
88#undef new
89#endif
90
91void * wxObject::operator new (size_t size, char * fileName, int lineNum)
92{
93 return wxDebugAlloc(size, fileName, lineNum, TRUE);
94}
95
96void wxObject::operator delete (void * buf)
97{
98 wxDebugFree(buf);
99}
100
101// Cause problems for VC++ - crashes
102#ifndef _MSC_VER
103void * wxObject::operator new[] (size_t size, char * fileName, int lineNum)
104{
105 return wxDebugAlloc(size, fileName, lineNum, TRUE, TRUE);
106}
107
108void wxObject::operator delete[] (void * buf)
109{
110 wxDebugFree(buf, TRUE);
111}
112#endif
113
114#endif
115
116/*
117 * Class info: provides run-time class type information.
118 */
119
c801d85f
KB
120wxClassInfo::wxClassInfo(char *cName, char *baseName1, char *baseName2, int sz, wxObjectConstructorFn constr)
121{
122 className = cName;
123 baseClassName1 = baseName1;
124 baseClassName2 = baseName2;
125
126 objectSize = sz;
127 objectConstructor = constr;
128
129 next = first;
130 first = this;
131
132 baseInfo1 = NULL;
133 baseInfo2 = NULL;
134}
135
c801d85f
KB
136wxObject *wxClassInfo::CreateObject(void)
137{
138 if (objectConstructor)
139 return (wxObject *)(*objectConstructor)();
140 else
141 return NULL;
142}
143
144wxClassInfo *wxClassInfo::FindClass(char *c)
145{
146 wxClassInfo *p = first;
147 while (p)
148 {
149 if (p && p->GetClassName() && strcmp(p->GetClassName(), c) == 0)
150 return p;
151 p = p->next;
152 }
153 return NULL;
154}
155
156// Climb upwards through inheritance hierarchy.
157// Dual inheritance is catered for.
158bool wxClassInfo::IsKindOf(wxClassInfo *info)
159{
160 if (info == NULL)
161 return FALSE;
162
163 // For some reason, when making/using a DLL, static data has to be included
164 // in both the DLL and the application. This can lead to duplicate
165 // wxClassInfo objects, so we have to test the name instead of the pointers.
166#if WXMAKINGDLL
167 if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0))
168 return TRUE;
169#else
170 if (this == info)
171 return TRUE;
172#endif
173
174 if (baseInfo1)
175 if (baseInfo1->IsKindOf(info))
176 return TRUE;
177
178 if (baseInfo2)
179 return baseInfo2->IsKindOf(info);
180
181 return FALSE;
182}
183
184// Set pointers to base class(es) to speed up IsKindOf
185void wxClassInfo::InitializeClasses(void)
186{
187 wxHashTable table(wxKEY_STRING);
188
189 // Index all class infos by their class name
190 wxClassInfo *info = first;
191 while (info)
192 {
193 if (info->className)
194 table.Put(info->className, (wxObject *)info);
195 info = info->next;
196 }
197
198 // Set base pointers for each wxClassInfo
199 info = first;
200 while (info)
201 {
202 if (info->GetBaseClassName1())
203 info->baseInfo1 = (wxClassInfo *)table.Get(info->GetBaseClassName1());
204 if (info->GetBaseClassName2())
205 info->baseInfo2 = (wxClassInfo *)table.Get(info->GetBaseClassName2());
206 info = info->next;
207 }
208}
209
210wxObject *wxCreateDynamicObject(char *name)
211{
212 wxClassInfo *info = wxClassInfo::first;
213 while (info)
214 {
215 if (info->className && strcmp(info->className, name) == 0)
216 return info->CreateObject();
217 info = info->next;
218 }
219 return NULL;
220}
221
222#ifdef USE_STORABLE_CLASSES
223
7a4b9130
GL
224#include "wx/serbase.h"
225#include "wx/dynlib.h"
226#include "wx/msgdlg.h"
227
1678ad78 228wxObject* wxCreateStoredObject( wxInputStream &stream )
c801d85f 229{
1678ad78
GL
230 wxObjectInputStream obj_s(stream);
231 return obj_s.LoadObject();
c801d85f
KB
232};
233
7a4b9130
GL
234void wxObject::StoreObject( wxObjectOutputStream& stream )
235{
236 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
237 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
238 WXSERIAL(wxObject) *serial =
239 (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
240
241 if (!serial) {
242 wxString message;
243
244 message.Printf("Can't find the serialization object (%s) for the object %s",
245 WXSTRINGCAST obj_name, WXSTRINGCAST GetClassInfo()->GetClassName());
246 wxMessageBox(message, "Alert !");
247 return;
248 }
249
250 serial->SetObject(this);
251 serial->StoreObject(stream);
252}
253
254void wxObject::LoadObject( wxObjectInputStream& stream )
255{
256 wxString obj_name = wxString(GetClassInfo()->GetClassName()) + "_Serialize";
257 wxLibrary *lib = wxTheLibraries.LoadLibrary("wxserial");
258 WXSERIAL(wxObject) *serial =
259 (WXSERIAL(wxObject) *)lib->CreateObject( obj_name );
260
261 if (!serial) {
262 wxString message;
263
264 message.Printf("Can't find the serialization object (%s) for the object %s",
265 WXSTRINGCAST obj_name,
266 WXSTRINGCAST GetClassInfo()->GetClassName());
267 wxMessageBox(message, "Alert !");
268 return;
269 }
270
271 serial->SetObject(this);
272 serial->LoadObject(stream);
273}
274
c801d85f
KB
275#endif
276
277/*
278 * wxObject: cloning of objects
279 */
280
281void wxObject::Ref(const wxObject& clone)
282{
283 // delete reference to old data
284 UnRef();
285 // reference new data
286 if (clone.m_refData) {
287 m_refData = clone.m_refData;
288 ++(m_refData->m_count);
289 }
290}
291
292void wxObject::UnRef(void)
293{
294 if (m_refData) {
295 assert(m_refData->m_count > 0);
296 --(m_refData->m_count);
297 if (m_refData->m_count == 0)
298 delete m_refData;
299 }
300 m_refData = NULL;
301}
302
303/*
304 * wxObjectData
305 */
306
307wxObjectRefData::wxObjectRefData(void) : m_count(1)
308{
309}
310
311wxObjectRefData::~wxObjectRefData(void)
312{
313}
314