1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxObject class, plus run-time type information macros
4 // Author: Julian Smart
5 // Modified by: Ron Lee
7 // Copyright: (c) 1997 Julian Smart
8 // (c) 2001 Ron Lee <ron@debian.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/memory.h"
21 #define wxDECLARE_CLASS_INFO_ITERATORS() \
22 class WXDLLIMPEXP_BASE const_iterator \
24 typedef wxHashTable_Node Node; \
26 typedef const wxClassInfo* value_type; \
27 typedef const value_type& const_reference; \
28 typedef const_iterator itor; \
29 typedef value_type* ptr_type; \
32 wxHashTable* m_table; \
34 typedef const_reference reference_type; \
35 typedef ptr_type pointer_type; \
37 const_iterator(Node* node, wxHashTable* table) \
38 : m_node(node), m_table(table) { } \
39 const_iterator() : m_node(NULL), m_table(NULL) { } \
40 value_type operator*() const; \
42 const itor operator++(int); \
43 bool operator!=(const itor& it) const \
44 { return it.m_node != m_node; } \
45 bool operator==(const itor& it) const \
46 { return it.m_node == m_node; } \
49 static const_iterator begin_classinfo(); \
50 static const_iterator end_classinfo()
52 // based on the value of wxUSE_EXTENDED_RTTI symbol,
53 // only one of the RTTI system will be compiled:
54 // - the "old" one (defined by rtti.h) or
55 // - the "new" one (defined by xti.h)
59 #define wxIMPLEMENT_CLASS(name, basename) \
60 wxIMPLEMENT_ABSTRACT_CLASS(name, basename)
62 #define wxIMPLEMENT_CLASS2(name, basename1, basename2) \
63 wxIMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
65 // -----------------------------------
66 // for pluggable classes
67 // -----------------------------------
69 // NOTE: this should probably be the very first statement
70 // in the class declaration so wxPluginSentinel is
71 // the first member initialised and the last destroyed.
73 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
75 #if wxUSE_NESTED_CLASSES
77 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
78 class exportdecl name##PluginSentinel { \
80 static const wxString sm_className; \
82 name##PluginSentinel(); \
83 ~name##PluginSentinel(); \
85 name##PluginSentinel m_pluginsentinel
87 #define _IMPLEMENT_DL_SENTINEL(name) \
88 const wxString name::name##PluginSentinel::sm_className(#name); \
89 name::name##PluginSentinel::name##PluginSentinel() { \
90 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
91 if( e != 0 ) { e->RefObj(); } \
93 name::name##PluginSentinel::~name##PluginSentinel() { \
94 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
95 if( e != 0 ) { e->UnrefObj(); } \
99 #define _DECLARE_DL_SENTINEL(name)
100 #define _IMPLEMENT_DL_SENTINEL(name)
102 #endif // wxUSE_NESTED_CLASSES
104 #define wxDECLARE_PLUGGABLE_CLASS(name) \
105 wxDECLARE_DYNAMIC_CLASS(name); _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE)
106 #define wxDECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
107 wxDECLARE_ABSTRACT_CLASS(name); _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE)
109 #define wxDECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
110 wxDECLARE_DYNAMIC_CLASS(name); _DECLARE_DL_SENTINEL(name, usergoo)
111 #define wxDECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
112 wxDECLARE_ABSTRACT_CLASS(name); _DECLARE_DL_SENTINEL(name, usergoo)
114 #define wxIMPLEMENT_PLUGGABLE_CLASS(name, basename) \
115 wxIMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
116 #define wxIMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
117 wxIMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
118 #define wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
119 wxIMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
120 #define wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
121 wxIMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
123 #define wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
124 wxIMPLEMENT_PLUGGABLE_CLASS(name, basename)
125 #define wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
126 wxIMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
127 #define wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
128 wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
129 #define wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
130 wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
132 #define wxCLASSINFO(name) (&name::ms_classInfo)
134 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
136 // Just seems a bit nicer-looking (pretend it's not a macro)
137 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
139 // this cast does some more checks at compile time as it uses static_cast
142 // note that it still has different semantics from dynamic_cast<> and so can't
143 // be replaced by it as long as there are any compilers not supporting it
144 #define wxDynamicCast(obj, className) \
145 ((className *) wxCheckDynamicCast( \
146 const_cast<wxObject *>(static_cast<const wxObject *>(\
147 const_cast<className *>(static_cast<const className *>(obj)))), \
148 &className::ms_classInfo))
150 // The 'this' pointer is always true, so use this version
151 // to cast the this pointer and avoid compiler warnings.
152 #define wxDynamicCastThis(className) \
153 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
155 // FIXME-VC6: dummy argument needed because VC6 doesn't support explicitly
156 // choosing the template function to call
158 inline T
*wxCheckCast(const void *ptr
, T
* = NULL
)
160 wxASSERT_MSG( wxDynamicCast(ptr
, T
), "wxStaticCast() used incorrectly" );
161 return const_cast<T
*>(static_cast<const T
*>(ptr
));
164 #define wxStaticCast(obj, className) wxCheckCast((obj), (className *)NULL)
166 // ----------------------------------------------------------------------------
167 // set up memory debugging macros
168 // ----------------------------------------------------------------------------
171 Which new/delete operator variants do we want?
173 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
174 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
175 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
176 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
177 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
178 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
179 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
182 #if wxUSE_MEMORY_TRACING
184 // All compilers get this one
185 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
187 // Everyone except Visage gets the next one
188 #ifndef __VISAGECPP__
189 #define _WX_WANT_DELETE_VOID
192 // Only visage gets this one under the correct circumstances
193 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
194 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
197 // Only VC++ 6 gets overloaded delete that matches new
198 #if (defined(__VISUALC__) && (__VISUALC__ >= 1200))
199 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
202 // Now see who (if anyone) gets the array memory operators
203 #if wxUSE_ARRAY_MEMORY_OPERATORS
205 // Everyone except Visual C++ (cause problems for VC++ - crashes)
206 #if !defined(__VISUALC__)
207 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
210 // Everyone except Visual C++ (cause problems for VC++ - crashes)
211 #if !defined(__VISUALC__)
212 #define _WX_WANT_ARRAY_DELETE_VOID
214 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
216 #endif // wxUSE_MEMORY_TRACING
218 // ----------------------------------------------------------------------------
219 // Compatibility macro aliases DECLARE group
220 // ----------------------------------------------------------------------------
221 // deprecated variants _not_ requiring a semicolon after them and without wx prefix.
222 // (note that also some wx-prefixed macro do _not_ require a semicolon because
223 // it's not always possible to force the compire to require it)
225 #define DECLARE_CLASS_INFO_ITERATORS() wxDECLARE_CLASS_INFO_ITERATORS();
226 #define DECLARE_ABSTRACT_CLASS(n) wxDECLARE_ABSTRACT_CLASS(n);
227 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(n) wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(n);
228 #define DECLARE_DYNAMIC_CLASS_NO_COPY(n) wxDECLARE_DYNAMIC_CLASS_NO_COPY(n);
229 #define DECLARE_DYNAMIC_CLASS(n) wxDECLARE_DYNAMIC_CLASS(n);
230 #define DECLARE_CLASS(n) wxDECLARE_CLASS(n);
232 #define DECLARE_PLUGGABLE_CLASS(n) wxDECLARE_PLUGGABLE_CLASS(n);
233 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(n) wxDECLARE_ABSTRACT_PLUGGABLE_CLASS(n);
234 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(n,u) wxDECLARE_USER_EXPORTED_PLUGGABLE_CLASS(n,u);
235 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,u) wxDECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,u);
237 // ----------------------------------------------------------------------------
238 // wxRefCounter: ref counted data "manager"
239 // ----------------------------------------------------------------------------
241 class WXDLLIMPEXP_BASE wxRefCounter
244 wxRefCounter() { m_count
= 1; }
246 int GetRefCount() const { return m_count
; }
248 void IncRef() { m_count
++; }
252 // this object should never be destroyed directly but only as a
253 // result of a DecRef() call:
254 virtual ~wxRefCounter() { }
260 // It doesn't make sense to copy the reference counted objects, a new ref
261 // counter should be created for a new object instead and compilation
262 // errors in the code using wxRefCounter due to the lack of copy ctor often
263 // indicate a problem, e.g. a forgotten copy ctor implementation somewhere.
264 wxDECLARE_NO_COPY_CLASS(wxRefCounter
);
267 // ----------------------------------------------------------------------------
268 // wxObjectRefData: ref counted data meant to be stored in wxObject
269 // ----------------------------------------------------------------------------
271 typedef wxRefCounter wxObjectRefData
;
273 // ----------------------------------------------------------------------------
274 // wxObjectDataPtr: helper class to avoid memleaks because of missing calls
275 // to wxObjectRefData::DecRef
276 // ----------------------------------------------------------------------------
279 class wxObjectDataPtr
282 typedef T element_type
;
284 wxEXPLICIT
wxObjectDataPtr(T
*ptr
= NULL
) : m_ptr(ptr
) {}
287 wxObjectDataPtr(const wxObjectDataPtr
<T
> &tocopy
)
288 : m_ptr(tocopy
.m_ptr
)
300 T
*get() const { return m_ptr
; }
302 // test for pointer validity: defining conversion to unspecified_bool_type
303 // and not more obvious bool to avoid implicit conversions to integer types
304 typedef T
*(wxObjectDataPtr
<T
>::*unspecified_bool_type
)() const;
305 operator unspecified_bool_type() const
307 return m_ptr
? &wxObjectDataPtr
<T
>::get
: NULL
;
312 wxASSERT(m_ptr
!= NULL
);
316 T
*operator->() const
318 wxASSERT(m_ptr
!= NULL
);
329 wxObjectDataPtr
& operator=(const wxObjectDataPtr
&tocopy
)
333 m_ptr
= tocopy
.m_ptr
;
339 wxObjectDataPtr
& operator=(T
*ptr
)
351 // ----------------------------------------------------------------------------
352 // wxObject: the root class of wxWidgets object hierarchy
353 // ----------------------------------------------------------------------------
355 class WXDLLIMPEXP_BASE wxObject
357 wxDECLARE_ABSTRACT_CLASS(wxObject
);
360 wxObject() { m_refData
= NULL
; }
361 virtual ~wxObject() { UnRef(); }
363 wxObject(const wxObject
& other
)
365 m_refData
= other
.m_refData
;
370 wxObject
& operator=(const wxObject
& other
)
372 if ( this != &other
)
379 bool IsKindOf(const wxClassInfo
*info
) const;
382 // Turn on the correct set of new and delete operators
384 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
385 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
388 #ifdef _WX_WANT_DELETE_VOID
389 void operator delete ( void * buf
);
392 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
393 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
396 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
397 void operator delete ( void *buf
, const wxChar
*, int );
400 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
401 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
404 #ifdef _WX_WANT_ARRAY_DELETE_VOID
405 void operator delete[] ( void *buf
);
408 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
409 void operator delete[] (void* buf
, const wxChar
*, int );
412 // ref counted data handling methods
415 wxObjectRefData
*GetRefData() const { return m_refData
; }
416 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
418 // make a 'clone' of the object
419 void Ref(const wxObject
& clone
);
421 // destroy a reference
424 // Make sure this object has only one reference
425 void UnShare() { AllocExclusive(); }
427 // check if this object references the same data as the other one
428 bool IsSameAs(const wxObject
& o
) const { return m_refData
== o
.m_refData
; }
431 // ensure that our data is not shared with anybody else: if we have no
432 // data, it is created using CreateRefData() below, if we have shared data
433 // it is copied using CloneRefData(), otherwise nothing is done
434 void AllocExclusive();
436 // both methods must be implemented if AllocExclusive() is used, not pure
437 // virtual only because of the backwards compatibility reasons
439 // create a new m_refData
440 virtual wxObjectRefData
*CreateRefData() const;
442 // create a new m_refData initialized with the given one
443 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
445 wxObjectRefData
*m_refData
;
448 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
450 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
455 // ----------------------------------------------------------------------------
456 // more debugging macros
457 // ----------------------------------------------------------------------------
459 #if wxUSE_DEBUG_NEW_ALWAYS
460 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
462 #if wxUSE_GLOBAL_MEMORY_OPERATORS
463 #define new WXDEBUG_NEW
464 #elif defined(__VISUALC__)
465 // Including this file redefines new and allows leak reports to
466 // contain line numbers
467 #include "wx/msw/msvcrt.h"
469 #endif // wxUSE_DEBUG_NEW_ALWAYS
471 // ----------------------------------------------------------------------------
472 // Compatibility macro aliases IMPLEMENT group
473 // ----------------------------------------------------------------------------
475 // deprecated variants _not_ requiring a semicolon after them and without wx prefix.
476 // (note that also some wx-prefixed macro do _not_ require a semicolon because
477 // it's not always possible to force the compire to require it)
479 #define IMPLEMENT_DYNAMIC_CLASS(n,b) wxIMPLEMENT_DYNAMIC_CLASS(n,b)
480 #define IMPLEMENT_DYNAMIC_CLASS2(n,b1,b2) wxIMPLEMENT_DYNAMIC_CLASS2(n,b1,b2)
481 #define IMPLEMENT_ABSTRACT_CLASS(n,b) wxIMPLEMENT_ABSTRACT_CLASS(n,b)
482 #define IMPLEMENT_ABSTRACT_CLASS2(n,b1,b2) wxIMPLEMENT_ABSTRACT_CLASS2(n,b1,b2)
483 #define IMPLEMENT_CLASS(n,b) wxIMPLEMENT_CLASS(n,b)
484 #define IMPLEMENT_CLASS2(n,b1,b2) wxIMPLEMENT_CLASS2(n,b1,b2)
486 #define IMPLEMENT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_PLUGGABLE_CLASS(n,b)
487 #define IMPLEMENT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_PLUGGABLE_CLASS2(n,b,b2)
488 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(n,b)
489 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2)
490 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(n,b)
491 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(n,b,b2)
492 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,b)
493 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2)
495 #define CLASSINFO(n) wxCLASSINFO(n)
497 #endif // _WX_OBJECTH__