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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "object.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/object.h"
31 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
32 #include "wx/memory.h"
35 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
36 #if defined(__VISAGECPP__)
37 #define DEBUG_PRINTF(NAME) { static int raz=0; \
38 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
40 #define DEBUG_PRINTF(NAME)
42 #endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
44 // we must disable optimizations for VC.NET because otherwise its too eager
45 // linker discards wxClassInfo objects in release build thus breaking many,
47 #if defined __VISUALC__ && __VISUALC__ >= 1300
48 #pragma optimize("", off)
51 #if wxUSE_EXTENDED_RTTI
52 const wxClassInfo
* wxObject::sm_classParentswxObject
[] = { NULL
} ;
53 wxObject
* wxVariantToObjectConverterwxObject ( wxxVariant
&data
)
54 { return data
.Get
<wxObject
*>() ; }
55 wxObject
* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant
&data
)
56 { return &data
.Get
<wxObject
>() ; }
57 wxxVariant
wxObjectToVariantConverterwxObject ( wxObject
*data
)
58 { return wxxVariant( dynamic_cast<wxObject
*> (data
) ) ; }
59 wxClassInfo
wxObject::sm_classwxObject(sm_classParentswxObject
, wxT("") , wxT("wxObject"),
60 (int) sizeof(wxObject
), \
61 (wxObjectConstructorFn
) 0 ,
62 (wxPropertyInfo
*) NULL
,(wxHandlerInfo
*) NULL
,0 , 0 ,
63 0 , wxVariantOfPtrToObjectConverterwxObject
, wxVariantToObjectConverterwxObject
, wxObjectToVariantConverterwxObject
);
64 template<> void wxStringReadValue(const wxString
& , wxObject
* & ){assert(0) ;}
65 template<> void wxStringWriteValue(wxString
& , wxObject
* const & ){assert(0) ;}
66 template<> void wxStringReadValue(const wxString
& , wxObject
& ){assert(0) ;}
67 template<> void wxStringWriteValue(wxString
& , wxObject
const & ){assert(0) ;}
68 template<> const wxTypeInfo
* wxGetTypeInfo( wxObject
** )
69 { static wxClassTypeInfo
s_typeInfo(wxT_OBJECT_PTR
, &wxObject::sm_classwxObject
) ; return &s_typeInfo
; }
70 template<> const wxTypeInfo
* wxGetTypeInfo( wxObject
* )
71 { static wxClassTypeInfo
s_typeInfo(wxT_OBJECT
, &wxObject::sm_classwxObject
) ; return &s_typeInfo
; }
73 wxClassInfo
wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
74 (int) sizeof(wxObject
),
75 (wxObjectConstructorFn
) 0 );
78 // restore optimizations
79 #if defined __VISUALC__ && __VISUALC__ >= 1300
80 #pragma optimize("", on)
83 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
84 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
86 // These are here so we can avoid 'always true/false' warnings
87 // by referring to these instead of TRUE/FALSE
88 const bool wxTrue
= TRUE
;
89 const bool wxFalse
= FALSE
;
91 // Is this object a kind of (a subclass of) 'info'?
92 // E.g. is wxWindow a kind of wxObject?
93 // Go from this class to superclass, taking into account
94 // two possible base classes.
95 bool wxObject::IsKindOf(wxClassInfo
*info
) const
97 wxClassInfo
*thisInfo
= GetClassInfo();
98 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : FALSE
;
101 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
106 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
107 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
109 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
);
113 #ifdef _WX_WANT_DELETE_VOID
114 void wxObject::operator delete ( void *buf
)
120 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
121 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
127 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
128 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
134 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
135 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
137 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
, TRUE
);
141 #ifdef _WX_WANT_ARRAY_DELETE_VOID
142 void wxObject::operator delete[] ( void *buf
)
144 wxDebugFree(buf
, TRUE
);
148 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
149 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
151 wxDebugFree(buf
, TRUE
);
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 wxClassInfo::~wxClassInfo()
162 // remove this object from the linked list of all class infos: if we don't
163 // do it, loading/unloading a DLL containing static wxClassInfo objects is
165 if ( this == sm_first
)
171 wxClassInfo
*info
= sm_first
;
174 if ( info
->m_next
== this )
176 info
->m_next
= m_next
;
186 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
190 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
194 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
196 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
204 void wxClassInfo::CleanUp()
208 delete sm_classTable
;
209 sm_classTable
= NULL
;
213 void wxClassInfo::Register()
215 if ( !sm_classTable
)
217 sm_classTable
= new wxHashTable(wxKEY_STRING
);
220 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
221 // link any object module twice mistakenly) will break this function
222 // because it will enter an infinite loop and eventually die with "out of
223 // memory" - as this is quite hard to detect if you're unaware of this,
224 // try to do some checks here
225 wxASSERT_MSG( sm_classTable
->Get(m_className
) == NULL
,
226 _T("class already in RTTI table - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
228 sm_classTable
->Put(m_className
, (wxObject
*)this);
231 void wxClassInfo::Unregister()
235 sm_classTable
->Delete(m_className
);
236 if ( sm_classTable
->GetCount() == 0 )
238 delete sm_classTable
;
239 sm_classTable
= NULL
;
244 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
246 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
247 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
250 if ( wxClassInfo::sm_classTable
)
252 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
253 return info
? info
->CreateObject() : NULL
;
255 else // no sm_classTable yet
257 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
259 info
= info
->m_next
)
261 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
262 return info
->CreateObject();
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 // Initialize ref data from another object (needed for copy constructor and
275 // assignment operator)
276 void wxObject::InitFrom(const wxObject
& other
)
278 m_refData
= other
.m_refData
;
280 m_refData
->m_count
++;
283 void wxObject::Ref(const wxObject
& clone
)
285 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
286 DEBUG_PRINTF(wxObject::Ref
)
289 // nothing to be done
290 if (m_refData
== clone
.m_refData
)
293 // delete reference to old data
296 // reference new data
297 if ( clone
.m_refData
)
299 m_refData
= clone
.m_refData
;
300 ++(m_refData
->m_count
);
304 void wxObject::UnRef()
308 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
310 if ( !--m_refData
->m_count
)
316 void wxObject::AllocExclusive()
320 m_refData
= CreateRefData();
322 else if ( m_refData
->GetRefCount() > 1 )
324 // note that ref is not going to be destroyed in this case
325 const wxObjectRefData
* ref
= m_refData
;
328 // ... so we can still access it
329 m_refData
= CloneRefData(ref
);
331 //else: ref count is 1, we are exclusive owners of m_refData anyhow
333 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
334 _T("wxObject::AllocExclusive() failed.") );
337 wxObjectRefData
*wxObject::CreateRefData() const
339 // if you use AllocExclusive() you must override this method
340 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
346 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
348 // if you use AllocExclusive() you must override this method
349 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );