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 wxClassInfo
wxObject::sm_classwxObject((wxChar
*) wxT("wxObject"), (wxChar
*) NULL
, (wxChar
*) NULL
, (int ) sizeof(wxObject
), (wxObjectConstructorFn
) NULL
);
48 wxClassInfo
* wxClassInfo::sm_first
= (wxClassInfo
*) NULL
;
49 wxHashTable
* wxClassInfo::sm_classTable
= (wxHashTable
*) NULL
;
51 // These are here so we can avoid 'always true/false' warnings
52 // by referring to these instead of TRUE/FALSE
53 const bool wxTrue
= TRUE
;
54 const bool wxFalse
= FALSE
;
57 * wxWindows root object.
62 m_refData
= (wxObjectRefData
*) NULL
;
64 m_serialObj
= (wxObject_Serialize
*)NULL
;
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 wxObject
*wxObject::Clone() const
95 wxObject
*object
= GetClassInfo()->CreateObject();
101 void wxObject::CopyObject(wxObject
& object_dest
) const
103 void wxObject::CopyObject(wxObject
& WXUNUSED(object_dest
)) const
104 #endif // Debug/!Debug
106 wxASSERT(object_dest
.GetClassInfo()->IsKindOf(GetClassInfo()));
109 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
110 void wxObject::Dump(ostream
& str
)
112 if (GetClassInfo() && GetClassInfo()->GetClassName())
113 str
<< GetClassInfo()->GetClassName();
115 str
<< "unknown object class";
119 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
125 void *wxObject::operator new (size_t size
, wxChar
* fileName
, int lineNum
)
127 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
130 void wxObject::operator delete (void * buf
)
136 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
137 void wxObject::operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
139 ::operator delete(pData
);
143 // Cause problems for VC++ - crashes
144 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
145 void * wxObject::operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
147 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
150 void wxObject::operator delete[] (void * buf
)
152 wxDebugFree(buf
, TRUE
);
159 * Class info: provides run-time class type information.
162 wxClassInfo::wxClassInfo(wxChar
*cName
, wxChar
*baseName1
, wxChar
*baseName2
, int sz
, wxObjectConstructorFn constr
)
165 m_baseClassName1
= baseName1
;
166 m_baseClassName2
= baseName2
;
169 m_objectConstructor
= constr
;
174 m_baseInfo1
= (wxClassInfo
*) NULL
;
175 m_baseInfo2
= (wxClassInfo
*) NULL
;
178 wxObject
*wxClassInfo::CreateObject()
180 if (m_objectConstructor
)
181 return (wxObject
*)(*m_objectConstructor
)();
183 return (wxObject
*) NULL
;
186 wxClassInfo
*wxClassInfo::FindClass(wxChar
*c
)
188 wxClassInfo
*p
= sm_first
;
191 if (p
&& p
->GetClassName() && wxStrcmp(p
->GetClassName(), c
) == 0)
195 return (wxClassInfo
*) NULL
;
198 // Climb upwards through inheritance hierarchy.
199 // Dual inheritance is catered for.
200 bool wxClassInfo::IsKindOf(wxClassInfo
*info
) const
209 if (m_baseInfo1
->IsKindOf(info
))
213 return m_baseInfo2
->IsKindOf(info
);
218 // Set pointers to base class(es) to speed up IsKindOf
219 void wxClassInfo::InitializeClasses()
221 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
222 // link any object module twice mistakenly) will break this function
223 // because it will enter an infinite loop and eventually die with "out of
224 // memory" - as this is quite hard to detect if you're unaware of this,
225 // try to do some checks here
227 // more classes than we'll ever have
228 static const size_t nMaxClasses
= 10000;
232 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
234 // Index all class infos by their class name
235 wxClassInfo
*info
= sm_first
;
238 if (info
->m_className
)
240 wxASSERT_MSG( ++nClass
< nMaxClasses
,
241 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
243 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
249 // Set base pointers for each wxClassInfo
253 if (info
->GetBaseClassName1())
254 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
255 if (info
->GetBaseClassName2())
256 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
261 void wxClassInfo::CleanUpClasses()
263 delete wxClassInfo::sm_classTable
;
264 wxClassInfo::sm_classTable
= NULL
;
267 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
269 if (wxClassInfo::sm_classTable
)
271 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
273 return (wxObject
*)NULL
;
275 return info
->CreateObject();
279 wxClassInfo
*info
= wxClassInfo::sm_first
;
282 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
283 return info
->CreateObject();
286 return (wxObject
*) NULL
;
292 #include "wx/serbase.h"
293 #include "wx/dynlib.h"
295 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
297 wxObjectInputStream
obj_s(stream
);
298 return obj_s
.LoadObject();
301 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
303 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
304 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
307 wxLogError(_("Can't load wxSerial dynamic library."));
311 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
314 wxLogError(_("Can't find the serialization object '%s' "
315 "for the object '%s'."),
317 GetClassInfo()->GetClassName());
320 m_serialObj
->SetObject(this);
323 m_serialObj
->StoreObject(stream
);
326 void wxObject::LoadObject( wxObjectInputStream
& stream
)
328 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
329 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
332 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
335 wxLogError(_("Can't find the serialization object '%s' "
336 "for the object '%s'."),
338 GetClassInfo()->GetClassName());
341 m_serialObj
->SetObject(this);
344 m_serialObj
->LoadObject(stream
);
347 #endif // wxUSE_SERIAL
350 * wxObject: cloning of objects
353 void wxObject::Ref(const wxObject
& clone
)
355 // delete reference to old data
357 // reference new data
358 if (clone
.m_refData
) {
359 m_refData
= clone
.m_refData
;
360 ++(m_refData
->m_count
);
364 void wxObject::UnRef()
368 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
370 if ( !--m_refData
->m_count
)
372 m_refData
= (wxObjectRefData
*) NULL
;
380 wxObjectRefData::wxObjectRefData(void) : m_count(1)
384 wxObjectRefData::~wxObjectRefData()