]>
git.saurik.com Git - wxWidgets.git/blob - src/common/object.cpp
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");
80 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
86 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
87 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
89 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
);
93 #ifdef _WX_WANT_DELETE_VOID
94 void wxObject::operator delete ( void *buf
)
100 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
101 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
107 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
108 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
114 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
115 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
117 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
, TRUE
);
121 #ifdef _WX_WANT_ARRAY_DELETE_VOID
122 void wxObject::operator delete[] ( void *buf
)
124 wxDebugFree(buf
, TRUE
);
128 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
129 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
131 wxDebugFree(buf
, TRUE
);
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
144 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
148 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
150 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
158 // a tiny InitializeClasses() helper
160 inline wxClassInfo
*wxClassInfo::GetBaseByName(const wxChar
*name
)
165 wxClassInfo
*classInfo
= (wxClassInfo
*)sm_classTable
->Get(name
);
167 // this must be fixed, other things risk work wrongly later if you get this
168 wxASSERT_MSG( classInfo
,
171 _T("base class '%s' is unknown to wxWindows RTTI"),
178 // Set pointers to base class(es) to speed up IsKindOf
179 void wxClassInfo::InitializeClasses()
181 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
182 // link any object module twice mistakenly) will break this function
183 // because it will enter an infinite loop and eventually die with "out of
184 // memory" - as this is quite hard to detect if you're unaware of this,
185 // try to do some checks here
188 static const size_t nMaxClasses
= 10000; // more than we'll ever have
192 sm_classTable
= new wxHashTable(wxKEY_STRING
);
194 // Index all class infos by their class name
197 for(info
= sm_first
; info
; info
= info
->m_next
)
199 if (info
->m_className
)
201 wxASSERT_MSG( ++nClass
< nMaxClasses
,
202 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
203 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
207 // Set base pointers for each wxClassInfo
209 for(info
= sm_first
; info
; info
= info
->m_next
)
211 info
->m_baseInfo1
= GetBaseByName(info
->GetBaseClassName1());
212 info
->m_baseInfo2
= GetBaseByName(info
->GetBaseClassName2());
216 void wxClassInfo::CleanUpClasses()
218 delete wxClassInfo::sm_classTable
;
219 wxClassInfo::sm_classTable
= NULL
;
223 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
225 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
226 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
229 if ( wxClassInfo::sm_classTable
)
231 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
232 return info
? info
->CreateObject() : NULL
;
234 else // no sm_classTable yet
236 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
238 info
= info
->m_next
)
240 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
241 return info
->CreateObject();
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 void wxObject::Ref(const wxObject
& clone
)
255 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
256 DEBUG_PRINTF(wxObject::Ref
)
259 // nothing to be done
260 if (m_refData
== clone
.m_refData
)
263 // delete reference to old data
266 // reference new data
267 if ( clone
.m_refData
)
269 m_refData
= clone
.m_refData
;
270 ++(m_refData
->m_count
);
274 void wxObject::UnRef()
278 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
280 if ( !--m_refData
->m_count
)
286 void wxObject::AllocExclusive()
290 m_refData
= CreateRefData();
292 else if ( m_refData
->GetRefCount() > 1 )
294 // note that ref is not going to be destroyed in this case
295 const wxObjectRefData
* ref
= m_refData
;
298 // ... so we can still access it
299 m_refData
= CloneRefData(ref
);
301 //else: ref count is 1, we are exclusive owners of m_refData anyhow
303 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
304 _T("wxObject::AllocExclusive() failed.") );
307 wxObjectRefData
*wxObject::CreateRefData() const
309 // if you use AllocExclusive() you must override this method
310 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
316 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
318 // if you use AllocExclusive() you must override this method
319 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
331 void __initialize_Cplusplus(void);
332 void wxWindowsDylibInit(void);
335 // Dynamic shared library (dylib) initialization routine
336 // required to initialize static C++ objects bacause of lazy dynamic linking
337 // http://developer.apple.com/techpubs/macosx/Essentials/
338 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
340 void wxWindowsDylibInit()
342 // The function __initialize_Cplusplus() must be called from the shared
343 // library initialization routine to cause the static C++ objects in
344 // the library to be initialized (reference number 2441683).
346 // This only seems to be necessary if the library initialization routine
347 // needs to use the static C++ objects
348 __initialize_Cplusplus();