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 wxHashTable_Node
;
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 typedef wxObject
*(*wxObjectConstructorFn
)(void);
80 class WXDLLIMPEXP_BASE wxClassInfo
82 friend class WXDLLIMPEXP_FWD_BASE wxObject
;
83 friend WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxString
& name
);
85 wxClassInfo( const wxChar
*className
,
86 const wxClassInfo
*baseInfo1
,
87 const wxClassInfo
*baseInfo2
,
89 wxObjectConstructorFn ctor
)
90 : m_className(className
)
92 , m_objectConstructor(ctor
)
93 , m_baseInfo1(baseInfo1
)
94 , m_baseInfo2(baseInfo2
)
103 wxObject
*CreateObject() const
104 { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
105 bool IsDynamic() const { return (NULL
!= m_objectConstructor
); }
107 const wxChar
*GetClassName() const { return m_className
; }
108 const wxChar
*GetBaseClassName1() const
109 { return m_baseInfo1
? m_baseInfo1
->GetClassName() : NULL
; }
110 const wxChar
*GetBaseClassName2() const
111 { return m_baseInfo2
? m_baseInfo2
->GetClassName() : NULL
; }
112 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
113 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
114 int GetSize() const { return m_objectSize
; }
116 wxObjectConstructorFn
GetConstructor() const
117 { return m_objectConstructor
; }
118 static const wxClassInfo
*GetFirst() { return sm_first
; }
119 const wxClassInfo
*GetNext() const { return m_next
; }
120 static wxClassInfo
*FindClass(const wxString
& className
);
122 // Climb upwards through inheritance hierarchy.
123 // Dual inheritance is catered for.
125 bool IsKindOf(const wxClassInfo
*info
) const
129 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
130 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
133 DECLARE_CLASS_INFO_ITERATORS()
135 const wxChar
*m_className
;
137 wxObjectConstructorFn m_objectConstructor
;
139 // Pointers to base wxClassInfos
141 const wxClassInfo
*m_baseInfo1
;
142 const wxClassInfo
*m_baseInfo2
;
144 // class info object live in a linked list:
145 // pointers to its head and the next element in it
147 static wxClassInfo
*sm_first
;
150 static wxHashTable
*sm_classTable
;
153 // registers the class
157 wxDECLARE_NO_COPY_CLASS(wxClassInfo
);
160 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxString
& name
);
162 // ----------------------------------------------------------------------------
163 // Dynamic class macros
164 // ----------------------------------------------------------------------------
166 #define DECLARE_ABSTRACT_CLASS(name) \
168 static wxClassInfo ms_classInfo; \
169 virtual wxClassInfo *GetClassInfo() const;
171 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
172 DECLARE_NO_ASSIGN_CLASS(name) \
173 DECLARE_DYNAMIC_CLASS(name)
175 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
176 DECLARE_NO_COPY_CLASS(name) \
177 DECLARE_DYNAMIC_CLASS(name)
179 #define DECLARE_DYNAMIC_CLASS(name) \
180 DECLARE_ABSTRACT_CLASS(name) \
181 static wxObject* wxCreateObject();
183 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
186 // common part of the macros below
187 #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
188 wxClassInfo name::ms_classInfo(wxT(#name), \
189 &basename::ms_classInfo, \
191 (int) sizeof(name), \
194 wxClassInfo *name::GetClassInfo() const \
195 { return &name::ms_classInfo; }
197 #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
198 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
200 #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
201 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func)
203 // -----------------------------------
204 // for concrete classes
205 // -----------------------------------
207 // Single inheritance with one base class
208 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
209 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
210 wxObject* name::wxCreateObject() \
213 // Multiple inheritance with two base classes
214 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
215 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
216 name::wxCreateObject) \
217 wxObject* name::wxCreateObject() \
220 // -----------------------------------
221 // for abstract classes
222 // -----------------------------------
224 // Single inheritance with one base class
226 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
227 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
229 // Multiple inheritance with two base classes
231 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
232 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
234 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
235 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
237 #endif // !wxUSE_EXTENDED_RTTI
240 // -----------------------------------
241 // for pluggable classes
242 // -----------------------------------
244 // NOTE: this should probably be the very first statement
245 // in the class declaration so wxPluginSentinel is
246 // the first member initialised and the last destroyed.
248 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
250 #if wxUSE_NESTED_CLASSES
252 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
253 class exportdecl name##PluginSentinel { \
255 static const wxString sm_className; \
257 name##PluginSentinel(); \
258 ~name##PluginSentinel(); \
260 name##PluginSentinel m_pluginsentinel;
262 #define _IMPLEMENT_DL_SENTINEL(name) \
263 const wxString name::name##PluginSentinel::sm_className(#name); \
264 name::name##PluginSentinel::name##PluginSentinel() { \
265 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
266 if( e != 0 ) { e->RefObj(); } \
268 name::name##PluginSentinel::~name##PluginSentinel() { \
269 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
270 if( e != 0 ) { e->UnrefObj(); } \
274 #define _DECLARE_DL_SENTINEL(name)
275 #define _IMPLEMENT_DL_SENTINEL(name)
277 #endif // wxUSE_NESTED_CLASSES
279 #define DECLARE_PLUGGABLE_CLASS(name) \
280 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE)
281 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
282 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE)
284 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
285 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
286 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
287 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
289 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
290 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
291 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
292 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
293 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
294 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
295 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
296 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
298 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
299 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
300 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
301 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
302 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
303 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
304 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
305 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
307 #define CLASSINFO(name) (&name::ms_classInfo)
309 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
311 // Just seems a bit nicer-looking (pretend it's not a macro)
312 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
314 // this cast does some more checks at compile time as it uses static_cast
317 // note that it still has different semantics from dynamic_cast<> and so can't
318 // be replaced by it as long as there are any compilers not supporting it
319 #define wxDynamicCast(obj, className) \
320 ((className *) wxCheckDynamicCast( \
321 const_cast<wxObject *>(static_cast<const wxObject *>(\
322 const_cast<className *>(static_cast<const className *>(obj)))), \
323 &className::ms_classInfo))
325 // The 'this' pointer is always true, so use this version
326 // to cast the this pointer and avoid compiler warnings.
327 #define wxDynamicCastThis(className) \
328 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
330 // FIXME-VC6: dummy argument needed because VC6 doesn't support explicitly
331 // choosing the template function to call
333 inline T
*wxCheckCast(const void *ptr
, T
* = NULL
)
335 wxASSERT_MSG( wxDynamicCast(ptr
, T
), "wxStaticCast() used incorrectly" );
336 return const_cast<T
*>(static_cast<const T
*>(ptr
));
339 #define wxStaticCast(obj, className) wxCheckCast((obj), (className *)NULL)
341 // ----------------------------------------------------------------------------
342 // set up memory debugging macros
343 // ----------------------------------------------------------------------------
346 Which new/delete operator variants do we want?
348 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
349 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
350 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
351 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
352 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
353 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
354 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
357 #if wxUSE_MEMORY_TRACING
359 // All compilers get this one
360 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
362 // Everyone except Visage gets the next one
363 #ifndef __VISAGECPP__
364 #define _WX_WANT_DELETE_VOID
367 // Only visage gets this one under the correct circumstances
368 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
369 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
372 // Only VC++ 6 and CodeWarrior get overloaded delete that matches new
373 #if (defined(__VISUALC__) && (__VISUALC__ >= 1200)) || \
374 (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
375 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
378 // Now see who (if anyone) gets the array memory operators
379 #if wxUSE_ARRAY_MEMORY_OPERATORS
381 // Everyone except Visual C++ (cause problems for VC++ - crashes)
382 #if !defined(__VISUALC__)
383 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
386 // Everyone except Visual C++ (cause problems for VC++ - crashes)
387 #if !defined(__VISUALC__)
388 #define _WX_WANT_ARRAY_DELETE_VOID
391 // Only CodeWarrior 6 or higher
392 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
393 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
396 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
398 #endif // wxUSE_MEMORY_TRACING
400 // ----------------------------------------------------------------------------
401 // wxRefCounter: ref counted data "manager"
402 // ----------------------------------------------------------------------------
404 class WXDLLIMPEXP_BASE wxRefCounter
407 wxRefCounter() { m_count
= 1; }
409 int GetRefCount() const { return m_count
; }
411 void IncRef() { m_count
++; }
415 // this object should never be destroyed directly but only as a
416 // result of a DecRef() call:
417 virtual ~wxRefCounter() { }
424 // ----------------------------------------------------------------------------
425 // wxObjectRefData: ref counted data meant to be stored in wxObject
426 // ----------------------------------------------------------------------------
428 typedef wxRefCounter 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 // test for pointer validity: defining conversion to unspecified_bool_type
461 // and not more obvious bool to avoid implicit conversions to integer types
462 typedef T
*(wxObjectDataPtr
<T
>::*unspecified_bool_type
)() const;
463 operator unspecified_bool_type() const
465 return m_ptr
? &wxObjectDataPtr
<T
>::get
: NULL
;
470 wxASSERT(m_ptr
!= NULL
);
474 T
*operator->() const
476 wxASSERT(m_ptr
!= NULL
);
487 wxObjectDataPtr
& operator=(const wxObjectDataPtr
&tocopy
)
491 m_ptr
= tocopy
.m_ptr
;
497 wxObjectDataPtr
& operator=(T
*ptr
)
509 // ----------------------------------------------------------------------------
510 // wxObject: the root class of wxWidgets object hierarchy
511 // ----------------------------------------------------------------------------
513 class WXDLLIMPEXP_BASE wxObject
515 DECLARE_ABSTRACT_CLASS(wxObject
)
518 wxObject() { m_refData
= NULL
; }
519 virtual ~wxObject() { UnRef(); }
521 wxObject(const wxObject
& other
)
523 m_refData
= other
.m_refData
;
528 wxObject
& operator=(const wxObject
& other
)
530 if ( this != &other
)
537 bool IsKindOf(const wxClassInfo
*info
) const;
540 // Turn on the correct set of new and delete operators
542 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
543 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
546 #ifdef _WX_WANT_DELETE_VOID
547 void operator delete ( void * buf
);
550 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
551 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
554 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
555 void operator delete ( void *buf
, const wxChar
*, int );
558 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
559 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
562 #ifdef _WX_WANT_ARRAY_DELETE_VOID
563 void operator delete[] ( void *buf
);
566 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
567 void operator delete[] (void* buf
, const wxChar
*, int );
570 // ref counted data handling methods
573 wxObjectRefData
*GetRefData() const { return m_refData
; }
574 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
576 // make a 'clone' of the object
577 void Ref(const wxObject
& clone
);
579 // destroy a reference
582 // Make sure this object has only one reference
583 void UnShare() { AllocExclusive(); }
585 // check if this object references the same data as the other one
586 bool IsSameAs(const wxObject
& o
) const { return m_refData
== o
.m_refData
; }
589 // ensure that our data is not shared with anybody else: if we have no
590 // data, it is created using CreateRefData() below, if we have shared data
591 // it is copied using CloneRefData(), otherwise nothing is done
592 void AllocExclusive();
594 // both methods must be implemented if AllocExclusive() is used, not pure
595 // virtual only because of the backwards compatibility reasons
597 // create a new m_refData
598 virtual wxObjectRefData
*CreateRefData() const;
600 // create a new m_refData initialized with the given one
601 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
603 wxObjectRefData
*m_refData
;
606 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
608 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
611 #if wxUSE_EXTENDED_RTTI
612 class WXDLLIMPEXP_BASE wxDynamicObject
: public wxObject
614 friend class WXDLLIMPEXP_FWD_BASE wxDynamicClassInfo
;
616 // instantiates this object with an instance of its superclass
617 wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
) ;
618 virtual ~wxDynamicObject();
620 void SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
);
621 wxxVariant
GetProperty (const wxChar
*propertyName
) const ;
623 // get the runtime identity of this object
624 wxClassInfo
*GetClassInfo() const
627 return (wxClassInfo
*) m_classInfo
;
629 wxDynamicClassInfo
*nonconst
= const_cast<wxDynamicClassInfo
*>(m_classInfo
);
630 return static_cast<wxClassInfo
*>(nonconst
);
634 wxObject
* GetSuperClassInstance() const
636 return m_superClassInstance
;
639 // removes an existing runtime-property
640 void RemoveProperty( const wxChar
*propertyName
) ;
642 // renames an existing runtime-property
643 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
) ;
645 wxObject
*m_superClassInstance
;
646 const wxDynamicClassInfo
*m_classInfo
;
647 struct wxDynamicObjectInternal
;
648 wxDynamicObjectInternal
*m_data
;
652 // ----------------------------------------------------------------------------
653 // more debugging macros
654 // ----------------------------------------------------------------------------
656 #if wxUSE_DEBUG_NEW_ALWAYS
657 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
659 #if wxUSE_GLOBAL_MEMORY_OPERATORS
660 #define new WXDEBUG_NEW
661 #elif defined(__VISUALC__)
662 // Including this file redefines new and allows leak reports to
663 // contain line numbers
664 #include "wx/msw/msvcrt.h"
666 #endif // wxUSE_DEBUG_NEW_ALWAYS
668 #endif // _WX_OBJECTH__