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 #if wxUSE_DYNAMIC_CLASSES
31 #ifndef wxUSE_EXTENDED_RTTI
32 #define wxUSE_EXTENDED_RTTI 0
35 #if wxUSE_EXTENDED_RTTI
39 // ----------------------------------------------------------------------------
40 // conditional compilation
41 // ----------------------------------------------------------------------------
43 // this shouldn't be needed any longer as <wx/msw/private.h> does it but it
44 // doesn't hurt neither
52 class WXDLLIMPEXP_BASE wxClassInfo
;
53 class WXDLLIMPEXP_BASE wxHashTable
;
54 class WXDLLIMPEXP_BASE wxObjectRefData
;
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 typedef wxObject
*(*wxObjectConstructorFn
)(void);
62 class WXDLLIMPEXP_BASE wxClassInfo
65 wxClassInfo( const wxChar
*className
,
66 const wxClassInfo
*baseInfo1
,
67 const wxClassInfo
*baseInfo2
,
69 wxObjectConstructorFn ctor
)
70 : m_className(className
)
72 , m_objectConstructor(ctor
)
73 , m_baseInfo1(baseInfo1
)
74 , m_baseInfo2(baseInfo2
)
83 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
85 const wxChar
*GetClassName() const { return m_className
; }
86 const wxChar
*GetBaseClassName1() const { return m_baseInfo1
->GetClassName(); }
87 const wxChar
*GetBaseClassName2() const { return m_baseInfo2
->GetClassName(); }
88 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
89 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
90 int GetSize() const { return m_objectSize
; }
92 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
93 static const wxClassInfo
*GetFirst() { return sm_first
; }
94 const wxClassInfo
*GetNext() const { return m_next
; }
95 static wxClassInfo
*FindClass(const wxChar
*className
);
97 // Climb upwards through inheritance hierarchy.
98 // Dual inheritance is catered for.
100 bool IsKindOf(const wxClassInfo
*info
) const
104 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
105 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
108 #ifdef WXWIN_COMPATIBILITY_2_4
109 // Initializes parent pointers and hash table for fast searching.
110 wxDEPRECATED( static void InitializeClasses() );
111 // Cleans up hash table used for fast searching.
112 wxDEPRECATED( static void CleanUpClasses() );
114 static void CleanUp();
117 const wxChar
*m_className
;
119 wxObjectConstructorFn m_objectConstructor
;
121 // Pointers to base wxClassInfos: set in InitializeClasses
123 const wxClassInfo
*m_baseInfo1
;
124 const wxClassInfo
*m_baseInfo2
;
126 // class info object live in a linked list:
127 // pointers to its head and the next element in it
129 static wxClassInfo
*sm_first
;
132 // FIXME: this should be private (currently used directly by way too
134 static wxHashTable
*sm_classTable
;
137 // InitializeClasses() helper
138 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
140 DECLARE_NO_COPY_CLASS(wxClassInfo
)
143 // registers the class
148 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
150 #ifdef WXWIN_COMPATIBILITY_2_4
151 inline void wxClassInfo::InitializeClasses() {}
152 inline void wxClassInfo::CleanUpClasses() {}
155 // ----------------------------------------------------------------------------
156 // Dynamic class macros
157 // ----------------------------------------------------------------------------
159 #define DECLARE_DYNAMIC_CLASS(name) \
161 static wxClassInfo sm_class##name; \
162 virtual wxClassInfo *GetClassInfo() const \
163 { return &name::sm_class##name; }
165 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
166 DECLARE_NO_ASSIGN_CLASS(name) \
167 DECLARE_DYNAMIC_CLASS(name)
169 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
170 DECLARE_NO_COPY_CLASS(name) \
171 DECLARE_DYNAMIC_CLASS(name)
173 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
174 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
176 // -----------------------------------
177 // for concrete classes
178 // -----------------------------------
180 // Single inheritance with one base class
182 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
183 wxObject* wxConstructorFor##name() \
184 { return new name; } \
185 wxClassInfo name::sm_class##name(wxT(#name), \
186 &basename::sm_class##basename, NULL, \
187 (int) sizeof(name), \
188 (wxObjectConstructorFn) wxConstructorFor##name);
190 // Multiple inheritance with two base classes
192 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
193 wxObject* wxConstructorFor##name() \
194 { return new name; } \
195 wxClassInfo name::sm_class##name(wxT(#name), \
196 &basename1::sm_class##basename1, \
197 &basename2::sm_class##basename2, \
198 wxT(#basename2), (int) sizeof(name), \
199 (wxObjectConstructorFn) wxConstructorFor##name);
201 // -----------------------------------
202 // for abstract classes
203 // -----------------------------------
205 // Single inheritance with one base class
207 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
208 wxClassInfo name::sm_class##name(wxT(#name), \
209 &basename::sm_class##basename, NULL, \
210 (int) sizeof(name), (wxObjectConstructorFn) 0);
212 // Multiple inheritance with two base classes
214 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
215 wxClassInfo name::sm_class##name(wxT(#name), \
216 &basename1::sm_class##basename1, \
217 &basename2::sm_class##basename2, \
218 (int) sizeof(name), \
219 (wxObjectConstructorFn) 0);
221 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
222 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
224 #endif // !wxUSE_EXTENDED_RTTI
227 // -----------------------------------
228 // for pluggable classes
229 // -----------------------------------
231 // NOTE: this should probably be the very first statement
232 // in the class declaration so wxPluginSentinel is
233 // the first member initialised and the last destroyed.
235 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
237 #if wxUSE_NESTED_CLASSES
239 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
240 class exportdecl name##PluginSentinel { \
242 static const wxString sm_className; \
244 name##PluginSentinel(); \
245 ~name##PluginSentinel(); \
247 name##PluginSentinel m_pluginsentinel;
249 #define _IMPLEMENT_DL_SENTINEL(name) \
250 const wxString name::name##PluginSentinel::sm_className(#name); \
251 name::name##PluginSentinel::name##PluginSentinel() { \
252 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
253 if( e != 0 ) { e->RefObj(); } \
255 name::name##PluginSentinel::~name##PluginSentinel() { \
256 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
257 if( e != 0 ) { e->UnrefObj(); } \
261 #define _DECLARE_DL_SENTINEL(name)
262 #define _IMPLEMENT_DL_SENTINEL(name)
264 #endif // wxUSE_NESTED_CLASSES
266 #define DECLARE_PLUGGABLE_CLASS(name) \
267 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
268 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
269 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
271 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
272 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
273 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
274 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
276 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
277 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
278 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
279 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
280 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
281 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
282 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
283 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
285 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
286 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
287 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
288 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
289 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
290 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
291 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
292 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
294 #define CLASSINFO(name) (&name::sm_class##name)
296 #else // !wxUSE_DYNAMIC_CLASSES
298 // No dynamic class system: so stub out the macros
300 #define DECLARE_DYNAMIC_CLASS(name)
301 #define DECLARE_ABSTRACT_CLASS(name)
302 #define DECLARE_CLASS(name)
303 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
304 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
305 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
306 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
307 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
308 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
310 #define DECLARE_PLUGGABLE_CLASS(name)
311 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
312 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
313 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
314 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
315 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
317 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
318 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
319 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
320 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
321 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
322 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
324 #endif // wxUSE_DYNAMIC_CLASSES
326 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
328 // Just seems a bit nicer-looking (pretend it's not a macro)
329 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
331 // to be replaced by dynamic_cast<> in the future
332 #define wxDynamicCast(obj, className) \
333 ((className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className))
335 // The 'this' pointer is always true, so use this version
336 // to cast the this pointer and avoid compiler warnings.
337 #define wxDynamicCastThis(className) \
338 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
340 #ifdef HAVE_CONST_CAST
341 #define wxConstCast(obj, className) const_cast<className *>(obj)
343 #define wxConstCast(obj, className) ((className *)(obj))
348 inline void wxCheckCast(void *ptr
)
350 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
352 #define wxStaticCast(obj, className) \
353 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
355 #else // !__WXDEBUG__
356 #define wxStaticCast(obj, className) ((className *)(obj))
358 #endif // __WXDEBUG__
360 // ----------------------------------------------------------------------------
361 // set up memory debugging macros
362 // ----------------------------------------------------------------------------
365 Which new/delete operator variants do we want?
367 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
368 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
369 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
370 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
371 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
372 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
373 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
376 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
378 // All compilers get this one
379 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
381 // Everyone except Visage gets the next one
382 #ifndef __VISAGECPP__
383 #define _WX_WANT_DELETE_VOID
386 // Only visage gets this one under the correct circumstances
387 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
388 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
391 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
392 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
393 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
396 // Now see who (if anyone) gets the array memory operators
397 #if wxUSE_ARRAY_MEMORY_OPERATORS
399 // Everyone except Visual C++ (cause problems for VC++ - crashes)
400 #if !defined(__VISUALC__)
401 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
404 // Everyone except Visual C++ (cause problems for VC++ - crashes)
405 #if !defined(__VISUALC__)
406 #define _WX_WANT_ARRAY_DELETE_VOID
409 // Only CodeWarrior 6 or higher
410 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
411 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
414 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
416 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
418 // ----------------------------------------------------------------------------
419 // wxObject: the root class of wxWindows object hierarchy
420 // ----------------------------------------------------------------------------
422 class WXDLLIMPEXP_BASE wxObject
424 DECLARE_ABSTRACT_CLASS(wxObject
)
427 void InitFrom(const wxObject
& other
);
430 wxObject() { m_refData
= NULL
; }
431 virtual ~wxObject() { UnRef(); }
433 wxObject(const wxObject
& other
)
438 wxObject
& operator=(const wxObject
& other
)
440 if ( this != &other
)
448 bool IsKindOf(wxClassInfo
*info
) const;
451 // Turn on the correct set of new and delete operators
453 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
454 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
457 #ifdef _WX_WANT_DELETE_VOID
458 void operator delete ( void * buf
);
461 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
462 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
465 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
466 void operator delete ( void *buf
, const wxChar
*, int );
469 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
470 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
473 #ifdef _WX_WANT_ARRAY_DELETE_VOID
474 void operator delete[] ( void *buf
);
477 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
478 void operator delete[] (void* buf
, const wxChar
*, int );
481 // ref counted data handling methods
484 wxObjectRefData
*GetRefData() const { return m_refData
; }
485 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
487 // make a 'clone' of the object
488 void Ref(const wxObject
& clone
);
490 // destroy a reference
494 // ensure that our data is not shared with anybody else: if we have no
495 // data, it is created using CreateRefData() below, if we have shared data
496 // it is copied using CloneRefData(), otherwise nothing is done
497 void AllocExclusive();
499 // both methods must be implemented if Unshare() is used, not pure virtual
500 // only because of the backwards compatibility reasons
502 // create a new m_refData
503 virtual wxObjectRefData
*CreateRefData() const;
505 // create a new m_refData initialized with the given one
506 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
508 wxObjectRefData
*m_refData
;
511 // ----------------------------------------------------------------------------
512 // wxObjectRefData: ref counted data meant to be stored in wxObject
513 // ----------------------------------------------------------------------------
515 class WXDLLIMPEXP_BASE wxObjectRefData
517 friend class WXDLLIMPEXP_BASE wxObject
;
520 wxObjectRefData() : m_count(1) { }
521 virtual ~wxObjectRefData() { }
523 int GetRefCount() const { return m_count
; }
530 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
532 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
535 #if wxUSE_EXTENDED_RTTI
536 class WXDLLIMPEXP_BASE wxDynamicObject
: public wxObject
539 // instantiates this object with an instance of its superclass
540 wxDynamicObject(wxObject
* superClassInstance
, const wxDynamicClassInfo
*info
) ;
543 void SetProperty (const wxChar
*PropertyName
, const wxxVariant
&Value
);
544 wxxVariant
GetProperty (const wxChar
*PropertyName
) const ;
546 // get the runtime identity of this object
547 wxClassInfo
*GetClassInfo() const
549 return const_cast<wxClassInfo
*>((const wxClassInfo
*)m_classInfo
);
552 wxObject
* GetSuperClassInstance() const
554 return m_superClassInstance
;
557 wxObject
*m_superClassInstance
;
558 const wxDynamicClassInfo
*m_classInfo
;
559 struct wxDynamicObjectInternal
;
560 wxDynamicObjectInternal
*m_data
;
564 // ----------------------------------------------------------------------------
565 // more debugging macros
566 // ----------------------------------------------------------------------------
570 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
572 #else // !__WXDEBUG__
573 #define WXDEBUG_NEW new
576 // Redefine new to be the debugging version. This doesn't work with all
577 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
578 // to use the debugging version.
580 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
581 #define new new(__TFILE__,__LINE__)
584 #endif // _WX_OBJECTH__