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 wxObject
*object
= GetClassInfo()->CreateObject();
97 void wxObject::CopyObject(wxObject
& object_dest
) const
99 wxASSERT(object_dest
.GetClassInfo()->IsKindOf(GetClassInfo()));
102 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
103 void wxObject::Dump(ostream
& str
)
105 if (GetClassInfo() && GetClassInfo()->GetClassName())
106 str
<< GetClassInfo()->GetClassName();
108 str
<< "unknown object class";
112 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
118 void *wxObject::operator new (size_t size
, wxChar
* fileName
, int lineNum
)
120 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
123 void wxObject::operator delete (void * buf
)
129 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
130 void wxObject::operator delete(void* pData
, char* /* fileName */, int /* lineNum */)
132 ::operator delete(pData
);
136 // Cause problems for VC++ - crashes
137 #if !defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS
138 void * wxObject::operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
140 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
143 void wxObject::operator delete[] (void * buf
)
145 wxDebugFree(buf
, TRUE
);
152 * Class info: provides run-time class type information.
155 wxClassInfo::wxClassInfo(wxChar
*cName
, wxChar
*baseName1
, wxChar
*baseName2
, int sz
, wxObjectConstructorFn constr
)
158 m_baseClassName1
= baseName1
;
159 m_baseClassName2
= baseName2
;
162 m_objectConstructor
= constr
;
167 m_baseInfo1
= (wxClassInfo
*) NULL
;
168 m_baseInfo2
= (wxClassInfo
*) NULL
;
171 wxObject
*wxClassInfo::CreateObject()
173 if (m_objectConstructor
)
174 return (wxObject
*)(*m_objectConstructor
)();
176 return (wxObject
*) NULL
;
179 wxClassInfo
*wxClassInfo::FindClass(wxChar
*c
)
181 wxClassInfo
*p
= sm_first
;
184 if (p
&& p
->GetClassName() && wxStrcmp(p
->GetClassName(), c
) == 0)
188 return (wxClassInfo
*) NULL
;
191 // Climb upwards through inheritance hierarchy.
192 // Dual inheritance is catered for.
193 bool wxClassInfo::IsKindOf(wxClassInfo
*info
) const
198 // For some reason, when making/using a DLL, static data has to be included
199 // in both the DLL and the application. This can lead to duplicate
200 // wxClassInfo objects, so we have to test the name instead of the pointers.
201 // PROBABLY NO LONGER TRUE now I've done DLL creation right.
204 if (GetClassName() && info->GetClassName() && (strcmp(GetClassName(), info->GetClassName()) == 0))
212 if (m_baseInfo1
->IsKindOf(info
))
216 return m_baseInfo2
->IsKindOf(info
);
221 // Set pointers to base class(es) to speed up IsKindOf
222 void wxClassInfo::InitializeClasses()
224 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
226 // Index all class infos by their class name
227 wxClassInfo
*info
= sm_first
;
230 if (info
->m_className
)
231 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
235 // Set base pointers for each wxClassInfo
239 if (info
->GetBaseClassName1())
240 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
241 if (info
->GetBaseClassName2())
242 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
247 void wxClassInfo::CleanUpClasses()
249 delete wxClassInfo::sm_classTable
;
250 wxClassInfo::sm_classTable
= NULL
;
253 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
255 if (wxClassInfo::sm_classTable
)
257 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
259 return (wxObject
*)NULL
;
261 return info
->CreateObject();
265 wxClassInfo
*info
= wxClassInfo::sm_first
;
268 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
269 return info
->CreateObject();
272 return (wxObject
*) NULL
;
274 return (wxObject
*) NULL
;
279 #include "wx/serbase.h"
280 #include "wx/dynlib.h"
281 #include "wx/msgdlg.h"
283 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
285 wxObjectInputStream
obj_s(stream
);
286 return obj_s
.LoadObject();
289 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
291 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
292 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
295 wxLogError(_("Can't load wxSerial dynamic library."));
299 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
302 wxLogError(_("Can't find the serialization object '%s' "
303 "for the object '%s'."),
305 GetClassInfo()->GetClassName());
308 m_serialObj
->SetObject(this);
311 m_serialObj
->StoreObject(stream
);
314 void wxObject::LoadObject( wxObjectInputStream
& stream
)
316 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
317 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
320 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
323 wxLogError(_("Can't find the serialization object '%s' "
324 "for the object '%s'."),
326 GetClassInfo()->GetClassName());
329 m_serialObj
->SetObject(this);
332 m_serialObj
->LoadObject(stream
);
335 #endif // wxUSE_SERIAL
338 * wxObject: cloning of objects
341 void wxObject::Ref(const wxObject
& clone
)
343 // delete reference to old data
345 // reference new data
346 if (clone
.m_refData
) {
347 m_refData
= clone
.m_refData
;
348 ++(m_refData
->m_count
);
352 void wxObject::UnRef()
355 assert(m_refData
->m_count
> 0);
356 --(m_refData
->m_count
);
357 if (m_refData
->m_count
== 0)
360 m_refData
= (wxObjectRefData
*) NULL
;
367 wxObjectRefData::wxObjectRefData(void) : m_count(1)
371 wxObjectRefData::~wxObjectRefData()
375 // These are here so we can avoid 'always true/false' warnings
376 // by referring to these instead of TRUE/FALSE
377 const bool wxTrue
= TRUE
;
378 const bool wxFalse
= FALSE
;