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
;
52 * wxWindows root object.
57 m_refData
= (wxObjectRefData
*) NULL
;
59 m_serialObj
= (wxObject_Serialize
*)NULL
;
73 * Is this object a kind of (a subclass of) 'info'?
74 * E.g. is wxWindow a kind of wxObject?
75 * Go from this class to superclass, taking into account
76 * two possible base classes.
79 bool wxObject::IsKindOf(wxClassInfo
*info
) const
81 wxClassInfo
*thisInfo
= GetClassInfo();
83 return thisInfo
->IsKindOf(info
);
88 wxObject
*wxObject::Clone() const
90 wxObject
*object
= GetClassInfo()->CreateObject();
96 void wxObject::CopyObject(wxObject
& object_dest
) const
98 void wxObject::CopyObject(wxObject
& WXUNUSED(object_dest
)) const
99 #endif // Debug/!Debug
101 wxASSERT(object_dest
.GetClassInfo()->IsKindOf(GetClassInfo()));
104 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
105 void wxObject::Dump(ostream
& str
)
107 if (GetClassInfo() && GetClassInfo()->GetClassName())
108 str
<< GetClassInfo()->GetClassName();
110 str
<< "unknown object class";
114 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
120 void *wxObject::operator new (size_t size
, wxChar
* fileName
, int lineNum
)
122 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
125 void wxObject::operator delete (void * buf
)
131 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
132 void wxObject::operator delete(void* pData
, char* /* fileName */, int /* lineNum */)
134 ::operator delete(pData
);
138 // Cause problems for VC++ - crashes
139 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
140 void * wxObject::operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
142 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
145 void wxObject::operator delete[] (void * buf
)
147 wxDebugFree(buf
, TRUE
);
154 * Class info: provides run-time class type information.
157 wxClassInfo::wxClassInfo(wxChar
*cName
, wxChar
*baseName1
, wxChar
*baseName2
, int sz
, wxObjectConstructorFn constr
)
160 m_baseClassName1
= baseName1
;
161 m_baseClassName2
= baseName2
;
164 m_objectConstructor
= constr
;
169 m_baseInfo1
= (wxClassInfo
*) NULL
;
170 m_baseInfo2
= (wxClassInfo
*) NULL
;
173 wxObject
*wxClassInfo::CreateObject()
175 if (m_objectConstructor
)
176 return (wxObject
*)(*m_objectConstructor
)();
178 return (wxObject
*) NULL
;
181 wxClassInfo
*wxClassInfo::FindClass(wxChar
*c
)
183 wxClassInfo
*p
= sm_first
;
186 if (p
&& p
->GetClassName() && wxStrcmp(p
->GetClassName(), c
) == 0)
190 return (wxClassInfo
*) NULL
;
193 // Climb upwards through inheritance hierarchy.
194 // Dual inheritance is catered for.
195 bool wxClassInfo::IsKindOf(wxClassInfo
*info
) const
200 // For some reason, when making/using a DLL, static data has to be included
201 // in both the DLL and the application. This can lead to duplicate
202 // wxClassInfo objects, so we have to test the name instead of the pointers.
203 // PROBABLY NO LONGER TRUE now I've done DLL creation right.
206 if (GetClassName() && info->GetClassName() && (wxStrcmp(GetClassName(), info->GetClassName()) == 0))
214 if (m_baseInfo1
->IsKindOf(info
))
218 return m_baseInfo2
->IsKindOf(info
);
223 // Set pointers to base class(es) to speed up IsKindOf
224 void wxClassInfo::InitializeClasses()
226 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
227 // link any object module twice mistakenly) will break this function
228 // because it will enter an infinite loop and eventually die with "out of
229 // memory" - as this is quite hard to detect if you're unaware of this,
230 // try to do some checks here
232 // more classes than we'll ever have
233 static const size_t nMaxClasses
= 10000;
237 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
239 // Index all class infos by their class name
240 wxClassInfo
*info
= sm_first
;
243 if (info
->m_className
)
245 wxASSERT_MSG( ++nClass
< nMaxClasses
,
246 _T("an infinite loop detected - have you used "
247 "IMPLEMENT_DYNAMIC_CLASS() twice (may be by "
248 "linking some object module(s) twice)?") );
250 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
256 // Set base pointers for each wxClassInfo
260 if (info
->GetBaseClassName1())
261 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
262 if (info
->GetBaseClassName2())
263 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
268 void wxClassInfo::CleanUpClasses()
270 delete wxClassInfo::sm_classTable
;
271 wxClassInfo::sm_classTable
= NULL
;
274 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
276 if (wxClassInfo::sm_classTable
)
278 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
280 return (wxObject
*)NULL
;
282 return info
->CreateObject();
286 wxClassInfo
*info
= wxClassInfo::sm_first
;
289 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
290 return info
->CreateObject();
293 return (wxObject
*) NULL
;
299 #include "wx/serbase.h"
300 #include "wx/dynlib.h"
302 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
304 wxObjectInputStream
obj_s(stream
);
305 return obj_s
.LoadObject();
308 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
310 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
311 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
314 wxLogError(_("Can't load wxSerial dynamic library."));
318 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
321 wxLogError(_("Can't find the serialization object '%s' "
322 "for the object '%s'."),
324 GetClassInfo()->GetClassName());
327 m_serialObj
->SetObject(this);
330 m_serialObj
->StoreObject(stream
);
333 void wxObject::LoadObject( wxObjectInputStream
& stream
)
335 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
336 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
339 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
342 wxLogError(_("Can't find the serialization object '%s' "
343 "for the object '%s'."),
345 GetClassInfo()->GetClassName());
348 m_serialObj
->SetObject(this);
351 m_serialObj
->LoadObject(stream
);
354 #endif // wxUSE_SERIAL
357 * wxObject: cloning of objects
360 void wxObject::Ref(const wxObject
& clone
)
362 // delete reference to old data
364 // reference new data
365 if (clone
.m_refData
) {
366 m_refData
= clone
.m_refData
;
367 ++(m_refData
->m_count
);
371 void wxObject::UnRef()
375 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
377 if ( !--m_refData
->m_count
)
378 #if defined(__VISAGECPP__) && IBMCPP < 400
380 // don't know what is going on with VA 3.0 but its got some memory problems here
381 // this delete causes a trap in CPPOOM3.DLL
388 m_refData
= (wxObjectRefData
*) NULL
;
397 wxObjectRefData::wxObjectRefData(void) : m_count(1)
401 wxObjectRefData::~wxObjectRefData()
405 // These are here so we can avoid 'always true/false' warnings
406 // by referring to these instead of TRUE/FALSE
407 const bool wxTrue
= TRUE
;
408 const bool wxFalse
= FALSE
;