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 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
21 #include "wx/object.h"
23 #include "wx/memory.h"
29 #if wxUSE_DEBUG_CONTEXT
30 #if defined(__VISAGECPP__)
31 #define DEBUG_PRINTF(NAME) { static int raz=0; \
32 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
34 #define DEBUG_PRINTF(NAME)
36 #endif // wxUSE_DEBUG_CONTEXT
38 // we must disable optimizations for VC.NET because otherwise its too eager
39 // linker discards wxClassInfo objects in release build thus breaking many,
41 #if defined __VISUALC__ && __VISUALC__ >= 1300
42 #pragma optimize("", off)
45 #if wxUSE_EXTENDED_RTTI
46 const wxClassInfo
* wxObject::ms_classParents
[] = { NULL
} ;
47 wxObject
* wxVariantToObjectConverterwxObject ( wxxVariant
&data
)
48 { return data
.wxTEMPLATED_MEMBER_CALL(Get
, wxObject
*) ; }
49 wxObject
* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant
&data
)
50 { return &data
.wxTEMPLATED_MEMBER_CALL(Get
, wxObject
) ; }
51 wxxVariant
wxObjectToVariantConverterwxObject ( wxObject
*data
)
52 { return wxxVariant( dynamic_cast<wxObject
*> (data
) ) ; }
53 wxClassInfo
wxObject::ms_classInfo(ms_classParents
, wxEmptyString
, wxT("wxObject"),
54 (int) sizeof(wxObject
), \
55 (wxObjectConstructorFn
) 0 ,
57 0 , wxVariantOfPtrToObjectConverterwxObject
, wxVariantToObjectConverterwxObject
, wxObjectToVariantConverterwxObject
);
58 template<> void wxStringReadValue(const wxString
& , wxObject
* & ){assert(0) ;}
59 template<> void wxStringWriteValue(wxString
& , wxObject
* const & ){assert(0) ;}
60 template<> void wxStringReadValue(const wxString
& , wxObject
& ){assert(0) ;}
61 template<> void wxStringWriteValue(wxString
& , wxObject
const & ){assert(0) ;}
62 wxClassTypeInfo
s_typeInfo(wxT_OBJECT_PTR
, &wxObject::ms_classInfo
, NULL
, NULL
, typeid(wxObject
*).name() ) ;
63 wxClassTypeInfo
s_typeInfowxObject(wxT_OBJECT
, &wxObject::ms_classInfo
, NULL
, NULL
, typeid(wxObject
).name() ) ;
65 wxClassInfo
wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
66 (int) sizeof(wxObject
),
67 (wxObjectConstructorFn
) 0 );
70 // restore optimizations
71 #if defined __VISUALC__ && __VISUALC__ >= 1300
72 #pragma optimize("", on)
75 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
76 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
78 // when using XTI, this method is already implemented inline inside
79 // DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function
80 // non-inline because this allows us to have a non-inline virtual function in
81 // all wx classes and this solves linking problems for HP-UX native toolchain
82 // and possibly others (we could make dtor non-inline as well but it's more
83 // useful to keep it inline than this function)
84 #if !wxUSE_EXTENDED_RTTI
86 wxClassInfo
*wxObject::GetClassInfo() const
88 return &wxObject::ms_classInfo
;
91 #endif // wxUSE_EXTENDED_RTTI
93 // this variable exists only so that we can avoid 'always true/false' warnings
94 const bool wxFalse
= false;
96 // Is this object a kind of (a subclass of) 'info'?
97 // E.g. is wxWindow a kind of wxObject?
98 // Go from this class to superclass, taking into account
99 // two possible base classes.
100 bool wxObject::IsKindOf(const wxClassInfo
*info
) const
102 const wxClassInfo
*thisInfo
= GetClassInfo();
103 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : false ;
106 #if wxUSE_MEMORY_TRACING && defined( new )
111 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
112 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
114 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, true);
118 #ifdef _WX_WANT_DELETE_VOID
119 void wxObject::operator delete ( void *buf
)
125 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
126 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
132 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
133 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
139 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
140 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
142 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, true, true);
146 #ifdef _WX_WANT_ARRAY_DELETE_VOID
147 void wxObject::operator delete[] ( void *buf
)
149 wxDebugFree(buf
, true);
153 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
154 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
156 wxDebugFree(buf
, true);
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 wxClassInfo::~wxClassInfo()
167 // remove this object from the linked list of all class infos: if we don't
168 // do it, loading/unloading a DLL containing static wxClassInfo objects is
170 if ( this == sm_first
)
176 wxClassInfo
*info
= sm_first
;
179 if ( info
->m_next
== this )
181 info
->m_next
= m_next
;
191 wxClassInfo
*wxClassInfo::FindClass(const wxString
& className
)
195 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
199 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
201 if ( className
== info
->GetClassName() )
209 // Reentrance can occur on some platforms (Solaris for one), as the use of hash
210 // and string objects can cause other modules to load and register classes
211 // before the original call returns. This is handled by keeping the hash table
212 // local when it is first created and only assigning it to the global variable
213 // when the function is ready to return.
215 // That does make the assumption that after the function has completed the
216 // first time the problem will no longer happen; all the modules it depends on
217 // will have been loaded. The assumption is checked using the 'entry' variable
218 // as a reentrance guard, it checks that once the hash table is global it is
219 // not accessed multiple times simulateously.
221 void wxClassInfo::Register()
224 // reentrance guard - see note above
225 static int entry
= 0;
226 #endif // wxDEBUG_LEVEL
228 wxHashTable
*classTable
;
230 if ( !sm_classTable
)
232 // keep the hash local initially, reentrance is possible
233 classTable
= new wxHashTable(wxKEY_STRING
);
237 // guard againt reentrance once the global has been created
238 wxASSERT_MSG(++entry
== 1, wxT("wxClassInfo::Register() reentrance"));
239 classTable
= sm_classTable
;
242 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
243 // link any object module twice mistakenly, or link twice against wx shared
244 // library) will break this function because it will enter an infinite loop
245 // and eventually die with "out of memory" - as this is quite hard to
246 // detect if you're unaware of this, try to do some checks here.
247 wxASSERT_MSG( classTable
->Get(m_className
) == NULL
,
250 wxT("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
255 classTable
->Put(m_className
, (wxObject
*)this);
257 // if we're using a local hash we need to try to make it global
258 if ( sm_classTable
!= classTable
)
260 if ( !sm_classTable
)
262 // make the hash global
263 sm_classTable
= classTable
;
267 // the gobal hash has already been created by a reentrant call,
268 // so delete the local hash and try again
276 #endif // wxDEBUG_LEVEL
279 void wxClassInfo::Unregister()
283 sm_classTable
->Delete(m_className
);
284 if ( sm_classTable
->GetCount() == 0 )
286 delete sm_classTable
;
287 sm_classTable
= NULL
;
292 wxObject
*wxCreateDynamicObject(const wxString
& name
)
294 #if wxUSE_DEBUG_CONTEXT
295 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
298 if ( wxClassInfo::sm_classTable
)
300 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
301 return info
? info
->CreateObject() : NULL
;
303 else // no sm_classTable yet
305 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
307 info
= info
->m_next
)
309 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
310 return info
->CreateObject();
317 // iterator interface
318 wxClassInfo::const_iterator::value_type
319 wxClassInfo::const_iterator::operator*() const
321 return (wxClassInfo
*)m_node
->GetData();
324 wxClassInfo::const_iterator
& wxClassInfo::const_iterator::operator++()
326 m_node
= m_table
->Next();
330 const wxClassInfo::const_iterator
wxClassInfo::const_iterator::operator++(int)
332 wxClassInfo::const_iterator tmp
= *this;
333 m_node
= m_table
->Next();
337 wxClassInfo::const_iterator
wxClassInfo::begin_classinfo()
339 sm_classTable
->BeginFind();
341 return const_iterator(sm_classTable
->Next(), sm_classTable
);
344 wxClassInfo::const_iterator
wxClassInfo::end_classinfo()
346 return const_iterator(NULL
, NULL
);
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 void wxRefCounter::DecRef()
355 wxASSERT_MSG( m_count
> 0, "invalid ref data count" );
357 if ( --m_count
== 0 )
362 // ----------------------------------------------------------------------------
364 // ----------------------------------------------------------------------------
366 void wxObject::Ref(const wxObject
& clone
)
368 #if wxUSE_DEBUG_CONTEXT
369 DEBUG_PRINTF(wxObject::Ref
)
372 // nothing to be done
373 if (m_refData
== clone
.m_refData
)
376 // delete reference to old data
379 // reference new data
380 if ( clone
.m_refData
)
382 m_refData
= clone
.m_refData
;
387 void wxObject::UnRef()
396 void wxObject::AllocExclusive()
400 m_refData
= CreateRefData();
402 else if ( m_refData
->GetRefCount() > 1 )
404 // note that ref is not going to be destroyed in this case
405 const wxObjectRefData
* ref
= m_refData
;
408 // ... so we can still access it
409 m_refData
= CloneRefData(ref
);
411 //else: ref count is 1, we are exclusive owners of m_refData anyhow
413 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
414 wxT("wxObject::AllocExclusive() failed.") );
417 wxObjectRefData
*wxObject::CreateRefData() const
419 // if you use AllocExclusive() you must override this method
420 wxFAIL_MSG( wxT("CreateRefData() must be overridden if called!") );
426 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
428 // if you use AllocExclusive() you must override this method
429 wxFAIL_MSG( wxT("CloneRefData() must be overridden if called!") );