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
)
85 wxClassInfo
* info
= sm_first
;
88 if (info
->m_next
== this)
90 info
->m_next
= m_next
;
98 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
100 const wxChar
*GetClassName() const { return m_className
; }
101 const wxChar
*GetBaseClassName1() const { return m_baseClassName1
; }
102 const wxChar
*GetBaseClassName2() const { return m_baseClassName2
; }
103 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
104 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
105 int GetSize() const { return m_objectSize
; }
107 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
108 static const wxClassInfo
*GetFirst() { return sm_first
; }
109 const wxClassInfo
*GetNext() const { return m_next
; }
110 static wxClassInfo
*FindClass(const wxChar
*className
);
112 // Climb upwards through inheritance hierarchy.
113 // Dual inheritance is catered for.
115 bool IsKindOf(const wxClassInfo
*info
) const
119 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
120 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
123 // Initializes parent pointers and hash table for fast searching.
125 static void InitializeClasses();
127 // Cleans up hash table used for fast searching.
129 static void CleanUpClasses();
132 const wxChar
*m_className
;
133 const wxChar
*m_baseClassName1
;
134 const wxChar
*m_baseClassName2
;
136 wxObjectConstructorFn m_objectConstructor
;
138 // Pointers to base wxClassInfos: set in InitializeClasses
140 const wxClassInfo
*m_baseInfo1
;
141 const wxClassInfo
*m_baseInfo2
;
143 // class info object live in a linked list:
144 // pointers to its head and the next element in it
146 static wxClassInfo
*sm_first
;
149 // FIXME: this should be private (currently used directly by way too
151 static wxHashTable
*sm_classTable
;
154 // InitializeClasses() helper
155 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
157 DECLARE_NO_COPY_CLASS(wxClassInfo
)
160 WXDLLEXPORT wxObject
*wxCreateDynamicObject(const wxChar
*name
);
162 // ----------------------------------------------------------------------------
163 // Dynamic class macros
164 // ----------------------------------------------------------------------------
166 #define DECLARE_DYNAMIC_CLASS(name) \
168 static wxClassInfo sm_class##name; \
169 virtual wxClassInfo *GetClassInfo() const \
170 { return &name::sm_class##name; }
172 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
173 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
175 // -----------------------------------
176 // for concrete classes
177 // -----------------------------------
179 // Single inheritance with one base class
181 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
182 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
183 { return new name; } \
184 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
185 0, (int) sizeof(name), \
186 (wxObjectConstructorFn) wxConstructorFor##name);
188 // Multiple inheritance with two base classes
190 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
191 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
192 { return new name; } \
193 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
194 wxT(#basename2), (int) sizeof(name), \
195 (wxObjectConstructorFn) wxConstructorFor##name);
197 // -----------------------------------
198 // for abstract classes
199 // -----------------------------------
201 // Single inheritance with one base class
203 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
204 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
205 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
207 // Multiple inheritance with two base classes
209 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
210 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
211 wxT(#basename2), (int) sizeof(name), \
212 (wxObjectConstructorFn) 0);
214 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
215 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
217 // -----------------------------------
218 // for pluggable classes
219 // -----------------------------------
221 // NOTE: this should probably be the very first statement
222 // in the class declaration so wxPluginSentinel is
223 // the first member initialised and the last destroyed.
225 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
227 #if wxUSE_NESTED_CLASSES
229 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
230 class exportdecl name##PluginSentinel { \
232 static const wxString sm_className; \
234 name##PluginSentinel(); \
235 ~name##PluginSentinel(); \
237 name##PluginSentinel m_pluginsentinel;
239 #define _IMPLEMENT_DL_SENTINEL(name) \
240 const wxString name::name##PluginSentinel::sm_className(#name); \
241 name::name##PluginSentinel::name##PluginSentinel() { \
242 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
243 if( e != 0 ) { e->RefObj(); } \
245 name::name##PluginSentinel::~name##PluginSentinel() { \
246 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
247 if( e != 0 ) { e->UnrefObj(); } \
251 #define _DECLARE_DL_SENTINEL(name)
252 #define _IMPLEMENT_DL_SENTINEL(name)
254 #endif // wxUSE_NESTED_CLASSES
256 #define DECLARE_PLUGGABLE_CLASS(name) \
257 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
258 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
259 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
261 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
262 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
263 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
264 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
266 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
267 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
268 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
269 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
270 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
271 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
272 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
273 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
275 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
276 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
277 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
278 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
279 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
280 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
281 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
282 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
285 #define CLASSINFO(name) (&name::sm_class##name)
287 #else // !wxUSE_DYNAMIC_CLASSES
289 // No dynamic class system: so stub out the macros
291 #define DECLARE_DYNAMIC_CLASS(name)
292 #define DECLARE_ABSTRACT_CLASS(name)
293 #define DECLARE_CLASS(name)
294 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
295 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
296 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
297 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
298 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
299 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
301 #define DECLARE_PLUGGABLE_CLASS(name)
302 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
303 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
304 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
305 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
306 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
308 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
309 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
310 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
311 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
312 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
313 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
315 #endif // wxUSE_DYNAMIC_CLASSES
318 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
320 // Just seems a bit nicer-looking (pretend it's not a macro)
321 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
323 // to be replaced by dynamic_cast<> in the future
324 #define wxDynamicCast(obj, className) \
325 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
327 // The 'this' pointer is always true, so use this version
328 // to cast the this pointer and avoid compiler warnings.
329 #define wxDynamicCastThis(className) \
330 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
332 #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__
349 // for some reason Borland seems to need this include.
350 #if wxUSE_STD_IOSTREAM \
351 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
352 && defined(__BORLANDC__)
354 #include <iostream.h>
358 #endif // wxUSE_IOSTREAMH
360 // ----------------------------------------------------------------------------
361 // set up memory debugging macros
362 // ----------------------------------------------------------------------------
365 Which new/delete operator variants do we want?
367 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
368 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
369 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
370 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
371 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
372 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
373 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
376 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
378 // All compilers get this one
379 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
381 // Everyone except Visage gets the next one
382 #ifndef __VISAGECPP__
383 #define _WX_WANT_DELETE_VOID
386 // Only visage gets this one under the correct circumstances
387 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
388 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
391 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
392 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
393 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
396 // Now see who (if anyone) gets the array memory operators
397 #if wxUSE_ARRAY_MEMORY_OPERATORS
399 // Everyone except Visual C++ (cause problems for VC++ - crashes)
400 #if !defined(__VISUALC__)
401 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
404 // Everyone except Visual C++ (cause problems for VC++ - crashes)
405 #if !defined(__VISUALC__)
406 #define _WX_WANT_ARRAY_DELETE_VOID
409 // Only CodeWarrior 6 or higher
410 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
411 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
414 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
416 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
419 // ----------------------------------------------------------------------------
420 // wxObject: the root class of wxWindows object hierarchy
421 // ----------------------------------------------------------------------------
423 class WXDLLEXPORT wxObject
425 DECLARE_ABSTRACT_CLASS(wxObject
)
428 void InitFrom(const wxObject
& other
);
431 wxObject() { m_refData
= NULL
; }
432 virtual ~wxObject() { UnRef(); }
434 wxObject(const wxObject
& other
)
439 wxObject
& operator=(const wxObject
& other
)
441 if ( this != &other
)
449 bool IsKindOf(wxClassInfo
*info
) const;
452 // Turn on the correct set of new and delete operators
454 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
455 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
458 #ifdef _WX_WANT_DELETE_VOID
459 void operator delete ( void * buf
);
462 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
463 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
466 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
467 void operator delete ( void *buf
, const wxChar
*, int );
470 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
471 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
474 #ifdef _WX_WANT_ARRAY_DELETE_VOID
475 void operator delete[] ( void *buf
);
478 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
479 void operator delete[] (void* buf
, const wxChar
*, int );
483 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
484 virtual void Dump(wxSTD ostream
& str
);
487 // ref counted data handling methods
490 wxObjectRefData
*GetRefData() const { return m_refData
; }
491 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
493 // make a 'clone' of the object
494 void Ref(const wxObject
& clone
);
496 // destroy a reference
500 // ensure that our data is not shared with anybody else: if we have no
501 // data, it is created using CreateRefData() below, if we have shared data
502 // it is copied using CloneRefData(), otherwise nothing is done
503 void AllocExclusive();
505 // both methods must be implemented if Unshare() is used, not pure virtual
506 // only because of the backwards compatibility reasons
508 // create a new m_refData
509 virtual wxObjectRefData
*CreateRefData() const;
511 // create a new m_refData initialized with the given one
512 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
514 wxObjectRefData
*m_refData
;
517 // ----------------------------------------------------------------------------
518 // wxObjectRefData: ref counted data meant to be stored in wxObject
519 // ----------------------------------------------------------------------------
521 class WXDLLEXPORT wxObjectRefData
523 friend class wxObject
;
526 wxObjectRefData() : m_count(1) { }
527 virtual ~wxObjectRefData() { }
529 int GetRefCount() const { return m_count
; }
536 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
538 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
541 // ----------------------------------------------------------------------------
542 // more debugging macros
543 // ----------------------------------------------------------------------------
547 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
549 #else // !__WXDEBUG__
550 #define WXDEBUG_NEW new
553 // Redefine new to be the debugging version. This doesn't work with all
554 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
555 // to use the debugging version.
557 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
558 #define new new(__TFILE__,__LINE__)
561 #endif // _WX_OBJECTH__