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 ( const wxxVariant
&data
)
57 { return data
.Get
<wxObject
*>() ; }
58 wxxVariant
wxObjectToVariantConverterwxObject ( wxObject
*data
)
59 { return wxxVariant( dynamic_cast<wxObject
*> (data
) ) ; }
60 wxClassInfo
wxObject::sm_classwxObject(sm_classParentswxObject
, wxT("") , wxT("wxObject"),
61 (int) sizeof(wxObject
), \
62 (wxObjectConstructorFn
) 0 ,
63 (wxPropertyInfo
*) NULL
,(wxHandlerInfo
*) NULL
,0 , 0 ,
64 0 , wxVariantToObjectConverterwxObject
, wxObjectToVariantConverterwxObject
);
65 template<> void wxStringReadValue(const wxString
& , wxObject
* & ){assert(0) ;}
66 template<> void wxStringWriteValue(wxString
& , wxObject
* const & ){assert(0) ;}
67 template<> const wxTypeInfo
* wxGetTypeInfo( wxObject
** )
68 { static wxClassTypeInfo
s_typeInfo(&wxObject::sm_classwxObject
) ; return &s_typeInfo
; }
70 wxClassInfo
wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
71 (int) sizeof(wxObject
),
72 (wxObjectConstructorFn
) 0 );
75 // restore optimizations
76 #if defined __VISUALC__ && __VISUALC__ >= 1300
77 #pragma optimize("", on)
80 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
81 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
83 // These are here so we can avoid 'always true/false' warnings
84 // by referring to these instead of TRUE/FALSE
85 const bool wxTrue
= TRUE
;
86 const bool wxFalse
= FALSE
;
88 // Is this object a kind of (a subclass of) 'info'?
89 // E.g. is wxWindow a kind of wxObject?
90 // Go from this class to superclass, taking into account
91 // two possible base classes.
92 bool wxObject::IsKindOf(wxClassInfo
*info
) const
94 wxClassInfo
*thisInfo
= GetClassInfo();
95 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : FALSE
;
98 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
99 void wxObject::Dump(wxSTD ostream
& str
)
101 if (GetClassInfo() && GetClassInfo()->GetClassName())
102 str
<< GetClassInfo()->GetClassName();
104 str
<< _T("unknown object class");
109 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
114 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
115 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
117 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
);
121 #ifdef _WX_WANT_DELETE_VOID
122 void wxObject::operator delete ( void *buf
)
128 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
129 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
135 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
136 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
142 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
143 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
145 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
, TRUE
);
149 #ifdef _WX_WANT_ARRAY_DELETE_VOID
150 void wxObject::operator delete[] ( void *buf
)
152 wxDebugFree(buf
, TRUE
);
156 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
157 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
159 wxDebugFree(buf
, TRUE
);
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 wxClassInfo::~wxClassInfo()
170 // remove this object from the linked list of all class infos: if we don't
171 // do it, loading/unloading a DLL containing static wxClassInfo objects is
173 if ( this == sm_first
)
179 wxClassInfo
*info
= sm_first
;
182 if ( info
->m_next
== this )
184 info
->m_next
= m_next
;
191 #if wxUSE_EXTENDED_RTTI
192 Unregister( m_className
) ;
196 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
200 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
204 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
206 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
214 // a tiny InitializeClasses() helper
216 inline wxClassInfo
*wxClassInfo::GetBaseByName(const wxChar
*name
)
221 wxClassInfo
*classInfo
= (wxClassInfo
*)sm_classTable
->Get(name
);
224 // this must be fixed, other things will work wrongly later if you get this
227 wxFAIL_MSG( wxString::Format
229 _T("base class '%s' is unknown to wxWindows RTTI"),
233 #endif // __WXDEBUG__
238 // Set pointers to base class(es) to speed up IsKindOf
239 void wxClassInfo::InitializeClasses()
241 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
242 // link any object module twice mistakenly) will break this function
243 // because it will enter an infinite loop and eventually die with "out of
244 // memory" - as this is quite hard to detect if you're unaware of this,
245 // try to do some checks here
248 static const size_t nMaxClasses
= 10000; // more than we'll ever have
252 sm_classTable
= new wxHashTable(wxKEY_STRING
);
254 // Index all class infos by their class name
257 for(info
= sm_first
; info
; info
= info
->m_next
)
259 if (info
->m_className
)
261 wxASSERT_MSG( ++nClass
< nMaxClasses
,
262 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
263 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
267 #if wxUSE_EXTENDED_RTTI == 0
268 // Set base pointers for each wxClassInfo
270 for(info
= sm_first
; info
; info
= info
->m_next
)
272 info
->m_baseInfo1
= GetBaseByName(info
->GetBaseClassName1());
273 info
->m_baseInfo2
= GetBaseByName(info
->GetBaseClassName2());
278 void wxClassInfo::CleanUpClasses()
280 delete wxClassInfo::sm_classTable
;
281 wxClassInfo::sm_classTable
= NULL
;
284 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
286 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
287 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
290 if ( wxClassInfo::sm_classTable
)
292 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
293 return info
? info
->CreateObject() : NULL
;
295 else // no sm_classTable yet
297 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
299 info
= info
->m_next
)
301 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
302 return info
->CreateObject();
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 // Initialize ref data from another object (needed for copy constructor and
315 // assignment operator)
316 void wxObject::InitFrom(const wxObject
& other
)
318 m_refData
= other
.m_refData
;
320 m_refData
->m_count
++;
323 void wxObject::Ref(const wxObject
& clone
)
325 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
326 DEBUG_PRINTF(wxObject::Ref
)
329 // nothing to be done
330 if (m_refData
== clone
.m_refData
)
333 // delete reference to old data
336 // reference new data
337 if ( clone
.m_refData
)
339 m_refData
= clone
.m_refData
;
340 ++(m_refData
->m_count
);
344 void wxObject::UnRef()
348 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
350 if ( !--m_refData
->m_count
)
356 void wxObject::AllocExclusive()
360 m_refData
= CreateRefData();
362 else if ( m_refData
->GetRefCount() > 1 )
364 // note that ref is not going to be destroyed in this case
365 const wxObjectRefData
* ref
= m_refData
;
368 // ... so we can still access it
369 m_refData
= CloneRefData(ref
);
371 //else: ref count is 1, we are exclusive owners of m_refData anyhow
373 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
374 _T("wxObject::AllocExclusive() failed.") );
377 wxObjectRefData
*wxObject::CreateRefData() const
379 // if you use AllocExclusive() you must override this method
380 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
386 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
388 // if you use AllocExclusive() you must override this method
389 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
398 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
401 void __initialize_Cplusplus(void);
402 void wxWindowsDylibInit(void);
405 // Dynamic shared library (dylib) initialization routine
406 // required to initialize static C++ objects bacause of lazy dynamic linking
407 // http://developer.apple.com/techpubs/macosx/Essentials/
408 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
410 void wxWindowsDylibInit()
412 // The function __initialize_Cplusplus() must be called from the shared
413 // library initialization routine to cause the static C++ objects in
414 // the library to be initialized (reference number 2441683).
416 // This only seems to be necessary if the library initialization routine
417 // needs to use the static C++ objects
418 __initialize_Cplusplus();