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 // These are here so we can avoid 'always true/false' warnings
61 // by referring to these instead of TRUE/FALSE
62 const bool wxTrue
= TRUE
;
63 const bool wxFalse
= FALSE
;
66 * wxWindows root object.
71 m_refData
= (wxObjectRefData
*) NULL
;
73 m_serialObj
= (wxObject_Serialize
*)NULL
;
87 * Is this object a kind of (a subclass of) 'info'?
88 * E.g. is wxWindow a kind of wxObject?
89 * Go from this class to superclass, taking into account
90 * two possible base classes.
93 bool wxObject::IsKindOf(wxClassInfo
*info
) const
95 wxClassInfo
*thisInfo
= GetClassInfo();
97 return thisInfo
->IsKindOf(info
);
102 wxObject
*wxObject::Clone() const
104 wxObject
*object
= GetClassInfo()->CreateObject();
110 void wxObject::CopyObject(wxObject
& object_dest
) const
112 void wxObject::CopyObject(wxObject
& WXUNUSED(object_dest
)) const
113 #endif // Debug/!Debug
115 wxASSERT(object_dest
.GetClassInfo()->IsKindOf(GetClassInfo()));
118 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
119 void wxObject::Dump(wxSTD ostream
& str
)
121 if (GetClassInfo() && GetClassInfo()->GetClassName())
122 str
<< GetClassInfo()->GetClassName();
124 str
<< "unknown object class";
128 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
134 void *wxObject::operator new (size_t size
, wxChar
* fileName
, int lineNum
)
136 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
139 #if defined(__VISAGECPP__)
141 void wxObject::operator delete (void * buf
,const char * _fname
, size_t _line
)
145 # endif //__DEBUG_ALLOC__
147 void wxObject::operator delete (void * buf
)
151 #endif // __VISAGECPP__
154 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
155 void wxObject::operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
157 ::operator delete(pData
);
161 // Cause problems for VC++ - crashes
162 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
163 void * wxObject::operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
165 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
168 void wxObject::operator delete[] (void * buf
)
170 wxDebugFree(buf
, TRUE
);
177 * Class info: provides run-time class type information.
180 wxClassInfo::wxClassInfo(const wxChar
*cName
,
181 const wxChar
*baseName1
,
182 const wxChar
*baseName2
,
184 wxObjectConstructorFn constr
)
187 m_baseClassName1
= baseName1
;
188 m_baseClassName2
= baseName2
;
191 m_objectConstructor
= constr
;
196 m_baseInfo1
= (wxClassInfo
*) NULL
;
197 m_baseInfo2
= (wxClassInfo
*) NULL
;
200 wxObject
*wxClassInfo::CreateObject()
202 if (m_objectConstructor
)
203 return (wxObject
*)(*m_objectConstructor
)();
205 return (wxObject
*) NULL
;
208 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*c
)
210 wxClassInfo
*p
= sm_first
;
213 if ( wxStrcmp(p
->GetClassName(), c
) == 0 )
222 // Climb upwards through inheritance hierarchy.
223 // Dual inheritance is catered for.
224 bool wxClassInfo::IsKindOf(const wxClassInfo
*info
) const
233 if (m_baseInfo1
->IsKindOf(info
))
237 return m_baseInfo2
->IsKindOf(info
);
242 // Set pointers to base class(es) to speed up IsKindOf
243 void wxClassInfo::InitializeClasses()
245 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
246 // link any object module twice mistakenly) will break this function
247 // because it will enter an infinite loop and eventually die with "out of
248 // memory" - as this is quite hard to detect if you're unaware of this,
249 // try to do some checks here
251 // more classes than we'll ever have
252 static const size_t nMaxClasses
= 10000;
256 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
258 // Index all class infos by their class name
259 wxClassInfo
*info
= sm_first
;
262 if (info
->m_className
)
264 wxASSERT_MSG( ++nClass
< nMaxClasses
,
265 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
267 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
273 // Set base pointers for each wxClassInfo
277 if (info
->GetBaseClassName1())
278 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
279 if (info
->GetBaseClassName2())
280 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
285 void wxClassInfo::CleanUpClasses()
287 delete wxClassInfo::sm_classTable
;
288 wxClassInfo::sm_classTable
= NULL
;
291 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
293 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
294 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
297 if (wxClassInfo::sm_classTable
)
299 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
301 return (wxObject
*)NULL
;
303 return info
->CreateObject();
307 wxClassInfo
*info
= wxClassInfo::sm_first
;
310 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
311 return info
->CreateObject();
314 return (wxObject
*) NULL
;
320 #include "wx/serbase.h"
321 #include "wx/dynlib.h"
323 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
325 wxObjectInputStream
obj_s(stream
);
326 return obj_s
.LoadObject();
329 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
331 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
332 DEBUG_PRINTF(wxObject::StoreObject
)
335 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
336 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
339 wxLogError(_("Can't load wxSerial dynamic library."));
343 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
346 wxLogError(_("Can't find the serialization object '%s' "
347 "for the object '%s'."),
349 GetClassInfo()->GetClassName());
352 m_serialObj
->SetObject(this);
355 m_serialObj
->StoreObject(stream
);
358 void wxObject::LoadObject( wxObjectInputStream
& stream
)
360 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
361 DEBUG_PRINTF(wxObject::LoadObject
)
364 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
365 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
368 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
371 wxLogError(_("Can't find the serialization object '%s' "
372 "for the object '%s'."),
374 GetClassInfo()->GetClassName());
377 m_serialObj
->SetObject(this);
380 m_serialObj
->LoadObject(stream
);
383 #endif // wxUSE_SERIAL
386 * wxObject: cloning of objects
389 void wxObject::Ref(const wxObject
& clone
)
391 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
392 DEBUG_PRINTF(wxObject::Ref
)
394 // delete reference to old data
396 // reference new data
397 if (clone
.m_refData
) {
398 m_refData
= clone
.m_refData
;
399 ++(m_refData
->m_count
);
403 void wxObject::UnRef()
407 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
409 if ( !--m_refData
->m_count
)
411 m_refData
= (wxObjectRefData
*) NULL
;
419 wxObjectRefData::wxObjectRefData(void) : m_count(1)
423 wxObjectRefData::~wxObjectRefData()