]>
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
46 // we must disable optimizations for VC.NET because otherwise its too eager
47 // linker discards wxClassInfo objects in release build thus breaking many,
49 #if defined __VISUALC__ && __VISUALC__ >= 1300
50 #pragma optimize("", off)
53 wxClassInfo
wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
54 (int) sizeof(wxObject
),
55 (wxObjectConstructorFn
) 0 );
57 // restore optimizations
58 #if defined __VISUALC__ && __VISUALC__ >= 1300
59 #pragma optimize("", on)
62 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
63 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
65 // These are here so we can avoid 'always true/false' warnings
66 // by referring to these instead of TRUE/FALSE
67 const bool wxTrue
= TRUE
;
68 const bool wxFalse
= FALSE
;
70 // Is this object a kind of (a subclass of) 'info'?
71 // E.g. is wxWindow a kind of wxObject?
72 // Go from this class to superclass, taking into account
73 // two possible base classes.
74 bool wxObject::IsKindOf(wxClassInfo
*info
) const
76 wxClassInfo
*thisInfo
= GetClassInfo();
77 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : FALSE
;
80 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
81 void wxObject::Dump(wxSTD ostream
& str
)
83 if (GetClassInfo() && GetClassInfo()->GetClassName())
84 str
<< GetClassInfo()->GetClassName();
86 str
<< _T("unknown object class");
91 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
96 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
97 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
99 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
);
103 #ifdef _WX_WANT_DELETE_VOID
104 void wxObject::operator delete ( void *buf
)
110 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
111 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
117 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
118 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
124 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
125 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
127 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
, TRUE
);
131 #ifdef _WX_WANT_ARRAY_DELETE_VOID
132 void wxObject::operator delete[] ( void *buf
)
134 wxDebugFree(buf
, TRUE
);
138 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
139 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
141 wxDebugFree(buf
, TRUE
);
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 wxClassInfo::~wxClassInfo()
152 // remove this object from the linked list of all class infos: if we don't
153 // do it, loading/unloading a DLL containing static wxClassInfo objects is
155 if ( this == sm_first
)
161 wxClassInfo
*info
= sm_first
;
164 if ( info
->m_next
== this )
166 info
->m_next
= m_next
;
175 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
179 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
183 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
185 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
193 // a tiny InitializeClasses() helper
195 inline wxClassInfo
*wxClassInfo::GetBaseByName(const wxChar
*name
)
200 wxClassInfo
*classInfo
= (wxClassInfo
*)sm_classTable
->Get(name
);
203 // this must be fixed, other things will work wrongly later if you get this
206 wxFAIL_MSG( wxString::Format
208 _T("base class '%s' is unknown to wxWindows RTTI"),
212 #endif // __WXDEBUG__
217 // Set pointers to base class(es) to speed up IsKindOf
218 void wxClassInfo::InitializeClasses()
220 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
221 // link any object module twice mistakenly) will break this function
222 // because it will enter an infinite loop and eventually die with "out of
223 // memory" - as this is quite hard to detect if you're unaware of this,
224 // try to do some checks here
227 static const size_t nMaxClasses
= 10000; // more than we'll ever have
231 sm_classTable
= new wxHashTable(wxKEY_STRING
);
233 // Index all class infos by their class name
236 for(info
= sm_first
; info
; info
= info
->m_next
)
238 if (info
->m_className
)
240 wxASSERT_MSG( ++nClass
< nMaxClasses
,
241 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
242 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
246 // Set base pointers for each wxClassInfo
248 for(info
= sm_first
; info
; info
= info
->m_next
)
250 info
->m_baseInfo1
= GetBaseByName(info
->GetBaseClassName1());
251 info
->m_baseInfo2
= GetBaseByName(info
->GetBaseClassName2());
255 void wxClassInfo::CleanUpClasses()
257 delete wxClassInfo::sm_classTable
;
258 wxClassInfo::sm_classTable
= NULL
;
262 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
264 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
265 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
268 if ( wxClassInfo::sm_classTable
)
270 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
271 return info
? info
->CreateObject() : NULL
;
273 else // no sm_classTable yet
275 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
277 info
= info
->m_next
)
279 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
280 return info
->CreateObject();
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 // Initialize ref data from another object (needed for copy constructor and
293 // assignment operator)
294 void wxObject::InitFrom(const wxObject
& other
)
296 m_refData
= other
.m_refData
;
298 m_refData
->m_count
++;
301 void wxObject::Ref(const wxObject
& clone
)
303 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
304 DEBUG_PRINTF(wxObject::Ref
)
307 // nothing to be done
308 if (m_refData
== clone
.m_refData
)
311 // delete reference to old data
314 // reference new data
315 if ( clone
.m_refData
)
317 m_refData
= clone
.m_refData
;
318 ++(m_refData
->m_count
);
322 void wxObject::UnRef()
326 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
328 if ( !--m_refData
->m_count
)
334 void wxObject::AllocExclusive()
338 m_refData
= CreateRefData();
340 else if ( m_refData
->GetRefCount() > 1 )
342 // note that ref is not going to be destroyed in this case
343 const wxObjectRefData
* ref
= m_refData
;
346 // ... so we can still access it
347 m_refData
= CloneRefData(ref
);
349 //else: ref count is 1, we are exclusive owners of m_refData anyhow
351 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
352 _T("wxObject::AllocExclusive() failed.") );
355 wxObjectRefData
*wxObject::CreateRefData() const
357 // if you use AllocExclusive() you must override this method
358 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
364 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
366 // if you use AllocExclusive() you must override this method
367 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
376 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
379 void __initialize_Cplusplus(void);
380 void wxWindowsDylibInit(void);
383 // Dynamic shared library (dylib) initialization routine
384 // required to initialize static C++ objects bacause of lazy dynamic linking
385 // http://developer.apple.com/techpubs/macosx/Essentials/
386 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
388 void wxWindowsDylibInit()
390 // The function __initialize_Cplusplus() must be called from the shared
391 // library initialization routine to cause the static C++ objects in
392 // the library to be initialized (reference number 2441683).
394 // This only seems to be necessary if the library initialization routine
395 // needs to use the static C++ objects
396 __initialize_Cplusplus();