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"
45 #if defined(__VISAGECPP__)
46 // help with VA debugging
47 #define DEBUG_PRINTF(NAME) { static int raz=0; \
48 printf( #NAME " %i\n",raz); fflush(stdout); \
52 #define DEBUG_PRINTF(NAME)
56 wxClassInfo
wxObject::sm_classwxObject((wxChar
*) wxT("wxObject"), (wxChar
*) NULL
, (wxChar
*) NULL
, (int ) sizeof(wxObject
), (wxObjectConstructorFn
) NULL
);
57 wxClassInfo
* wxClassInfo::sm_first
= (wxClassInfo
*) NULL
;
58 wxHashTable
* wxClassInfo::sm_classTable
= (wxHashTable
*) NULL
;
60 #if defined(__WXDEBUG__) && defined(__VISAGECPP__)
61 int wxObject::N
= 0; // total number of objects
62 int wxObject::Nid
= 0;// object serial counter
65 // These are here so we can avoid 'always true/false' warnings
66 // by referring to these instead of TRUE/FALSE
67 const bool wxTrue
= TRUE
;
68 const bool wxFalse
= FALSE
;
71 * wxWindows root object.
76 m_refData
= (wxObjectRefData
*) NULL
;
78 m_serialObj
= (wxObject_Serialize
*)NULL
;
80 #if defined(__WXDEBUG__) && defined(__VISAGECPP__)
83 // { printf("wxObject %i/%i \t",id,N);
95 #if defined(__WXDEBUG__) && defined(__VISAGECPP__)
97 // { printf("~wxObject %i/%i \t",id,N);
103 * Is this object a kind of (a subclass of) 'info'?
104 * E.g. is wxWindow a kind of wxObject?
105 * Go from this class to superclass, taking into account
106 * two possible base classes.
109 bool wxObject::IsKindOf(wxClassInfo
*info
) const
111 wxClassInfo
*thisInfo
= GetClassInfo();
113 return thisInfo
->IsKindOf(info
);
118 wxObject
*wxObject::Clone() const
120 wxObject
*object
= GetClassInfo()->CreateObject();
126 void wxObject::CopyObject(wxObject
& object_dest
) const
128 void wxObject::CopyObject(wxObject
& WXUNUSED(object_dest
)) const
129 #endif // Debug/!Debug
131 wxASSERT(object_dest
.GetClassInfo()->IsKindOf(GetClassInfo()));
134 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
135 void wxObject::Dump(ostream
& str
)
137 if (GetClassInfo() && GetClassInfo()->GetClassName())
138 str
<< GetClassInfo()->GetClassName();
140 str
<< "unknown object class";
144 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
150 void *wxObject::operator new (size_t size
, wxChar
* fileName
, int lineNum
)
152 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
155 #if defined(__VISAGECPP__)
157 void wxObject::operator delete (void * buf
,const char * _fname
, size_t _line
)
161 # endif //__DEBUG_ALLOC__
163 void wxObject::operator delete (void * buf
)
167 #endif // __VISAGECPP__
170 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
171 void wxObject::operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
173 ::operator delete(pData
);
177 // Cause problems for VC++ - crashes
178 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
179 void * wxObject::operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
181 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
184 void wxObject::operator delete[] (void * buf
)
186 wxDebugFree(buf
, TRUE
);
193 * Class info: provides run-time class type information.
196 wxClassInfo::wxClassInfo(const wxChar
*cName
,
197 const wxChar
*baseName1
,
198 const wxChar
*baseName2
,
200 wxObjectConstructorFn constr
)
203 m_baseClassName1
= baseName1
;
204 m_baseClassName2
= baseName2
;
207 m_objectConstructor
= constr
;
212 m_baseInfo1
= (wxClassInfo
*) NULL
;
213 m_baseInfo2
= (wxClassInfo
*) NULL
;
216 wxObject
*wxClassInfo::CreateObject()
218 if (m_objectConstructor
)
219 return (wxObject
*)(*m_objectConstructor
)();
221 return (wxObject
*) NULL
;
224 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*c
)
226 wxClassInfo
*p
= sm_first
;
229 if ( wxStrcmp(p
->GetClassName(), c
) == 0 )
238 // Climb upwards through inheritance hierarchy.
239 // Dual inheritance is catered for.
240 bool wxClassInfo::IsKindOf(const wxClassInfo
*info
) const
249 if (m_baseInfo1
->IsKindOf(info
))
253 return m_baseInfo2
->IsKindOf(info
);
258 // Set pointers to base class(es) to speed up IsKindOf
259 void wxClassInfo::InitializeClasses()
261 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
262 // link any object module twice mistakenly) will break this function
263 // because it will enter an infinite loop and eventually die with "out of
264 // memory" - as this is quite hard to detect if you're unaware of this,
265 // try to do some checks here
267 // more classes than we'll ever have
268 static const size_t nMaxClasses
= 10000;
272 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
274 // Index all class infos by their class name
275 wxClassInfo
*info
= sm_first
;
278 if (info
->m_className
)
280 wxASSERT_MSG( ++nClass
< nMaxClasses
,
281 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
283 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
289 // Set base pointers for each wxClassInfo
293 if (info
->GetBaseClassName1())
294 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
295 if (info
->GetBaseClassName2())
296 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
301 void wxClassInfo::CleanUpClasses()
303 delete wxClassInfo::sm_classTable
;
304 wxClassInfo::sm_classTable
= NULL
;
307 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
309 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
310 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
313 if (wxClassInfo::sm_classTable
)
315 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
317 return (wxObject
*)NULL
;
319 return info
->CreateObject();
323 wxClassInfo
*info
= wxClassInfo::sm_first
;
326 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
327 return info
->CreateObject();
330 return (wxObject
*) NULL
;
336 #include "wx/serbase.h"
337 #include "wx/dynlib.h"
339 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
341 wxObjectInputStream
obj_s(stream
);
342 return obj_s
.LoadObject();
345 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
347 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
348 DEBUG_PRINTF(wxObject::StoreObject
)
351 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
352 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
355 wxLogError(_("Can't load wxSerial dynamic library."));
359 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
362 wxLogError(_("Can't find the serialization object '%s' "
363 "for the object '%s'."),
365 GetClassInfo()->GetClassName());
368 m_serialObj
->SetObject(this);
371 m_serialObj
->StoreObject(stream
);
374 void wxObject::LoadObject( wxObjectInputStream
& stream
)
376 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
377 DEBUG_PRINTF(wxObject::LoadObject
)
380 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
381 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
384 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
387 wxLogError(_("Can't find the serialization object '%s' "
388 "for the object '%s'."),
390 GetClassInfo()->GetClassName());
393 m_serialObj
->SetObject(this);
396 m_serialObj
->LoadObject(stream
);
399 #endif // wxUSE_SERIAL
402 * wxObject: cloning of objects
405 void wxObject::Ref(const wxObject
& clone
)
407 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
408 DEBUG_PRINTF(wxObject::Ref
)
410 // delete reference to old data
412 // reference new data
413 if (clone
.m_refData
) {
414 m_refData
= clone
.m_refData
;
415 ++(m_refData
->m_count
);
419 void wxObject::UnRef()
423 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
425 if ( !--m_refData
->m_count
)
427 m_refData
= (wxObjectRefData
*) NULL
;
435 wxObjectRefData::wxObjectRefData(void) : m_count(1)
439 wxObjectRefData::~wxObjectRefData()