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"
22 #include "wx/object.h"
27 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
28 #include "wx/memory.h"
31 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
32 #if defined(__VISAGECPP__)
33 #define DEBUG_PRINTF(NAME) { static int raz=0; \
34 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
36 #define DEBUG_PRINTF(NAME)
38 #endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
40 // we must disable optimizations for VC.NET because otherwise its too eager
41 // linker discards wxClassInfo objects in release build thus breaking many,
43 #if defined __VISUALC__ && __VISUALC__ >= 1300
44 #pragma optimize("", off)
47 #if wxUSE_EXTENDED_RTTI
48 const wxClassInfo
* wxObject::ms_classParents
[] = { NULL
} ;
49 wxObject
* wxVariantToObjectConverterwxObject ( wxxVariant
&data
)
50 { return data
.wxTEMPLATED_MEMBER_CALL(Get
, wxObject
*) ; }
51 wxObject
* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant
&data
)
52 { return &data
.wxTEMPLATED_MEMBER_CALL(Get
, wxObject
) ; }
53 wxxVariant
wxObjectToVariantConverterwxObject ( wxObject
*data
)
54 { return wxxVariant( dynamic_cast<wxObject
*> (data
) ) ; }
55 wxClassInfo
wxObject::ms_classInfo(ms_classParents
, wxEmptyString
, wxT("wxObject"),
56 (int) sizeof(wxObject
), \
57 (wxObjectConstructorFn
) 0 ,
58 (wxPropertyInfo
*) NULL
,(wxHandlerInfo
*) NULL
,0 , 0 ,
59 0 , wxVariantOfPtrToObjectConverterwxObject
, wxVariantToObjectConverterwxObject
, wxObjectToVariantConverterwxObject
);
60 template<> void wxStringReadValue(const wxString
& , wxObject
* & ){assert(0) ;}
61 template<> void wxStringWriteValue(wxString
& , wxObject
* const & ){assert(0) ;}
62 template<> void wxStringReadValue(const wxString
& , wxObject
& ){assert(0) ;}
63 template<> void wxStringWriteValue(wxString
& , wxObject
const & ){assert(0) ;}
64 wxClassTypeInfo
s_typeInfo(wxT_OBJECT_PTR
, &wxObject::ms_classInfo
, NULL
, NULL
, typeid(wxObject
*).name() ) ;
65 wxClassTypeInfo
s_typeInfowxObject(wxT_OBJECT
, &wxObject::ms_classInfo
, NULL
, NULL
, typeid(wxObject
).name() ) ;
67 wxClassInfo
wxObject::ms_classInfo( wxT("wxObject"), 0, 0,
68 (int) sizeof(wxObject
),
69 (wxObjectConstructorFn
) 0 );
72 // restore optimizations
73 #if defined __VISUALC__ && __VISUALC__ >= 1300
74 #pragma optimize("", on)
77 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
78 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
80 // when using XTI, this method is already implemented inline inside
81 // DECLARE_DYNAMIC_CLASS but otherwise we intentionally make this function
82 // non-inline because this allows us to have a non-inline virtual function in
83 // all wx classes and this solves linking problems for HP-UX native toolchain
84 // and possibly others (we could make dtor non-inline as well but it's more
85 // useful to keep it inline than this function)
86 #if !wxUSE_EXTENDED_RTTI
88 wxClassInfo
*wxObject::GetClassInfo() const
90 return &wxObject::ms_classInfo
;
93 #endif // wxUSE_EXTENDED_RTTI
95 // this variable exists only so that we can avoid 'always true/false' warnings
96 const bool wxFalse
= false;
98 // Is this object a kind of (a subclass of) 'info'?
99 // E.g. is wxWindow a kind of wxObject?
100 // Go from this class to superclass, taking into account
101 // two possible base classes.
102 bool wxObject::IsKindOf(wxClassInfo
*info
) const
104 wxClassInfo
*thisInfo
= GetClassInfo();
105 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : false ;
108 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
113 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
114 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
116 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, true);
120 #ifdef _WX_WANT_DELETE_VOID
121 void wxObject::operator delete ( void *buf
)
127 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
128 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
134 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
135 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
141 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
142 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
144 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, true, true);
148 #ifdef _WX_WANT_ARRAY_DELETE_VOID
149 void wxObject::operator delete[] ( void *buf
)
151 wxDebugFree(buf
, true);
155 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
156 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
158 wxDebugFree(buf
, true);
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 wxClassInfo::~wxClassInfo()
169 // remove this object from the linked list of all class infos: if we don't
170 // do it, loading/unloading a DLL containing static wxClassInfo objects is
172 if ( this == sm_first
)
178 wxClassInfo
*info
= sm_first
;
181 if ( info
->m_next
== this )
183 info
->m_next
= m_next
;
193 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
197 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
201 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
203 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
211 void wxClassInfo::Register()
213 if ( !sm_classTable
)
215 sm_classTable
= new wxHashTable(wxKEY_STRING
);
218 // Using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
219 // link any object module twice mistakenly, or link twice against wx shared
220 // library) will break this function because it will enter an infinite loop
221 // and eventually die with "out of memory" - as this is quite hard to
222 // detect if you're unaware of this, try to do some checks here.
223 wxASSERT_MSG( sm_classTable
->Get(m_className
) == NULL
,
226 _T("Class \"%s\" already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() multiple times or linked some object file twice)?"),
231 sm_classTable
->Put(m_className
, (wxObject
*)this);
234 void wxClassInfo::Unregister()
238 sm_classTable
->Delete(m_className
);
239 if ( sm_classTable
->GetCount() == 0 )
241 delete sm_classTable
;
242 sm_classTable
= NULL
;
247 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
249 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
250 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
253 if ( wxClassInfo::sm_classTable
)
255 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
256 return info
? info
->CreateObject() : NULL
;
258 else // no sm_classTable yet
260 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
262 info
= info
->m_next
)
264 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
265 return info
->CreateObject();
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 void wxObject::Ref(const wxObject
& clone
)
279 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
280 DEBUG_PRINTF(wxObject::Ref
)
283 // nothing to be done
284 if (m_refData
== clone
.m_refData
)
287 // delete reference to old data
290 // reference new data
291 if ( clone
.m_refData
)
293 m_refData
= clone
.m_refData
;
294 ++(m_refData
->m_count
);
298 void wxObject::UnRef()
302 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
304 if ( --m_refData
->m_count
== 0 )
310 void wxObject::AllocExclusive()
314 m_refData
= CreateRefData();
316 else if ( m_refData
->GetRefCount() > 1 )
318 // note that ref is not going to be destroyed in this case
319 const wxObjectRefData
* ref
= m_refData
;
322 // ... so we can still access it
323 m_refData
= CloneRefData(ref
);
325 //else: ref count is 1, we are exclusive owners of m_refData anyhow
327 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
328 _T("wxObject::AllocExclusive() failed.") );
331 wxObjectRefData
*wxObject::CreateRefData() const
333 // if you use AllocExclusive() you must override this method
334 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
340 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
342 // if you use AllocExclusive() you must override this method
343 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );