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 // based on the value of wxUSE_EXTENDED_RTTI symbol,
23 // only one of the RTTI system will be compiled:
24 // - the "old" one (defined by rtti.h) or
25 // - the "new" one (defined by xti.h)
29 #define wxIMPLEMENT_CLASS(name, basename) \
30 wxIMPLEMENT_ABSTRACT_CLASS(name, basename)
32 #define wxIMPLEMENT_CLASS2(name, basename1, basename2) \
33 wxIMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
35 // -----------------------------------
36 // for pluggable classes
37 // -----------------------------------
39 // NOTE: this should probably be the very first statement
40 // in the class declaration so wxPluginSentinel is
41 // the first member initialised and the last destroyed.
43 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
45 #if wxUSE_NESTED_CLASSES
47 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
48 class exportdecl name##PluginSentinel { \
50 static const wxString sm_className; \
52 name##PluginSentinel(); \
53 ~name##PluginSentinel(); \
55 name##PluginSentinel m_pluginsentinel
57 #define _IMPLEMENT_DL_SENTINEL(name) \
58 const wxString name::name##PluginSentinel::sm_className(#name); \
59 name::name##PluginSentinel::name##PluginSentinel() { \
60 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
61 if( e != 0 ) { e->RefObj(); } \
63 name::name##PluginSentinel::~name##PluginSentinel() { \
64 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
65 if( e != 0 ) { e->UnrefObj(); } \
69 #define _DECLARE_DL_SENTINEL(name)
70 #define _IMPLEMENT_DL_SENTINEL(name)
72 #endif // wxUSE_NESTED_CLASSES
74 #define wxDECLARE_PLUGGABLE_CLASS(name) \
75 wxDECLARE_DYNAMIC_CLASS(name); _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE)
76 #define wxDECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
77 wxDECLARE_ABSTRACT_CLASS(name); _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE)
79 #define wxDECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
80 wxDECLARE_DYNAMIC_CLASS(name); _DECLARE_DL_SENTINEL(name, usergoo)
81 #define wxDECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
82 wxDECLARE_ABSTRACT_CLASS(name); _DECLARE_DL_SENTINEL(name, usergoo)
84 #define wxIMPLEMENT_PLUGGABLE_CLASS(name, basename) \
85 wxIMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
86 #define wxIMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
87 wxIMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
88 #define wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
89 wxIMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
90 #define wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
91 wxIMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
93 #define wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
94 wxIMPLEMENT_PLUGGABLE_CLASS(name, basename)
95 #define wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
96 wxIMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
97 #define wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
98 wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
99 #define wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
100 wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
102 #define wxCLASSINFO(name) (&name::ms_classInfo)
104 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
106 // Just seems a bit nicer-looking (pretend it's not a macro)
107 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
109 // this cast does some more checks at compile time as it uses static_cast
112 // note that it still has different semantics from dynamic_cast<> and so can't
113 // be replaced by it as long as there are any compilers not supporting it
114 #define wxDynamicCast(obj, className) \
115 ((className *) wxCheckDynamicCast( \
116 const_cast<wxObject *>(static_cast<const wxObject *>(\
117 const_cast<className *>(static_cast<const className *>(obj)))), \
118 &className::ms_classInfo))
120 // The 'this' pointer is always true, so use this version
121 // to cast the this pointer and avoid compiler warnings.
122 #define wxDynamicCastThis(className) \
123 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
125 // FIXME-VC6: dummy argument needed because VC6 doesn't support explicitly
126 // choosing the template function to call
128 inline T
*wxCheckCast(const void *ptr
, T
* = NULL
)
130 wxASSERT_MSG( wxDynamicCast(ptr
, T
), "wxStaticCast() used incorrectly" );
131 return const_cast<T
*>(static_cast<const T
*>(ptr
));
134 #define wxStaticCast(obj, className) wxCheckCast((obj), (className *)NULL)
136 // ----------------------------------------------------------------------------
137 // set up memory debugging macros
138 // ----------------------------------------------------------------------------
141 Which new/delete operator variants do we want?
143 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
144 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
145 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
146 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
147 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
148 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
149 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
152 #if wxUSE_MEMORY_TRACING
154 // All compilers get this one
155 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
157 // Everyone except Visage gets the next one
158 #ifndef __VISAGECPP__
159 #define _WX_WANT_DELETE_VOID
162 // Only visage gets this one under the correct circumstances
163 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
164 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
167 // Only VC++ 6 and CodeWarrior get overloaded delete that matches new
168 #if (defined(__VISUALC__) && (__VISUALC__ >= 1200)) || \
169 (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
170 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
173 // Now see who (if anyone) gets the array memory operators
174 #if wxUSE_ARRAY_MEMORY_OPERATORS
176 // Everyone except Visual C++ (cause problems for VC++ - crashes)
177 #if !defined(__VISUALC__)
178 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
181 // Everyone except Visual C++ (cause problems for VC++ - crashes)
182 #if !defined(__VISUALC__)
183 #define _WX_WANT_ARRAY_DELETE_VOID
186 // Only CodeWarrior 6 or higher
187 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
188 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
191 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
193 #endif // wxUSE_MEMORY_TRACING
195 // ----------------------------------------------------------------------------
196 // Compatibility macro aliases DECLARE group
197 // ----------------------------------------------------------------------------
198 // deprecated variants _not_ requiring a semicolon after them and without wx prefix.
199 // (note that also some wx-prefixed macro do _not_ require a semicolon because
200 // it's not always possible to force the compire to require it)
202 #define DECLARE_CLASS_INFO_ITERATORS() wxDECLARE_CLASS_INFO_ITERATORS();
203 #define DECLARE_ABSTRACT_CLASS(n) wxDECLARE_ABSTRACT_CLASS(n);
204 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(n) wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(n);
205 #define DECLARE_DYNAMIC_CLASS_NO_COPY(n) wxDECLARE_DYNAMIC_CLASS_NO_COPY(n);
206 #define DECLARE_DYNAMIC_CLASS(n) wxDECLARE_DYNAMIC_CLASS(n);
207 #define DECLARE_CLASS(n) wxDECLARE_CLASS(n);
209 #define DECLARE_PLUGGABLE_CLASS(n) wxDECLARE_PLUGGABLE_CLASS(n);
210 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(n) wxDECLARE_ABSTRACT_PLUGGABLE_CLASS(n);
211 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(n,u) wxDECLARE_USER_EXPORTED_PLUGGABLE_CLASS(n,u);
212 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,u) wxDECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,u);
214 // ----------------------------------------------------------------------------
215 // wxRefCounter: ref counted data "manager"
216 // ----------------------------------------------------------------------------
218 class WXDLLIMPEXP_BASE wxRefCounter
221 wxRefCounter() { m_count
= 1; }
223 int GetRefCount() const { return m_count
; }
225 void IncRef() { m_count
++; }
229 // this object should never be destroyed directly but only as a
230 // result of a DecRef() call:
231 virtual ~wxRefCounter() { }
237 // It doesn't make sense to copy the reference counted objects, a new ref
238 // counter should be created for a new object instead and compilation
239 // errors in the code using wxRefCounter due to the lack of copy ctor often
240 // indicate a problem, e.g. a forgotten copy ctor implementation somewhere.
241 wxDECLARE_NO_COPY_CLASS(wxRefCounter
);
244 // ----------------------------------------------------------------------------
245 // wxObjectRefData: ref counted data meant to be stored in wxObject
246 // ----------------------------------------------------------------------------
248 typedef wxRefCounter wxObjectRefData
;
250 // ----------------------------------------------------------------------------
251 // wxObjectDataPtr: helper class to avoid memleaks because of missing calls
252 // to wxObjectRefData::DecRef
253 // ----------------------------------------------------------------------------
256 class wxObjectDataPtr
259 typedef T element_type
;
261 wxEXPLICIT
wxObjectDataPtr(T
*ptr
= NULL
) : m_ptr(ptr
) {}
264 wxObjectDataPtr(const wxObjectDataPtr
<T
> &tocopy
)
265 : m_ptr(tocopy
.m_ptr
)
277 T
*get() const { return m_ptr
; }
279 // test for pointer validity: defining conversion to unspecified_bool_type
280 // and not more obvious bool to avoid implicit conversions to integer types
281 typedef T
*(wxObjectDataPtr
<T
>::*unspecified_bool_type
)() const;
282 operator unspecified_bool_type() const
284 return m_ptr
? &wxObjectDataPtr
<T
>::get
: NULL
;
289 wxASSERT(m_ptr
!= NULL
);
293 T
*operator->() const
295 wxASSERT(m_ptr
!= NULL
);
306 wxObjectDataPtr
& operator=(const wxObjectDataPtr
&tocopy
)
310 m_ptr
= tocopy
.m_ptr
;
316 wxObjectDataPtr
& operator=(T
*ptr
)
328 // ----------------------------------------------------------------------------
329 // wxObject: the root class of wxWidgets object hierarchy
330 // ----------------------------------------------------------------------------
332 class WXDLLIMPEXP_BASE wxObject
334 wxDECLARE_ABSTRACT_CLASS(wxObject
);
337 wxObject() { m_refData
= NULL
; }
338 virtual ~wxObject() { UnRef(); }
340 wxObject(const wxObject
& other
)
342 m_refData
= other
.m_refData
;
347 wxObject
& operator=(const wxObject
& other
)
349 if ( this != &other
)
356 bool IsKindOf(const wxClassInfo
*info
) const;
359 // Turn on the correct set of new and delete operators
361 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
362 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
365 #ifdef _WX_WANT_DELETE_VOID
366 void operator delete ( void * buf
);
369 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
370 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
373 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
374 void operator delete ( void *buf
, const wxChar
*, int );
377 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
378 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
381 #ifdef _WX_WANT_ARRAY_DELETE_VOID
382 void operator delete[] ( void *buf
);
385 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
386 void operator delete[] (void* buf
, const wxChar
*, int );
389 // ref counted data handling methods
392 wxObjectRefData
*GetRefData() const { return m_refData
; }
393 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
395 // make a 'clone' of the object
396 void Ref(const wxObject
& clone
);
398 // destroy a reference
401 // Make sure this object has only one reference
402 void UnShare() { AllocExclusive(); }
404 // check if this object references the same data as the other one
405 bool IsSameAs(const wxObject
& o
) const { return m_refData
== o
.m_refData
; }
408 // ensure that our data is not shared with anybody else: if we have no
409 // data, it is created using CreateRefData() below, if we have shared data
410 // it is copied using CloneRefData(), otherwise nothing is done
411 void AllocExclusive();
413 // both methods must be implemented if AllocExclusive() is used, not pure
414 // virtual only because of the backwards compatibility reasons
416 // create a new m_refData
417 virtual wxObjectRefData
*CreateRefData() const;
419 // create a new m_refData initialized with the given one
420 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
422 wxObjectRefData
*m_refData
;
425 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
427 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
432 // ----------------------------------------------------------------------------
433 // more debugging macros
434 // ----------------------------------------------------------------------------
436 #if wxUSE_DEBUG_NEW_ALWAYS
437 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
439 #if wxUSE_GLOBAL_MEMORY_OPERATORS
440 #define new WXDEBUG_NEW
441 #elif defined(__VISUALC__)
442 // Including this file redefines new and allows leak reports to
443 // contain line numbers
444 #include "wx/msw/msvcrt.h"
446 #endif // wxUSE_DEBUG_NEW_ALWAYS
448 // ----------------------------------------------------------------------------
449 // Compatibility macro aliases IMPLEMENT group
450 // ----------------------------------------------------------------------------
452 // deprecated variants _not_ requiring a semicolon after them and without wx prefix.
453 // (note that also some wx-prefixed macro do _not_ require a semicolon because
454 // it's not always possible to force the compire to require it)
456 #define IMPLEMENT_DYNAMIC_CLASS(n,b) wxIMPLEMENT_DYNAMIC_CLASS(n,b)
457 #define IMPLEMENT_DYNAMIC_CLASS2(n,b1,b2) wxIMPLEMENT_DYNAMIC_CLASS2(n,b1,b2)
458 #define IMPLEMENT_ABSTRACT_CLASS(n,b) wxIMPLEMENT_ABSTRACT_CLASS(n,b)
459 #define IMPLEMENT_ABSTRACT_CLASS2(n,b1,b2) wxIMPLEMENT_ABSTRACT_CLASS2(n,b1,b2)
460 #define IMPLEMENT_CLASS(n,b) wxIMPLEMENT_CLASS(n,b)
461 #define IMPLEMENT_CLASS2(n,b1,b2) wxIMPLEMENT_CLASS2(n,b1,b2)
463 #define IMPLEMENT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_PLUGGABLE_CLASS(n,b)
464 #define IMPLEMENT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_PLUGGABLE_CLASS2(n,b,b2)
465 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(n,b)
466 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2)
467 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(n,b)
468 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(n,b,b2)
469 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,b)
470 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2)
472 #define CLASSINFO(n) wxCLASSINFO(n)
474 #endif // _WX_OBJECTH__