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"
28 #if defined(__WXDEBUG__) || 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 // __WXDEBUG__ || 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
* wxVariantToObjectConverterwxObject ( wxxVariant
&data
)
47 { return data
.wxTEMPLATED_MEMBER_CALL(Get
, wxObject
*) ; }
48 wxObject
* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant
&data
)
49 { return &data
.wxTEMPLATED_MEMBER_CALL(Get
, wxObject
) ; }
50 wxxVariant
wxObjectToVariantConverterwxObject ( wxObject
*data
)
51 { return wxxVariant( dynamic_cast<wxObject
*> (data
) ) ; }
52 wxClassInfo
wxObject::ms_classInfo(ms_classParents
, wxEmptyString
, wxT("wxObject"),
53 (int) sizeof(wxObject
), \
54 (wxObjectConstructorFn
) 0 ,
55 (wxPropertyInfo
*) NULL
,(wxHandlerInfo
*) NULL
,0 , 0 ,
56 0 , wxVariantOfPtrToObjectConverterwxObject
, wxVariantToObjectConverterwxObject
, wxObjectToVariantConverterwxObject
);
57 template<> void wxStringReadValue(const wxString
& , wxObject
* & ){assert(0) ;}
58 template<> void wxStringWriteValue(wxString
& , wxObject
* const & ){assert(0) ;}
59 template<> void wxStringReadValue(const wxString
& , wxObject
& ){assert(0) ;}
60 template<> void wxStringWriteValue(wxString
& , wxObject
const & ){assert(0) ;}
61 wxClassTypeInfo
s_typeInfo(wxT_OBJECT_PTR
, &wxObject::ms_classInfo
, NULL
, NULL
, typeid(wxObject
*).name() ) ;
62 wxClassTypeInfo
s_typeInfowxObject(wxT_OBJECT
, &wxObject::ms_classInfo
, NULL
, NULL
, typeid(wxObject
).name() ) ;
64 wxClassInfo
wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
65 (int) sizeof(wxObject
),
66 (wxObjectConstructorFn
) 0 );
69 // restore optimizations
70 #if defined __VISUALC__ && __VISUALC__ >= 1300
71 #pragma optimize("", on)
74 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
75 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
77 // when using XTI, this method is already implemented inline inside
78 // DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function
79 // non-inline because this allows us to have a non-inline virtual function in
80 // all wx classes and this solves linking problems for HP-UX native toolchain
81 // and possibly others (we could make dtor non-inline as well but it's more
82 // useful to keep it inline than this function)
83 #if !wxUSE_EXTENDED_RTTI
85 wxClassInfo
*wxObject::GetClassInfo() const
87 return &wxObject::ms_classInfo
;
90 #endif // wxUSE_EXTENDED_RTTI
92 // this variable exists only so that we can avoid 'always true/false' warnings
93 const bool wxFalse
= false;
95 // Is this object a kind of (a subclass of) 'info'?
96 // E.g. is wxWindow a kind of wxObject?
97 // Go from this class to superclass, taking into account
98 // two possible base classes.
99 bool wxObject::IsKindOf(wxClassInfo
*info
) const
101 wxClassInfo
*thisInfo
= GetClassInfo();
102 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : false ;
105 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
110 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
111 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
113 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, true);
117 #ifdef _WX_WANT_DELETE_VOID
118 void wxObject::operator delete ( void *buf
)
124 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
125 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
131 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
132 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
138 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
139 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
141 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, true, true);
145 #ifdef _WX_WANT_ARRAY_DELETE_VOID
146 void wxObject::operator delete[] ( void *buf
)
148 wxDebugFree(buf
, true);
152 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
153 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
155 wxDebugFree(buf
, true);
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 wxClassInfo::~wxClassInfo()
166 // remove this object from the linked list of all class infos: if we don't
167 // do it, loading/unloading a DLL containing static wxClassInfo objects is
169 if ( this == sm_first
)
175 wxClassInfo
*info
= sm_first
;
178 if ( info
->m_next
== this )
180 info
->m_next
= m_next
;
190 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
194 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
198 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
200 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
208 // This function wasn't written to be reentrant but there is a possiblity of
209 // reentrance if something it does causes a shared lib to load and register
210 // classes. On Solaris this happens when the wxHashTable is newed, so the first
211 // part of the function has been modified to handle it, and a wxASSERT checks
212 // against reentrance in the remainder of the function.
214 void wxClassInfo::Register()
216 if ( !sm_classTable
)
218 wxHashTable
*classTable
= new wxHashTable(wxKEY_STRING
);
220 // check for reentrance
224 sm_classTable
= classTable
;
228 // reentrance guard - see note above
229 static int entry
= 0;
230 wxASSERT_MSG(++entry
== 1, _T("wxClassInfo::Register() reentrance"));
233 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
234 // link any object module twice mistakenly, or link twice against wx shared
235 // library) will break this function because it will enter an infinite loop
236 // and eventually die with "out of memory" - as this is quite hard to
237 // detect if you're unaware of this, try to do some checks here.
238 wxASSERT_MSG( sm_classTable
->Get(m_className
) == NULL
,
241 _T("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
246 sm_classTable
->Put(m_className
, (wxObject
*)this);
253 void wxClassInfo::Unregister()
257 sm_classTable
->Delete(m_className
);
258 if ( sm_classTable
->GetCount() == 0 )
260 delete sm_classTable
;
261 sm_classTable
= NULL
;
266 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
268 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
269 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
272 if ( wxClassInfo::sm_classTable
)
274 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
275 return info
? info
->CreateObject() : NULL
;
277 else // no sm_classTable yet
279 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
281 info
= info
->m_next
)
283 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
284 return info
->CreateObject();
291 // iterator interface
292 wxClassInfo::const_iterator::value_type
293 wxClassInfo::const_iterator::operator*() const
295 return (wxClassInfo
*)m_node
->GetData();
298 wxClassInfo::const_iterator
& wxClassInfo::const_iterator::operator++()
300 m_node
= m_table
->Next();
304 const wxClassInfo::const_iterator
wxClassInfo::const_iterator::operator++(int)
306 wxClassInfo::const_iterator tmp
= *this;
307 m_node
= m_table
->Next();
311 wxClassInfo::const_iterator
wxClassInfo::begin_classinfo()
313 sm_classTable
->BeginFind();
315 return const_iterator(sm_classTable
->Next(), sm_classTable
);
318 wxClassInfo::const_iterator
wxClassInfo::end_classinfo()
320 return const_iterator(NULL
, NULL
);
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 void wxObjectRefData::DecRef()
329 if ( --m_count
== 0 )
334 // ----------------------------------------------------------------------------
336 // ----------------------------------------------------------------------------
338 void wxObject::Ref(const wxObject
& clone
)
340 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
341 DEBUG_PRINTF(wxObject::Ref
)
344 // nothing to be done
345 if (m_refData
== clone
.m_refData
)
348 // delete reference to old data
351 // reference new data
352 if ( clone
.m_refData
)
354 m_refData
= clone
.m_refData
;
359 void wxObject::UnRef()
363 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
370 void wxObject::AllocExclusive()
374 m_refData
= CreateRefData();
376 else if ( m_refData
->GetRefCount() > 1 )
378 // note that ref is not going to be destroyed in this case
379 const wxObjectRefData
* ref
= m_refData
;
382 // ... so we can still access it
383 m_refData
= CloneRefData(ref
);
385 //else: ref count is 1, we are exclusive owners of m_refData anyhow
387 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
388 _T("wxObject::AllocExclusive() failed.") );
391 wxObjectRefData
*wxObject::CreateRefData() const
393 // if you use AllocExclusive() you must override this method
394 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
400 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
402 // if you use AllocExclusive() you must override this method
403 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );