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<> const wxTypeInfo
* wxGetTypeInfo( wxObject
** )
70 { static wxClassTypeInfo
s_typeInfo(wxT_OBJECT_PTR
, &wxObject::sm_classwxObject
) ; return &s_typeInfo
; }
72 wxClassInfo
wxObject::sm_classwxObject( wxT("wxObject"), 0, 0,
73 (int) sizeof(wxObject
),
74 (wxObjectConstructorFn
) 0 );
77 // restore optimizations
78 #if defined __VISUALC__ && __VISUALC__ >= 1300
79 #pragma optimize("", on)
82 wxClassInfo
* wxClassInfo::sm_first
= NULL
;
83 wxHashTable
* wxClassInfo::sm_classTable
= NULL
;
85 // These are here so we can avoid 'always true/false' warnings
86 // by referring to these instead of TRUE/FALSE
87 const bool wxTrue
= TRUE
;
88 const bool wxFalse
= FALSE
;
90 // Is this object a kind of (a subclass of) 'info'?
91 // E.g. is wxWindow a kind of wxObject?
92 // Go from this class to superclass, taking into account
93 // two possible base classes.
94 bool wxObject::IsKindOf(wxClassInfo
*info
) const
96 wxClassInfo
*thisInfo
= GetClassInfo();
97 return (thisInfo
) ? thisInfo
->IsKindOf(info
) : FALSE
;
100 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
101 void wxObject::Dump(wxSTD ostream
& str
)
103 if (GetClassInfo() && GetClassInfo()->GetClassName())
104 str
<< GetClassInfo()->GetClassName();
106 str
<< _T("unknown object class");
111 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING && defined( new )
116 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
117 void *wxObject::operator new ( size_t size
, const wxChar
*fileName
, int lineNum
)
119 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
);
123 #ifdef _WX_WANT_DELETE_VOID
124 void wxObject::operator delete ( void *buf
)
130 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
131 void wxObject::operator delete ( void *buf
, const char *_fname
, size_t _line
)
137 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
138 void wxObject::operator delete ( void *buf
, const wxChar
*WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
144 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
145 void *wxObject::operator new[] ( size_t size
, const wxChar
* fileName
, int lineNum
)
147 return wxDebugAlloc(size
, (wxChar
*) fileName
, lineNum
, TRUE
, TRUE
);
151 #ifdef _WX_WANT_ARRAY_DELETE_VOID
152 void wxObject::operator delete[] ( void *buf
)
154 wxDebugFree(buf
, TRUE
);
158 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
159 void wxObject::operator delete[] (void * buf
, const wxChar
* WXUNUSED(fileName
), int WXUNUSED(lineNum
) )
161 wxDebugFree(buf
, TRUE
);
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 wxClassInfo::~wxClassInfo()
172 // remove this object from the linked list of all class infos: if we don't
173 // do it, loading/unloading a DLL containing static wxClassInfo objects is
175 if ( this == sm_first
)
181 wxClassInfo
*info
= sm_first
;
184 if ( info
->m_next
== this )
186 info
->m_next
= m_next
;
193 #if wxUSE_EXTENDED_RTTI
194 Unregister( m_className
) ;
198 wxClassInfo
*wxClassInfo::FindClass(const wxChar
*className
)
202 return (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(className
);
206 for ( wxClassInfo
*info
= sm_first
; info
; info
= info
->m_next
)
208 if ( wxStrcmp(info
->GetClassName(), className
) == 0 )
216 // a tiny InitializeClasses() helper
218 inline wxClassInfo
*wxClassInfo::GetBaseByName(const wxChar
*name
)
223 wxClassInfo
*classInfo
= (wxClassInfo
*)sm_classTable
->Get(name
);
226 // this must be fixed, other things will work wrongly later if you get this
229 wxFAIL_MSG( wxString::Format
231 _T("base class '%s' is unknown to wxWindows RTTI"),
235 #endif // __WXDEBUG__
240 // Set pointers to base class(es) to speed up IsKindOf
241 void wxClassInfo::InitializeClasses()
243 // using IMPLEMENT_DYNAMIC_CLASS() macro twice (which may happen if you
244 // link any object module twice mistakenly) will break this function
245 // because it will enter an infinite loop and eventually die with "out of
246 // memory" - as this is quite hard to detect if you're unaware of this,
247 // try to do some checks here
250 static const size_t nMaxClasses
= 10000; // more than we'll ever have
254 sm_classTable
= new wxHashTable(wxKEY_STRING
);
256 // Index all class infos by their class name
259 for(info
= sm_first
; info
; info
= info
->m_next
)
261 if (info
->m_className
)
263 wxASSERT_MSG( ++nClass
< nMaxClasses
,
264 _T("an infinite loop detected - have you used IMPLEMENT_DYNAMIC_CLASS() twice (may be by linking some object module(s) twice)?") );
265 sm_classTable
->Put(info
->m_className
, (wxObject
*)info
);
269 #if wxUSE_EXTENDED_RTTI == 0
270 // Set base pointers for each wxClassInfo
272 for(info
= sm_first
; info
; info
= info
->m_next
)
274 info
->m_baseInfo1
= GetBaseByName(info
->GetBaseClassName1());
275 info
->m_baseInfo2
= GetBaseByName(info
->GetBaseClassName2());
280 void wxClassInfo::CleanUpClasses()
282 delete wxClassInfo::sm_classTable
;
283 wxClassInfo::sm_classTable
= NULL
;
286 wxObject
*wxCreateDynamicObject(const wxChar
*name
)
288 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
289 DEBUG_PRINTF(wxObject
*wxCreateDynamicObject
)
292 if ( wxClassInfo::sm_classTable
)
294 wxClassInfo
*info
= (wxClassInfo
*)wxClassInfo::sm_classTable
->Get(name
);
295 return info
? info
->CreateObject() : NULL
;
297 else // no sm_classTable yet
299 for ( wxClassInfo
*info
= wxClassInfo::sm_first
;
301 info
= info
->m_next
)
303 if (info
->m_className
&& wxStrcmp(info
->m_className
, name
) == 0)
304 return info
->CreateObject();
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 // Initialize ref data from another object (needed for copy constructor and
317 // assignment operator)
318 void wxObject::InitFrom(const wxObject
& other
)
320 m_refData
= other
.m_refData
;
322 m_refData
->m_count
++;
325 void wxObject::Ref(const wxObject
& clone
)
327 #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT
328 DEBUG_PRINTF(wxObject::Ref
)
331 // nothing to be done
332 if (m_refData
== clone
.m_refData
)
335 // delete reference to old data
338 // reference new data
339 if ( clone
.m_refData
)
341 m_refData
= clone
.m_refData
;
342 ++(m_refData
->m_count
);
346 void wxObject::UnRef()
350 wxASSERT_MSG( m_refData
->m_count
> 0, _T("invalid ref data count") );
352 if ( !--m_refData
->m_count
)
358 void wxObject::AllocExclusive()
362 m_refData
= CreateRefData();
364 else if ( m_refData
->GetRefCount() > 1 )
366 // note that ref is not going to be destroyed in this case
367 const wxObjectRefData
* ref
= m_refData
;
370 // ... so we can still access it
371 m_refData
= CloneRefData(ref
);
373 //else: ref count is 1, we are exclusive owners of m_refData anyhow
375 wxASSERT_MSG( m_refData
&& m_refData
->GetRefCount() == 1,
376 _T("wxObject::AllocExclusive() failed.") );
379 wxObjectRefData
*wxObject::CreateRefData() const
381 // if you use AllocExclusive() you must override this method
382 wxFAIL_MSG( _T("CreateRefData() must be overridden if called!") );
388 wxObject::CloneRefData(const wxObjectRefData
* WXUNUSED(data
)) const
390 // if you use AllocExclusive() you must override this method
391 wxFAIL_MSG( _T("CloneRefData() must be overridden if called!") );
396 // ----------------------------------------------------------------------------
398 // ----------------------------------------------------------------------------
400 #if defined(__DARWIN__) && defined(WXMAKINGDLL)
403 void __initialize_Cplusplus(void);
404 void wxWindowsDylibInit(void);
407 // Dynamic shared library (dylib) initialization routine
408 // required to initialize static C++ objects bacause of lazy dynamic linking
409 // http://developer.apple.com/techpubs/macosx/Essentials/
410 // SystemOverview/Frameworks/Dynamic_Shared_Libraries.html
412 void wxWindowsDylibInit()
414 // The function __initialize_Cplusplus() must be called from the shared
415 // library initialization routine to cause the static C++ objects in
416 // the library to be initialized (reference number 2441683).
418 // This only seems to be necessary if the library initialization routine
419 // needs to use the static C++ objects
420 __initialize_Cplusplus();