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
) ) );
98 const wxChar
*m_className
;
100 wxObjectConstructorFn m_objectConstructor
;
102 // Pointers to base wxClassInfos
104 const wxClassInfo
*m_baseInfo1
;
105 const wxClassInfo
*m_baseInfo2
;
107 // class info object live in a linked list:
108 // pointers to its head and the next element in it
110 static wxClassInfo
*sm_first
;
113 // FIXME: this should be private (currently used directly by way too
115 static wxHashTable
*sm_classTable
;
118 // registers the class
122 DECLARE_NO_COPY_CLASS(wxClassInfo
)
125 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
127 // ----------------------------------------------------------------------------
128 // Dynamic class macros
129 // ----------------------------------------------------------------------------
131 #define DECLARE_ABSTRACT_CLASS(name) \
133 static wxClassInfo ms_classInfo; \
134 virtual wxClassInfo *GetClassInfo() const;
136 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
137 DECLARE_NO_ASSIGN_CLASS(name) \
138 DECLARE_DYNAMIC_CLASS(name)
140 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
141 DECLARE_NO_COPY_CLASS(name) \
142 DECLARE_DYNAMIC_CLASS(name)
144 #define DECLARE_DYNAMIC_CLASS(name) \
145 DECLARE_ABSTRACT_CLASS(name) \
146 static wxObject* wxCreateObject();
148 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
151 // common part of the macros below
152 #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
153 wxClassInfo name::ms_classInfo(wxT(#name), \
154 &basename::ms_classInfo, \
156 (int) sizeof(name), \
157 (wxObjectConstructorFn) func); \
159 wxClassInfo *name::GetClassInfo() const \
160 { return &name::ms_classInfo; }
162 #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
163 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
165 #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
166 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func)
168 // -----------------------------------
169 // for concrete classes
170 // -----------------------------------
172 // Single inheritance with one base class
173 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
174 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
175 wxObject* name::wxCreateObject() \
178 // Multiple inheritance with two base classes
179 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
180 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
181 name::wxCreateObject) \
182 wxObject* name::wxCreateObject() \
185 // -----------------------------------
186 // for abstract classes
187 // -----------------------------------
189 // Single inheritance with one base class
191 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
192 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
194 // Multiple inheritance with two base classes
196 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
197 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
199 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
200 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
202 #endif // !wxUSE_EXTENDED_RTTI
205 // -----------------------------------
206 // for pluggable classes
207 // -----------------------------------
209 // NOTE: this should probably be the very first statement
210 // in the class declaration so wxPluginSentinel is
211 // the first member initialised and the last destroyed.
213 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
215 #if wxUSE_NESTED_CLASSES
217 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
218 class exportdecl name##PluginSentinel { \
220 static const wxString sm_className; \
222 name##PluginSentinel(); \
223 ~name##PluginSentinel(); \
225 name##PluginSentinel m_pluginsentinel;
227 #define _IMPLEMENT_DL_SENTINEL(name) \
228 const wxString name::name##PluginSentinel::sm_className(#name); \
229 name::name##PluginSentinel::name##PluginSentinel() { \
230 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
231 if( e != 0 ) { e->RefObj(); } \
233 name::name##PluginSentinel::~name##PluginSentinel() { \
234 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
235 if( e != 0 ) { e->UnrefObj(); } \
239 #define _DECLARE_DL_SENTINEL(name)
240 #define _IMPLEMENT_DL_SENTINEL(name)
242 #endif // wxUSE_NESTED_CLASSES
244 #define DECLARE_PLUGGABLE_CLASS(name) \
245 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
246 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
247 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
249 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
250 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
251 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
252 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
254 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
255 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
256 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
257 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
258 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
259 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
260 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
261 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
263 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
264 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
265 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
266 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
267 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
268 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
269 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
270 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
272 #define CLASSINFO(name) (&name::ms_classInfo)
274 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
276 // Just seems a bit nicer-looking (pretend it's not a macro)
277 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
279 // this cast does some more checks at compile time as it uses static_cast
282 // note that it still has different semantics from dynamic_cast<> and so can't
283 // be replaced by it as long as there are any compilers not supporting it
284 #define wxDynamicCast(obj, className) \
285 ((className *) wxCheckDynamicCast( \
286 wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \
287 wx_const_cast(className *, wx_static_cast(const className *, obj)))), \
288 &className::ms_classInfo))
290 // The 'this' pointer is always true, so use this version
291 // to cast the this pointer and avoid compiler warnings.
292 #define wxDynamicCastThis(className) \
293 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
296 inline void* wxCheckCast(void *ptr
)
298 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
301 #define wxStaticCast(obj, className) \
302 ((className *)wxCheckCast(wxDynamicCast(obj, className)))
304 #else // !__WXDEBUG__
305 #define wxStaticCast(obj, className) \
306 wx_const_cast(className *, wx_static_cast(const className *, obj))
308 #endif // __WXDEBUG__
310 // ----------------------------------------------------------------------------
311 // set up memory debugging macros
312 // ----------------------------------------------------------------------------
315 Which new/delete operator variants do we want?
317 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
318 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
319 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
320 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
321 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
322 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
323 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
326 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
328 // All compilers get this one
329 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
331 // Everyone except Visage gets the next one
332 #ifndef __VISAGECPP__
333 #define _WX_WANT_DELETE_VOID
336 // Only visage gets this one under the correct circumstances
337 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
338 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
341 // Only VC++ 6 and CodeWarrior get overloaded delete that matches new
342 #if (defined(__VISUALC__) && (__VISUALC__ >= 1200)) || \
343 (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
344 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
347 // Now see who (if anyone) gets the array memory operators
348 #if wxUSE_ARRAY_MEMORY_OPERATORS
350 // Everyone except Visual C++ (cause problems for VC++ - crashes)
351 #if !defined(__VISUALC__)
352 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
355 // Everyone except Visual C++ (cause problems for VC++ - crashes)
356 #if !defined(__VISUALC__)
357 #define _WX_WANT_ARRAY_DELETE_VOID
360 // Only CodeWarrior 6 or higher
361 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
362 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
365 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
367 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
369 // ----------------------------------------------------------------------------
370 // wxObjectRefData: ref counted data meant to be stored in wxObject
371 // ----------------------------------------------------------------------------
373 class WXDLLIMPEXP_BASE wxObjectRefData
375 friend class WXDLLIMPEXP_BASE wxObject
;
378 wxObjectRefData() : m_count(1) { }
380 int GetRefCount() const { return m_count
; }
382 void IncRef() { m_count
++; }
386 // this object should never be destroyed directly but only as a
387 // result of a DecRef() call:
388 virtual ~wxObjectRefData() { }
395 // ----------------------------------------------------------------------------
396 // wxObjectDataPtr: helper class to avoid memleaks because of missing calls
397 // to wxObjectRefData::DecRef
398 // ----------------------------------------------------------------------------
401 class wxObjectDataPtr
404 typedef T element_type
;
406 wxEXPLICIT
wxObjectDataPtr(T
*ptr
= NULL
) : m_ptr(ptr
) {}
409 wxObjectDataPtr(const wxObjectDataPtr
<T
> &tocopy
)
410 : m_ptr(tocopy
.m_ptr
)
422 T
*get() const { return m_ptr
; }
423 T
*operator->() const { return get(); }
432 wxObjectDataPtr
& operator=(const wxObjectDataPtr
&tocopy
)
436 m_ptr
= tocopy
.m_ptr
;
442 wxObjectDataPtr
& operator=(T
*ptr
)
456 // ----------------------------------------------------------------------------
457 // wxObject: the root class of wxWidgets object hierarchy
458 // ----------------------------------------------------------------------------
460 class WXDLLIMPEXP_BASE wxObject
462 DECLARE_ABSTRACT_CLASS(wxObject
)
465 wxObject() { m_refData
= NULL
; }
466 virtual ~wxObject() { UnRef(); }
468 wxObject(const wxObject
& other
)
470 m_refData
= other
.m_refData
;
472 m_refData
->m_count
++;
475 wxObject
& operator=(const wxObject
& other
)
477 if ( this != &other
)
484 bool IsKindOf(wxClassInfo
*info
) const;
487 // Turn on the correct set of new and delete operators
489 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
490 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
493 #ifdef _WX_WANT_DELETE_VOID
494 void operator delete ( void * buf
);
497 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
498 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
501 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
502 void operator delete ( void *buf
, const wxChar
*, int );
505 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
506 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
509 #ifdef _WX_WANT_ARRAY_DELETE_VOID
510 void operator delete[] ( void *buf
);
513 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
514 void operator delete[] (void* buf
, const wxChar
*, int );
517 // ref counted data handling methods
520 wxObjectRefData
*GetRefData() const { return m_refData
; }
521 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
523 // make a 'clone' of the object
524 void Ref(const wxObject
& clone
);
526 // destroy a reference
529 // Make sure this object has only one reference
530 void UnShare() { AllocExclusive(); }
532 // check if this object references the same data as the other one
533 bool IsSameAs(const wxObject
& o
) const { return m_refData
== o
.m_refData
; }
536 // ensure that our data is not shared with anybody else: if we have no
537 // data, it is created using CreateRefData() below, if we have shared data
538 // it is copied using CloneRefData(), otherwise nothing is done
539 void AllocExclusive();
541 // both methods must be implemented if AllocExclusive() is used, not pure
542 // virtual only because of the backwards compatibility reasons
544 // create a new m_refData
545 virtual wxObjectRefData
*CreateRefData() const;
547 // create a new m_refData initialized with the given one
548 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
550 wxObjectRefData
*m_refData
;
553 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
555 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
558 #if wxUSE_EXTENDED_RTTI
559 class WXDLLIMPEXP_BASE wxDynamicObject
: public wxObject
561 friend class WXDLLIMPEXP_BASE wxDynamicClassInfo
;
563 // instantiates this object with an instance of its superclass
564 wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
) ;
565 virtual ~wxDynamicObject();
567 void SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
);
568 wxxVariant
GetProperty (const wxChar
*propertyName
) const ;
570 // get the runtime identity of this object
571 wxClassInfo
*GetClassInfo() const
574 return (wxClassInfo
*) m_classInfo
;
576 wxDynamicClassInfo
*nonconst
= wx_const_cast(wxDynamicClassInfo
*, m_classInfo
);
577 return wx_static_cast(wxClassInfo
*, nonconst
);
581 wxObject
* GetSuperClassInstance() const
583 return m_superClassInstance
;
586 // removes an existing runtime-property
587 void RemoveProperty( const wxChar
*propertyName
) ;
589 // renames an existing runtime-property
590 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
) ;
592 wxObject
*m_superClassInstance
;
593 const wxDynamicClassInfo
*m_classInfo
;
594 struct wxDynamicObjectInternal
;
595 wxDynamicObjectInternal
*m_data
;
599 // ----------------------------------------------------------------------------
600 // more debugging macros
601 // ----------------------------------------------------------------------------
603 // Redefine new to be the debugging version. This doesn't work with all
604 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
605 // to use the debugging version.
608 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
610 #if wxUSE_DEBUG_NEW_ALWAYS
611 #if wxUSE_GLOBAL_MEMORY_OPERATORS
612 #define new WXDEBUG_NEW
613 #elif defined(__VISUALC__)
614 // Including this file redefines new and allows leak reports to
615 // contain line numbers
616 #include "wx/msw/msvcrt.h"
618 #endif // wxUSE_DEBUG_NEW_ALWAYS
619 #else // !__WXDEBUG__
620 #define WXDEBUG_NEW new
621 #endif // __WXDEBUG__/!__WXDEBUG__
623 #endif // _WX_OBJECTH__