1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/object.cpp
3 // Purpose: wxObject implementation
4 // Author: Julian Smart
5 // Modified by: Ron Lee
7 // Copyright: (c) 1998 Julian Smart
8 // (c) 2001 Ron Lee <ron@debian.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
20 #include "wx/object.h"
22 #include "wx/memory.h"
28 #if wxUSE_DEBUG_CONTEXT
29 #if defined(__VISAGECPP__)
30 #define DEBUG_PRINTF(NAME) { static int raz=0; \
31 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
33 #define DEBUG_PRINTF(NAME)
35 #endif // wxUSE_DEBUG_CONTEXT
37 // we must disable optimizations for VC.NET because otherwise its too eager
38 // linker discards wxClassInfo objects in release build thus breaking many,
40 #if defined __VISUALC__ && __VISUALC__ >= 1300
41 #pragma optimize("", off)
44 #if wxUSE_EXTENDED_RTTI
45 const wxClassInfo
* wxObject::ms_classParents
[] = { NULL
} ;
46 wxObject
* wxVariantOfPtrToObjectConverterwxObject ( const wxAny
&data
)
47 { return wxANY_AS(data
, wxObject
*); }
48 wxAny
wxObjectToVariantConverterwxObject ( wxObject
*data
)
49 { return wxAny( dynamic_cast<wxObject
*> (data
) ) ; }
51 wxClassInfo
wxObject::ms_classInfo(ms_classParents
, wxEmptyString
, wxT("wxObject"),
52 (int) sizeof(wxObject
), \
53 (wxObjectConstructorFn
) 0 ,
55 0 , wxVariantOfPtrToObjectConverterwxObject
, 0 , wxObjectToVariantConverterwxObject
);
57 template<> void wxStringWriteValue(wxString
& , wxObject
* const & ){ wxFAIL_MSG("unreachable"); }
58 template<> void wxStringWriteValue(wxString
& , wxObject
const & ){ wxFAIL_MSG("unreachable"); }
60 wxClassTypeInfo
s_typeInfo(wxT_OBJECT_PTR
, &wxObject::ms_classInfo
, NULL
, NULL
, typeid(wxObject
*).name() ) ;
61 wxClassTypeInfo
s_typeInfowxObject(wxT_OBJECT
, &wxObject::ms_classInfo
, NULL
, NULL
, typeid(wxObject
).name() ) ;
63 wxClassInfo
wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
64 (int) sizeof(wxObject
),
65 (wxObjectConstructorFn
) 0 );
68 // restore optimizations
69 #if defined __VISUALC__ && __VISUALC__ >= 1300
70 #pragma optimize("", on)
73 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
74 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
76 // when using XTI, this method is already implemented inline inside
77 // DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function
78 // non-inline because this allows us to have a non-inline virtual function in
79 // all wx classes and this solves linking problems for HP-UX native toolchain
80 // and possibly others (we could make dtor non-inline as well but it's more
81 // useful to keep it inline than this function)
82 #if !wxUSE_EXTENDED_RTTI
84 wxClassInfo
*wxObject::GetClassInfo() const
86 return &wxObject::ms_classInfo
;
89 #endif // wxUSE_EXTENDED_RTTI
91 // this variable exists only so that we can avoid 'always true/false' warnings
92 const bool wxFalse
= false;
94 // Is this object a kind of (a subclass of) 'info'?
95 // E.g. is wxWindow a kind of wxObject?
96 // Go from this class to superclass, taking into account
97 // two possible base classes.
98 bool wxObject::IsKindOf(const wxClassInfo
*info
) const
100 const wxClassInfo
*thisInfo
= GetClassInfo();
101 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : false ;
104 #if wxUSE_MEMORY_TRACING && defined( new )
109 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
110 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
112 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, true);
116 #ifdef _WX_WANT_DELETE_VOID
117 void wxObject::operator delete ( void *buf
)
123 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
124 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
130 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
131 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
137 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
138 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
140 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, true, true);
144 #ifdef _WX_WANT_ARRAY_DELETE_VOID
145 void wxObject::operator delete[] ( void *buf
)
147 wxDebugFree(buf
, true);
151 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
152 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
154 wxDebugFree(buf
, true);
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 wxClassInfo::~wxClassInfo()
165 // remove this object from the linked list of all class infos: if we don't
166 // do it, loading/unloading a DLL containing static wxClassInfo objects is
168 if ( this == sm_first
)
174 wxClassInfo
*info
= sm_first
;
177 if ( info
->m_next
== this )
179 info
->m_next
= m_next
;
189 wxClassInfo
*wxClassInfo::FindClass(const wxString
& className
)
193 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
197 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
199 if ( className
== info
->GetClassName() )
207 // Reentrance can occur on some platforms (Solaris for one), as the use of hash
208 // and string objects can cause other modules to load and register classes
209 // before the original call returns. This is handled by keeping the hash table
210 // local when it is first created and only assigning it to the global variable
211 // when the function is ready to return.
213 // That does make the assumption that after the function has completed the
214 // first time the problem will no longer happen; all the modules it depends on
215 // will have been loaded. The assumption is checked using the 'entry' variable
216 // as a reentrance guard, it checks that once the hash table is global it is
217 // not accessed multiple times simulateously.
219 void wxClassInfo::Register()
222 // reentrance guard - see note above
223 static int entry
= 0;
224 #endif // wxDEBUG_LEVEL
226 wxHashTable
*classTable
;
228 if ( !sm_classTable
)
230 // keep the hash local initially, reentrance is possible
231 classTable
= new wxHashTable(wxKEY_STRING
);
235 // guard againt reentrance once the global has been created
236 wxASSERT_MSG(++entry
== 1, wxT("wxClassInfo::Register() reentrance"));
237 classTable
= sm_classTable
;
240 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
241 // link any object module twice mistakenly, or link twice against wx shared
242 // library) will break this function because it will enter an infinite loop
243 // and eventually die with "out of memory" - as this is quite hard to
244 // detect if you're unaware of this, try to do some checks here.
245 wxASSERT_MSG( classTable
->Get(m_className
) == NULL
,
248 wxT("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
253 classTable
->Put(m_className
, (wxObject
*)this);
255 // if we're using a local hash we need to try to make it global
256 if ( sm_classTable
!= classTable
)
258 if ( !sm_classTable
)
260 // make the hash global
261 sm_classTable
= classTable
;
265 // the gobal hash has already been created by a reentrant call,
266 // so delete the local hash and try again
274 #endif // wxDEBUG_LEVEL
277 void wxClassInfo::Unregister()
281 sm_classTable
->Delete(m_className
);
282 if ( sm_classTable
->GetCount() == 0 )
284 wxDELETE(sm_classTable
);
289 wxObject
*wxCreateDynamicObject(const wxString
& name
)
291 #if wxUSE_DEBUG_CONTEXT
292 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
295 if ( wxClassInfo::sm_classTable
)
297 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
298 return info
? info
->CreateObject() : NULL
;
300 else // no sm_classTable yet
302 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
304 info
= info
->m_next
)
306 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
307 return info
->CreateObject();
314 // iterator interface
315 wxClassInfo::const_iterator::value_type
316 wxClassInfo::const_iterator::operator*() const
318 return (wxClassInfo
*)m_node
->GetData();
321 wxClassInfo::const_iterator
& wxClassInfo::const_iterator::operator++()
323 m_node
= m_table
->Next();
327 const wxClassInfo::const_iterator
wxClassInfo::const_iterator::operator++(int)
329 wxClassInfo::const_iterator tmp
= *this;
330 m_node
= m_table
->Next();
334 wxClassInfo::const_iterator
wxClassInfo::begin_classinfo()
336 sm_classTable
->BeginFind();
338 return const_iterator(sm_classTable
->Next(), sm_classTable
);
341 wxClassInfo::const_iterator
wxClassInfo::end_classinfo()
343 return const_iterator(NULL
, NULL
);
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
350 void wxRefCounter::DecRef()
352 wxASSERT_MSG( m_count
> 0, "invalid ref data count" );
354 if ( --m_count
== 0 )
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
363 void wxObject::Ref(const wxObject
& clone
)
365 #if wxUSE_DEBUG_CONTEXT
366 DEBUG_PRINTF(wxObject::Ref
)
369 // nothing to be done
370 if (m_refData
== clone
.m_refData
)
373 // delete reference to old data
376 // reference new data
377 if ( clone
.m_refData
)
379 m_refData
= clone
.m_refData
;
384 void wxObject::UnRef()
393 void wxObject::AllocExclusive()
397 m_refData
= CreateRefData();
399 else if ( m_refData
->GetRefCount() > 1 )
401 // note that ref is not going to be destroyed in this case
402 const wxObjectRefData
* ref
= m_refData
;
405 // ... so we can still access it
406 m_refData
= CloneRefData(ref
);
408 //else: ref count is 1, we are exclusive owners of m_refData anyhow
410 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
411 wxT("wxObject::AllocExclusive() failed.") );
414 wxObjectRefData
*wxObject::CreateRefData() const
416 // if you use AllocExclusive() you must override this method
417 wxFAIL_MSG( wxT("CreateRefData() must be overridden if called!") );
423 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
425 // if you use AllocExclusive() you must override this method
426 wxFAIL_MSG( wxT("CloneRefData() must be overridden if called!") );