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 defined(__WXDEBUG__) || 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 // __WXDEBUG__ || 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 defined(__WXDEBUG__) && 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 // This function wasn't written to be reentrant but there is a possiblity of
210 // reentrance if something it does causes a shared lib to load and register
211 // classes. On Solaris this happens when the wxHashTable is newed, so the first
212 // part of the function has been modified to handle it, and a wxASSERT checks
213 // against reentrance in the remainder of the function.
215 void wxClassInfo::Register()
217 if ( !sm_classTable
)
219 wxHashTable
*classTable
= new wxHashTable(wxKEY_STRING
);
221 // check for reentrance
225 sm_classTable
= classTable
;
229 // reentrance guard - see note above
230 static int entry
= 0;
231 wxASSERT_MSG(++entry
== 1, _T("wxClassInfo::Register() reentrance"));
234 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
235 // link any object module twice mistakenly, or link twice against wx shared
236 // library) will break this function because it will enter an infinite loop
237 // and eventually die with "out of memory" - as this is quite hard to
238 // detect if you're unaware of this, try to do some checks here.
239 wxASSERT_MSG( sm_classTable
->Get(m_className
) == NULL
,
242 _T("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
247 sm_classTable
->Put(m_className
, (wxObject
*)this);
254 void wxClassInfo::Unregister()
258 sm_classTable
->Delete(m_className
);
259 if ( sm_classTable
->GetCount() == 0 )
261 delete sm_classTable
;
262 sm_classTable
= NULL
;
267 wxObject
*wxCreateDynamicObject(const wxString
& name
)
269 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
270 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
273 if ( wxClassInfo::sm_classTable
)
275 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
276 return info
? info
->CreateObject() : NULL
;
278 else // no sm_classTable yet
280 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
282 info
= info
->m_next
)
284 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
285 return info
->CreateObject();
292 // iterator interface
293 wxClassInfo::const_iterator::value_type
294 wxClassInfo::const_iterator::operator*() const
296 return (wxClassInfo
*)m_node
->GetData();
299 wxClassInfo::const_iterator
& wxClassInfo::const_iterator::operator++()
301 m_node
= m_table
->Next();
305 const wxClassInfo::const_iterator
wxClassInfo::const_iterator::operator++(int)
307 wxClassInfo::const_iterator tmp
= *this;
308 m_node
= m_table
->Next();
312 wxClassInfo::const_iterator
wxClassInfo::begin_classinfo()
314 sm_classTable
->BeginFind();
316 return const_iterator(sm_classTable
->Next(), sm_classTable
);
319 wxClassInfo::const_iterator
wxClassInfo::end_classinfo()
321 return const_iterator(NULL
, NULL
);
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 void wxObjectRefData::DecRef()
330 if ( --m_count
== 0 )
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 void wxObject::Ref(const wxObject
& clone
)
341 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
342 DEBUG_PRINTF(wxObject::Ref
)
345 // nothing to be done
346 if (m_refData
== clone
.m_refData
)
349 // delete reference to old data
352 // reference new data
353 if ( clone
.m_refData
)
355 m_refData
= clone
.m_refData
;
360 void wxObject::UnRef()
364 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
371 void wxObject::AllocExclusive()
375 m_refData
= CreateRefData();
377 else if ( m_refData
->GetRefCount() > 1 )
379 // note that ref is not going to be destroyed in this case
380 const wxObjectRefData
* ref
= m_refData
;
383 // ... so we can still access it
384 m_refData
= CloneRefData(ref
);
386 //else: ref count is 1, we are exclusive owners of m_refData anyhow
388 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
389 _T("wxObject::AllocExclusive() failed.") );
392 wxObjectRefData
*wxObject::CreateRefData() const
394 // if you use AllocExclusive() you must override this method
395 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
401 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
403 // if you use AllocExclusive() you must override this method
404 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );