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 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
103 void wxObject::Dump(wxSTD ostream
& str
)
105 if (GetClassInfo() && GetClassInfo()->GetClassName())
106 str
<< GetClassInfo()->GetClassName();
108 str
<< "unknown object class";
112 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
118 void *wxObject::operator new (size_t size
, wxChar
* fileName
, int lineNum
)
120 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
123 #if defined(__VISAGECPP__)
125 void wxObject::operator delete (void * buf
,const char * _fname
, size_t _line
)
129 # endif //__DEBUG_ALLOC__
131 void wxObject::operator delete (void * buf
)
135 #endif // __VISAGECPP__
138 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
139 void wxObject::operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
141 ::operator delete(pData
);
145 // Cause problems for VC++ - crashes
146 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
147 void * wxObject::operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
149 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
152 void wxObject::operator delete[] (void * buf
)
154 wxDebugFree(buf
, TRUE
);
161 * Class info: provides run-time class type information.
164 wxClassInfo::wxClassInfo(const wxChar
*cName
,
165 const wxChar
*baseName1
,
166 const wxChar
*baseName2
,
168 wxObjectConstructorFn constr
)
171 m_baseClassName1
= baseName1
;
172 m_baseClassName2
= baseName2
;
175 m_objectConstructor
= constr
;
180 m_baseInfo1
= (wxClassInfo
*) NULL
;
181 m_baseInfo2
= (wxClassInfo
*) NULL
;
184 wxObject
*wxClassInfo::CreateObject()
186 if (m_objectConstructor
)
187 return (wxObject
*)(*m_objectConstructor
)();
189 return (wxObject
*) NULL
;
192 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*c
)
194 wxClassInfo
*p
= sm_first
;
197 if ( wxStrcmp(p
->GetClassName(), c
) == 0 )
206 // Climb upwards through inheritance hierarchy.
207 // Dual inheritance is catered for.
208 bool wxClassInfo::IsKindOf(const wxClassInfo
*info
) const
217 if (m_baseInfo1
->IsKindOf(info
))
221 return m_baseInfo2
->IsKindOf(info
);
226 // Set pointers to base class(es) to speed up IsKindOf
227 void wxClassInfo::InitializeClasses()
229 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
230 // link any object module twice mistakenly) will break this function
231 // because it will enter an infinite loop and eventually die with "out of
232 // memory" - as this is quite hard to detect if you're unaware of this,
233 // try to do some checks here
235 // more classes than we'll ever have
236 static const size_t nMaxClasses
= 10000;
240 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
242 // Index all class infos by their class name
243 wxClassInfo
*info
= sm_first
;
246 if (info
->m_className
)
248 wxASSERT_MSG( ++nClass
< nMaxClasses
,
249 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
251 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
257 // Set base pointers for each wxClassInfo
261 if (info
->GetBaseClassName1())
262 info
->m_baseInfo1
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName1());
263 if (info
->GetBaseClassName2())
264 info
->m_baseInfo2
= (wxClassInfo
*)sm_classTable
->Get(info
->GetBaseClassName2());
269 void wxClassInfo::CleanUpClasses()
271 delete wxClassInfo::sm_classTable
;
272 wxClassInfo::sm_classTable
= NULL
;
275 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
277 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
278 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
281 if (wxClassInfo::sm_classTable
)
283 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
285 return (wxObject
*)NULL
;
287 return info
->CreateObject();
291 wxClassInfo
*info
= wxClassInfo::sm_first
;
294 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
295 return info
->CreateObject();
298 return (wxObject
*) NULL
;
304 #include "wx/serbase.h"
305 #include "wx/dynlib.h"
307 wxObject
* wxCreateStoredObject( wxInputStream
&stream
)
309 wxObjectInputStream
obj_s(stream
);
310 return obj_s
.LoadObject();
313 void wxObject::StoreObject( wxObjectOutputStream
& stream
)
315 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
316 DEBUG_PRINTF(wxObject::StoreObject
)
319 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
320 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
323 wxLogError(_("Can't load wxSerial dynamic library."));
327 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
330 wxLogError(_("Can't find the serialization object '%s' "
331 "for the object '%s'."),
333 GetClassInfo()->GetClassName());
336 m_serialObj
->SetObject(this);
339 m_serialObj
->StoreObject(stream
);
342 void wxObject::LoadObject( wxObjectInputStream
& stream
)
344 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
345 DEBUG_PRINTF(wxObject::LoadObject
)
348 wxString obj_name
= wxString(GetClassInfo()->GetClassName()) + "_Serialize";
349 wxLibrary
*lib
= wxTheLibraries
.LoadLibrary("wxserial");
352 m_serialObj
= (WXSERIAL(wxObject
) *)lib
->CreateObject( obj_name
);
355 wxLogError(_("Can't find the serialization object '%s' "
356 "for the object '%s'."),
358 GetClassInfo()->GetClassName());
361 m_serialObj
->SetObject(this);
364 m_serialObj
->LoadObject(stream
);
367 #endif // wxUSE_SERIAL
370 * wxObject: cloning of objects
373 void wxObject::Ref(const wxObject
& clone
)
375 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
376 DEBUG_PRINTF(wxObject::Ref
)
378 // delete reference to old data
380 // reference new data
381 if (clone
.m_refData
) {
382 m_refData
= clone
.m_refData
;
383 ++(m_refData
->m_count
);
387 void wxObject::UnRef()
391 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
393 if ( !--m_refData
->m_count
)
395 m_refData
= (wxObjectRefData
*) NULL
;
403 wxObjectRefData::wxObjectRefData(void) : m_count(1)
407 wxObjectRefData::~wxObjectRefData()