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 // ----------------------------------------------------------------------------
21 #include "wx/memory.h"
23 class WXDLLIMPEXP_BASE wxObject
;
25 #ifndef wxUSE_EXTENDED_RTTI
26 #define wxUSE_EXTENDED_RTTI 0
29 #if wxUSE_EXTENDED_RTTI
33 // ----------------------------------------------------------------------------
34 // conditional compilation
35 // ----------------------------------------------------------------------------
37 class WXDLLIMPEXP_BASE wxClassInfo
;
38 class WXDLLIMPEXP_BASE wxHashTable
;
39 class WXDLLIMPEXP_BASE wxObjectRefData
;
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 typedef wxObject
*(*wxObjectConstructorFn
)(void);
47 class WXDLLIMPEXP_BASE wxClassInfo
50 wxClassInfo( const wxChar
*className
,
51 const wxClassInfo
*baseInfo1
,
52 const wxClassInfo
*baseInfo2
,
54 wxObjectConstructorFn ctor
)
55 : m_className(className
)
57 , m_objectConstructor(ctor
)
58 , m_baseInfo1(baseInfo1
)
59 , m_baseInfo2(baseInfo2
)
68 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
70 const wxChar
*GetClassName() const { return m_className
; }
71 const wxChar
*GetBaseClassName1() const
72 { return m_baseInfo1
? m_baseInfo1
->GetClassName() : NULL
; }
73 const wxChar
*GetBaseClassName2() const
74 { return m_baseInfo2
? m_baseInfo2
->GetClassName() : NULL
; }
75 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
76 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
77 int GetSize() const { return m_objectSize
; }
79 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
80 static const wxClassInfo
*GetFirst() { return sm_first
; }
81 const wxClassInfo
*GetNext() const { return m_next
; }
82 static wxClassInfo
*FindClass(const wxChar
*className
);
84 // Climb upwards through inheritance hierarchy.
85 // Dual inheritance is catered for.
87 bool IsKindOf(const wxClassInfo
*info
) const
91 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
92 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
95 #if WXWIN_COMPATIBILITY_2_4
96 // Initializes parent pointers and hash table for fast searching.
97 wxDEPRECATED( static void InitializeClasses() );
98 // Cleans up hash table used for fast searching.
99 wxDEPRECATED( static void CleanUpClasses() );
103 const wxChar
*m_className
;
105 wxObjectConstructorFn m_objectConstructor
;
107 // Pointers to base wxClassInfos: set in InitializeClasses
109 const wxClassInfo
*m_baseInfo1
;
110 const wxClassInfo
*m_baseInfo2
;
112 // class info object live in a linked list:
113 // pointers to its head and the next element in it
115 static wxClassInfo
*sm_first
;
118 // FIXME: this should be private (currently used directly by way too
120 static wxHashTable
*sm_classTable
;
123 // InitializeClasses() helper
124 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
126 DECLARE_NO_COPY_CLASS(wxClassInfo
)
129 // registers the class
134 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
136 #if WXWIN_COMPATIBILITY_2_4
137 inline void wxClassInfo::InitializeClasses() {}
138 inline void wxClassInfo::CleanUpClasses() {}
141 // ----------------------------------------------------------------------------
142 // Dynamic class macros
143 // ----------------------------------------------------------------------------
145 #define DECLARE_ABSTRACT_CLASS(name) \
147 static wxClassInfo ms_classInfo; \
148 virtual wxClassInfo *GetClassInfo() const;
150 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
151 DECLARE_NO_ASSIGN_CLASS(name) \
152 DECLARE_DYNAMIC_CLASS(name)
154 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
155 DECLARE_NO_COPY_CLASS(name) \
156 DECLARE_DYNAMIC_CLASS(name)
158 #define DECLARE_DYNAMIC_CLASS(name) \
159 DECLARE_ABSTRACT_CLASS(name) \
160 static wxObject* wxCreateObject();
162 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
165 // common part of the macros below
166 #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
167 wxClassInfo name::ms_classInfo(wxT(#name), \
168 &basename::ms_classInfo, \
170 (int) sizeof(name), \
171 (wxObjectConstructorFn) func); \
173 wxClassInfo *name::GetClassInfo() const \
174 { return &name::ms_classInfo; }
176 #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
177 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
179 #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
180 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func)
182 // -----------------------------------
183 // for concrete classes
184 // -----------------------------------
186 // Single inheritance with one base class
187 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
188 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
189 wxObject* name::wxCreateObject() \
192 // Multiple inheritance with two base classes
193 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
194 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
195 name::wxCreateObject) \
196 wxObject* name::wxCreateObject() \
199 // -----------------------------------
200 // for abstract classes
201 // -----------------------------------
203 // Single inheritance with one base class
205 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
206 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
208 // Multiple inheritance with two base classes
210 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
211 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
213 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
214 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
216 #endif // !wxUSE_EXTENDED_RTTI
219 // -----------------------------------
220 // for pluggable classes
221 // -----------------------------------
223 // NOTE: this should probably be the very first statement
224 // in the class declaration so wxPluginSentinel is
225 // the first member initialised and the last destroyed.
227 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
229 #if wxUSE_NESTED_CLASSES
231 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
232 class exportdecl name##PluginSentinel { \
234 static const wxString sm_className; \
236 name##PluginSentinel(); \
237 ~name##PluginSentinel(); \
239 name##PluginSentinel m_pluginsentinel;
241 #define _IMPLEMENT_DL_SENTINEL(name) \
242 const wxString name::name##PluginSentinel::sm_className(#name); \
243 name::name##PluginSentinel::name##PluginSentinel() { \
244 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
245 if( e != 0 ) { e->RefObj(); } \
247 name::name##PluginSentinel::~name##PluginSentinel() { \
248 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
249 if( e != 0 ) { e->UnrefObj(); } \
253 #define _DECLARE_DL_SENTINEL(name)
254 #define _IMPLEMENT_DL_SENTINEL(name)
256 #endif // wxUSE_NESTED_CLASSES
258 #define DECLARE_PLUGGABLE_CLASS(name) \
259 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
260 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
261 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
263 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
264 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
265 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
266 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
268 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
269 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
270 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
271 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
272 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
273 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
274 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
275 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
277 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
278 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
279 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
280 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
281 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
282 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
283 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
284 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
286 #define CLASSINFO(name) (&name::ms_classInfo)
288 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
290 // Just seems a bit nicer-looking (pretend it's not a macro)
291 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
293 // this cast does some more checks at compile time as it uses static_cast
296 // note that it still has different semantics from dynamic_cast<> and so can't
297 // be replaced by it as long as there are any compilers not supporting it
298 #define wxDynamicCast(obj, className) \
299 ((className *) wxCheckDynamicCast( \
300 wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \
301 wx_const_cast(className *, wx_static_cast(const className *, obj)))), \
302 &className::ms_classInfo))
304 // The 'this' pointer is always true, so use this version
305 // to cast the this pointer and avoid compiler warnings.
306 #define wxDynamicCastThis(className) \
307 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
310 inline void* wxCheckCast(void *ptr
)
312 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
315 #define wxStaticCast(obj, className) \
316 ((className *)wxCheckCast(wxDynamicCast(obj, className)))
318 #else // !__WXDEBUG__
319 #define wxStaticCast(obj, className) \
320 wx_const_cast(className *, wx_static_cast(const className *, obj))
322 #endif // __WXDEBUG__
324 // ----------------------------------------------------------------------------
325 // set up memory debugging macros
326 // ----------------------------------------------------------------------------
329 Which new/delete operator variants do we want?
331 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
332 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
333 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
334 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
335 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
336 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
337 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
340 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
342 // All compilers get this one
343 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
345 // Everyone except Visage gets the next one
346 #ifndef __VISAGECPP__
347 #define _WX_WANT_DELETE_VOID
350 // Only visage gets this one under the correct circumstances
351 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
352 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
355 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
356 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
357 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
360 // Now see who (if anyone) gets the array memory operators
361 #if wxUSE_ARRAY_MEMORY_OPERATORS
363 // Everyone except Visual C++ (cause problems for VC++ - crashes)
364 #if !defined(__VISUALC__)
365 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
368 // Everyone except Visual C++ (cause problems for VC++ - crashes)
369 #if !defined(__VISUALC__)
370 #define _WX_WANT_ARRAY_DELETE_VOID
373 // Only CodeWarrior 6 or higher
374 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
375 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
378 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
380 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
382 // ----------------------------------------------------------------------------
383 // wxObjectRefData: ref counted data meant to be stored in wxObject
384 // ----------------------------------------------------------------------------
386 class WXDLLIMPEXP_BASE wxObjectRefData
388 friend class WXDLLIMPEXP_BASE wxObject
;
391 wxObjectRefData() : m_count(1) { }
392 virtual ~wxObjectRefData() { }
394 int GetRefCount() const { return m_count
; }
400 // ----------------------------------------------------------------------------
401 // wxObject: the root class of wxWidgets object hierarchy
402 // ----------------------------------------------------------------------------
404 class WXDLLIMPEXP_BASE wxObject
406 DECLARE_ABSTRACT_CLASS(wxObject
)
409 wxObject() { m_refData
= NULL
; }
410 virtual ~wxObject() { UnRef(); }
412 wxObject(const wxObject
& other
)
414 m_refData
= other
.m_refData
;
416 m_refData
->m_count
++;
419 wxObject
& operator=(const wxObject
& other
)
421 if ( this != &other
)
428 bool IsKindOf(wxClassInfo
*info
) const;
431 // Turn on the correct set of new and delete operators
433 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
434 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
437 #ifdef _WX_WANT_DELETE_VOID
438 void operator delete ( void * buf
);
441 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
442 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
445 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
446 void operator delete ( void *buf
, const wxChar
*, int );
449 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
450 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
453 #ifdef _WX_WANT_ARRAY_DELETE_VOID
454 void operator delete[] ( void *buf
);
457 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
458 void operator delete[] (void* buf
, const wxChar
*, int );
461 // ref counted data handling methods
464 wxObjectRefData
*GetRefData() const { return m_refData
; }
465 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
467 // make a 'clone' of the object
468 void Ref(const wxObject
& clone
);
470 // destroy a reference
474 // ensure that our data is not shared with anybody else: if we have no
475 // data, it is created using CreateRefData() below, if we have shared data
476 // it is copied using CloneRefData(), otherwise nothing is done
477 void AllocExclusive();
479 // both methods must be implemented if Unshare() is used, not pure virtual
480 // only because of the backwards compatibility reasons
482 // create a new m_refData
483 virtual wxObjectRefData
*CreateRefData() const;
485 // create a new m_refData initialized with the given one
486 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
488 wxObjectRefData
*m_refData
;
491 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
493 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
496 #if wxUSE_EXTENDED_RTTI
497 class WXDLLIMPEXP_BASE wxDynamicObject
: public wxObject
499 friend class WXDLLIMPEXP_BASE wxDynamicClassInfo
;
501 // instantiates this object with an instance of its superclass
502 wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
) ;
505 void SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
);
506 wxxVariant
GetProperty (const wxChar
*propertyName
) const ;
508 // get the runtime identity of this object
509 wxClassInfo
*GetClassInfo() const
512 return (wxClassInfo
*) m_classInfo
;
514 return wx_const_cast(wxClassInfo
*, m_classInfo
);
518 wxObject
* GetSuperClassInstance() const
520 return m_superClassInstance
;
523 // removes an existing runtime-property
524 void RemoveProperty( const wxChar
*propertyName
) ;
526 // renames an existing runtime-property
527 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
) ;
529 wxObject
*m_superClassInstance
;
530 const wxDynamicClassInfo
*m_classInfo
;
531 struct wxDynamicObjectInternal
;
532 wxDynamicObjectInternal
*m_data
;
536 // ----------------------------------------------------------------------------
537 // more debugging macros
538 // ----------------------------------------------------------------------------
542 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
544 #else // !__WXDEBUG__
545 #define WXDEBUG_NEW new
548 // Redefine new to be the debugging version. This doesn't work with all
549 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
550 // to use the debugging version.
552 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
553 #define new new(__TFILE__,__LINE__)
554 #elif (defined(__WXDEBUG__) && defined(__VISUALC__) && !wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS)
555 // Including this file redefines new and allows leak reports to contain line numbers
556 #include "wx/msw/msvcrt.h"
559 #endif // _WX_OBJECTH__