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(__APPLE__)
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
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 wxChar
*baseName1
,
69 const wxChar
*baseName2
,
71 wxObjectConstructorFn ctor
)
72 : m_className(className
)
73 , m_baseClassName1(baseName1
)
74 , m_baseClassName2(baseName2
)
76 , m_objectConstructor(ctor
)
84 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
86 const wxChar
*GetClassName() const { return m_className
; }
87 const wxChar
*GetBaseClassName1() const { return m_baseClassName1
; }
88 const wxChar
*GetBaseClassName2() const { return m_baseClassName2
; }
89 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
90 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
91 int GetSize() const { return m_objectSize
; }
93 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
94 static const wxClassInfo
*GetFirst() { return sm_first
; }
95 const wxClassInfo
*GetNext() const { return m_next
; }
96 static wxClassInfo
*FindClass(const wxChar
*className
);
98 // Climb upwards through inheritance hierarchy.
99 // Dual inheritance is catered for.
101 bool IsKindOf(const wxClassInfo
*info
) const
105 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
106 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
109 // Initializes parent pointers and hash table for fast searching.
111 static void InitializeClasses();
113 // Cleans up hash table used for fast searching.
115 static void CleanUpClasses();
119 const wxChar
*m_className
;
120 const wxChar
*m_baseClassName1
;
121 const wxChar
*m_baseClassName2
;
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 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
149 // ----------------------------------------------------------------------------
150 // Dynamic class macros
151 // ----------------------------------------------------------------------------
153 #define DECLARE_DYNAMIC_CLASS(name) \
155 static wxClassInfo sm_class##name; \
156 virtual wxClassInfo *GetClassInfo() const \
157 { return &name::sm_class##name; }
159 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
160 DECLARE_NO_ASSIGN_CLASS(name) \
161 DECLARE_DYNAMIC_CLASS(name)
163 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
164 DECLARE_NO_COPY_CLASS(name) \
165 DECLARE_DYNAMIC_CLASS(name)
167 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
168 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
170 // -----------------------------------
171 // for concrete classes
172 // -----------------------------------
174 // Single inheritance with one base class
176 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
177 wxObject* wxConstructorFor##name() \
178 { return new name; } \
179 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
180 0, (int) sizeof(name), \
181 (wxObjectConstructorFn) wxConstructorFor##name);
183 // Multiple inheritance with two base classes
185 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
186 wxObject* wxConstructorFor##name() \
187 { return new name; } \
188 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
189 wxT(#basename2), (int) sizeof(name), \
190 (wxObjectConstructorFn) wxConstructorFor##name);
192 // -----------------------------------
193 // for abstract classes
194 // -----------------------------------
196 // Single inheritance with one base class
198 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
199 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
200 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
202 // Multiple inheritance with two base classes
204 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
205 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
206 wxT(#basename2), (int) sizeof(name), \
207 (wxObjectConstructorFn) 0);
209 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
210 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
212 #endif // !wxUSE_EXTENDED_RTTI
215 // -----------------------------------
216 // for pluggable classes
217 // -----------------------------------
219 // NOTE: this should probably be the very first statement
220 // in the class declaration so wxPluginSentinel is
221 // the first member initialised and the last destroyed.
223 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
225 #if wxUSE_NESTED_CLASSES
227 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
228 class exportdecl name##PluginSentinel { \
230 static const wxString sm_className; \
232 name##PluginSentinel(); \
233 ~name##PluginSentinel(); \
235 name##PluginSentinel m_pluginsentinel;
237 #define _IMPLEMENT_DL_SENTINEL(name) \
238 const wxString name::name##PluginSentinel::sm_className(#name); \
239 name::name##PluginSentinel::name##PluginSentinel() { \
240 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
241 if( e != 0 ) { e->RefObj(); } \
243 name::name##PluginSentinel::~name##PluginSentinel() { \
244 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
245 if( e != 0 ) { e->UnrefObj(); } \
249 #define _DECLARE_DL_SENTINEL(name)
250 #define _IMPLEMENT_DL_SENTINEL(name)
252 #endif // wxUSE_NESTED_CLASSES
254 #define DECLARE_PLUGGABLE_CLASS(name) \
255 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
256 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
257 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
259 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
260 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
261 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
262 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
264 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
265 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
266 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
267 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
268 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
269 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
270 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
271 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
273 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
274 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
275 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
276 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
277 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
278 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
279 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
280 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
282 #define CLASSINFO(name) (&name::sm_class##name)
284 #else // !wxUSE_DYNAMIC_CLASSES
286 // No dynamic class system: so stub out the macros
288 #define DECLARE_DYNAMIC_CLASS(name)
289 #define DECLARE_ABSTRACT_CLASS(name)
290 #define DECLARE_CLASS(name)
291 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
292 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
293 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
294 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
295 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
296 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
298 #define DECLARE_PLUGGABLE_CLASS(name)
299 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
300 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
301 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
302 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
303 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
305 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
306 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
307 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
308 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
309 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
310 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
312 #endif // wxUSE_DYNAMIC_CLASSES
314 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
316 // Just seems a bit nicer-looking (pretend it's not a macro)
317 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
319 // to be replaced by dynamic_cast<> in the future
320 #define wxDynamicCast(obj, className) \
321 ((className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className))
323 // The 'this' pointer is always true, so use this version
324 // to cast the this pointer and avoid compiler warnings.
325 #define wxDynamicCastThis(className) \
326 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
328 #ifdef HAVE_CONST_CAST
329 #define wxConstCast(obj, className) const_cast<className *>(obj)
331 #define wxConstCast(obj, className) ((className *)(obj))
336 inline void wxCheckCast(void *ptr
)
338 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
340 #define wxStaticCast(obj, className) \
341 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
343 #else // !__WXDEBUG__
344 #define wxStaticCast(obj, className) ((className *)(obj))
346 #endif // __WXDEBUG__
348 // ----------------------------------------------------------------------------
349 // set up memory debugging macros
350 // ----------------------------------------------------------------------------
353 Which new/delete operator variants do we want?
355 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
356 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
357 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
358 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
359 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
360 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
361 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
364 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
366 // All compilers get this one
367 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
369 // Everyone except Visage gets the next one
370 #ifndef __VISAGECPP__
371 #define _WX_WANT_DELETE_VOID
374 // Only visage gets this one under the correct circumstances
375 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
376 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
379 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
380 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
381 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
384 // Now see who (if anyone) gets the array memory operators
385 #if wxUSE_ARRAY_MEMORY_OPERATORS
387 // Everyone except Visual C++ (cause problems for VC++ - crashes)
388 #if !defined(__VISUALC__)
389 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
392 // Everyone except Visual C++ (cause problems for VC++ - crashes)
393 #if !defined(__VISUALC__)
394 #define _WX_WANT_ARRAY_DELETE_VOID
397 // Only CodeWarrior 6 or higher
398 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
399 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
402 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
404 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
406 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
407 // needed by wxObject::Dump
408 #include "wx/iosfwrap.h"
411 // ----------------------------------------------------------------------------
412 // wxObject: the root class of wxWindows object hierarchy
413 // ----------------------------------------------------------------------------
415 class WXDLLIMPEXP_BASE wxObject
417 DECLARE_ABSTRACT_CLASS(wxObject
)
420 void InitFrom(const wxObject
& other
);
423 wxObject() { m_refData
= NULL
; }
424 virtual ~wxObject() { UnRef(); }
426 wxObject(const wxObject
& other
)
431 wxObject
& operator=(const wxObject
& other
)
433 if ( this != &other
)
441 bool IsKindOf(wxClassInfo
*info
) const;
444 // Turn on the correct set of new and delete operators
446 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
447 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
450 #ifdef _WX_WANT_DELETE_VOID
451 void operator delete ( void * buf
);
454 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
455 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
458 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
459 void operator delete ( void *buf
, const wxChar
*, int );
462 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
463 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
466 #ifdef _WX_WANT_ARRAY_DELETE_VOID
467 void operator delete[] ( void *buf
);
470 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
471 void operator delete[] (void* buf
, const wxChar
*, int );
475 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
476 virtual void Dump(wxSTD ostream
& str
);
479 // ref counted data handling methods
482 wxObjectRefData
*GetRefData() const { return m_refData
; }
483 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
485 // make a 'clone' of the object
486 void Ref(const wxObject
& clone
);
488 // destroy a reference
492 // ensure that our data is not shared with anybody else: if we have no
493 // data, it is created using CreateRefData() below, if we have shared data
494 // it is copied using CloneRefData(), otherwise nothing is done
495 void AllocExclusive();
497 // both methods must be implemented if Unshare() is used, not pure virtual
498 // only because of the backwards compatibility reasons
500 // create a new m_refData
501 virtual wxObjectRefData
*CreateRefData() const;
503 // create a new m_refData initialized with the given one
504 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
506 wxObjectRefData
*m_refData
;
509 // ----------------------------------------------------------------------------
510 // wxObjectRefData: ref counted data meant to be stored in wxObject
511 // ----------------------------------------------------------------------------
513 class WXDLLIMPEXP_BASE wxObjectRefData
515 friend class WXDLLIMPEXP_BASE wxObject
;
518 wxObjectRefData() : m_count(1) { }
519 virtual ~wxObjectRefData() { }
521 int GetRefCount() const { return m_count
; }
528 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
530 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
533 // ----------------------------------------------------------------------------
534 // more debugging macros
535 // ----------------------------------------------------------------------------
539 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
541 #else // !__WXDEBUG__
542 #define WXDEBUG_NEW new
545 // Redefine new to be the debugging version. This doesn't work with all
546 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
547 // to use the debugging version.
549 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
550 #define new new(__TFILE__,__LINE__)
553 #endif // _WX_OBJECTH__