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 && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
35 #include "wx/memory.h"
38 #if WXDEBUG || wxUSE_DEBUG_CONTEXT
43 #if !USE_SHARED_LIBRARY
44 wxClassInfo
wxObject::sm_classwxObject((char *) "wxObject", (char *) NULL
, (char *) NULL
, (int ) sizeof(wxObject
), (wxObjectConstructorFn
) NULL
);
45 wxClassInfo
* wxClassInfo::sm_first
= (wxClassInfo
*) NULL
;
46 wxHashTable
* wxClassInfo::sm_classTable
= (wxHashTable
*) NULL
;
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
) const
79 wxClassInfo
*thisInfo
= GetClassInfo();
81 return thisInfo
->IsKindOf(info
);
86 #if WXDEBUG || wxUSE_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 && wxUSE_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
)
114 void operator delete(void* pData
, char* /* fileName */, int /* lineNum */)
116 ::operator delete(pData
);
120 // Cause problems for VC++ - crashes
122 void * wxObject::operator new[] (size_t size
, char * fileName
, int lineNum
)
124 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
127 void wxObject::operator delete[] (void * buf
)
129 wxDebugFree(buf
, TRUE
);
136 * Class info: provides run-time class type information.
139 wxClassInfo::wxClassInfo(char *cName
, char *baseName1
, char *baseName2
, int sz
, wxObjectConstructorFn constr
)
142 m_baseClassName1
= baseName1
;
143 m_baseClassName2
= baseName2
;
146 m_objectConstructor
= constr
;
151 m_baseInfo1
= (wxClassInfo
*) NULL
;
152 m_baseInfo2
= (wxClassInfo
*) NULL
;
155 wxObject
*wxClassInfo::CreateObject(void)
157 if (m_objectConstructor
)
158 return (wxObject
*)(*m_objectConstructor
)();
160 return (wxObject
*) NULL
;
163 wxClassInfo
*wxClassInfo::FindClass(char *c
)
165 wxClassInfo
*p
= sm_first
;
168 if (p
&& p
->GetClassName() && strcmp(p
->GetClassName(), c
) == 0)
172 return (wxClassInfo
*) NULL
;
175 // Climb upwards through inheritance hierarchy.
176 // Dual inheritance is catered for.
177 bool wxClassInfo::IsKindOf(wxClassInfo
*info
) const
182 // For some reason, when making/using a DLL, static data has to be included
183 // in both the DLL and the application. This can lead to duplicate
184 // wxClassInfo objects, so we have to test the name instead of the pointers.
185 // PROBABLY NO LONGER TRUE now I've done DLL creation right.
188 if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0))
196 if (m_baseInfo1
->IsKindOf(info
))
200 return m_baseInfo2
->IsKindOf(info
);
205 // Set pointers to base class(es) to speed up IsKindOf
206 void wxClassInfo::InitializeClasses(void)
208 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
210 // Index all class infos by their class name
211 wxClassInfo
*info
= sm_first
;
214 if (info
->m_className
)
215 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
219 // Set base pointers for each wxClassInfo
223 if (info
->GetBaseClassName1())
224 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
225 if (info
->GetBaseClassName2())
226 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
231 void wxClassInfo::CleanUpClasses(void)
233 delete wxClassInfo::sm_classTable
;
234 wxClassInfo::sm_classTable
= NULL
;
237 wxObject
*wxCreateDynamicObject(const char *name
)
239 if (wxClassInfo::sm_classTable
)
241 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
243 return (wxObject
*)NULL
;
245 return info
->CreateObject();
249 wxClassInfo
*info
= wxClassInfo::sm_first
;
252 if (info
->m_className
&& strcmp(info
->m_className
, name
) == 0)
253 return info
->CreateObject();
256 return (wxObject
*) NULL
;
258 return (wxObject
*) NULL
;
263 #include "wx/serbase.h"
264 #include "wx/dynlib.h"
265 #include "wx/msgdlg.h"
267 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
269 wxObjectInputStream
obj_s(stream
);
270 return obj_s
.LoadObject();
273 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
275 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
276 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
279 wxMessageBox("Can't load wxSerial dynamic library.", "Alert !");
283 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
288 message
.Printf("Can't find the serialization object (%s) for the object %s",
289 WXSTRINGCAST obj_name
,
290 WXSTRINGCAST
GetClassInfo()->GetClassName());
291 wxMessageBox(message
, "Alert !");
294 m_serialObj
->SetObject(this);
297 m_serialObj
->StoreObject(stream
);
300 void wxObject::LoadObject( wxObjectInputStream
& stream
)
302 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
303 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
306 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
311 message
.Printf("Can't find the serialization object (%s) for the object %s",
312 WXSTRINGCAST obj_name
,
313 WXSTRINGCAST
GetClassInfo()->GetClassName());
314 wxMessageBox(message
, "Alert !");
317 m_serialObj
->SetObject(this);
320 m_serialObj
->LoadObject(stream
);
326 * wxObject: cloning of objects
329 void wxObject::Ref(const wxObject
& clone
)
331 // delete reference to old data
333 // reference new data
334 if (clone
.m_refData
) {
335 m_refData
= clone
.m_refData
;
336 ++(m_refData
->m_count
);
340 void wxObject::UnRef(void)
343 assert(m_refData
->m_count
> 0);
344 --(m_refData
->m_count
);
345 if (m_refData
->m_count
== 0)
348 m_refData
= (wxObjectRefData
*) NULL
;
355 wxObjectRefData::wxObjectRefData(void) : m_count(1)
359 wxObjectRefData::~wxObjectRefData(void)
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
;