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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma interface "object.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
25 #include "wx/memory.h"
27 class WXDLLIMPEXP_BASE wxObject
;
29 // FIXME: remove in wx-2.7:
30 class WXDLLIMPEXP_BASE wxEvent
;
31 class WXDLLIMPEXP_BASE wxEvtHandler
;
33 #ifndef wxUSE_EXTENDED_RTTI
34 #define wxUSE_EXTENDED_RTTI 0
37 #if wxUSE_EXTENDED_RTTI
41 // ----------------------------------------------------------------------------
42 // conditional compilation
43 // ----------------------------------------------------------------------------
45 // this shouldn't be needed any longer as <wx/msw/private.h> does it but it
46 // doesn't hurt neither
54 class WXDLLIMPEXP_BASE wxClassInfo
;
55 class WXDLLIMPEXP_BASE wxHashTable
;
56 class WXDLLIMPEXP_BASE wxObjectRefData
;
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 typedef wxObject
*(*wxObjectConstructorFn
)(void);
64 class WXDLLIMPEXP_BASE wxClassInfo
67 wxClassInfo( const wxChar
*className
,
68 const wxClassInfo
*baseInfo1
,
69 const wxClassInfo
*baseInfo2
,
71 wxObjectConstructorFn ctor
)
72 : m_className(className
)
74 , m_objectConstructor(ctor
)
75 , m_baseInfo1(baseInfo1
)
76 , m_baseInfo2(baseInfo2
)
85 wxObject
*CreateObject() { return m_objectConstructor ?
(*m_objectConstructor
)() : 0; }
87 const wxChar
*GetClassName() const { return m_className
; }
88 const wxChar
*GetBaseClassName1() const
89 { return m_baseInfo1 ? m_baseInfo1
->GetClassName() : NULL
; }
90 const wxChar
*GetBaseClassName2() const
91 { return m_baseInfo2 ? m_baseInfo2
->GetClassName() : NULL
; }
92 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
93 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
94 int GetSize() const { return m_objectSize
; }
96 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
97 static const wxClassInfo
*GetFirst() { return sm_first
; }
98 const wxClassInfo
*GetNext() const { return m_next
; }
99 static wxClassInfo
*FindClass(const wxChar
*className
);
101 // Climb upwards through inheritance hierarchy.
102 // Dual inheritance is catered for.
104 bool IsKindOf(const wxClassInfo
*info
) const
108 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
109 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
112 #if WXWIN_COMPATIBILITY_2_4
113 // Initializes parent pointers and hash table for fast searching.
114 wxDEPRECATED( static void InitializeClasses() );
115 // Cleans up hash table used for fast searching.
116 wxDEPRECATED( static void CleanUpClasses() );
118 static void CleanUp();
121 const wxChar
*m_className
;
123 wxObjectConstructorFn m_objectConstructor
;
125 // Pointers to base wxClassInfos: set in InitializeClasses
127 const wxClassInfo
*m_baseInfo1
;
128 const wxClassInfo
*m_baseInfo2
;
130 // class info object live in a linked list:
131 // pointers to its head and the next element in it
133 static wxClassInfo
*sm_first
;
136 // FIXME: this should be private (currently used directly by way too
138 static wxHashTable
*sm_classTable
;
141 // InitializeClasses() helper
142 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
144 DECLARE_NO_COPY_CLASS(wxClassInfo
)
147 // registers the class
152 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
154 #if WXWIN_COMPATIBILITY_2_4
155 inline void wxClassInfo
::InitializeClasses() {}
156 inline void wxClassInfo
::CleanUpClasses() {}
159 // ----------------------------------------------------------------------------
160 // Dynamic class macros
161 // ----------------------------------------------------------------------------
163 #define DECLARE_ABSTRACT_CLASS(name) \
165 static wxClassInfo ms_classInfo; \
166 virtual wxClassInfo *GetClassInfo() const;
168 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
169 DECLARE_NO_ASSIGN_CLASS(name) \
170 DECLARE_DYNAMIC_CLASS(name)
172 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
173 DECLARE_NO_COPY_CLASS(name) \
174 DECLARE_DYNAMIC_CLASS(name)
176 #define DECLARE_DYNAMIC_CLASS(name) \
177 DECLARE_ABSTRACT_CLASS(name) \
178 static wxObject* wxCreateObject();
180 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
183 // common part of the macros below
184 #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
185 wxClassInfo name::ms_classInfo(wxT(#name), \
186 &basename::ms_classInfo, \
188 (int) sizeof(name), \
189 (wxObjectConstructorFn) func); \
191 wxClassInfo *name::GetClassInfo() const \
192 { return &name::ms_classInfo; }
194 #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
195 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
197 #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
198 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func)
200 // -----------------------------------
201 // for concrete classes
202 // -----------------------------------
204 // Single inheritance with one base class
205 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
206 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
207 wxObject* name::wxCreateObject() \
210 // Multiple inheritance with two base classes
211 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
212 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
213 name::wxCreateObject) \
214 wxObject* name::wxCreateObject() \
217 // -----------------------------------
218 // for abstract classes
219 // -----------------------------------
221 // Single inheritance with one base class
223 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
224 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
226 // Multiple inheritance with two base classes
228 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
229 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
231 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
232 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
234 #endif // !wxUSE_EXTENDED_RTTI
237 // -----------------------------------
238 // for pluggable classes
239 // -----------------------------------
241 // NOTE: this should probably be the very first statement
242 // in the class declaration so wxPluginSentinel is
243 // the first member initialised and the last destroyed.
245 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
247 #if wxUSE_NESTED_CLASSES
249 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
250 class exportdecl name##PluginSentinel { \
252 static const wxString sm_className; \
254 name##PluginSentinel(); \
255 ~name##PluginSentinel(); \
257 name##PluginSentinel m_pluginsentinel;
259 #define _IMPLEMENT_DL_SENTINEL(name) \
260 const wxString name::name##PluginSentinel::sm_className(#name); \
261 name::name##PluginSentinel::name##PluginSentinel() { \
262 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
263 if( e != 0 ) { e->RefObj(); } \
265 name::name##PluginSentinel::~name##PluginSentinel() { \
266 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
267 if( e != 0 ) { e->UnrefObj(); } \
271 #define _DECLARE_DL_SENTINEL(name)
272 #define _IMPLEMENT_DL_SENTINEL(name)
274 #endif // wxUSE_NESTED_CLASSES
276 #define DECLARE_PLUGGABLE_CLASS(name) \
277 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
278 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
279 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
281 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
282 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
283 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
284 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
286 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
287 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
288 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
289 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
290 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
291 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
292 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
293 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
295 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
296 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
297 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
298 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
299 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
300 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
301 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
302 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
304 #define CLASSINFO(name) (&name::ms_classInfo)
306 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
308 // Just seems a bit nicer-looking (pretend it's not a macro)
309 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
311 // this cast does some more checks at compile time as it uses static_cast
314 // note that it still has different semantics from dynamic_cast<> and so can't
315 // be replaced by it as long as there are any compilers not supporting it
316 #define wxDynamicCast(obj, className) \
317 ((className *) wxCheckDynamicCast( \
318 wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \
319 wx_const_cast(className *, wx_static_cast(const className *, obj)))), \
320 &className::ms_classInfo))
322 // The 'this' pointer is always true, so use this version
323 // to cast the this pointer and avoid compiler warnings.
324 #define wxDynamicCastThis(className) \
325 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
328 inline void* wxCheckCast(void *ptr
)
330 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
333 #define wxStaticCast(obj, className) \
334 ((className *)wxCheckCast(wxDynamicCast(obj, className)))
336 #else // !__WXDEBUG__
337 #define wxStaticCast(obj, className) \
338 wx_const_cast(className *, wx_static_cast(const className *, obj))
340 #endif // __WXDEBUG__
342 // ----------------------------------------------------------------------------
343 // set up memory debugging macros
344 // ----------------------------------------------------------------------------
347 Which new/delete operator variants do we want?
349 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
350 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
351 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
352 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
353 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
354 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
355 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
358 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
360 // All compilers get this one
361 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
363 // Everyone except Visage gets the next one
364 #ifndef __VISAGECPP__
365 #define _WX_WANT_DELETE_VOID
368 // Only visage gets this one under the correct circumstances
369 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
370 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
373 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
374 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
375 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
378 // Now see who (if anyone) gets the array memory operators
379 #if wxUSE_ARRAY_MEMORY_OPERATORS
381 // Everyone except Visual C++ (cause problems for VC++ - crashes)
382 #if !defined(__VISUALC__)
383 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
386 // Everyone except Visual C++ (cause problems for VC++ - crashes)
387 #if !defined(__VISUALC__)
388 #define _WX_WANT_ARRAY_DELETE_VOID
391 // Only CodeWarrior 6 or higher
392 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
393 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
396 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
398 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
400 // ----------------------------------------------------------------------------
401 // wxObject: the root class of wxWidgets object hierarchy
402 // ----------------------------------------------------------------------------
404 class WXDLLIMPEXP_BASE wxObject
406 DECLARE_ABSTRACT_CLASS(wxObject
)
409 void InitFrom(const wxObject
& other
);
412 wxObject() { m_refData
= NULL
; }
413 virtual ~wxObject() { UnRef(); }
415 wxObject(const wxObject
& other
)
420 wxObject
& operator=(const wxObject
& other
)
422 if ( this != &other
)
430 bool IsKindOf(wxClassInfo
*info
) const;
433 // Turn on the correct set of new and delete operators
435 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
436 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
439 #ifdef _WX_WANT_DELETE_VOID
440 void operator delete ( void * buf
);
443 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
444 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
447 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
448 void operator delete ( void *buf
, const wxChar
*, int );
451 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
452 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
455 #ifdef _WX_WANT_ARRAY_DELETE_VOID
456 void operator delete[] ( void *buf
);
459 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
460 void operator delete[] (void* buf
, const wxChar
*, int );
463 // ref counted data handling methods
466 wxObjectRefData
*GetRefData() const { return m_refData
; }
467 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
469 // make a 'clone' of the object
470 void Ref(const wxObject
& clone
);
472 // destroy a reference
476 // Reserved for future use
477 virtual void ReservedObjectFunc1() {}
478 virtual void ReservedObjectFunc2() {}
479 virtual void ReservedObjectFunc3() {}
480 virtual void ReservedObjectFunc4() {}
481 virtual void ReservedObjectFunc5() {}
482 virtual void ReservedObjectFunc6() {}
483 virtual void ReservedObjectFunc7() {}
484 virtual void ReservedObjectFunc8() {}
485 // FIXME: turn back into ReservedObjectFunc9() in wx-2.7 (see also FIXME
486 // near the top of this file)
487 virtual void DoHandleEvent(void (wxEvtHandler
::*)(wxEvent
&), wxEvent
&) {}
490 // ensure that our data is not shared with anybody else: if we have no
491 // data, it is created using CreateRefData() below, if we have shared data
492 // it is copied using CloneRefData(), otherwise nothing is done
493 void AllocExclusive();
495 // both methods must be implemented if Unshare() is used, not pure virtual
496 // only because of the backwards compatibility reasons
498 // create a new m_refData
499 virtual wxObjectRefData
*CreateRefData() const;
501 // create a new m_refData initialized with the given one
502 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
504 wxObjectRefData
*m_refData
;
507 // ----------------------------------------------------------------------------
508 // wxObjectRefData: ref counted data meant to be stored in wxObject
509 // ----------------------------------------------------------------------------
511 class WXDLLIMPEXP_BASE wxObjectRefData
513 friend class WXDLLIMPEXP_BASE wxObject
;
516 wxObjectRefData() : m_count(1) { }
517 virtual ~wxObjectRefData() { }
519 int GetRefCount() const { return m_count
; }
526 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
528 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
531 #if wxUSE_EXTENDED_RTTI
532 class WXDLLIMPEXP_BASE wxDynamicObject
: public wxObject
534 friend class WXDLLIMPEXP_BASE wxDynamicClassInfo
;
536 // instantiates this object with an instance of its superclass
537 wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
) ;
540 void SetProperty (const wxChar
*propertyName
, const wxxVariant
&value
);
541 wxxVariant
GetProperty (const wxChar
*propertyName
) const ;
543 // get the runtime identity of this object
544 wxClassInfo
*GetClassInfo() const
547 return (wxClassInfo
*) m_classInfo
;
549 return wx_const_cast(wxClassInfo
*, m_classInfo
);
553 wxObject
* GetSuperClassInstance() const
555 return m_superClassInstance
;
558 // removes an existing runtime-property
559 void RemoveProperty( const wxChar
*propertyName
) ;
561 // renames an existing runtime-property
562 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
) ;
564 wxObject
*m_superClassInstance
;
565 const wxDynamicClassInfo
*m_classInfo
;
566 struct wxDynamicObjectInternal
;
567 wxDynamicObjectInternal
*m_data
;
571 // ----------------------------------------------------------------------------
572 // more debugging macros
573 // ----------------------------------------------------------------------------
577 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
579 #else // !__WXDEBUG__
580 #define WXDEBUG_NEW new
583 // Redefine new to be the debugging version. This doesn't work with all
584 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
585 // to use the debugging version.
587 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
588 #define new new(__TFILE__,__LINE__)
589 #elif (defined(__WXDEBUG__) && defined(__VISUALC__) && !wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS)
590 // Including this file redefines new and allows leak reports to contain line numbers
591 #include "wx/msw/msvcrt.h"
594 #endif // _WX_OBJECTH__