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: set in InitializeClasses
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 // InitializeClasses() helper
119 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
121 DECLARE_NO_COPY_CLASS(wxClassInfo
)
124 // registers the class
129 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
131 // ----------------------------------------------------------------------------
132 // Dynamic class macros
133 // ----------------------------------------------------------------------------
135 #define DECLARE_ABSTRACT_CLASS(name) \
137 static wxClassInfo ms_classInfo; \
138 virtual wxClassInfo *GetClassInfo() const;
140 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
141 DECLARE_NO_ASSIGN_CLASS(name) \
142 DECLARE_DYNAMIC_CLASS(name)
144 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
145 DECLARE_NO_COPY_CLASS(name) \
146 DECLARE_DYNAMIC_CLASS(name)
148 #define DECLARE_DYNAMIC_CLASS(name) \
149 DECLARE_ABSTRACT_CLASS(name) \
150 static wxObject* wxCreateObject();
152 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
155 // common part of the macros below
156 #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
157 wxClassInfo name::ms_classInfo(wxT(#name), \
158 &basename::ms_classInfo, \
160 (int) sizeof(name), \
161 (wxObjectConstructorFn) func); \
163 wxClassInfo *name::GetClassInfo() const \
164 { return &name::ms_classInfo; }
166 #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
167 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
169 #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
170 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func)
172 // -----------------------------------
173 // for concrete classes
174 // -----------------------------------
176 // Single inheritance with one base class
177 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
178 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
179 wxObject* name::wxCreateObject() \
182 // Multiple inheritance with two base classes
183 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
184 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
185 name::wxCreateObject) \
186 wxObject* name::wxCreateObject() \
189 // -----------------------------------
190 // for abstract classes
191 // -----------------------------------
193 // Single inheritance with one base class
195 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
196 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
198 // Multiple inheritance with two base classes
200 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
201 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
203 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
204 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
206 #endif // !wxUSE_EXTENDED_RTTI
209 // -----------------------------------
210 // for pluggable classes
211 // -----------------------------------
213 // NOTE: this should probably be the very first statement
214 // in the class declaration so wxPluginSentinel is
215 // the first member initialised and the last destroyed.
217 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
219 #if wxUSE_NESTED_CLASSES
221 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
222 class exportdecl name##PluginSentinel { \
224 static const wxString sm_className; \
226 name##PluginSentinel(); \
227 ~name##PluginSentinel(); \
229 name##PluginSentinel m_pluginsentinel;
231 #define _IMPLEMENT_DL_SENTINEL(name) \
232 const wxString name::name##PluginSentinel::sm_className(#name); \
233 name::name##PluginSentinel::name##PluginSentinel() { \
234 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
235 if( e != 0 ) { e->RefObj(); } \
237 name::name##PluginSentinel::~name##PluginSentinel() { \
238 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
239 if( e != 0 ) { e->UnrefObj(); } \
243 #define _DECLARE_DL_SENTINEL(name)
244 #define _IMPLEMENT_DL_SENTINEL(name)
246 #endif // wxUSE_NESTED_CLASSES
248 #define DECLARE_PLUGGABLE_CLASS(name) \
249 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
250 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
251 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
253 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
254 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
255 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
256 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
258 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
259 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
260 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
261 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
262 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
263 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
264 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
265 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
267 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
268 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
269 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
270 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
271 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
272 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
273 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
274 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
276 #define CLASSINFO(name) (&name::ms_classInfo)
278 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
280 // Just seems a bit nicer-looking (pretend it's not a macro)
281 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
283 // this cast does some more checks at compile time as it uses static_cast
286 // note that it still has different semantics from dynamic_cast<> and so can't
287 // be replaced by it as long as there are any compilers not supporting it
288 #define wxDynamicCast(obj, className) \
289 ((className *) wxCheckDynamicCast( \
290 wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \
291 wx_const_cast(className *, wx_static_cast(const className *, obj)))), \
292 &className::ms_classInfo))
294 // The 'this' pointer is always true, so use this version
295 // to cast the this pointer and avoid compiler warnings.
296 #define wxDynamicCastThis(className) \
297 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
300 inline void* wxCheckCast(void *ptr
)
302 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
305 #define wxStaticCast(obj, className) \
306 ((className *)wxCheckCast(wxDynamicCast(obj, className)))
308 #else // !__WXDEBUG__
309 #define wxStaticCast(obj, className) \
310 wx_const_cast(className *, wx_static_cast(const className *, obj))
312 #endif // __WXDEBUG__
314 // ----------------------------------------------------------------------------
315 // set up memory debugging macros
316 // ----------------------------------------------------------------------------
319 Which new/delete operator variants do we want?
321 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
322 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
323 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
324 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
325 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
326 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
327 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
330 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
332 // All compilers get this one
333 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
335 // Everyone except Visage gets the next one
336 #ifndef __VISAGECPP__
337 #define _WX_WANT_DELETE_VOID
340 // Only visage gets this one under the correct circumstances
341 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
342 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
345 // Only VC++ 6 and CodeWarrior get overloaded delete that matches new
346 #if (defined(__VISUALC__) && (__VISUALC__ >= 1200)) || \
347 (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
348 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
351 // Now see who (if anyone) gets the array memory operators
352 #if wxUSE_ARRAY_MEMORY_OPERATORS
354 // Everyone except Visual C++ (cause problems for VC++ - crashes)
355 #if !defined(__VISUALC__)
356 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
359 // Everyone except Visual C++ (cause problems for VC++ - crashes)
360 #if !defined(__VISUALC__)
361 #define _WX_WANT_ARRAY_DELETE_VOID
364 // Only CodeWarrior 6 or higher
365 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
366 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
369 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
371 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
373 // ----------------------------------------------------------------------------
374 // wxObjectRefData: ref counted data meant to be stored in wxObject
375 // ----------------------------------------------------------------------------
377 class WXDLLIMPEXP_BASE wxObjectRefData
379 friend class WXDLLIMPEXP_BASE wxObject
;
382 wxObjectRefData() : m_count(1) { }
384 int GetRefCount() const { return m_count
; }
386 void IncRef() { m_count
++; }
390 // this object should never be destroyed directly but only as a
391 // result of a DecRef() call:
392 virtual ~wxObjectRefData() { }
399 // ----------------------------------------------------------------------------
400 // wxObjectDataPtr: helper class to avoid memleaks because of missing calls
401 // to wxObjectRefData::DecRef
402 // ----------------------------------------------------------------------------
405 class wxObjectDataPtr
408 typedef T element_type
;
410 wxEXPLICIT
wxObjectDataPtr(T
*ptr
= NULL
) : m_ptr(ptr
) {}
413 wxObjectDataPtr(const wxObjectDataPtr
<T
> &tocopy
)
414 : m_ptr(tocopy
.m_ptr
)
426 T
*get() const { return m_ptr
; }
427 T
*operator->() const { return get(); }
436 wxObjectDataPtr
& operator=(const wxObjectDataPtr
&tocopy
)
440 m_ptr
= tocopy
.m_ptr
;
446 wxObjectDataPtr
& operator=(T
*ptr
)
460 // ----------------------------------------------------------------------------
461 // wxObject: the root class of wxWidgets object hierarchy
462 // ----------------------------------------------------------------------------
464 class WXDLLIMPEXP_BASE wxObject
466 DECLARE_ABSTRACT_CLASS(wxObject
)
469 wxObject() { m_refData
= NULL
; }
470 virtual ~wxObject() { UnRef(); }
472 wxObject(const wxObject
& other
)
474 m_refData
= other
.m_refData
;
476 m_refData
->m_count
++;
479 wxObject
& operator=(const wxObject
& other
)
481 if ( this != &other
)
488 bool IsKindOf(wxClassInfo
*info
) const;
491 // Turn on the correct set of new and delete operators
493 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
494 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
497 #ifdef _WX_WANT_DELETE_VOID
498 void operator delete ( void * buf
);
501 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
502 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
505 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
506 void operator delete ( void *buf
, const wxChar
*, int );
509 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
510 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
513 #ifdef _WX_WANT_ARRAY_DELETE_VOID
514 void operator delete[] ( void *buf
);
517 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
518 void operator delete[] (void* buf
, const wxChar
*, int );
521 // ref counted data handling methods
524 wxObjectRefData
*GetRefData() const { return m_refData
; }
525 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
527 // make a 'clone' of the object
528 void Ref(const wxObject
& clone
);
530 // destroy a reference
533 // Make sure this object has only one reference
534 void UnShare() { AllocExclusive(); }
536 // check if this object references the same data as the other one
537 bool IsSameAs(const wxObject
& o
) const { return m_refData
== o
.m_refData
; }
540 // ensure that our data is not shared with anybody else: if we have no
541 // data, it is created using CreateRefData() below, if we have shared data
542 // it is copied using CloneRefData(), otherwise nothing is done
543 void AllocExclusive();
545 // both methods must be implemented if AllocExclusive() is used, not pure
546 // virtual only because of the backwards compatibility reasons
548 // create a new m_refData
549 virtual wxObjectRefData
*CreateRefData() const;
551 // create a new m_refData initialized with the given one
552 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
554 wxObjectRefData
*m_refData
;
557 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
559 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
562 #if wxUSE_EXTENDED_RTTI
563 class WXDLLIMPEXP_BASE wxDynamicObject
: public wxObject
565 friend class WXDLLIMPEXP_BASE wxDynamicClassInfo
;
567 // instantiates this object with an instance of its superclass
568 wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
) ;
569 virtual ~wxDynamicObject();
571 void SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
);
572 wxxVariant
GetProperty (const wxChar
*propertyName
) const ;
574 // get the runtime identity of this object
575 wxClassInfo
*GetClassInfo() const
578 return (wxClassInfo
*) m_classInfo
;
580 wxDynamicClassInfo
*nonconst
= wx_const_cast(wxDynamicClassInfo
*, m_classInfo
);
581 return wx_static_cast(wxClassInfo
*, nonconst
);
585 wxObject
* GetSuperClassInstance() const
587 return m_superClassInstance
;
590 // removes an existing runtime-property
591 void RemoveProperty( const wxChar
*propertyName
) ;
593 // renames an existing runtime-property
594 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
) ;
596 wxObject
*m_superClassInstance
;
597 const wxDynamicClassInfo
*m_classInfo
;
598 struct wxDynamicObjectInternal
;
599 wxDynamicObjectInternal
*m_data
;
603 // ----------------------------------------------------------------------------
604 // more debugging macros
605 // ----------------------------------------------------------------------------
607 // Redefine new to be the debugging version. This doesn't work with all
608 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
609 // to use the debugging version.
612 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
614 #if wxUSE_DEBUG_NEW_ALWAYS
615 #if wxUSE_GLOBAL_MEMORY_OPERATORS
616 #define new WXDEBUG_NEW
617 #elif defined(__VISUALC__)
618 // Including this file redefines new and allows leak reports to
619 // contain line numbers
620 #include "wx/msw/msvcrt.h"
622 #endif // wxUSE_DEBUG_NEW_ALWAYS
623 #else // !__WXDEBUG__
624 #define WXDEBUG_NEW new
625 #endif // __WXDEBUG__/!__WXDEBUG__
627 #endif // _WX_OBJECTH__