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 and Markus Holzem
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
17 #pragma interface "object.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
25 #include "wx/memory.h"
27 class WXDLLEXPORT wxObject
;
29 #if wxUSE_DYNAMIC_CLASSES
31 // ----------------------------------------------------------------------------
32 // conditional compilation
33 // ----------------------------------------------------------------------------
35 // this shouldn't be needed any longer as <wx/msw/private.h> does it but it
36 // doesn't hurt neither
44 class WXDLLEXPORT wxClassInfo
;
45 class WXDLLEXPORT wxHashTable
;
46 class WXDLLEXPORT wxObjectRefData
;
48 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
49 #include "wx/ioswrap.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 typedef wxObject
*(*wxObjectConstructorFn
)(void);
59 class WXDLLEXPORT wxClassInfo
62 wxClassInfo( const wxChar
*className
,
63 const wxChar
*baseName1
,
64 const wxChar
*baseName2
,
66 wxObjectConstructorFn ctor
)
67 : m_className(className
)
68 , m_baseClassName1(baseName1
)
69 , m_baseClassName2(baseName2
)
71 , m_objectConstructor(ctor
)
77 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
79 const wxChar
*GetClassName() const { return m_className
; }
80 const wxChar
*GetBaseClassName1() const { return m_baseClassName1
; }
81 const wxChar
*GetBaseClassName2() const { return m_baseClassName2
; }
82 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
83 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
84 int GetSize() const { return m_objectSize
; }
86 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
87 static const wxClassInfo
*GetFirst() { return sm_first
; }
88 const wxClassInfo
*GetNext() const { return m_next
; }
89 static wxClassInfo
*FindClass(const wxChar
*className
);
91 // Climb upwards through inheritance hierarchy.
92 // Dual inheritance is catered for.
94 bool IsKindOf(const wxClassInfo
*info
) const
98 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
99 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
102 // Initializes parent pointers and hash table for fast searching.
104 static void InitializeClasses();
106 // Cleans up hash table used for fast searching.
108 static void CleanUpClasses();
111 const wxChar
*m_className
;
112 const wxChar
*m_baseClassName1
;
113 const wxChar
*m_baseClassName2
;
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
);
137 WXDLLEXPORT wxObject
*wxCreateDynamicObject(const wxChar
*name
);
139 // ----------------------------------------------------------------------------
140 // Dynamic class macros
141 // ----------------------------------------------------------------------------
143 #define DECLARE_DYNAMIC_CLASS(name) \
145 static wxClassInfo sm_class##name; \
146 virtual wxClassInfo *GetClassInfo() const \
147 { return &name::sm_class##name; }
149 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
150 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
152 // -----------------------------------
153 // for concrete classes
154 // -----------------------------------
156 // Single inheritance with one base class
158 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
159 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
160 { return new name; } \
161 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
162 0, (int) sizeof(name), \
163 (wxObjectConstructorFn) wxConstructorFor##name);
165 // Multiple inheritance with two base classes
167 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
168 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
169 { return new name; } \
170 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
171 wxT(#basename2), (int) sizeof(name), \
172 (wxObjectConstructorFn) wxConstructorFor##name);
174 // -----------------------------------
175 // for abstract classes
176 // -----------------------------------
178 // Single inheritance with one base class
180 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
181 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
182 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
184 // Multiple inheritance with two base classes
186 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
187 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
188 wxT(#basename2), (int) sizeof(name), \
189 (wxObjectConstructorFn) 0);
191 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
192 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
194 // -----------------------------------
195 // for pluggable classes
196 // -----------------------------------
198 // NOTE: this should probably be the very first statement
199 // in the class declaration so wxPluginSentinel is
200 // the first member initialised and the last destroyed.
202 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
204 #if wxUSE_NESTED_CLASSES
206 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
207 class exportdecl name##PluginSentinel { \
209 static const wxString sm_className; \
211 name##PluginSentinel(); \
212 ~name##PluginSentinel(); \
214 name##PluginSentinel m_pluginsentinel;
216 #define _IMPLEMENT_DL_SENTINEL(name) \
217 const wxString name::name##PluginSentinel::sm_className(#name); \
218 name::name##PluginSentinel::name##PluginSentinel() { \
219 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
220 if( e != 0 ) { e->RefObj(); } \
222 name::name##PluginSentinel::~name##PluginSentinel() { \
223 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
224 if( e != 0 ) { e->UnrefObj(); } \
228 #define _DECLARE_DL_SENTINEL(name)
229 #define _IMPLEMENT_DL_SENTINEL(name)
231 #endif // wxUSE_NESTED_CLASSES
233 #define DECLARE_PLUGGABLE_CLASS(name) \
234 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
235 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
236 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
238 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
239 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
240 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
241 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
243 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
244 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
245 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
246 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
247 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
248 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
249 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
250 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
252 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
253 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
254 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
255 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
256 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
257 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
258 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
259 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
262 #define CLASSINFO(name) (&name::sm_class##name)
264 #else // !wxUSE_DYNAMIC_CLASSES
266 // No dynamic class system: so stub out the macros
268 #define DECLARE_DYNAMIC_CLASS(name)
269 #define DECLARE_ABSTRACT_CLASS(name)
270 #define DECLARE_CLASS(name)
271 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
272 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
273 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
274 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
275 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
276 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
278 #define DECLARE_PLUGGABLE_CLASS(name)
279 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
280 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
281 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
282 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
283 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
285 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
286 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
287 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
288 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
289 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
290 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
292 #endif // wxUSE_DYNAMIC_CLASSES
295 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
297 // Just seems a bit nicer-looking (pretend it's not a macro)
298 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
300 // to be replaced by dynamic_cast<> in the future
301 #define wxDynamicCast(obj, className) \
302 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
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::sm_class##className) ? (className *)(this) : (className *)0)
309 #define wxConstCast(obj, className) ((className *)(obj))
313 inline void wxCheckCast(void *ptr
)
315 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
317 #define wxStaticCast(obj, className) \
318 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
320 #else // !__WXDEBUG__
321 #define wxStaticCast(obj, className) ((className *)(obj))
323 #endif // __WXDEBUG__
326 // for some reason Borland seems to need this include.
327 #if wxUSE_STD_IOSTREAM \
328 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
329 && defined(__BORLANDC__)
331 #include <iostream.h>
335 #endif // wxUSE_IOSTREAMH
337 // ----------------------------------------------------------------------------
338 // set up memory debugging macros
339 // ----------------------------------------------------------------------------
342 Which new/delete operator variants do we want?
344 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
345 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
346 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
347 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
348 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
349 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
350 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
353 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
355 // All compilers get this one
356 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
358 // Everyone except Visage gets the next one
359 #ifndef __VISAGECPP__
360 #define _WX_WANT_DELETE_VOID
363 // Only visage gets this one under the correct circumstances
364 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
365 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
368 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
369 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || defined(__MWERKS__)
370 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
373 // Now see who (if anyone) gets the array memory operators
374 #if wxUSE_ARRAY_MEMORY_OPERATORS
376 // Everyone except Visual C++ (cause problems for VC++ - crashes)
377 #if !defined(__VISUALC__)
378 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
381 // Everyone except Visual C++ (cause problems for VC++ - crashes)
382 #if !defined(__VISUALC__)
383 #define _WX_WANT_ARRAY_DELETE_VOID
386 // Only CodeWarrior 6 or higher
387 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
388 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
391 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
393 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
396 // ----------------------------------------------------------------------------
397 // wxObject: the root class of wxWindows object hierarchy
398 // ----------------------------------------------------------------------------
400 class WXDLLEXPORT wxObject
402 DECLARE_ABSTRACT_CLASS(wxObject
)
405 wxObject() { m_refData
= NULL
; }
406 virtual ~wxObject() { UnRef(); }
408 bool IsKindOf(wxClassInfo
*info
) const;
411 // Turn on the correct set of new and delete operators
413 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
414 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
417 #ifdef _WX_WANT_DELETE_VOID
418 void operator delete ( void * buf
);
421 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
422 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
425 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
426 void operator delete ( void *buf
, const wxChar
*, int );
429 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
430 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
433 #ifdef _WX_WANT_ARRAY_DELETE_VOID
434 void operator delete[] ( void *buf
);
437 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
438 void operator delete[] (void* buf
, const wxChar
*, int );
442 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
443 virtual void Dump(wxSTD ostream
& str
);
446 // ref counted data handling methods
449 wxObjectRefData
*GetRefData() const { return m_refData
; }
450 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
452 // make a 'clone' of the object
453 void Ref(const wxObject
& clone
);
455 // destroy a reference
459 // ensure that our data is not shared with anybody else: if we have no
460 // data, it is created using CreateRefData() below, if we have shared data
461 // it is copied using CloneRefData(), otherwise nothing is done
462 void AllocExclusive();
464 // both methods must be implemented if Unshare() is used, not pure virtual
465 // only because of the backwards compatibility reasons
467 // create a new m_refData
468 virtual wxObjectRefData
*CreateRefData() const;
470 // create a new m_refData initialized with the given one
471 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
473 wxObjectRefData
*m_refData
;
476 // ----------------------------------------------------------------------------
477 // wxObjectRefData: ref counted data meant to be stored in wxObject
478 // ----------------------------------------------------------------------------
480 class WXDLLEXPORT wxObjectRefData
482 friend class wxObject
;
485 wxObjectRefData() : m_count(1) { }
486 virtual ~wxObjectRefData() { }
488 int GetRefCount() const { return m_count
; }
495 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
497 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
500 // ----------------------------------------------------------------------------
501 // more debugging macros
502 // ----------------------------------------------------------------------------
506 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
508 #else // !__WXDEBUG__
509 #define WXDEBUG_NEW new
512 // Redefine new to be the debugging version. This doesn't work with all
513 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
514 // to use the debugging version.
516 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
517 #define new new(__TFILE__,__LINE__)
520 #endif // _WX_OBJECTH__