]>
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
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
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 )
85 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
86 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
88 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
);
92 #ifdef _WX_WANT_DELETE_VOID
93 void wxObject::operator delete ( void *buf
)
99 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
100 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
106 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
107 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
113 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
114 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
116 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
, TRUE
);
120 #ifdef _WX_WANT_ARRAY_DELETE_VOID
121 void wxObject::operator delete[] ( void *buf
)
123 wxDebugFree(buf
, TRUE
);
127 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
128 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
130 wxDebugFree(buf
, TRUE
);
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 wxClassInfo::~wxClassInfo()
141 // remove this object from the linked list of all class infos: if we don't
142 // do it, loading/unloading a DLL containing static wxClassInfo objects is
144 if ( this == sm_first
)
150 wxClassInfo
*info
= sm_first
;
153 if ( info
->m_next
== this )
155 info
->m_next
= m_next
;
164 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
168 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
172 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
174 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
182 // a tiny InitializeClasses() helper
184 inline wxClassInfo
*wxClassInfo::GetBaseByName(const wxChar
*name
)
189 wxClassInfo
*classInfo
= (wxClassInfo
*)sm_classTable
->Get(name
);
191 // this must be fixed, other things risk work wrongly later if you get this
192 wxASSERT_MSG( classInfo
,
195 _T("base class '%s' is unknown to wxWindows RTTI"),
202 // Set pointers to base class(es) to speed up IsKindOf
203 void wxClassInfo::InitializeClasses()
205 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
206 // link any object module twice mistakenly) will break this function
207 // because it will enter an infinite loop and eventually die with "out of
208 // memory" - as this is quite hard to detect if you're unaware of this,
209 // try to do some checks here
212 static const size_t nMaxClasses
= 10000; // more than we'll ever have
216 sm_classTable
= new wxHashTable(wxKEY_STRING
);
218 // Index all class infos by their class name
221 for(info
= sm_first
; info
; info
= info
->m_next
)
223 if (info
->m_className
)
225 wxASSERT_MSG( ++nClass
< nMaxClasses
,
226 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
227 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
231 // Set base pointers for each wxClassInfo
233 for(info
= sm_first
; info
; info
= info
->m_next
)
235 info
->m_baseInfo1
= GetBaseByName(info
->GetBaseClassName1());
236 info
->m_baseInfo2
= GetBaseByName(info
->GetBaseClassName2());
240 void wxClassInfo::CleanUpClasses()
242 delete wxClassInfo::sm_classTable
;
243 wxClassInfo::sm_classTable
= NULL
;
247 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
249 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
250 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
253 if ( wxClassInfo::sm_classTable
)
255 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
256 return info
? info
->CreateObject() : NULL
;
258 else // no sm_classTable yet
260 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
262 info
= info
->m_next
)
264 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
265 return info
->CreateObject();
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 // Initialize ref data from another object (needed for copy constructor and
278 // assignment operator)
279 void wxObject::InitFrom(const wxObject
& other
)
281 m_refData
= other
.m_refData
;
283 m_refData
->m_count
++;
286 void wxObject::Ref(const wxObject
& clone
)
288 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
289 DEBUG_PRINTF(wxObject::Ref
)
292 // nothing to be done
293 if (m_refData
== clone
.m_refData
)
296 // delete reference to old data
299 // reference new data
300 if ( clone
.m_refData
)
302 m_refData
= clone
.m_refData
;
303 ++(m_refData
->m_count
);
307 void wxObject::UnRef()
311 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
313 if ( !--m_refData
->m_count
)
319 void wxObject::AllocExclusive()
323 m_refData
= CreateRefData();
325 else if ( m_refData
->GetRefCount() > 1 )
327 // note that ref is not going to be destroyed in this case
328 const wxObjectRefData
* ref
= m_refData
;
331 // ... so we can still access it
332 m_refData
= CloneRefData(ref
);
334 //else: ref count is 1, we are exclusive owners of m_refData anyhow
336 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
337 _T("wxObject::AllocExclusive() failed.") );
340 wxObjectRefData
*wxObject::CreateRefData() const
342 // if you use AllocExclusive() you must override this method
343 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
349 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
351 // if you use AllocExclusive() you must override this method
352 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
361 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
364 void __initialize_Cplusplus(void);
365 void wxWindowsDylibInit(void);
368 // Dynamic shared library (dylib) initialization routine
369 // required to initialize static C++ objects bacause of lazy dynamic linking
370 // http://developer.apple.com/techpubs/macosx/Essentials/
371 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
373 void wxWindowsDylibInit()
375 // The function __initialize_Cplusplus() must be called from the shared
376 // library initialization routine to cause the static C++ objects in
377 // the library to be initialized (reference number 2441683).
379 // This only seems to be necessary if the library initialization routine
380 // needs to use the static C++ objects
381 __initialize_Cplusplus();