]>
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"
26 #include "wx/object.h"
31 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
32 #include "wx/memory.h"
35 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
37 #include "wx/ioswrap.h"
39 #if defined(__VISAGECPP__)
40 #define DEBUG_PRINTF(NAME) { static int raz=0; \
41 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
43 #define DEBUG_PRINTF(NAME)
45 #endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
47 // we must disable optimizations for VC.NET because otherwise its too eager
48 // linker discards wxClassInfo objects in release build thus breaking many,
50 #if defined __VISUALC__ && __VISUALC__ >= 1300
51 #pragma optimize("", off)
54 wxClassInfo
wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
55 (int) sizeof(wxObject
),
56 (wxObjectConstructorFn
) 0 );
58 // restore optimizations
59 #if defined __VISUALC__ && __VISUALC__ >= 1300
60 #pragma optimize("", on)
63 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
64 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
66 // These are here so we can avoid 'always true/false' warnings
67 // by referring to these instead of TRUE/FALSE
68 const bool wxTrue
= TRUE
;
69 const bool wxFalse
= FALSE
;
71 // Is this object a kind of (a subclass of) 'info'?
72 // E.g. is wxWindow a kind of wxObject?
73 // Go from this class to superclass, taking into account
74 // two possible base classes.
75 bool wxObject::IsKindOf(wxClassInfo
*info
) const
77 wxClassInfo
*thisInfo
= GetClassInfo();
78 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : FALSE
;
81 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
82 void wxObject::Dump(wxSTD ostream
& str
)
84 if (GetClassInfo() && GetClassInfo()->GetClassName())
85 str
<< GetClassInfo()->GetClassName();
87 str
<< _T("unknown object class");
92 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
97 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
98 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
100 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
);
104 #ifdef _WX_WANT_DELETE_VOID
105 void wxObject::operator delete ( void *buf
)
111 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
112 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
118 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
119 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
125 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
126 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
128 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
, TRUE
);
132 #ifdef _WX_WANT_ARRAY_DELETE_VOID
133 void wxObject::operator delete[] ( void *buf
)
135 wxDebugFree(buf
, TRUE
);
139 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
140 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
142 wxDebugFree(buf
, TRUE
);
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 wxClassInfo::~wxClassInfo()
153 // remove this object from the linked list of all class infos: if we don't
154 // do it, loading/unloading a DLL containing static wxClassInfo objects is
156 if ( this == sm_first
)
162 wxClassInfo
*info
= sm_first
;
165 if ( info
->m_next
== this )
167 info
->m_next
= m_next
;
176 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
180 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
184 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
186 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
194 // a tiny InitializeClasses() helper
196 inline wxClassInfo
*wxClassInfo::GetBaseByName(const wxChar
*name
)
201 wxClassInfo
*classInfo
= (wxClassInfo
*)sm_classTable
->Get(name
);
204 // this must be fixed, other things will work wrongly later if you get this
207 wxFAIL_MSG( wxString::Format
209 _T("base class '%s' is unknown to wxWindows RTTI"),
213 #endif // __WXDEBUG__
218 // Set pointers to base class(es) to speed up IsKindOf
219 void wxClassInfo::InitializeClasses()
221 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
222 // link any object module twice mistakenly) will break this function
223 // because it will enter an infinite loop and eventually die with "out of
224 // memory" - as this is quite hard to detect if you're unaware of this,
225 // try to do some checks here
228 static const size_t nMaxClasses
= 10000; // more than we'll ever have
232 sm_classTable
= new wxHashTable(wxKEY_STRING
);
234 // Index all class infos by their class name
237 for(info
= sm_first
; info
; info
= info
->m_next
)
239 if (info
->m_className
)
241 wxASSERT_MSG( ++nClass
< nMaxClasses
,
242 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
243 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
247 // Set base pointers for each wxClassInfo
249 for(info
= sm_first
; info
; info
= info
->m_next
)
251 info
->m_baseInfo1
= GetBaseByName(info
->GetBaseClassName1());
252 info
->m_baseInfo2
= GetBaseByName(info
->GetBaseClassName2());
256 void wxClassInfo::CleanUpClasses()
258 delete wxClassInfo::sm_classTable
;
259 wxClassInfo::sm_classTable
= NULL
;
263 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
265 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
266 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
269 if ( wxClassInfo::sm_classTable
)
271 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
272 return info
? info
->CreateObject() : NULL
;
274 else // no sm_classTable yet
276 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
278 info
= info
->m_next
)
280 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
281 return info
->CreateObject();
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 // Initialize ref data from another object (needed for copy constructor and
294 // assignment operator)
295 void wxObject::InitFrom(const wxObject
& other
)
297 m_refData
= other
.m_refData
;
299 m_refData
->m_count
++;
302 void wxObject::Ref(const wxObject
& clone
)
304 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
305 DEBUG_PRINTF(wxObject::Ref
)
308 // nothing to be done
309 if (m_refData
== clone
.m_refData
)
312 // delete reference to old data
315 // reference new data
316 if ( clone
.m_refData
)
318 m_refData
= clone
.m_refData
;
319 ++(m_refData
->m_count
);
323 void wxObject::UnRef()
327 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
329 if ( !--m_refData
->m_count
)
335 void wxObject::AllocExclusive()
339 m_refData
= CreateRefData();
341 else if ( m_refData
->GetRefCount() > 1 )
343 // note that ref is not going to be destroyed in this case
344 const wxObjectRefData
* ref
= m_refData
;
347 // ... so we can still access it
348 m_refData
= CloneRefData(ref
);
350 //else: ref count is 1, we are exclusive owners of m_refData anyhow
352 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
353 _T("wxObject::AllocExclusive() failed.") );
356 wxObjectRefData
*wxObject::CreateRefData() const
358 // if you use AllocExclusive() you must override this method
359 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
365 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
367 // if you use AllocExclusive() you must override this method
368 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
380 void __initialize_Cplusplus(void);
381 void wxWindowsDylibInit(void);
384 // Dynamic shared library (dylib) initialization routine
385 // required to initialize static C++ objects bacause of lazy dynamic linking
386 // http://developer.apple.com/techpubs/macosx/Essentials/
387 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
389 void wxWindowsDylibInit()
391 // The function __initialize_Cplusplus() must be called from the shared
392 // library initialization routine to cause the static C++ objects in
393 // the library to be initialized (reference number 2441683).
395 // This only seems to be necessary if the library initialization routine
396 // needs to use the static C++ objects
397 __initialize_Cplusplus();