]>
git.saurik.com Git - wxWidgets.git/blob - src/common/object.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxObject implementation
4 // Author: Julian Smart
5 // Modified by: Ron Lee
8 // Copyright: (c) 1998 Julian Smart and Markus Holzem
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "object.h"
17 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
32 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
33 #include "wx/memory.h"
36 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
38 #include "wx/ioswrap.h"
40 #if defined(__VISAGECPP__)
41 #define DEBUG_PRINTF(NAME) { static int raz=0; \
42 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
44 #define DEBUG_PRINTF(NAME)
50 wxClassInfo
wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
51 (int) sizeof(wxObject
),
52 (wxObjectConstructorFn
) 0 );
53 wxClassInfo
* wxClassInfo::sm_first
= 0;
54 wxHashTable
* wxClassInfo::sm_classTable
= 0;
56 // These are here so we can avoid 'always true/false' warnings
57 // by referring to these instead of TRUE/FALSE
59 const bool wxTrue
= TRUE
;
60 const bool wxFalse
= FALSE
;
62 // Is this object a kind of (a subclass of) 'info'?
63 // E.g. is wxWindow a kind of wxObject?
64 // Go from this class to superclass, taking into account
65 // two possible base classes.
67 bool wxObject::IsKindOf(wxClassInfo
*info
) const
69 wxClassInfo
*thisInfo
= GetClassInfo();
70 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : FALSE
;
73 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
74 void wxObject::Dump(wxSTD ostream
& str
)
76 if (GetClassInfo() && GetClassInfo()->GetClassName())
77 str
<< GetClassInfo()->GetClassName();
79 str
<< "unknown object class";
83 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
89 void *wxObject::operator new (size_t size
, wxChar
*fileName
, int lineNum
)
91 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
95 void wxObject::operator delete (void *buf
)
100 void wxObject::operator delete (void *buf
, const char *_fname
, size_t _line
)
108 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
109 void wxObject::operator delete(void *pData
, wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
))
111 ::operator delete(pData
);
115 // Cause problems for VC++ - crashes
117 #if (!defined(__VISUALC__) && wxUSE_ARRAY_MEMORY_OPERATORS ) || defined(__MWERKS__)
118 void *wxObject::operator new[] (size_t size
, wxChar
*fileName
, int lineNum
)
120 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
123 void wxObject::operator delete[] (void *buf
)
125 wxDebugFree(buf
, TRUE
);
129 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
140 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
144 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
146 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
154 // a tiny InitializeClasses() helper
156 inline wxClassInfo
*wxClassInfo::GetBaseByName(const wxChar
*name
)
161 wxClassInfo
*classInfo
= (wxClassInfo
*)sm_classTable
->Get(name
);
163 // this must be fixed, other things risk work wrongly later if you get this
164 wxASSERT_MSG( classInfo
,
167 _T("base class '%s' is unknown to wxWindows RTTI"),
174 // Set pointers to base class(es) to speed up IsKindOf
175 void wxClassInfo::InitializeClasses()
177 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
178 // link any object module twice mistakenly) will break this function
179 // because it will enter an infinite loop and eventually die with "out of
180 // memory" - as this is quite hard to detect if you're unaware of this,
181 // try to do some checks here
184 static const size_t nMaxClasses
= 10000; // more than we'll ever have
188 wxClassInfo::sm_classTable
= new wxHashTable(wxKEY_STRING
);
190 // Index all class infos by their class name
193 for(info
= sm_first
; info
; info
= info
->m_next
)
195 if (info
->m_className
)
197 wxASSERT_MSG( ++nClass
< nMaxClasses
,
198 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
199 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
203 // Set base pointers for each wxClassInfo
205 for(info
= sm_first
; info
; info
= info
->m_next
)
207 info
->m_baseInfo1
= GetBaseByName(info
->GetBaseClassName1());
208 info
->m_baseInfo2
= GetBaseByName(info
->GetBaseClassName2());
212 void wxClassInfo::CleanUpClasses()
214 delete wxClassInfo::sm_classTable
;
215 wxClassInfo::sm_classTable
= 0;
219 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
221 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
222 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
225 if (wxClassInfo::sm_classTable
)
227 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
228 return info
!= 0 ? info
->CreateObject() : 0;
232 for(wxClassInfo
*info
= wxClassInfo::sm_first
; info
; info
= info
->m_next
)
233 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
234 return info
->CreateObject();
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 void wxObject::Ref(const wxObject
& clone
)
246 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
247 DEBUG_PRINTF(wxObject::Ref
)
250 // nothing to be done
251 if (m_refData
== clone
.m_refData
)
254 // delete reference to old data
257 // reference new data
258 if ( clone
.m_refData
)
260 m_refData
= clone
.m_refData
;
261 ++(m_refData
->m_count
);
265 void wxObject::UnRef()
269 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
271 if ( !--m_refData
->m_count
)
277 void wxObject::AllocExclusive()
281 m_refData
= CreateRefData();
283 else if ( m_refData
->GetRefCount() > 1 )
285 // note that ref is not going to be destroyed in this case
286 const wxObjectRefData
* ref
= m_refData
;
289 // ... so we can still access it
290 m_refData
= CloneRefData(ref
);
292 //else: ref count is 1, we are exclusive owners of m_refData anyhow
294 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
295 _T("wxObject::AllocExclusive() failed.") );
298 wxObjectRefData
*wxObject::CreateRefData() const
300 // if you use AllocExclusive() you must override this method
301 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
307 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
309 // if you use AllocExclusive() you must override this method
310 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 #if defined(__DARWIN__) && defined(DYLIB_INIT)
322 void __initialize_Cplusplus(void);
323 void wxWindowsDylibInit(void);
326 // Dynamic shared library (dylib) initialization routine
327 // required to initialize static C++ objects bacause of lazy dynamic linking
328 // http://developer.apple.com/techpubs/macosx/Essentials/
329 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
331 void wxWindowsDylibInit()
333 // The function __initialize_Cplusplus() must be called from the shared
334 // library initialization routine to cause the static C++ objects in
335 // the library to be initialized (reference number 2441683).
337 __initialize_Cplusplus();