1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxObject class, plus run-time type information macros
4 // Author: Julian Smart
5 // Modified by: Ron Lee
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/memory.h"
22 class WXDLLIMPEXP_BASE wxObject
;
24 #ifndef wxUSE_EXTENDED_RTTI
25 #define wxUSE_EXTENDED_RTTI 0
28 #if wxUSE_EXTENDED_RTTI
32 // ----------------------------------------------------------------------------
33 // conditional compilation
34 // ----------------------------------------------------------------------------
36 class WXDLLIMPEXP_BASE wxClassInfo
;
37 class WXDLLIMPEXP_BASE wxHashTable
;
38 class WXDLLIMPEXP_BASE wxObjectRefData
;
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 typedef wxObject
*(*wxObjectConstructorFn
)(void);
46 class WXDLLIMPEXP_BASE wxClassInfo
49 wxClassInfo( const wxChar
*className
,
50 const wxClassInfo
*baseInfo1
,
51 const wxClassInfo
*baseInfo2
,
53 wxObjectConstructorFn ctor
)
54 : m_className(className
)
56 , m_objectConstructor(ctor
)
57 , m_baseInfo1(baseInfo1
)
58 , m_baseInfo2(baseInfo2
)
67 wxObject
*CreateObject() const
68 { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
69 bool IsDynamic() const { return (NULL
!= m_objectConstructor
); }
71 const wxChar
*GetClassName() const { return m_className
; }
72 const wxChar
*GetBaseClassName1() const
73 { return m_baseInfo1
? m_baseInfo1
->GetClassName() : NULL
; }
74 const wxChar
*GetBaseClassName2() const
75 { return m_baseInfo2
? m_baseInfo2
->GetClassName() : NULL
; }
76 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
77 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
78 int GetSize() const { return m_objectSize
; }
80 wxObjectConstructorFn
GetConstructor() const
81 { return m_objectConstructor
; }
82 static const wxClassInfo
*GetFirst() { return sm_first
; }
83 const wxClassInfo
*GetNext() const { return m_next
; }
84 static wxClassInfo
*FindClass(const wxChar
*className
);
86 // Climb upwards through inheritance hierarchy.
87 // Dual inheritance is catered for.
89 bool IsKindOf(const wxClassInfo
*info
) const
93 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
94 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
97 #if WXWIN_COMPATIBILITY_2_4
98 // Initializes parent pointers and hash table for fast searching.
99 wxDEPRECATED( static void InitializeClasses() );
100 // Cleans up hash table used for fast searching.
101 wxDEPRECATED( static void CleanUpClasses() );
105 const wxChar
*m_className
;
107 wxObjectConstructorFn m_objectConstructor
;
109 // Pointers to base wxClassInfos: set in InitializeClasses
111 const wxClassInfo
*m_baseInfo1
;
112 const wxClassInfo
*m_baseInfo2
;
114 // class info object live in a linked list:
115 // pointers to its head and the next element in it
117 static wxClassInfo
*sm_first
;
120 // FIXME: this should be private (currently used directly by way too
122 static wxHashTable
*sm_classTable
;
125 // InitializeClasses() helper
126 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
128 DECLARE_NO_COPY_CLASS(wxClassInfo
)
131 // registers the class
136 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
138 #if WXWIN_COMPATIBILITY_2_4
139 inline void wxClassInfo::InitializeClasses() {}
140 inline void wxClassInfo::CleanUpClasses() {}
143 // ----------------------------------------------------------------------------
144 // Dynamic class macros
145 // ----------------------------------------------------------------------------
147 #define DECLARE_ABSTRACT_CLASS(name) \
149 static wxClassInfo ms_classInfo; \
150 virtual wxClassInfo *GetClassInfo() const;
152 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
153 DECLARE_NO_ASSIGN_CLASS(name) \
154 DECLARE_DYNAMIC_CLASS(name)
156 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
157 DECLARE_NO_COPY_CLASS(name) \
158 DECLARE_DYNAMIC_CLASS(name)
160 #define DECLARE_DYNAMIC_CLASS(name) \
161 DECLARE_ABSTRACT_CLASS(name) \
162 static wxObject* wxCreateObject();
164 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
167 // common part of the macros below
168 #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
169 wxClassInfo name::ms_classInfo(wxT(#name), \
170 &basename::ms_classInfo, \
172 (int) sizeof(name), \
173 (wxObjectConstructorFn) func); \
175 wxClassInfo *name::GetClassInfo() const \
176 { return &name::ms_classInfo; }
178 #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
179 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
181 #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
182 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func)
184 // -----------------------------------
185 // for concrete classes
186 // -----------------------------------
188 // Single inheritance with one base class
189 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
190 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
191 wxObject* name::wxCreateObject() \
194 // Multiple inheritance with two base classes
195 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
196 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
197 name::wxCreateObject) \
198 wxObject* name::wxCreateObject() \
201 // -----------------------------------
202 // for abstract classes
203 // -----------------------------------
205 // Single inheritance with one base class
207 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
208 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
210 // Multiple inheritance with two base classes
212 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
213 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
215 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
216 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
218 #endif // !wxUSE_EXTENDED_RTTI
221 // -----------------------------------
222 // for pluggable classes
223 // -----------------------------------
225 // NOTE: this should probably be the very first statement
226 // in the class declaration so wxPluginSentinel is
227 // the first member initialised and the last destroyed.
229 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
231 #if wxUSE_NESTED_CLASSES
233 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
234 class exportdecl name##PluginSentinel { \
236 static const wxString sm_className; \
238 name##PluginSentinel(); \
239 ~name##PluginSentinel(); \
241 name##PluginSentinel m_pluginsentinel;
243 #define _IMPLEMENT_DL_SENTINEL(name) \
244 const wxString name::name##PluginSentinel::sm_className(#name); \
245 name::name##PluginSentinel::name##PluginSentinel() { \
246 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
247 if( e != 0 ) { e->RefObj(); } \
249 name::name##PluginSentinel::~name##PluginSentinel() { \
250 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
251 if( e != 0 ) { e->UnrefObj(); } \
255 #define _DECLARE_DL_SENTINEL(name)
256 #define _IMPLEMENT_DL_SENTINEL(name)
258 #endif // wxUSE_NESTED_CLASSES
260 #define DECLARE_PLUGGABLE_CLASS(name) \
261 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
262 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
263 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
265 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
266 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
267 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
268 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
270 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
271 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
272 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
273 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
274 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
275 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
276 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
277 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
279 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
280 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
281 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
282 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
283 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
284 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
285 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
286 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
288 #define CLASSINFO(name) (&name::ms_classInfo)
290 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
292 // Just seems a bit nicer-looking (pretend it's not a macro)
293 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
295 // this cast does some more checks at compile time as it uses static_cast
298 // note that it still has different semantics from dynamic_cast<> and so can't
299 // be replaced by it as long as there are any compilers not supporting it
300 #define wxDynamicCast(obj, className) \
301 ((className *) wxCheckDynamicCast( \
302 wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \
303 wx_const_cast(className *, wx_static_cast(const className *, obj)))), \
304 &className::ms_classInfo))
306 // The 'this' pointer is always true, so use this version
307 // to cast the this pointer and avoid compiler warnings.
308 #define wxDynamicCastThis(className) \
309 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
312 inline void* wxCheckCast(void *ptr
)
314 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
317 #define wxStaticCast(obj, className) \
318 ((className *)wxCheckCast(wxDynamicCast(obj, className)))
320 #else // !__WXDEBUG__
321 #define wxStaticCast(obj, className) \
322 wx_const_cast(className *, wx_static_cast(const className *, obj))
324 #endif // __WXDEBUG__
326 // ----------------------------------------------------------------------------
327 // set up memory debugging macros
328 // ----------------------------------------------------------------------------
331 Which new/delete operator variants do we want?
333 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
334 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
335 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
336 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
337 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
338 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
339 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
342 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
344 // All compilers get this one
345 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
347 // Everyone except Visage gets the next one
348 #ifndef __VISAGECPP__
349 #define _WX_WANT_DELETE_VOID
352 // Only visage gets this one under the correct circumstances
353 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
354 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
357 // Only VC++ 6 and CodeWarrior get overloaded delete that matches new
358 #if (defined(__VISUALC__) && (__VISUALC__ >= 1200)) || \
359 (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
360 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
363 // Now see who (if anyone) gets the array memory operators
364 #if wxUSE_ARRAY_MEMORY_OPERATORS
366 // Everyone except Visual C++ (cause problems for VC++ - crashes)
367 #if !defined(__VISUALC__)
368 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
371 // Everyone except Visual C++ (cause problems for VC++ - crashes)
372 #if !defined(__VISUALC__)
373 #define _WX_WANT_ARRAY_DELETE_VOID
376 // Only CodeWarrior 6 or higher
377 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
378 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
381 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
383 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
385 // ----------------------------------------------------------------------------
386 // wxObjectRefData: ref counted data meant to be stored in wxObject
387 // ----------------------------------------------------------------------------
389 class WXDLLIMPEXP_BASE wxObjectRefData
391 friend class WXDLLIMPEXP_BASE wxObject
;
394 wxObjectRefData() : m_count(1) { }
396 int GetRefCount() const { return m_count
; }
398 void IncRef() { m_count
++; }
402 // this object should never be destroyed directly but only as a
403 // result of a DecRef() call:
404 virtual ~wxObjectRefData() { }
411 // ----------------------------------------------------------------------------
412 // wxObjectDataPtr: helper class to avoid memleaks because of missing calls
413 // to wxObjectRefData::DecRef
414 // ----------------------------------------------------------------------------
417 class wxObjectDataPtr
420 typedef T element_type
;
422 wxEXPLICIT
wxObjectDataPtr(T
*ptr
= NULL
) : m_ptr(ptr
) {}
425 wxObjectDataPtr(const wxObjectDataPtr
<T
> &tocopy
)
426 : m_ptr(tocopy
.m_ptr
)
438 T
*get() const { return m_ptr
; }
439 T
*operator->() const { return get(); }
448 wxObjectDataPtr
& operator=(const wxObjectDataPtr
&tocopy
)
452 m_ptr
= tocopy
.m_ptr
;
458 wxObjectDataPtr
& operator=(T
*ptr
)
472 // ----------------------------------------------------------------------------
473 // wxObject: the root class of wxWidgets object hierarchy
474 // ----------------------------------------------------------------------------
476 class WXDLLIMPEXP_BASE wxObject
478 DECLARE_ABSTRACT_CLASS(wxObject
)
481 wxObject() { m_refData
= NULL
; }
482 virtual ~wxObject() { UnRef(); }
484 wxObject(const wxObject
& other
)
486 m_refData
= other
.m_refData
;
488 m_refData
->m_count
++;
491 wxObject
& operator=(const wxObject
& other
)
493 if ( this != &other
)
500 bool IsKindOf(wxClassInfo
*info
) const;
503 // Turn on the correct set of new and delete operators
505 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
506 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
509 #ifdef _WX_WANT_DELETE_VOID
510 void operator delete ( void * buf
);
513 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
514 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
517 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
518 void operator delete ( void *buf
, const wxChar
*, int );
521 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
522 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
525 #ifdef _WX_WANT_ARRAY_DELETE_VOID
526 void operator delete[] ( void *buf
);
529 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
530 void operator delete[] (void* buf
, const wxChar
*, int );
533 // ref counted data handling methods
536 wxObjectRefData
*GetRefData() const { return m_refData
; }
537 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
539 // make a 'clone' of the object
540 void Ref(const wxObject
& clone
);
542 // destroy a reference
545 // Make sure this object has only one reference
546 void UnShare() { AllocExclusive(); }
548 // check if this object references the same data as the other one
549 bool IsSameAs(const wxObject
& o
) const { return m_refData
== o
.m_refData
; }
552 // ensure that our data is not shared with anybody else: if we have no
553 // data, it is created using CreateRefData() below, if we have shared data
554 // it is copied using CloneRefData(), otherwise nothing is done
555 void AllocExclusive();
557 // both methods must be implemented if AllocExclusive() is used, not pure
558 // virtual only because of the backwards compatibility reasons
560 // create a new m_refData
561 virtual wxObjectRefData
*CreateRefData() const;
563 // create a new m_refData initialized with the given one
564 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
566 wxObjectRefData
*m_refData
;
569 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
571 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
574 #if wxUSE_EXTENDED_RTTI
575 class WXDLLIMPEXP_BASE wxDynamicObject
: public wxObject
577 friend class WXDLLIMPEXP_BASE wxDynamicClassInfo
;
579 // instantiates this object with an instance of its superclass
580 wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
) ;
581 virtual ~wxDynamicObject();
583 void SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
);
584 wxxVariant
GetProperty (const wxChar
*propertyName
) const ;
586 // get the runtime identity of this object
587 wxClassInfo
*GetClassInfo() const
590 return (wxClassInfo
*) m_classInfo
;
592 wxDynamicClassInfo
*nonconst
= wx_const_cast(wxDynamicClassInfo
*, m_classInfo
);
593 return wx_static_cast(wxClassInfo
*, nonconst
);
597 wxObject
* GetSuperClassInstance() const
599 return m_superClassInstance
;
602 // removes an existing runtime-property
603 void RemoveProperty( const wxChar
*propertyName
) ;
605 // renames an existing runtime-property
606 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
) ;
608 wxObject
*m_superClassInstance
;
609 const wxDynamicClassInfo
*m_classInfo
;
610 struct wxDynamicObjectInternal
;
611 wxDynamicObjectInternal
*m_data
;
615 // ----------------------------------------------------------------------------
616 // more debugging macros
617 // ----------------------------------------------------------------------------
619 // Redefine new to be the debugging version. This doesn't work with all
620 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
621 // to use the debugging version.
624 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
626 #if wxUSE_DEBUG_NEW_ALWAYS
627 #if wxUSE_GLOBAL_MEMORY_OPERATORS
628 #define new WXDEBUG_NEW
629 #elif defined(__VISUALC__)
630 // Including this file redefines new and allows leak reports to
631 // contain line numbers
632 #include "wx/msw/msvcrt.h"
634 #endif // wxUSE_DEBUG_NEW_ALWAYS
635 #else // !__WXDEBUG__
636 #define WXDEBUG_NEW new
637 #endif // __WXDEBUG__/!__WXDEBUG__
639 #endif // _WX_OBJECTH__