1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/object.cpp
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".
18 #include "wx/wxprec.h"
30 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
31 #include "wx/memory.h"
34 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
36 #include "wx/ioswrap.h"
38 #if defined(__VISAGECPP__)
39 #define DEBUG_PRINTF(NAME) { static int raz=0; \
40 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
42 #define DEBUG_PRINTF(NAME)
44 #endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
47 wxClassInfo wxObject
::sm_classwxObject( wxT("wxObject"), 0, 0,
48 (int) sizeof(wxObject
),
49 (wxObjectConstructorFn
) 0 );
51 wxClassInfo
* wxClassInfo
::sm_first
= NULL
;
52 wxHashTable
* wxClassInfo
::sm_classTable
= NULL
;
54 // These are here so we can avoid 'always true/false' warnings
55 // by referring to these instead of TRUE/FALSE
56 const bool wxTrue
= TRUE
;
57 const bool wxFalse
= FALSE
;
59 // Is this object a kind of (a subclass of) 'info'?
60 // E.g. is wxWindow a kind of wxObject?
61 // Go from this class to superclass, taking into account
62 // two possible base classes.
63 bool wxObject
::IsKindOf(wxClassInfo
*info
) const
65 wxClassInfo
*thisInfo
= GetClassInfo();
66 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : FALSE
;
69 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
70 void wxObject
::Dump(wxSTD ostream
& str
)
72 if (GetClassInfo() && GetClassInfo()->GetClassName())
73 str
<< GetClassInfo()->GetClassName();
75 str
<< _T("unknown object class");
81 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
82 void *wxObject
::operator new ( size_t size
, wxChar
*fileName
, int lineNum
)
84 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
);
88 #ifdef _WX_WANT_DELETE_VOID
89 void wxObject
::operator delete ( void *buf
)
95 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
96 void wxObject
::operator delete ( void *buf
, const char *_fname
, size_t _line
)
102 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
103 void wxObject
::operator delete ( void *buf
, wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
109 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
110 void *wxObject
::operator new[] ( size_t size
, wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
112 return wxDebugAlloc(size
, fileName
, lineNum
, TRUE
, TRUE
);
116 #ifdef _WX_WANT_ARRAY_DELETE_VOID
117 void wxObject
::operator delete[] ( void *buf
)
119 wxDebugFree(buf
, TRUE
);
123 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
124 void wxObject
::operator delete[] (void * buf
, wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
126 wxDebugFree(buf
, TRUE
);
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 wxClassInfo
*wxClassInfo
::FindClass(const wxChar
*className
)
139 return (wxClassInfo
*)wxClassInfo
::sm_classTable
->Get(className
);
143 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
145 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
153 // a tiny InitializeClasses() helper
155 inline wxClassInfo
*wxClassInfo
::GetBaseByName(const wxChar
*name
)
160 wxClassInfo
*classInfo
= (wxClassInfo
*)sm_classTable
->Get(name
);
162 // this must be fixed, other things risk work wrongly later if you get this
163 wxASSERT_MSG( classInfo
,
166 _T("base class '%s' is unknown to wxWindows RTTI"),
173 // Set pointers to base class(es) to speed up IsKindOf
174 void wxClassInfo
::InitializeClasses()
176 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
177 // link any object module twice mistakenly) will break this function
178 // because it will enter an infinite loop and eventually die with "out of
179 // memory" - as this is quite hard to detect if you're unaware of this,
180 // try to do some checks here
183 static const size_t nMaxClasses
= 10000; // more than we'll ever have
187 wxClassInfo
::sm_classTable
= new wxHashTable(wxKEY_STRING
);
189 // Index all class infos by their class name
192 for(info
= sm_first
; info
; info
= info
->m_next
)
194 if (info
->m_className
)
196 wxASSERT_MSG( ++nClass
< nMaxClasses
,
197 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
198 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
202 // Set base pointers for each wxClassInfo
204 for(info
= sm_first
; info
; info
= info
->m_next
)
206 info
->m_baseInfo1
= GetBaseByName(info
->GetBaseClassName1());
207 info
->m_baseInfo2
= GetBaseByName(info
->GetBaseClassName2());
211 void wxClassInfo
::CleanUpClasses()
213 delete wxClassInfo
::sm_classTable
;
214 wxClassInfo
::sm_classTable
= NULL
;
218 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
220 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
221 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
224 if ( wxClassInfo
::sm_classTable
)
226 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo
::sm_classTable
->Get(name
);
227 return info ? info
->CreateObject() : NULL
;
229 else // no sm_classTable yet
231 for ( wxClassInfo
*info
= wxClassInfo
::sm_first
;
233 info
= info
->m_next
)
235 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
236 return info
->CreateObject();
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 void wxObject
::Ref(const wxObject
& clone
)
250 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
251 DEBUG_PRINTF(wxObject
::Ref
)
254 // nothing to be done
255 if (m_refData
== clone
.m_refData
)
258 // delete reference to old data
261 // reference new data
262 if ( clone
.m_refData
)
264 m_refData
= clone
.m_refData
;
265 ++(m_refData
->m_count
);
269 void wxObject
::UnRef()
273 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
275 if ( !--m_refData
->m_count
)
281 void wxObject
::AllocExclusive()
285 m_refData
= CreateRefData();
287 else if ( m_refData
->GetRefCount() > 1 )
289 // note that ref is not going to be destroyed in this case
290 const wxObjectRefData
* ref
= m_refData
;
293 // ... so we can still access it
294 m_refData
= CloneRefData(ref
);
296 //else: ref count is 1, we are exclusive owners of m_refData anyhow
298 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
299 _T("wxObject::AllocExclusive() failed.") );
302 wxObjectRefData
*wxObject
::CreateRefData() const
304 // if you use AllocExclusive() you must override this method
305 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
311 wxObject
::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
313 // if you use AllocExclusive() you must override this method
314 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
323 #if defined(__DARWIN__) && defined(DYLIB_INIT)
326 void __initialize_Cplusplus(void);
327 void wxWindowsDylibInit(void);
330 // Dynamic shared library (dylib) initialization routine
331 // required to initialize static C++ objects bacause of lazy dynamic linking
332 // http://developer.apple.com/techpubs/macosx/Essentials/
333 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
335 void wxWindowsDylibInit()
337 // The function __initialize_Cplusplus() must be called from the shared
338 // library initialization routine to cause the static C++ objects in
339 // the library to be initialized (reference number 2441683).
341 __initialize_Cplusplus();