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 // this shouldn't be needed any longer as <wx/msw/private.h> does it but it
38 // doesn't hurt neither
46 class WXDLLIMPEXP_BASE wxClassInfo
;
47 class WXDLLIMPEXP_BASE wxHashTable
;
48 class WXDLLIMPEXP_BASE wxObjectRefData
;
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 typedef wxObject
*(*wxObjectConstructorFn
)(void);
56 class WXDLLIMPEXP_BASE wxClassInfo
59 wxClassInfo( const wxChar
*className
,
60 const wxClassInfo
*baseInfo1
,
61 const wxClassInfo
*baseInfo2
,
63 wxObjectConstructorFn ctor
)
64 : m_className(className
)
66 , m_objectConstructor(ctor
)
67 , m_baseInfo1(baseInfo1
)
68 , m_baseInfo2(baseInfo2
)
77 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
79 const wxChar
*GetClassName() const { return m_className
; }
80 const wxChar
*GetBaseClassName1() const
81 { return m_baseInfo1
? m_baseInfo1
->GetClassName() : NULL
; }
82 const wxChar
*GetBaseClassName2() const
83 { return m_baseInfo2
? m_baseInfo2
->GetClassName() : NULL
; }
84 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
85 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
86 int GetSize() const { return m_objectSize
; }
88 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
89 static const wxClassInfo
*GetFirst() { return sm_first
; }
90 const wxClassInfo
*GetNext() const { return m_next
; }
91 static wxClassInfo
*FindClass(const wxChar
*className
);
93 // Climb upwards through inheritance hierarchy.
94 // Dual inheritance is catered for.
96 bool IsKindOf(const wxClassInfo
*info
) const
100 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
101 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
104 #if WXWIN_COMPATIBILITY_2_4
105 // Initializes parent pointers and hash table for fast searching.
106 wxDEPRECATED( static void InitializeClasses() );
107 // Cleans up hash table used for fast searching.
108 wxDEPRECATED( static void CleanUpClasses() );
110 static void CleanUp();
113 const wxChar
*m_className
;
115 wxObjectConstructorFn m_objectConstructor
;
117 // Pointers to base wxClassInfos: set in InitializeClasses
119 const wxClassInfo
*m_baseInfo1
;
120 const wxClassInfo
*m_baseInfo2
;
122 // class info object live in a linked list:
123 // pointers to its head and the next element in it
125 static wxClassInfo
*sm_first
;
128 // FIXME: this should be private (currently used directly by way too
130 static wxHashTable
*sm_classTable
;
133 // InitializeClasses() helper
134 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
136 DECLARE_NO_COPY_CLASS(wxClassInfo
)
139 // registers the class
144 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
146 #if WXWIN_COMPATIBILITY_2_4
147 inline void wxClassInfo::InitializeClasses() {}
148 inline void wxClassInfo::CleanUpClasses() {}
151 // ----------------------------------------------------------------------------
152 // Dynamic class macros
153 // ----------------------------------------------------------------------------
155 #define DECLARE_ABSTRACT_CLASS(name) \
157 static wxClassInfo ms_classInfo; \
158 virtual wxClassInfo *GetClassInfo() const;
160 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
161 DECLARE_NO_ASSIGN_CLASS(name) \
162 DECLARE_DYNAMIC_CLASS(name)
164 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
165 DECLARE_NO_COPY_CLASS(name) \
166 DECLARE_DYNAMIC_CLASS(name)
168 #define DECLARE_DYNAMIC_CLASS(name) \
169 DECLARE_ABSTRACT_CLASS(name) \
170 static wxObject* wxCreateObject();
172 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
175 // common part of the macros below
176 #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
177 wxClassInfo name::ms_classInfo(wxT(#name), \
178 &basename::ms_classInfo, \
180 (int) sizeof(name), \
181 (wxObjectConstructorFn) func); \
183 wxClassInfo *name::GetClassInfo() const \
184 { return &name::ms_classInfo; }
186 #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
187 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
189 #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
190 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func)
192 // -----------------------------------
193 // for concrete classes
194 // -----------------------------------
196 // Single inheritance with one base class
197 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
198 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
199 wxObject* name::wxCreateObject() \
202 // Multiple inheritance with two base classes
203 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
204 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
205 name::wxCreateObject) \
206 wxObject* name::wxCreateObject() \
209 // -----------------------------------
210 // for abstract classes
211 // -----------------------------------
213 // Single inheritance with one base class
215 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
216 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
218 // Multiple inheritance with two base classes
220 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
221 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
223 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
224 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
226 #endif // !wxUSE_EXTENDED_RTTI
229 // -----------------------------------
230 // for pluggable classes
231 // -----------------------------------
233 // NOTE: this should probably be the very first statement
234 // in the class declaration so wxPluginSentinel is
235 // the first member initialised and the last destroyed.
237 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
239 #if wxUSE_NESTED_CLASSES
241 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
242 class exportdecl name##PluginSentinel { \
244 static const wxString sm_className; \
246 name##PluginSentinel(); \
247 ~name##PluginSentinel(); \
249 name##PluginSentinel m_pluginsentinel;
251 #define _IMPLEMENT_DL_SENTINEL(name) \
252 const wxString name::name##PluginSentinel::sm_className(#name); \
253 name::name##PluginSentinel::name##PluginSentinel() { \
254 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
255 if( e != 0 ) { e->RefObj(); } \
257 name::name##PluginSentinel::~name##PluginSentinel() { \
258 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
259 if( e != 0 ) { e->UnrefObj(); } \
263 #define _DECLARE_DL_SENTINEL(name)
264 #define _IMPLEMENT_DL_SENTINEL(name)
266 #endif // wxUSE_NESTED_CLASSES
268 #define DECLARE_PLUGGABLE_CLASS(name) \
269 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
270 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
271 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
273 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
274 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
275 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
276 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
278 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
279 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
280 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
281 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
282 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
283 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
284 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
285 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
287 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
288 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
289 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
290 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
291 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
292 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
293 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
294 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
296 #define CLASSINFO(name) (&name::ms_classInfo)
298 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
300 // Just seems a bit nicer-looking (pretend it's not a macro)
301 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
303 // this cast does some more checks at compile time as it uses static_cast
306 // note that it still has different semantics from dynamic_cast<> and so can't
307 // be replaced by it as long as there are any compilers not supporting it
308 #define wxDynamicCast(obj, className) \
309 ((className *) wxCheckDynamicCast( \
310 wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \
311 wx_const_cast(className *, wx_static_cast(const className *, obj)))), \
312 &className::ms_classInfo))
314 // The 'this' pointer is always true, so use this version
315 // to cast the this pointer and avoid compiler warnings.
316 #define wxDynamicCastThis(className) \
317 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
320 inline void* wxCheckCast(void *ptr
)
322 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
325 #define wxStaticCast(obj, className) \
326 ((className *)wxCheckCast(wxDynamicCast(obj, className)))
328 #else // !__WXDEBUG__
329 #define wxStaticCast(obj, className) \
330 wx_const_cast(className *, wx_static_cast(const className *, obj))
332 #endif // __WXDEBUG__
334 // ----------------------------------------------------------------------------
335 // set up memory debugging macros
336 // ----------------------------------------------------------------------------
339 Which new/delete operator variants do we want?
341 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
342 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
343 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
344 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
345 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
346 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
347 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
350 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
352 // All compilers get this one
353 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
355 // Everyone except Visage gets the next one
356 #ifndef __VISAGECPP__
357 #define _WX_WANT_DELETE_VOID
360 // Only visage gets this one under the correct circumstances
361 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
362 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
365 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
366 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
367 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
370 // Now see who (if anyone) gets the array memory operators
371 #if wxUSE_ARRAY_MEMORY_OPERATORS
373 // Everyone except Visual C++ (cause problems for VC++ - crashes)
374 #if !defined(__VISUALC__)
375 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
378 // Everyone except Visual C++ (cause problems for VC++ - crashes)
379 #if !defined(__VISUALC__)
380 #define _WX_WANT_ARRAY_DELETE_VOID
383 // Only CodeWarrior 6 or higher
384 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
385 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
388 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
390 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
392 // ----------------------------------------------------------------------------
393 // wxObject: the root class of wxWidgets object hierarchy
394 // ----------------------------------------------------------------------------
396 class WXDLLIMPEXP_BASE wxObject
398 DECLARE_ABSTRACT_CLASS(wxObject
)
401 void InitFrom(const wxObject
& other
);
404 wxObject() { m_refData
= NULL
; }
405 virtual ~wxObject() { UnRef(); }
407 wxObject(const wxObject
& other
)
412 wxObject
& operator=(const wxObject
& other
)
414 if ( this != &other
)
422 bool IsKindOf(wxClassInfo
*info
) const;
425 // Turn on the correct set of new and delete operators
427 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
428 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
431 #ifdef _WX_WANT_DELETE_VOID
432 void operator delete ( void * buf
);
435 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
436 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
439 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
440 void operator delete ( void *buf
, const wxChar
*, int );
443 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
444 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
447 #ifdef _WX_WANT_ARRAY_DELETE_VOID
448 void operator delete[] ( void *buf
);
451 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
452 void operator delete[] (void* buf
, const wxChar
*, int );
455 // ref counted data handling methods
458 wxObjectRefData
*GetRefData() const { return m_refData
; }
459 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
461 // make a 'clone' of the object
462 void Ref(const wxObject
& clone
);
464 // destroy a reference
468 // Reserved for future use
469 virtual void ReservedObjectFunc1() {}
470 virtual void ReservedObjectFunc2() {}
471 virtual void ReservedObjectFunc3() {}
472 virtual void ReservedObjectFunc4() {}
473 virtual void ReservedObjectFunc5() {}
474 virtual void ReservedObjectFunc6() {}
475 virtual void ReservedObjectFunc7() {}
476 virtual void ReservedObjectFunc8() {}
477 virtual void ReservedObjectFunc9() {}
480 // ensure that our data is not shared with anybody else: if we have no
481 // data, it is created using CreateRefData() below, if we have shared data
482 // it is copied using CloneRefData(), otherwise nothing is done
483 void AllocExclusive();
485 // both methods must be implemented if Unshare() is used, not pure virtual
486 // only because of the backwards compatibility reasons
488 // create a new m_refData
489 virtual wxObjectRefData
*CreateRefData() const;
491 // create a new m_refData initialized with the given one
492 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
494 wxObjectRefData
*m_refData
;
497 // ----------------------------------------------------------------------------
498 // wxObjectRefData: ref counted data meant to be stored in wxObject
499 // ----------------------------------------------------------------------------
501 class WXDLLIMPEXP_BASE wxObjectRefData
503 friend class WXDLLIMPEXP_BASE wxObject
;
506 wxObjectRefData() : m_count(1) { }
507 virtual ~wxObjectRefData() { }
509 int GetRefCount() const { return m_count
; }
516 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
518 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
521 #if wxUSE_EXTENDED_RTTI
522 class WXDLLIMPEXP_BASE wxDynamicObject
: public wxObject
524 friend class WXDLLIMPEXP_BASE wxDynamicClassInfo
;
526 // instantiates this object with an instance of its superclass
527 wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
) ;
530 void SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
);
531 wxxVariant
GetProperty (const wxChar
*propertyName
) const ;
533 // get the runtime identity of this object
534 wxClassInfo
*GetClassInfo() const
537 return (wxClassInfo
*) m_classInfo
;
539 return wx_const_cast(wxClassInfo
*, m_classInfo
);
543 wxObject
* GetSuperClassInstance() const
545 return m_superClassInstance
;
548 // removes an existing runtime-property
549 void RemoveProperty( const wxChar
*propertyName
) ;
551 // renames an existing runtime-property
552 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
) ;
554 wxObject
*m_superClassInstance
;
555 const wxDynamicClassInfo
*m_classInfo
;
556 struct wxDynamicObjectInternal
;
557 wxDynamicObjectInternal
*m_data
;
561 // ----------------------------------------------------------------------------
562 // more debugging macros
563 // ----------------------------------------------------------------------------
567 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
569 #else // !__WXDEBUG__
570 #define WXDEBUG_NEW new
573 // Redefine new to be the debugging version. This doesn't work with all
574 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
575 // to use the debugging version.
577 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
578 #define new new(__TFILE__,__LINE__)
579 #elif (defined(__WXDEBUG__) && defined(__VISUALC__) && !wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS)
580 // Including this file redefines new and allows leak reports to contain line numbers
581 #include "wx/msw/msvcrt.h"
584 #endif // _WX_OBJECTH__