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 (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
35 #include "wx/memory.h"
38 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
41 # include <iostream.h>
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
;
57 * wxWindows root object.
60 wxObject::wxObject(void)
62 m_refData
= (wxObjectRefData
*) NULL
;
64 m_serialObj
= (wxObject_Serialize
*)NULL
;
68 wxObject::~wxObject(void)
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.
84 bool wxObject::IsKindOf(wxClassInfo
*info
) const
86 wxClassInfo
*thisInfo
= GetClassInfo();
88 return thisInfo
->IsKindOf(info
);
93 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
94 void wxObject::Dump(ostream
& str
)
96 if (GetClassInfo() && GetClassInfo()->GetClassName())
97 str
<< GetClassInfo()->GetClassName();
99 str
<< "unknown object class";
103 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
109 void * wxObject::operator new (size_t size
, char * fileName
, int lineNum
)
111 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
114 void wxObject::operator delete (void * buf
)
121 void wxObject::operator delete(void* pData
, char* /* fileName */, int /* lineNum */)
123 ::operator delete(pData
);
127 // Cause problems for VC++ - crashes
129 void * wxObject::operator new[] (size_t size
, char * fileName
, int lineNum
)
131 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
134 void wxObject::operator delete[] (void * buf
)
136 wxDebugFree(buf
, TRUE
);
143 * Class info: provides run-time class type information.
146 wxClassInfo::wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn constr
)
149 m_baseClassName1
= baseName1
;
150 m_baseClassName2
= baseName2
;
153 m_objectConstructor
= constr
;
158 m_baseInfo1
= (wxClassInfo
*) NULL
;
159 m_baseInfo2
= (wxClassInfo
*) NULL
;
162 wxObject
*wxClassInfo::CreateObject(void)
164 if (m_objectConstructor
)
165 return (wxObject
*)(*m_objectConstructor
)();
167 return (wxObject
*) NULL
;
170 wxClassInfo
*wxClassInfo::FindClass(char *c
)
172 wxClassInfo
*p
= sm_first
;
175 if (p
&& p
->GetClassName() && strcmp(p
->GetClassName(), c
) == 0)
179 return (wxClassInfo
*) NULL
;
182 // Climb upwards through inheritance hierarchy.
183 // Dual inheritance is catered for.
184 bool wxClassInfo::IsKindOf(wxClassInfo
*info
) const
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.
195 if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0))
203 if (m_baseInfo1
->IsKindOf(info
))
207 return m_baseInfo2
->IsKindOf(info
);
212 // Set pointers to base class(es) to speed up IsKindOf
213 void wxClassInfo::InitializeClasses(void)
215 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
217 // Index all class infos by their class name
218 wxClassInfo
*info
= sm_first
;
221 if (info
->m_className
)
222 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
226 // Set base pointers for each wxClassInfo
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());
238 void wxClassInfo::CleanUpClasses(void)
240 delete wxClassInfo::sm_classTable
;
241 wxClassInfo::sm_classTable
= NULL
;
244 wxObject
*wxCreateDynamicObject(const char *name
)
246 if (wxClassInfo::sm_classTable
)
248 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
250 return (wxObject
*)NULL
;
252 return info
->CreateObject();
256 wxClassInfo
*info
= wxClassInfo::sm_first
;
259 if (info
->m_className
&& strcmp(info
->m_className
, name
) == 0)
260 return info
->CreateObject();
263 return (wxObject
*) NULL
;
265 return (wxObject
*) NULL
;
270 #include "wx/serbase.h"
271 #include "wx/dynlib.h"
272 #include "wx/msgdlg.h"
274 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
276 wxObjectInputStream
obj_s(stream
);
277 return obj_s
.LoadObject();
280 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
282 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
283 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
286 wxMessageBox("Can't load wxSerial dynamic library.", "Alert !");
290 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
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 !");
301 m_serialObj
->SetObject(this);
304 m_serialObj
->StoreObject(stream
);
307 void wxObject::LoadObject( wxObjectInputStream
& stream
)
309 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
310 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
313 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
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 !");
324 m_serialObj
->SetObject(this);
327 m_serialObj
->LoadObject(stream
);
333 * wxObject: cloning of objects
336 void wxObject::Ref(const wxObject
& clone
)
338 // delete reference to old data
340 // reference new data
341 if (clone
.m_refData
) {
342 m_refData
= clone
.m_refData
;
343 ++(m_refData
->m_count
);
347 void wxObject::UnRef(void)
350 assert(m_refData
->m_count
> 0);
351 --(m_refData
->m_count
);
352 if (m_refData
->m_count
== 0)
355 m_refData
= (wxObjectRefData
*) NULL
;
362 wxObjectRefData::wxObjectRefData(void) : m_count(1)
366 wxObjectRefData::~wxObjectRefData(void)
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
;