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 /////////////////////////////////////////////////////////////////////////////
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
37 #include "wx/ioswrap.h"
39 #if defined(__VISAGECPP__)
40 #define DEBUG_PRINTF(NAME) { static int raz=0; \
41 printf( #NAME " %i\n",raz); fflush(stdout); raz++; }
43 #define DEBUG_PRINTF(NAME)
45 #endif // __WXDEBUG__ || wxUSE_DEBUG_CONTEXT
47 // we must disable optimizations for VC.NET because otherwise its too eager
48 // linker discards wxClassInfo objects in release build thus breaking many,
50 #if defined __VISUALC__ && __VISUALC__ >= 1300
51 #pragma optimize("", off)
54 #if wxUSE_EXTENDED_RTTI
55 const wxClassInfo
* wxObject::sm_classParentswxObject
[] = { NULL
} ;
56 wxObject
* wxVariantToObjectConverterwxObject ( wxxVariant
&data
)
57 { return data
.Get
<wxObject
*>() ; }
58 wxObject
* wxVariantOfPtrToObjectConverterwxObject ( wxxVariant
&data
)
59 { return &data
.Get
<wxObject
>() ; }
60 wxxVariant
wxObjectToVariantConverterwxObject ( wxObject
*data
)
61 { return wxxVariant( dynamic_cast<wxObject
*> (data
) ) ; }
62 wxClassInfo
wxObject::sm_classwxObject(sm_classParentswxObject
, wxT("") , wxT("wxObject"),
63 (int) sizeof(wxObject
), \
64 (wxObjectConstructorFn
) 0 ,
65 (wxPropertyInfo
*) NULL
,(wxHandlerInfo
*) NULL
,0 , 0 ,
66 0 , wxVariantOfPtrToObjectConverterwxObject
, wxVariantToObjectConverterwxObject
, wxObjectToVariantConverterwxObject
);
67 template<> void wxStringReadValue(const wxString
& , wxObject
* & ){assert(0) ;}
68 template<> void wxStringWriteValue(wxString
& , wxObject
* const & ){assert(0) ;}
69 template<> void wxStringReadValue(const wxString
& , wxObject
& ){assert(0) ;}
70 template<> void wxStringWriteValue(wxString
& , wxObject
const & ){assert(0) ;}
71 template<> const wxTypeInfo
* wxGetTypeInfo( wxObject
** )
72 { static wxClassTypeInfo
s_typeInfo(wxT_OBJECT_PTR
, &wxObject::sm_classwxObject
) ; return &s_typeInfo
; }
73 template<> const wxTypeInfo
* wxGetTypeInfo( wxObject
* )
74 { static wxClassTypeInfo
s_typeInfo(wxT_OBJECT
, &wxObject::sm_classwxObject
) ; return &s_typeInfo
; }
76 wxClassInfo
wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
77 (int) sizeof(wxObject
),
78 (wxObjectConstructorFn
) 0 );
81 // restore optimizations
82 #if defined __VISUALC__ && __VISUALC__ >= 1300
83 #pragma optimize("", on)
86 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
87 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
89 // These are here so we can avoid 'always true/false' warnings
90 // by referring to these instead of TRUE/FALSE
91 const bool wxTrue
= TRUE
;
92 const bool wxFalse
= FALSE
;
94 // Is this object a kind of (a subclass of) 'info'?
95 // E.g. is wxWindow a kind of wxObject?
96 // Go from this class to superclass, taking into account
97 // two possible base classes.
98 bool wxObject::IsKindOf(wxClassInfo
*info
) const
100 wxClassInfo
*thisInfo
= GetClassInfo();
101 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : FALSE
;
104 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
105 void wxObject::Dump(wxSTD ostream
& str
)
107 if (GetClassInfo() && GetClassInfo()->GetClassName())
108 str
<< GetClassInfo()->GetClassName();
110 str
<< _T("unknown object class");
115 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
120 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
121 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
123 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
);
127 #ifdef _WX_WANT_DELETE_VOID
128 void wxObject::operator delete ( void *buf
)
134 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
135 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
141 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
142 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
148 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
149 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
151 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
, TRUE
);
155 #ifdef _WX_WANT_ARRAY_DELETE_VOID
156 void wxObject::operator delete[] ( void *buf
)
158 wxDebugFree(buf
, TRUE
);
162 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
163 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
165 wxDebugFree(buf
, TRUE
);
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 wxClassInfo::~wxClassInfo()
176 // remove this object from the linked list of all class infos: if we don't
177 // do it, loading/unloading a DLL containing static wxClassInfo objects is
179 if ( this == sm_first
)
185 wxClassInfo
*info
= sm_first
;
188 if ( info
->m_next
== this )
190 info
->m_next
= m_next
;
197 #if wxUSE_EXTENDED_RTTI
198 Unregister( m_className
) ;
202 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
206 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
210 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
212 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
220 // a tiny InitializeClasses() helper
222 inline wxClassInfo
*wxClassInfo::GetBaseByName(const wxChar
*name
)
227 wxClassInfo
*classInfo
= (wxClassInfo
*)sm_classTable
->Get(name
);
230 // this must be fixed, other things will work wrongly later if you get this
233 wxFAIL_MSG( wxString::Format
235 _T("base class '%s' is unknown to wxWindows RTTI"),
239 #endif // __WXDEBUG__
244 // Set pointers to base class(es) to speed up IsKindOf
245 void wxClassInfo::InitializeClasses()
247 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
248 // link any object module twice mistakenly) will break this function
249 // because it will enter an infinite loop and eventually die with "out of
250 // memory" - as this is quite hard to detect if you're unaware of this,
251 // try to do some checks here
254 static const size_t nMaxClasses
= 10000; // more than we'll ever have
258 sm_classTable
= new wxHashTable(wxKEY_STRING
);
260 // Index all class infos by their class name
263 for(info
= sm_first
; info
; info
= info
->m_next
)
265 if (info
->m_className
)
267 wxASSERT_MSG( ++nClass
< nMaxClasses
,
268 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
269 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
273 #if wxUSE_EXTENDED_RTTI == 0
274 // Set base pointers for each wxClassInfo
276 for(info
= sm_first
; info
; info
= info
->m_next
)
278 info
->m_baseInfo1
= GetBaseByName(info
->GetBaseClassName1());
279 info
->m_baseInfo2
= GetBaseByName(info
->GetBaseClassName2());
284 void wxClassInfo::CleanUpClasses()
286 delete wxClassInfo::sm_classTable
;
287 wxClassInfo::sm_classTable
= NULL
;
290 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
292 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
293 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
296 if ( wxClassInfo::sm_classTable
)
298 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
299 return info
? info
->CreateObject() : NULL
;
301 else // no sm_classTable yet
303 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
305 info
= info
->m_next
)
307 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
308 return info
->CreateObject();
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 // Initialize ref data from another object (needed for copy constructor and
321 // assignment operator)
322 void wxObject::InitFrom(const wxObject
& other
)
324 m_refData
= other
.m_refData
;
326 m_refData
->m_count
++;
329 void wxObject::Ref(const wxObject
& clone
)
331 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
332 DEBUG_PRINTF(wxObject::Ref
)
335 // nothing to be done
336 if (m_refData
== clone
.m_refData
)
339 // delete reference to old data
342 // reference new data
343 if ( clone
.m_refData
)
345 m_refData
= clone
.m_refData
;
346 ++(m_refData
->m_count
);
350 void wxObject::UnRef()
354 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
356 if ( !--m_refData
->m_count
)
362 void wxObject::AllocExclusive()
366 m_refData
= CreateRefData();
368 else if ( m_refData
->GetRefCount() > 1 )
370 // note that ref is not going to be destroyed in this case
371 const wxObjectRefData
* ref
= m_refData
;
374 // ... so we can still access it
375 m_refData
= CloneRefData(ref
);
377 //else: ref count is 1, we are exclusive owners of m_refData anyhow
379 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
380 _T("wxObject::AllocExclusive() failed.") );
383 wxObjectRefData
*wxObject::CreateRefData() const
385 // if you use AllocExclusive() you must override this method
386 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
392 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
394 // if you use AllocExclusive() you must override this method
395 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
400 // ----------------------------------------------------------------------------
402 // ----------------------------------------------------------------------------
404 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
407 void __initialize_Cplusplus(void);
408 void wxWindowsDylibInit(void);
411 // Dynamic shared library (dylib) initialization routine
412 // required to initialize static C++ objects bacause of lazy dynamic linking
413 // http://developer.apple.com/techpubs/macosx/Essentials/
414 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
416 void wxWindowsDylibInit()
418 // The function __initialize_Cplusplus() must be called from the shared
419 // library initialization routine to cause the static C++ objects in
420 // the library to be initialized (reference number 2441683).
422 // This only seems to be necessary if the library initialization routine
423 // needs to use the static C++ objects
424 __initialize_Cplusplus();