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_FWD_BASE wxObject
;
23 class WXDLLIMPEXP_FWD_BASE wxString
;
25 #ifndef wxUSE_EXTENDED_RTTI
26 #define wxUSE_EXTENDED_RTTI 0
29 #define DECLARE_CLASS_INFO_ITERATORS() \
30 class WXDLLIMPEXP_BASE const_iterator \
32 typedef wxHashTable_Node Node; \
34 typedef const wxClassInfo* value_type; \
35 typedef const value_type& const_reference; \
36 typedef const_iterator itor; \
37 typedef value_type* ptr_type; \
40 wxHashTable* m_table; \
42 typedef const_reference reference_type; \
43 typedef ptr_type pointer_type; \
45 const_iterator(Node* node, wxHashTable* table) \
46 : m_node(node), m_table(table) { } \
47 const_iterator() : m_node(NULL), m_table(NULL) { } \
48 value_type operator*() const; \
50 const itor operator++(int); \
51 bool operator!=(const itor& it) const \
52 { return it.m_node != m_node; } \
53 bool operator==(const itor& it) const \
54 { return it.m_node == m_node; } \
57 static const_iterator begin_classinfo(); \
58 static const_iterator end_classinfo();
60 #if wxUSE_EXTENDED_RTTI
64 // ----------------------------------------------------------------------------
65 // conditional compilation
66 // ----------------------------------------------------------------------------
68 class WXDLLIMPEXP_FWD_BASE wxClassInfo
;
69 class WXDLLIMPEXP_FWD_BASE wxHashTable
;
70 class WXDLLIMPEXP_FWD_BASE wxObject
;
71 class WXDLLIMPEXP_FWD_BASE wxPluginLibrary
;
72 class WXDLLIMPEXP_FWD_BASE wxObjectRefData
;
73 class WXDLLIMPEXP_FWD_BASE wxHashTable_Node
;
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 typedef wxObject
*(*wxObjectConstructorFn
)(void);
81 class WXDLLIMPEXP_BASE wxClassInfo
83 friend class WXDLLIMPEXP_FWD_BASE wxObject
;
84 friend WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxString
& name
);
86 wxClassInfo( const wxChar
*className
,
87 const wxClassInfo
*baseInfo1
,
88 const wxClassInfo
*baseInfo2
,
90 wxObjectConstructorFn ctor
)
91 : m_className(className
)
93 , m_objectConstructor(ctor
)
94 , m_baseInfo1(baseInfo1
)
95 , m_baseInfo2(baseInfo2
)
104 wxObject
*CreateObject() const
105 { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
106 bool IsDynamic() const { return (NULL
!= m_objectConstructor
); }
108 const wxChar
*GetClassName() const { return m_className
; }
109 const wxChar
*GetBaseClassName1() const
110 { return m_baseInfo1
? m_baseInfo1
->GetClassName() : NULL
; }
111 const wxChar
*GetBaseClassName2() const
112 { return m_baseInfo2
? m_baseInfo2
->GetClassName() : NULL
; }
113 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
114 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
115 int GetSize() const { return m_objectSize
; }
117 wxObjectConstructorFn
GetConstructor() const
118 { return m_objectConstructor
; }
119 static const wxClassInfo
*GetFirst() { return sm_first
; }
120 const wxClassInfo
*GetNext() const { return m_next
; }
121 static wxClassInfo
*FindClass(const wxString
& className
);
123 // Climb upwards through inheritance hierarchy.
124 // Dual inheritance is catered for.
126 bool IsKindOf(const wxClassInfo
*info
) const
130 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
131 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
134 DECLARE_CLASS_INFO_ITERATORS()
136 const wxChar
*m_className
;
138 wxObjectConstructorFn m_objectConstructor
;
140 // Pointers to base wxClassInfos
142 const wxClassInfo
*m_baseInfo1
;
143 const wxClassInfo
*m_baseInfo2
;
145 // class info object live in a linked list:
146 // pointers to its head and the next element in it
148 static wxClassInfo
*sm_first
;
151 static wxHashTable
*sm_classTable
;
154 // registers the class
158 DECLARE_NO_COPY_CLASS(wxClassInfo
)
161 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxString
& name
);
163 // ----------------------------------------------------------------------------
164 // Dynamic class macros
165 // ----------------------------------------------------------------------------
167 #define DECLARE_ABSTRACT_CLASS(name) \
169 static wxClassInfo ms_classInfo; \
170 virtual wxClassInfo *GetClassInfo() const;
172 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
173 DECLARE_NO_ASSIGN_CLASS(name) \
174 DECLARE_DYNAMIC_CLASS(name)
176 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
177 DECLARE_NO_COPY_CLASS(name) \
178 DECLARE_DYNAMIC_CLASS(name)
180 #define DECLARE_DYNAMIC_CLASS(name) \
181 DECLARE_ABSTRACT_CLASS(name) \
182 static wxObject* wxCreateObject();
184 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
187 // common part of the macros below
188 #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
189 wxClassInfo name::ms_classInfo(wxT(#name), \
190 &basename::ms_classInfo, \
192 (int) sizeof(name), \
193 (wxObjectConstructorFn) func); \
195 wxClassInfo *name::GetClassInfo() const \
196 { return &name::ms_classInfo; }
198 #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
199 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
201 #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
202 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func)
204 // -----------------------------------
205 // for concrete classes
206 // -----------------------------------
208 // Single inheritance with one base class
209 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
210 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
211 wxObject* name::wxCreateObject() \
214 // Multiple inheritance with two base classes
215 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
216 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
217 name::wxCreateObject) \
218 wxObject* name::wxCreateObject() \
221 // -----------------------------------
222 // for abstract classes
223 // -----------------------------------
225 // Single inheritance with one base class
227 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
228 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
230 // Multiple inheritance with two base classes
232 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
233 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
235 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
236 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
238 #endif // !wxUSE_EXTENDED_RTTI
241 // -----------------------------------
242 // for pluggable classes
243 // -----------------------------------
245 // NOTE: this should probably be the very first statement
246 // in the class declaration so wxPluginSentinel is
247 // the first member initialised and the last destroyed.
249 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
251 #if wxUSE_NESTED_CLASSES
253 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
254 class exportdecl name##PluginSentinel { \
256 static const wxString sm_className; \
258 name##PluginSentinel(); \
259 ~name##PluginSentinel(); \
261 name##PluginSentinel m_pluginsentinel;
263 #define _IMPLEMENT_DL_SENTINEL(name) \
264 const wxString name::name##PluginSentinel::sm_className(#name); \
265 name::name##PluginSentinel::name##PluginSentinel() { \
266 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
267 if( e != 0 ) { e->RefObj(); } \
269 name::name##PluginSentinel::~name##PluginSentinel() { \
270 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
271 if( e != 0 ) { e->UnrefObj(); } \
275 #define _DECLARE_DL_SENTINEL(name)
276 #define _IMPLEMENT_DL_SENTINEL(name)
278 #endif // wxUSE_NESTED_CLASSES
280 #define DECLARE_PLUGGABLE_CLASS(name) \
281 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
282 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
283 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
285 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
286 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
287 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
288 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
290 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
291 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
292 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
293 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
294 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
295 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
296 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
297 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
299 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
300 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
301 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
302 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
303 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
304 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
305 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
306 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
308 #define CLASSINFO(name) (&name::ms_classInfo)
310 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
312 // Just seems a bit nicer-looking (pretend it's not a macro)
313 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
315 // this cast does some more checks at compile time as it uses static_cast
318 // note that it still has different semantics from dynamic_cast<> and so can't
319 // be replaced by it as long as there are any compilers not supporting it
320 #define wxDynamicCast(obj, className) \
321 ((className *) wxCheckDynamicCast( \
322 wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \
323 wx_const_cast(className *, wx_static_cast(const className *, obj)))), \
324 &className::ms_classInfo))
326 // The 'this' pointer is always true, so use this version
327 // to cast the this pointer and avoid compiler warnings.
328 #define wxDynamicCastThis(className) \
329 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
332 inline void* wxCheckCast(void *ptr
)
334 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
337 #define wxStaticCast(obj, className) \
338 ((className *)wxCheckCast(wxDynamicCast(obj, className)))
340 #else // !__WXDEBUG__
341 #define wxStaticCast(obj, className) \
342 wx_const_cast(className *, wx_static_cast(const className *, obj))
344 #endif // __WXDEBUG__
346 // ----------------------------------------------------------------------------
347 // set up memory debugging macros
348 // ----------------------------------------------------------------------------
351 Which new/delete operator variants do we want?
353 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
354 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
355 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
356 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
357 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
358 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
359 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
362 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
364 // All compilers get this one
365 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
367 // Everyone except Visage gets the next one
368 #ifndef __VISAGECPP__
369 #define _WX_WANT_DELETE_VOID
372 // Only visage gets this one under the correct circumstances
373 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
374 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
377 // Only VC++ 6 and CodeWarrior get overloaded delete that matches new
378 #if (defined(__VISUALC__) && (__VISUALC__ >= 1200)) || \
379 (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
380 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
383 // Now see who (if anyone) gets the array memory operators
384 #if wxUSE_ARRAY_MEMORY_OPERATORS
386 // Everyone except Visual C++ (cause problems for VC++ - crashes)
387 #if !defined(__VISUALC__)
388 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
391 // Everyone except Visual C++ (cause problems for VC++ - crashes)
392 #if !defined(__VISUALC__)
393 #define _WX_WANT_ARRAY_DELETE_VOID
396 // Only CodeWarrior 6 or higher
397 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
398 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
401 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
403 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
405 // ----------------------------------------------------------------------------
406 // wxObjectRefData: ref counted data meant to be stored in wxObject
407 // ----------------------------------------------------------------------------
409 class WXDLLIMPEXP_BASE wxObjectRefData
411 friend class WXDLLIMPEXP_FWD_BASE wxObject
;
414 wxObjectRefData() : m_count(1) { }
416 int GetRefCount() const { return m_count
; }
418 void IncRef() { m_count
++; }
422 // this object should never be destroyed directly but only as a
423 // result of a DecRef() call:
424 virtual ~wxObjectRefData() { }
431 // ----------------------------------------------------------------------------
432 // wxObjectDataPtr: helper class to avoid memleaks because of missing calls
433 // to wxObjectRefData::DecRef
434 // ----------------------------------------------------------------------------
437 class wxObjectDataPtr
440 typedef T element_type
;
442 wxEXPLICIT
wxObjectDataPtr(T
*ptr
= NULL
) : m_ptr(ptr
) {}
445 wxObjectDataPtr(const wxObjectDataPtr
<T
> &tocopy
)
446 : m_ptr(tocopy
.m_ptr
)
458 T
*get() const { return m_ptr
; }
460 T
*operator->() const
462 wxASSERT(m_ptr
!= NULL
);
473 wxObjectDataPtr
& operator=(const wxObjectDataPtr
&tocopy
)
477 m_ptr
= tocopy
.m_ptr
;
483 wxObjectDataPtr
& operator=(T
*ptr
)
495 // ----------------------------------------------------------------------------
496 // wxObject: the root class of wxWidgets object hierarchy
497 // ----------------------------------------------------------------------------
499 class WXDLLIMPEXP_BASE wxObject
501 DECLARE_ABSTRACT_CLASS(wxObject
)
504 wxObject() { m_refData
= NULL
; }
505 virtual ~wxObject() { UnRef(); }
507 wxObject(const wxObject
& other
)
509 m_refData
= other
.m_refData
;
511 m_refData
->m_count
++;
514 wxObject
& operator=(const wxObject
& other
)
516 if ( this != &other
)
523 bool IsKindOf(wxClassInfo
*info
) const;
526 // Turn on the correct set of new and delete operators
528 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
529 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
532 #ifdef _WX_WANT_DELETE_VOID
533 void operator delete ( void * buf
);
536 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
537 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
540 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
541 void operator delete ( void *buf
, const wxChar
*, int );
544 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
545 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
548 #ifdef _WX_WANT_ARRAY_DELETE_VOID
549 void operator delete[] ( void *buf
);
552 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
553 void operator delete[] (void* buf
, const wxChar
*, int );
556 // ref counted data handling methods
559 wxObjectRefData
*GetRefData() const { return m_refData
; }
560 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
562 // make a 'clone' of the object
563 void Ref(const wxObject
& clone
);
565 // destroy a reference
568 // Make sure this object has only one reference
569 void UnShare() { AllocExclusive(); }
571 // check if this object references the same data as the other one
572 bool IsSameAs(const wxObject
& o
) const { return m_refData
== o
.m_refData
; }
575 // ensure that our data is not shared with anybody else: if we have no
576 // data, it is created using CreateRefData() below, if we have shared data
577 // it is copied using CloneRefData(), otherwise nothing is done
578 void AllocExclusive();
580 // both methods must be implemented if AllocExclusive() is used, not pure
581 // virtual only because of the backwards compatibility reasons
583 // create a new m_refData
584 virtual wxObjectRefData
*CreateRefData() const;
586 // create a new m_refData initialized with the given one
587 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
589 wxObjectRefData
*m_refData
;
592 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
594 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
597 #if wxUSE_EXTENDED_RTTI
598 class WXDLLIMPEXP_BASE wxDynamicObject
: public wxObject
600 friend class WXDLLIMPEXP_FWD_BASE wxDynamicClassInfo
;
602 // instantiates this object with an instance of its superclass
603 wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
) ;
604 virtual ~wxDynamicObject();
606 void SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
);
607 wxxVariant
GetProperty (const wxChar
*propertyName
) const ;
609 // get the runtime identity of this object
610 wxClassInfo
*GetClassInfo() const
613 return (wxClassInfo
*) m_classInfo
;
615 wxDynamicClassInfo
*nonconst
= wx_const_cast(wxDynamicClassInfo
*, m_classInfo
);
616 return wx_static_cast(wxClassInfo
*, nonconst
);
620 wxObject
* GetSuperClassInstance() const
622 return m_superClassInstance
;
625 // removes an existing runtime-property
626 void RemoveProperty( const wxChar
*propertyName
) ;
628 // renames an existing runtime-property
629 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
) ;
631 wxObject
*m_superClassInstance
;
632 const wxDynamicClassInfo
*m_classInfo
;
633 struct wxDynamicObjectInternal
;
634 wxDynamicObjectInternal
*m_data
;
638 // ----------------------------------------------------------------------------
639 // more debugging macros
640 // ----------------------------------------------------------------------------
642 // Redefine new to be the debugging version. This doesn't work with all
643 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
644 // to use the debugging version.
647 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
649 #if wxUSE_DEBUG_NEW_ALWAYS
650 #if wxUSE_GLOBAL_MEMORY_OPERATORS
651 #define new WXDEBUG_NEW
652 #elif defined(__VISUALC__)
653 // Including this file redefines new and allows leak reports to
654 // contain line numbers
655 #include "wx/msw/msvcrt.h"
657 #endif // wxUSE_DEBUG_NEW_ALWAYS
658 #else // !__WXDEBUG__
659 #define WXDEBUG_NEW new
660 #endif // __WXDEBUG__/!__WXDEBUG__
662 #endif // _WX_OBJECTH__