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"
32 #endif // wxUSE_SERIAL
38 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
39 #include "wx/memory.h"
42 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
44 #include "wx/ioswrap.h"
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
;
54 * wxWindows root object.
59 m_refData
= (wxObjectRefData
*) NULL
;
61 m_serialObj
= (wxObject_Serialize
*)NULL
;
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.
81 bool wxObject::IsKindOf(wxClassInfo
*info
) const
83 wxClassInfo
*thisInfo
= GetClassInfo();
85 return thisInfo
->IsKindOf(info
);
90 wxObject
*wxObject::Clone() const
92 return GetClassInfo()->CreateObject();
95 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
96 void wxObject::Dump(ostream
& str
)
98 if (GetClassInfo() && GetClassInfo()->GetClassName())
99 str
<< GetClassInfo()->GetClassName();
101 str
<< "unknown object class";
105 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
111 void *wxObject::operator new (size_t size
, wxChar
* fileName
, int lineNum
)
113 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
116 void wxObject::operator delete (void * buf
)
122 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
123 void wxObject::operator delete(void* pData
, char* /* fileName */, int /* lineNum */)
125 ::operator delete(pData
);
129 // Cause problems for VC++ - crashes
130 #if !defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS
131 void * wxObject::operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
133 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
136 void wxObject::operator delete[] (void * buf
)
138 wxDebugFree(buf
, TRUE
);
145 * Class info: provides run-time class type information.
148 wxClassInfo::wxClassInfo(wxChar
*cName
, wxChar
*baseName1
, wxChar
*baseName2
, int sz
, wxObjectConstructorFn constr
)
151 m_baseClassName1
= baseName1
;
152 m_baseClassName2
= baseName2
;
155 m_objectConstructor
= constr
;
160 m_baseInfo1
= (wxClassInfo
*) NULL
;
161 m_baseInfo2
= (wxClassInfo
*) NULL
;
164 wxObject
*wxClassInfo::CreateObject()
166 if (m_objectConstructor
)
167 return (wxObject
*)(*m_objectConstructor
)();
169 return (wxObject
*) NULL
;
172 wxClassInfo
*wxClassInfo::FindClass(wxChar
*c
)
174 wxClassInfo
*p
= sm_first
;
177 if (p
&& p
->GetClassName() && wxStrcmp(p
->GetClassName(), c
) == 0)
181 return (wxClassInfo
*) NULL
;
184 // Climb upwards through inheritance hierarchy.
185 // Dual inheritance is catered for.
186 bool wxClassInfo::IsKindOf(wxClassInfo
*info
) const
191 // For some reason, when making/using a DLL, static data has to be included
192 // in both the DLL and the application. This can lead to duplicate
193 // wxClassInfo objects, so we have to test the name instead of the pointers.
194 // PROBABLY NO LONGER TRUE now I've done DLL creation right.
197 if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0))
205 if (m_baseInfo1
->IsKindOf(info
))
209 return m_baseInfo2
->IsKindOf(info
);
214 // Set pointers to base class(es) to speed up IsKindOf
215 void wxClassInfo::InitializeClasses()
217 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
219 // Index all class infos by their class name
220 wxClassInfo
*info
= sm_first
;
223 if (info
->m_className
)
224 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
228 // Set base pointers for each wxClassInfo
232 if (info
->GetBaseClassName1())
233 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
234 if (info
->GetBaseClassName2())
235 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
240 void wxClassInfo::CleanUpClasses()
242 delete wxClassInfo::sm_classTable
;
243 wxClassInfo::sm_classTable
= NULL
;
246 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
248 if (wxClassInfo::sm_classTable
)
250 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
252 return (wxObject
*)NULL
;
254 return info
->CreateObject();
258 wxClassInfo
*info
= wxClassInfo::sm_first
;
261 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
262 return info
->CreateObject();
265 return (wxObject
*) NULL
;
267 return (wxObject
*) NULL
;
272 #include "wx/serbase.h"
273 #include "wx/dynlib.h"
274 #include "wx/msgdlg.h"
276 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
278 wxObjectInputStream
obj_s(stream
);
279 return obj_s
.LoadObject();
282 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
284 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
285 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
288 wxLogError(_("Can't load wxSerial dynamic library."));
292 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
295 wxLogError(_("Can't find the serialization object '%s' "
296 "for the object '%s'."),
298 GetClassInfo()->GetClassName());
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
);
316 wxLogError(_("Can't find the serialization object '%s' "
317 "for the object '%s'."),
319 GetClassInfo()->GetClassName());
322 m_serialObj
->SetObject(this);
325 m_serialObj
->LoadObject(stream
);
328 #endif // wxUSE_SERIAL
331 * wxObject: cloning of objects
334 void wxObject::Ref(const wxObject
& clone
)
336 // delete reference to old data
338 // reference new data
339 if (clone
.m_refData
) {
340 m_refData
= clone
.m_refData
;
341 ++(m_refData
->m_count
);
345 void wxObject::UnRef()
348 assert(m_refData
->m_count
> 0);
349 --(m_refData
->m_count
);
350 if (m_refData
->m_count
== 0)
353 m_refData
= (wxObjectRefData
*) NULL
;
360 wxObjectRefData::wxObjectRefData(void) : m_count(1)
364 wxObjectRefData::~wxObjectRefData()
368 // These are here so we can avoid 'always true/false' warnings
369 // by referring to these instead of TRUE/FALSE
370 const bool wxTrue
= TRUE
;
371 const bool wxFalse
= FALSE
;