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 if (sm_classTable
!= NULL
) {
78 wxString
msg(_T("too late binding of class info (lazy binding) for "));
86 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
88 const wxChar
*GetClassName() const { return m_className
; }
89 const wxChar
*GetBaseClassName1() const { return m_baseClassName1
; }
90 const wxChar
*GetBaseClassName2() const { return m_baseClassName2
; }
91 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
92 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
93 int GetSize() const { return m_objectSize
; }
95 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
96 static const wxClassInfo
*GetFirst() { return sm_first
; }
97 const wxClassInfo
*GetNext() const { return m_next
; }
98 static wxClassInfo
*FindClass(const wxChar
*className
);
100 // Climb upwards through inheritance hierarchy.
101 // Dual inheritance is catered for.
103 bool IsKindOf(const wxClassInfo
*info
) const
107 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
108 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
111 // Initializes parent pointers and hash table for fast searching.
113 static void InitializeClasses();
115 // Cleans up hash table used for fast searching.
117 static void CleanUpClasses();
120 const wxChar
*m_className
;
121 const wxChar
*m_baseClassName1
;
122 const wxChar
*m_baseClassName2
;
124 wxObjectConstructorFn m_objectConstructor
;
126 // Pointers to base wxClassInfos: set in InitializeClasses
128 const wxClassInfo
*m_baseInfo1
;
129 const wxClassInfo
*m_baseInfo2
;
131 // class info object live in a linked list:
132 // pointers to its head and the next element in it
134 static wxClassInfo
*sm_first
;
137 // FIXME: this should be private (currently used directly by way too
139 static wxHashTable
*sm_classTable
;
142 // InitializeClasses() helper
143 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
145 DECLARE_NO_COPY_CLASS(wxClassInfo
)
148 WXDLLEXPORT wxObject
*wxCreateDynamicObject(const wxChar
*name
);
150 // ----------------------------------------------------------------------------
151 // Dynamic class macros
152 // ----------------------------------------------------------------------------
154 #define DECLARE_DYNAMIC_CLASS(name) \
156 static wxClassInfo sm_class##name; \
157 virtual wxClassInfo *GetClassInfo() const \
158 { return &name::sm_class##name; }
160 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
161 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
163 // -----------------------------------
164 // for concrete classes
165 // -----------------------------------
167 // Single inheritance with one base class
169 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
170 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
171 { return new name; } \
172 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
173 0, (int) sizeof(name), \
174 (wxObjectConstructorFn) wxConstructorFor##name);
176 // Multiple inheritance with two base classes
178 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
179 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
180 { return new name; } \
181 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
182 wxT(#basename2), (int) sizeof(name), \
183 (wxObjectConstructorFn) wxConstructorFor##name);
185 // -----------------------------------
186 // for abstract classes
187 // -----------------------------------
189 // Single inheritance with one base class
191 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
192 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
193 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
195 // Multiple inheritance with two base classes
197 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
198 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
199 wxT(#basename2), (int) sizeof(name), \
200 (wxObjectConstructorFn) 0);
202 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
203 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
205 // -----------------------------------
206 // for pluggable classes
207 // -----------------------------------
209 // NOTE: this should probably be the very first statement
210 // in the class declaration so wxPluginSentinel is
211 // the first member initialised and the last destroyed.
213 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
215 #if wxUSE_NESTED_CLASSES
217 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
218 class exportdecl name##PluginSentinel { \
220 static const wxString sm_className; \
222 name##PluginSentinel(); \
223 ~name##PluginSentinel(); \
225 name##PluginSentinel m_pluginsentinel;
227 #define _IMPLEMENT_DL_SENTINEL(name) \
228 const wxString name::name##PluginSentinel::sm_className(#name); \
229 name::name##PluginSentinel::name##PluginSentinel() { \
230 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
231 if( e != 0 ) { e->RefObj(); } \
233 name::name##PluginSentinel::~name##PluginSentinel() { \
234 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
235 if( e != 0 ) { e->UnrefObj(); } \
239 #define _DECLARE_DL_SENTINEL(name)
240 #define _IMPLEMENT_DL_SENTINEL(name)
242 #endif // wxUSE_NESTED_CLASSES
244 #define DECLARE_PLUGGABLE_CLASS(name) \
245 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
246 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
247 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
249 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
250 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
251 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
252 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
254 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
255 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
256 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
257 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
258 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
259 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
260 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
261 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
263 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
264 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
265 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
266 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
267 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
268 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
269 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
270 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
273 #define CLASSINFO(name) (&name::sm_class##name)
275 #else // !wxUSE_DYNAMIC_CLASSES
277 // No dynamic class system: so stub out the macros
279 #define DECLARE_DYNAMIC_CLASS(name)
280 #define DECLARE_ABSTRACT_CLASS(name)
281 #define DECLARE_CLASS(name)
282 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
283 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
284 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
285 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
286 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
287 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
289 #define DECLARE_PLUGGABLE_CLASS(name)
290 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
291 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
292 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
293 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
294 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
296 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
297 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
298 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
299 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
300 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
301 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
303 #endif // wxUSE_DYNAMIC_CLASSES
306 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
308 // Just seems a bit nicer-looking (pretend it's not a macro)
309 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
311 // to be replaced by dynamic_cast<> in the future
312 #define wxDynamicCast(obj, className) \
313 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
315 // The 'this' pointer is always true, so use this version
316 // to cast the this pointer and avoid compiler warnings.
317 #define wxDynamicCastThis(className) \
318 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
320 #define wxConstCast(obj, className) ((className *)(obj))
324 inline void wxCheckCast(void *ptr
)
326 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
328 #define wxStaticCast(obj, className) \
329 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
331 #else // !__WXDEBUG__
332 #define wxStaticCast(obj, className) ((className *)(obj))
334 #endif // __WXDEBUG__
337 // for some reason Borland seems to need this include.
338 #if wxUSE_STD_IOSTREAM \
339 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
340 && defined(__BORLANDC__)
342 #include <iostream.h>
346 #endif // wxUSE_IOSTREAMH
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
407 // ----------------------------------------------------------------------------
408 // wxObject: the root class of wxWindows object hierarchy
409 // ----------------------------------------------------------------------------
411 class WXDLLEXPORT wxObject
413 DECLARE_ABSTRACT_CLASS(wxObject
)
416 void InitFrom(const wxObject
& other
);
419 wxObject() { m_refData
= NULL
; }
420 virtual ~wxObject() { UnRef(); }
422 wxObject(const wxObject
& other
)
427 wxObject
& operator=(const wxObject
& other
)
429 if ( this != &other
)
437 bool IsKindOf(wxClassInfo
*info
) const;
440 // Turn on the correct set of new and delete operators
442 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
443 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
446 #ifdef _WX_WANT_DELETE_VOID
447 void operator delete ( void * buf
);
450 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
451 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
454 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
455 void operator delete ( void *buf
, const wxChar
*, int );
458 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
459 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
462 #ifdef _WX_WANT_ARRAY_DELETE_VOID
463 void operator delete[] ( void *buf
);
466 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
467 void operator delete[] (void* buf
, const wxChar
*, int );
471 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
472 virtual void Dump(wxSTD ostream
& str
);
475 // ref counted data handling methods
478 wxObjectRefData
*GetRefData() const { return m_refData
; }
479 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
481 // make a 'clone' of the object
482 void Ref(const wxObject
& clone
);
484 // destroy a reference
488 // ensure that our data is not shared with anybody else: if we have no
489 // data, it is created using CreateRefData() below, if we have shared data
490 // it is copied using CloneRefData(), otherwise nothing is done
491 void AllocExclusive();
493 // both methods must be implemented if Unshare() is used, not pure virtual
494 // only because of the backwards compatibility reasons
496 // create a new m_refData
497 virtual wxObjectRefData
*CreateRefData() const;
499 // create a new m_refData initialized with the given one
500 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
502 wxObjectRefData
*m_refData
;
505 // ----------------------------------------------------------------------------
506 // wxObjectRefData: ref counted data meant to be stored in wxObject
507 // ----------------------------------------------------------------------------
509 class WXDLLEXPORT wxObjectRefData
511 friend class wxObject
;
514 wxObjectRefData() : m_count(1) { }
515 virtual ~wxObjectRefData() { }
517 int GetRefCount() const { return m_count
; }
524 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
526 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
529 // ----------------------------------------------------------------------------
530 // more debugging macros
531 // ----------------------------------------------------------------------------
535 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
537 #else // !__WXDEBUG__
538 #define WXDEBUG_NEW new
541 // Redefine new to be the debugging version. This doesn't work with all
542 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
543 // to use the debugging version.
545 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
546 #define new new(__TFILE__,__LINE__)
549 #endif // _WX_OBJECTH__