1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxObject implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "object.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/objstrm.h"
27 #include "wx/serbase.h"
34 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
35 #include "wx/memory.h"
38 #if WXDEBUG || USE_DEBUG_CONTEXT
43 #if !USE_SHARED_LIBRARY
44 wxClassInfo
wxObject::classwxObject((char *) "wxObject", (char *) NULL
, (char *) NULL
, (int ) sizeof(wxObject
), (wxObjectConstructorFn
) NULL
);
45 wxClassInfo
*wxClassInfo::first
= (wxClassInfo
*) NULL
;
46 wxHashTable
wxClassInfo::classTable(wxKEY_STRING
);
50 * wxWindows root object.
53 wxObject::wxObject(void)
55 m_refData
= (wxObjectRefData
*) NULL
;
57 m_serialObj
= (wxObject_Serialize
*)NULL
;
61 wxObject::~wxObject(void)
71 * Is this object a kind of (a subclass of) 'info'?
72 * E.g. is wxWindow a kind of wxObject?
73 * Go from this class to superclass, taking into account
74 * two possible base classes.
77 bool wxObject::IsKindOf(wxClassInfo
*info
)
79 wxClassInfo
*thisInfo
= GetClassInfo();
81 return thisInfo
->IsKindOf(info
);
86 #if WXDEBUG || USE_DEBUG_CONTEXT
87 void wxObject::Dump(ostream
& str
)
89 if (GetClassInfo() && GetClassInfo()->GetClassName())
90 str
<< GetClassInfo()->GetClassName();
92 str
<< "unknown object class";
96 #if WXDEBUG && USE_MEMORY_TRACING
102 void * wxObject::operator new (size_t size
, char * fileName
, int lineNum
)
104 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
107 void wxObject::operator delete (void * buf
)
112 // Cause problems for VC++ - crashes
114 void * wxObject::operator new[] (size_t size
, char * fileName
, int lineNum
)
116 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
119 void wxObject::operator delete[] (void * buf
)
121 wxDebugFree(buf
, TRUE
);
128 * Class info: provides run-time class type information.
131 wxClassInfo::wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn constr
)
134 baseClassName1
= baseName1
;
135 baseClassName2
= baseName2
;
138 objectConstructor
= constr
;
143 baseInfo1
= (wxClassInfo
*) NULL
;
144 baseInfo2
= (wxClassInfo
*) NULL
;
147 wxObject
*wxClassInfo::CreateObject(void)
149 if (objectConstructor
)
150 return (wxObject
*)(*objectConstructor
)();
152 return (wxObject
*) NULL
;
155 wxClassInfo
*wxClassInfo::FindClass(char *c
)
157 wxClassInfo
*p
= first
;
160 if (p
&& p
->GetClassName() && strcmp(p
->GetClassName(), c
) == 0)
164 return (wxClassInfo
*) NULL
;
167 // Climb upwards through inheritance hierarchy.
168 // Dual inheritance is catered for.
169 bool wxClassInfo::IsKindOf(wxClassInfo
*info
)
174 // For some reason, when making/using a DLL, static data has to be included
175 // in both the DLL and the application. This can lead to duplicate
176 // wxClassInfo objects, so we have to test the name instead of the pointers.
178 if (GetClassName() && info
->GetClassName() && (strcmp(GetClassName(), info
->GetClassName()) == 0))
186 if (baseInfo1
->IsKindOf(info
))
190 return baseInfo2
->IsKindOf(info
);
195 // Set pointers to base class(es) to speed up IsKindOf
196 void wxClassInfo::InitializeClasses(void)
198 // Index all class infos by their class name
199 wxClassInfo
*info
= first
;
203 classTable
.Put(info
->className
, (wxObject
*)info
);
207 // Set base pointers for each wxClassInfo
211 if (info
->GetBaseClassName1())
212 info
->baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
213 if (info
->GetBaseClassName2())
214 info
->baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
220 wxObject
*wxCreateDynamicObject(char *name
)
224 info
= (wxClassInfo
*)wxClassInfo::classTable
.Get(name
);
226 return (wxObject
*)NULL
;
228 return info
->CreateObject();
233 #include "wx/serbase.h"
234 #include "wx/dynlib.h"
235 #include "wx/msgdlg.h"
237 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
239 wxObjectInputStream
obj_s(stream
);
240 return obj_s
.LoadObject();
243 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
245 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
246 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
249 wxMessageBox("Can't load wxSerial dynamic library.", "Alert !");
253 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
258 message
.Printf("Can't find the serialization object (%s) for the object %s",
259 WXSTRINGCAST obj_name
,
260 WXSTRINGCAST
GetClassInfo()->GetClassName());
261 wxMessageBox(message
, "Alert !");
264 m_serialObj
->SetObject(this);
267 m_serialObj
->StoreObject(stream
);
270 void wxObject::LoadObject( wxObjectInputStream
& stream
)
272 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
273 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
276 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
281 message
.Printf("Can't find the serialization object (%s) for the object %s",
282 WXSTRINGCAST obj_name
,
283 WXSTRINGCAST
GetClassInfo()->GetClassName());
284 wxMessageBox(message
, "Alert !");
287 m_serialObj
->SetObject(this);
290 m_serialObj
->LoadObject(stream
);
296 * wxObject: cloning of objects
299 void wxObject::Ref(const wxObject
& clone
)
301 // delete reference to old data
303 // reference new data
304 if (clone
.m_refData
) {
305 m_refData
= clone
.m_refData
;
306 ++(m_refData
->m_count
);
310 void wxObject::UnRef(void)
313 assert(m_refData
->m_count
> 0);
314 --(m_refData
->m_count
);
315 if (m_refData
->m_count
== 0)
318 m_refData
= (wxObjectRefData
*) NULL
;
325 wxObjectRefData::wxObjectRefData(void) : m_count(1)
329 wxObjectRefData::~wxObjectRefData(void)