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
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 // Initializes parent pointers and hash table for fast searching.
109 static void InitializeClasses();
111 // Cleans up hash table used for fast searching.
112 static void CleanUpClasses();
115 const wxChar
*m_className
;
117 wxObjectConstructorFn m_objectConstructor
;
119 // Pointers to base wxClassInfos: set in InitializeClasses
121 const wxClassInfo
*m_baseInfo1
;
122 const wxClassInfo
*m_baseInfo2
;
124 // class info object live in a linked list:
125 // pointers to its head and the next element in it
127 static wxClassInfo
*sm_first
;
130 // FIXME: this should be private (currently used directly by way too
132 static wxHashTable
*sm_classTable
;
135 // InitializeClasses() helper
136 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
138 DECLARE_NO_COPY_CLASS(wxClassInfo
)
141 // registers the class
146 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
148 // ----------------------------------------------------------------------------
149 // Dynamic class macros
150 // ----------------------------------------------------------------------------
152 #define DECLARE_DYNAMIC_CLASS(name) \
154 static wxClassInfo sm_class##name; \
155 virtual wxClassInfo *GetClassInfo() const \
156 { return &name::sm_class##name; }
158 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
159 DECLARE_NO_ASSIGN_CLASS(name) \
160 DECLARE_DYNAMIC_CLASS(name)
162 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
163 DECLARE_NO_COPY_CLASS(name) \
164 DECLARE_DYNAMIC_CLASS(name)
166 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
167 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
169 // -----------------------------------
170 // for concrete classes
171 // -----------------------------------
173 // Single inheritance with one base class
175 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
176 wxObject* wxConstructorFor##name() \
177 { return new name; } \
178 wxClassInfo name::sm_class##name(wxT(#name), \
179 &basename::sm_class##basename, NULL, \
180 (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), \
189 &basename1::sm_class##basename1, \
190 &basename2::sm_class##basename2, \
191 wxT(#basename2), (int) sizeof(name), \
192 (wxObjectConstructorFn) wxConstructorFor##name);
194 // -----------------------------------
195 // for abstract classes
196 // -----------------------------------
198 // Single inheritance with one base class
200 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
201 wxClassInfo name::sm_class##name(wxT(#name), \
202 &basename::sm_class##basename, NULL, \
203 (int) sizeof(name), (wxObjectConstructorFn) 0);
205 // Multiple inheritance with two base classes
207 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
208 wxClassInfo name::sm_class##name(wxT(#name), \
209 &basename1::sm_class##basename1, \
210 &basename2::sm_class##basename2, \
211 (int) sizeof(name), \
212 (wxObjectConstructorFn) 0);
214 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
215 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
217 #endif // !wxUSE_EXTENDED_RTTI
220 // -----------------------------------
221 // for pluggable classes
222 // -----------------------------------
224 // NOTE: this should probably be the very first statement
225 // in the class declaration so wxPluginSentinel is
226 // the first member initialised and the last destroyed.
228 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
230 #if wxUSE_NESTED_CLASSES
232 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
233 class exportdecl name##PluginSentinel { \
235 static const wxString sm_className; \
237 name##PluginSentinel(); \
238 ~name##PluginSentinel(); \
240 name##PluginSentinel m_pluginsentinel;
242 #define _IMPLEMENT_DL_SENTINEL(name) \
243 const wxString name::name##PluginSentinel::sm_className(#name); \
244 name::name##PluginSentinel::name##PluginSentinel() { \
245 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
246 if( e != 0 ) { e->RefObj(); } \
248 name::name##PluginSentinel::~name##PluginSentinel() { \
249 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
250 if( e != 0 ) { e->UnrefObj(); } \
254 #define _DECLARE_DL_SENTINEL(name)
255 #define _IMPLEMENT_DL_SENTINEL(name)
257 #endif // wxUSE_NESTED_CLASSES
259 #define DECLARE_PLUGGABLE_CLASS(name) \
260 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
261 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
262 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
264 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
265 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
266 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
267 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
269 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
270 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
271 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
272 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
273 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
274 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
275 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
276 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
278 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
279 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
280 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
281 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
282 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
283 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
284 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
285 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
287 #define CLASSINFO(name) (&name::sm_class##name)
289 #else // !wxUSE_DYNAMIC_CLASSES
291 // No dynamic class system: so stub out the macros
293 #define DECLARE_DYNAMIC_CLASS(name)
294 #define DECLARE_ABSTRACT_CLASS(name)
295 #define DECLARE_CLASS(name)
296 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
297 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
298 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
299 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
300 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
301 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
303 #define DECLARE_PLUGGABLE_CLASS(name)
304 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
305 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
306 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
307 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
308 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
310 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
311 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
312 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
313 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
314 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
315 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
317 #endif // wxUSE_DYNAMIC_CLASSES
319 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
321 // Just seems a bit nicer-looking (pretend it's not a macro)
322 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
324 // to be replaced by dynamic_cast<> in the future
325 #define wxDynamicCast(obj, className) \
326 ((className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className))
328 // The 'this' pointer is always true, so use this version
329 // to cast the this pointer and avoid compiler warnings.
330 #define wxDynamicCastThis(className) \
331 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
333 #ifdef HAVE_CONST_CAST
334 #define wxConstCast(obj, className) const_cast<className *>(obj)
336 #define wxConstCast(obj, className) ((className *)(obj))
341 inline void wxCheckCast(void *ptr
)
343 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
345 #define wxStaticCast(obj, className) \
346 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
348 #else // !__WXDEBUG__
349 #define wxStaticCast(obj, className) ((className *)(obj))
351 #endif // __WXDEBUG__
353 // ----------------------------------------------------------------------------
354 // set up memory debugging macros
355 // ----------------------------------------------------------------------------
358 Which new/delete operator variants do we want?
360 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
361 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
362 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
363 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
364 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
365 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
366 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
369 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
371 // All compilers get this one
372 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
374 // Everyone except Visage gets the next one
375 #ifndef __VISAGECPP__
376 #define _WX_WANT_DELETE_VOID
379 // Only visage gets this one under the correct circumstances
380 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
381 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
384 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
385 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
386 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
389 // Now see who (if anyone) gets the array memory operators
390 #if wxUSE_ARRAY_MEMORY_OPERATORS
392 // Everyone except Visual C++ (cause problems for VC++ - crashes)
393 #if !defined(__VISUALC__)
394 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
397 // Everyone except Visual C++ (cause problems for VC++ - crashes)
398 #if !defined(__VISUALC__)
399 #define _WX_WANT_ARRAY_DELETE_VOID
402 // Only CodeWarrior 6 or higher
403 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
404 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
407 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
409 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
411 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
412 // needed by wxObject::Dump
413 #include "wx/iosfwrap.h"
416 // ----------------------------------------------------------------------------
417 // wxObject: the root class of wxWindows object hierarchy
418 // ----------------------------------------------------------------------------
420 class WXDLLIMPEXP_BASE wxObject
422 DECLARE_ABSTRACT_CLASS(wxObject
)
425 void InitFrom(const wxObject
& other
);
428 wxObject() { m_refData
= NULL
; }
429 virtual ~wxObject() { UnRef(); }
431 wxObject(const wxObject
& other
)
436 wxObject
& operator=(const wxObject
& other
)
438 if ( this != &other
)
446 bool IsKindOf(wxClassInfo
*info
) const;
449 // Turn on the correct set of new and delete operators
451 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
452 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
455 #ifdef _WX_WANT_DELETE_VOID
456 void operator delete ( void * buf
);
459 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
460 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
463 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
464 void operator delete ( void *buf
, const wxChar
*, int );
467 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
468 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
471 #ifdef _WX_WANT_ARRAY_DELETE_VOID
472 void operator delete[] ( void *buf
);
475 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
476 void operator delete[] (void* buf
, const wxChar
*, int );
480 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
481 virtual void Dump(wxSTD ostream
& str
);
484 // ref counted data handling methods
487 wxObjectRefData
*GetRefData() const { return m_refData
; }
488 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
490 // make a 'clone' of the object
491 void Ref(const wxObject
& clone
);
493 // destroy a reference
497 // ensure that our data is not shared with anybody else: if we have no
498 // data, it is created using CreateRefData() below, if we have shared data
499 // it is copied using CloneRefData(), otherwise nothing is done
500 void AllocExclusive();
502 // both methods must be implemented if Unshare() is used, not pure virtual
503 // only because of the backwards compatibility reasons
505 // create a new m_refData
506 virtual wxObjectRefData
*CreateRefData() const;
508 // create a new m_refData initialized with the given one
509 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
511 wxObjectRefData
*m_refData
;
514 // ----------------------------------------------------------------------------
515 // wxObjectRefData: ref counted data meant to be stored in wxObject
516 // ----------------------------------------------------------------------------
518 class WXDLLIMPEXP_BASE wxObjectRefData
520 friend class WXDLLIMPEXP_BASE wxObject
;
523 wxObjectRefData() : m_count(1) { }
524 virtual ~wxObjectRefData() { }
526 int GetRefCount() const { return m_count
; }
533 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
535 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
538 // ----------------------------------------------------------------------------
539 // more debugging macros
540 // ----------------------------------------------------------------------------
544 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
546 #else // !__WXDEBUG__
547 #define WXDEBUG_NEW new
550 // Redefine new to be the debugging version. This doesn't work with all
551 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
552 // to use the debugging version.
554 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
555 #define new new(__TFILE__,__LINE__)
558 #endif // _WX_OBJECTH__