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 wxChar
*baseName1
,
67 const wxChar
*baseName2
,
69 wxObjectConstructorFn ctor
)
70 : m_className(className
)
71 , m_baseClassName1(baseName1
)
72 , m_baseClassName2(baseName2
)
74 , m_objectConstructor(ctor
)
82 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
84 const wxChar
*GetClassName() const { return m_className
; }
85 const wxChar
*GetBaseClassName1() const { return m_baseClassName1
; }
86 const wxChar
*GetBaseClassName2() const { return m_baseClassName2
; }
87 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
88 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
89 int GetSize() const { return m_objectSize
; }
91 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
92 static const wxClassInfo
*GetFirst() { return sm_first
; }
93 const wxClassInfo
*GetNext() const { return m_next
; }
94 static wxClassInfo
*FindClass(const wxChar
*className
);
96 // Climb upwards through inheritance hierarchy.
97 // Dual inheritance is catered for.
99 bool IsKindOf(const wxClassInfo
*info
) const
103 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
104 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
107 // Initializes parent pointers and hash table for fast searching.
109 static void InitializeClasses();
111 // Cleans up hash table used for fast searching.
113 static void CleanUpClasses();
117 const wxChar
*m_className
;
118 const wxChar
*m_baseClassName1
;
119 const wxChar
*m_baseClassName2
;
121 wxObjectConstructorFn m_objectConstructor
;
123 // Pointers to base wxClassInfos: set in InitializeClasses
125 const wxClassInfo
*m_baseInfo1
;
126 const wxClassInfo
*m_baseInfo2
;
128 // class info object live in a linked list:
129 // pointers to its head and the next element in it
131 static wxClassInfo
*sm_first
;
134 // FIXME: this should be private (currently used directly by way too
136 static wxHashTable
*sm_classTable
;
139 // InitializeClasses() helper
140 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
142 DECLARE_NO_COPY_CLASS(wxClassInfo
)
145 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
147 // ----------------------------------------------------------------------------
148 // Dynamic class macros
149 // ----------------------------------------------------------------------------
151 #define DECLARE_DYNAMIC_CLASS(name) \
153 static wxClassInfo sm_class##name; \
154 virtual wxClassInfo *GetClassInfo() const \
155 { return &name::sm_class##name; }
157 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
158 DECLARE_NO_ASSIGN_CLASS(name) \
159 DECLARE_DYNAMIC_CLASS(name)
161 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
162 DECLARE_NO_COPY_CLASS(name) \
163 DECLARE_DYNAMIC_CLASS(name)
165 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
166 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
168 // -----------------------------------
169 // for concrete classes
170 // -----------------------------------
172 // Single inheritance with one base class
174 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
175 wxObject* wxConstructorFor##name() \
176 { return new name; } \
177 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
178 0, (int) sizeof(name), \
179 (wxObjectConstructorFn) wxConstructorFor##name);
181 // Multiple inheritance with two base classes
183 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
184 wxObject* wxConstructorFor##name() \
185 { return new name; } \
186 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
187 wxT(#basename2), (int) sizeof(name), \
188 (wxObjectConstructorFn) wxConstructorFor##name);
190 // -----------------------------------
191 // for abstract classes
192 // -----------------------------------
194 // Single inheritance with one base class
196 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
197 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
198 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
200 // Multiple inheritance with two base classes
202 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
203 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
204 wxT(#basename2), (int) sizeof(name), \
205 (wxObjectConstructorFn) 0);
207 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
208 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
210 #endif // !wxUSE_EXTENDED_RTTI
213 // -----------------------------------
214 // for pluggable classes
215 // -----------------------------------
217 // NOTE: this should probably be the very first statement
218 // in the class declaration so wxPluginSentinel is
219 // the first member initialised and the last destroyed.
221 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
223 #if wxUSE_NESTED_CLASSES
225 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
226 class exportdecl name##PluginSentinel { \
228 static const wxString sm_className; \
230 name##PluginSentinel(); \
231 ~name##PluginSentinel(); \
233 name##PluginSentinel m_pluginsentinel;
235 #define _IMPLEMENT_DL_SENTINEL(name) \
236 const wxString name::name##PluginSentinel::sm_className(#name); \
237 name::name##PluginSentinel::name##PluginSentinel() { \
238 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
239 if( e != 0 ) { e->RefObj(); } \
241 name::name##PluginSentinel::~name##PluginSentinel() { \
242 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
243 if( e != 0 ) { e->UnrefObj(); } \
247 #define _DECLARE_DL_SENTINEL(name)
248 #define _IMPLEMENT_DL_SENTINEL(name)
250 #endif // wxUSE_NESTED_CLASSES
252 #define DECLARE_PLUGGABLE_CLASS(name) \
253 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
254 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
255 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
257 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
258 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
259 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
260 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
262 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
263 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
264 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
265 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
266 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
267 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
268 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
269 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
271 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
272 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
273 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
274 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
275 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
276 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
277 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
278 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
280 #define CLASSINFO(name) (&name::sm_class##name)
282 #else // !wxUSE_DYNAMIC_CLASSES
284 // No dynamic class system: so stub out the macros
286 #define DECLARE_DYNAMIC_CLASS(name)
287 #define DECLARE_ABSTRACT_CLASS(name)
288 #define DECLARE_CLASS(name)
289 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
290 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
291 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
292 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
293 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
294 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
296 #define DECLARE_PLUGGABLE_CLASS(name)
297 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
298 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
299 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
300 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
301 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
303 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
304 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
305 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
306 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
307 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
308 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
310 #endif // wxUSE_DYNAMIC_CLASSES
312 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
314 // Just seems a bit nicer-looking (pretend it's not a macro)
315 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
317 // to be replaced by dynamic_cast<> in the future
318 #define wxDynamicCast(obj, className) \
319 ((className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className))
321 // The 'this' pointer is always true, so use this version
322 // to cast the this pointer and avoid compiler warnings.
323 #define wxDynamicCastThis(className) \
324 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
326 #ifdef HAVE_CONST_CAST
327 #define wxConstCast(obj, className) const_cast<className *>(obj)
329 #define wxConstCast(obj, className) ((className *)(obj))
334 inline void wxCheckCast(void *ptr
)
336 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
338 #define wxStaticCast(obj, className) \
339 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
341 #else // !__WXDEBUG__
342 #define wxStaticCast(obj, className) ((className *)(obj))
344 #endif // __WXDEBUG__
346 // ----------------------------------------------------------------------------
347 // set up memory debugging macros
348 // ----------------------------------------------------------------------------
351 Which new/delete operator variants do we want?
353 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
354 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
355 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
356 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
357 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
358 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
359 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
362 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
364 // All compilers get this one
365 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
367 // Everyone except Visage gets the next one
368 #ifndef __VISAGECPP__
369 #define _WX_WANT_DELETE_VOID
372 // Only visage gets this one under the correct circumstances
373 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
374 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
377 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
378 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
379 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
382 // Now see who (if anyone) gets the array memory operators
383 #if wxUSE_ARRAY_MEMORY_OPERATORS
385 // Everyone except Visual C++ (cause problems for VC++ - crashes)
386 #if !defined(__VISUALC__)
387 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
390 // Everyone except Visual C++ (cause problems for VC++ - crashes)
391 #if !defined(__VISUALC__)
392 #define _WX_WANT_ARRAY_DELETE_VOID
395 // Only CodeWarrior 6 or higher
396 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
397 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
400 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
402 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
404 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
405 // needed by wxObject::Dump
406 #include "wx/iosfwrap.h"
409 // ----------------------------------------------------------------------------
410 // wxObject: the root class of wxWindows object hierarchy
411 // ----------------------------------------------------------------------------
413 class WXDLLIMPEXP_BASE wxObject
415 DECLARE_ABSTRACT_CLASS(wxObject
)
418 void InitFrom(const wxObject
& other
);
421 wxObject() { m_refData
= NULL
; }
422 virtual ~wxObject() { UnRef(); }
424 wxObject(const wxObject
& other
)
429 wxObject
& operator=(const wxObject
& other
)
431 if ( this != &other
)
439 bool IsKindOf(wxClassInfo
*info
) const;
442 // Turn on the correct set of new and delete operators
444 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
445 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
448 #ifdef _WX_WANT_DELETE_VOID
449 void operator delete ( void * buf
);
452 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
453 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
456 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
457 void operator delete ( void *buf
, const wxChar
*, int );
460 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
461 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
464 #ifdef _WX_WANT_ARRAY_DELETE_VOID
465 void operator delete[] ( void *buf
);
468 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
469 void operator delete[] (void* buf
, const wxChar
*, int );
473 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
474 virtual void Dump(wxSTD ostream
& str
);
477 // ref counted data handling methods
480 wxObjectRefData
*GetRefData() const { return m_refData
; }
481 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
483 // make a 'clone' of the object
484 void Ref(const wxObject
& clone
);
486 // destroy a reference
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 // ----------------------------------------------------------------------------
532 // more debugging macros
533 // ----------------------------------------------------------------------------
537 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
539 #else // !__WXDEBUG__
540 #define WXDEBUG_NEW new
543 // Redefine new to be the debugging version. This doesn't work with all
544 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
545 // to use the debugging version.
547 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
548 #define new new(__TFILE__,__LINE__)
551 #endif // _WX_OBJECTH__