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
*) wxT("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();
98 void wxObject::CopyObject(wxObject
& object_dest
) const
100 void wxObject::CopyObject(wxObject
& WXUNUSED(object_dest
)) const
101 #endif // Debug/!Debug
103 wxASSERT(object_dest
.GetClassInfo()->IsKindOf(GetClassInfo()));
106 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
107 void wxObject::Dump(ostream
& str
)
109 if (GetClassInfo() && GetClassInfo()->GetClassName())
110 str
<< GetClassInfo()->GetClassName();
112 str
<< "unknown object class";
116 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
122 void *wxObject::operator new (size_t size
, wxChar
* fileName
, int lineNum
)
124 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
127 void wxObject::operator delete (void * buf
)
133 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
134 void wxObject::operator delete(void* pData
, char* /* fileName */, int /* lineNum */)
136 ::operator delete(pData
);
140 // Cause problems for VC++ - crashes
141 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
142 void * wxObject::operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
144 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
147 void wxObject::operator delete[] (void * buf
)
149 wxDebugFree(buf
, TRUE
);
156 * Class info: provides run-time class type information.
159 wxClassInfo::wxClassInfo(wxChar
*cName
, wxChar
*baseName1
, wxChar
*baseName2
, int sz
, wxObjectConstructorFn constr
)
162 m_baseClassName1
= baseName1
;
163 m_baseClassName2
= baseName2
;
166 m_objectConstructor
= constr
;
171 m_baseInfo1
= (wxClassInfo
*) NULL
;
172 m_baseInfo2
= (wxClassInfo
*) NULL
;
175 wxObject
*wxClassInfo::CreateObject()
177 if (m_objectConstructor
)
178 return (wxObject
*)(*m_objectConstructor
)();
180 return (wxObject
*) NULL
;
183 wxClassInfo
*wxClassInfo::FindClass(wxChar
*c
)
185 wxClassInfo
*p
= sm_first
;
188 if (p
&& p
->GetClassName() && wxStrcmp(p
->GetClassName(), c
) == 0)
192 return (wxClassInfo
*) NULL
;
195 // Climb upwards through inheritance hierarchy.
196 // Dual inheritance is catered for.
197 bool wxClassInfo::IsKindOf(wxClassInfo
*info
) const
202 // For some reason, when making/using a DLL, static data has to be included
203 // in both the DLL and the application. This can lead to duplicate
204 // wxClassInfo objects, so we have to test the name instead of the pointers.
205 // PROBABLY NO LONGER TRUE now I've done DLL creation right.
208 if (GetClassName() && info->GetClassName() && (wxStrcmp(GetClassName(), info->GetClassName()) == 0))
216 if (m_baseInfo1
->IsKindOf(info
))
220 return m_baseInfo2
->IsKindOf(info
);
225 // Set pointers to base class(es) to speed up IsKindOf
226 void wxClassInfo::InitializeClasses()
228 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
229 // link any object module twice mistakenly) will break this function
230 // because it will enter an infinite loop and eventually die with "out of
231 // memory" - as this is quite hard to detect if you're unaware of this,
232 // try to do some checks here
234 // more classes than we'll ever have
235 static const size_t nMaxClasses
= 10000;
239 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
241 // Index all class infos by their class name
242 wxClassInfo
*info
= sm_first
;
245 if (info
->m_className
)
247 wxASSERT_MSG( ++nClass
< nMaxClasses
,
248 _T("an infinite loop detected - have you used "
249 "IMPLEMENT_DYNAMIC_CLASS() twice (may be by "
250 "linking some object module(s) twice)?") );
252 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
258 // Set base pointers for each wxClassInfo
262 if (info
->GetBaseClassName1())
263 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
264 if (info
->GetBaseClassName2())
265 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
270 void wxClassInfo::CleanUpClasses()
272 delete wxClassInfo::sm_classTable
;
273 wxClassInfo::sm_classTable
= NULL
;
276 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
278 if (wxClassInfo::sm_classTable
)
280 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
282 return (wxObject
*)NULL
;
284 return info
->CreateObject();
288 wxClassInfo
*info
= wxClassInfo::sm_first
;
291 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
292 return info
->CreateObject();
295 return (wxObject
*) NULL
;
301 #include "wx/serbase.h"
302 #include "wx/dynlib.h"
303 #include "wx/msgdlg.h"
305 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
307 wxObjectInputStream
obj_s(stream
);
308 return obj_s
.LoadObject();
311 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
313 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
314 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
317 wxLogError(_("Can't load wxSerial dynamic library."));
321 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
324 wxLogError(_("Can't find the serialization object '%s' "
325 "for the object '%s'."),
327 GetClassInfo()->GetClassName());
330 m_serialObj
->SetObject(this);
333 m_serialObj
->StoreObject(stream
);
336 void wxObject::LoadObject( wxObjectInputStream
& stream
)
338 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
339 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
342 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
345 wxLogError(_("Can't find the serialization object '%s' "
346 "for the object '%s'."),
348 GetClassInfo()->GetClassName());
351 m_serialObj
->SetObject(this);
354 m_serialObj
->LoadObject(stream
);
357 #endif // wxUSE_SERIAL
360 * wxObject: cloning of objects
363 void wxObject::Ref(const wxObject
& clone
)
365 // delete reference to old data
367 // reference new data
368 if (clone
.m_refData
) {
369 m_refData
= clone
.m_refData
;
370 ++(m_refData
->m_count
);
374 void wxObject::UnRef()
378 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
380 if ( !--m_refData
->m_count
)
383 m_refData
= (wxObjectRefData
*) NULL
;
391 wxObjectRefData::wxObjectRefData(void) : m_count(1)
395 wxObjectRefData::~wxObjectRefData()
399 // These are here so we can avoid 'always true/false' warnings
400 // by referring to these instead of TRUE/FALSE
401 const bool wxTrue
= TRUE
;
402 const bool wxFalse
= FALSE
;