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 // ----------------------------------------------------------------------------
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 WXDLLIMPEXP_BASE wxClassInfo
;
45 class WXDLLIMPEXP_BASE wxHashTable
;
46 class WXDLLIMPEXP_BASE wxObjectRefData
;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 typedef wxObject
*(*wxObjectConstructorFn
)(void);
54 class WXDLLIMPEXP_BASE wxClassInfo
57 wxClassInfo( const wxChar
*className
,
58 const wxChar
*baseName1
,
59 const wxChar
*baseName2
,
61 wxObjectConstructorFn ctor
)
62 : m_className(className
)
63 , m_baseClassName1(baseName1
)
64 , m_baseClassName2(baseName2
)
66 , m_objectConstructor(ctor
)
74 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
76 const wxChar
*GetClassName() const { return m_className
; }
77 const wxChar
*GetBaseClassName1() const { return m_baseClassName1
; }
78 const wxChar
*GetBaseClassName2() const { return m_baseClassName2
; }
79 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
80 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
81 int GetSize() const { return m_objectSize
; }
83 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
84 static const wxClassInfo
*GetFirst() { return sm_first
; }
85 const wxClassInfo
*GetNext() const { return m_next
; }
86 static wxClassInfo
*FindClass(const wxChar
*className
);
88 // Climb upwards through inheritance hierarchy.
89 // Dual inheritance is catered for.
91 bool IsKindOf(const wxClassInfo
*info
) const
95 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
96 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
99 // Initializes parent pointers and hash table for fast searching.
101 static void InitializeClasses();
103 // Cleans up hash table used for fast searching.
105 static void CleanUpClasses();
108 const wxChar
*m_className
;
109 const wxChar
*m_baseClassName1
;
110 const wxChar
*m_baseClassName2
;
112 wxObjectConstructorFn m_objectConstructor
;
114 // Pointers to base wxClassInfos: set in InitializeClasses
116 const wxClassInfo
*m_baseInfo1
;
117 const wxClassInfo
*m_baseInfo2
;
119 // class info object live in a linked list:
120 // pointers to its head and the next element in it
122 static wxClassInfo
*sm_first
;
125 // FIXME: this should be private (currently used directly by way too
127 static wxHashTable
*sm_classTable
;
130 // InitializeClasses() helper
131 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
133 DECLARE_NO_COPY_CLASS(wxClassInfo
)
136 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
138 // ----------------------------------------------------------------------------
139 // Dynamic class macros
140 // ----------------------------------------------------------------------------
142 #define DECLARE_DYNAMIC_CLASS(name) \
144 static wxClassInfo sm_class##name; \
145 virtual wxClassInfo *GetClassInfo() const \
146 { return &name::sm_class##name; }
148 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
149 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
151 // -----------------------------------
152 // for concrete classes
153 // -----------------------------------
155 // Single inheritance with one base class
157 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
158 wxObject* wxConstructorFor##name() \
159 { return new name; } \
160 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
161 0, (int) sizeof(name), \
162 (wxObjectConstructorFn) wxConstructorFor##name);
164 // Multiple inheritance with two base classes
166 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
167 wxObject* wxConstructorFor##name() \
168 { return new name; } \
169 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
170 wxT(#basename2), (int) sizeof(name), \
171 (wxObjectConstructorFn) wxConstructorFor##name);
173 // -----------------------------------
174 // for abstract classes
175 // -----------------------------------
177 // Single inheritance with one base class
179 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
180 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
181 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
183 // Multiple inheritance with two base classes
185 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
186 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
187 wxT(#basename2), (int) sizeof(name), \
188 (wxObjectConstructorFn) 0);
190 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
191 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
193 // -----------------------------------
194 // for pluggable classes
195 // -----------------------------------
197 // NOTE: this should probably be the very first statement
198 // in the class declaration so wxPluginSentinel is
199 // the first member initialised and the last destroyed.
201 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
203 #if wxUSE_NESTED_CLASSES
205 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
206 class exportdecl name##PluginSentinel { \
208 static const wxString sm_className; \
210 name##PluginSentinel(); \
211 ~name##PluginSentinel(); \
213 name##PluginSentinel m_pluginsentinel;
215 #define _IMPLEMENT_DL_SENTINEL(name) \
216 const wxString name::name##PluginSentinel::sm_className(#name); \
217 name::name##PluginSentinel::name##PluginSentinel() { \
218 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
219 if( e != 0 ) { e->RefObj(); } \
221 name::name##PluginSentinel::~name##PluginSentinel() { \
222 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
223 if( e != 0 ) { e->UnrefObj(); } \
227 #define _DECLARE_DL_SENTINEL(name)
228 #define _IMPLEMENT_DL_SENTINEL(name)
230 #endif // wxUSE_NESTED_CLASSES
232 #define DECLARE_PLUGGABLE_CLASS(name) \
233 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
234 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
235 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
237 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
238 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
239 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
240 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
242 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
243 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
244 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
245 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
246 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
247 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
248 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
249 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
251 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
252 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
253 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
254 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
255 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
256 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
257 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
258 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
261 #define CLASSINFO(name) (&name::sm_class##name)
263 #else // !wxUSE_DYNAMIC_CLASSES
265 // No dynamic class system: so stub out the macros
267 #define DECLARE_DYNAMIC_CLASS(name)
268 #define DECLARE_ABSTRACT_CLASS(name)
269 #define DECLARE_CLASS(name)
270 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
271 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
272 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
273 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
274 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
275 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
277 #define DECLARE_PLUGGABLE_CLASS(name)
278 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
279 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
280 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
281 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
282 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
284 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
285 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
286 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
287 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
288 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
289 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
291 #endif // wxUSE_DYNAMIC_CLASSES
294 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
296 // Just seems a bit nicer-looking (pretend it's not a macro)
297 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
299 // to be replaced by dynamic_cast<> in the future
300 #define wxDynamicCast(obj, className) \
301 ((className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className))
303 // The 'this' pointer is always true, so use this version
304 // to cast the this pointer and avoid compiler warnings.
305 #define wxDynamicCastThis(className) \
306 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
308 #ifdef HAVE_CONST_CAST
309 #define wxConstCast(obj, className) const_cast<className *>(obj)
311 #define wxConstCast(obj, className) ((className *)(obj))
316 inline void wxCheckCast(void *ptr
)
318 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
320 #define wxStaticCast(obj, className) \
321 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
323 #else // !__WXDEBUG__
324 #define wxStaticCast(obj, className) ((className *)(obj))
326 #endif // __WXDEBUG__
328 // ----------------------------------------------------------------------------
329 // set up memory debugging macros
330 // ----------------------------------------------------------------------------
333 Which new/delete operator variants do we want?
335 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
336 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
337 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
338 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
339 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
340 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
341 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
344 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
346 // All compilers get this one
347 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
349 // Everyone except Visage gets the next one
350 #ifndef __VISAGECPP__
351 #define _WX_WANT_DELETE_VOID
354 // Only visage gets this one under the correct circumstances
355 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
356 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
359 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
360 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
361 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
364 // Now see who (if anyone) gets the array memory operators
365 #if wxUSE_ARRAY_MEMORY_OPERATORS
367 // Everyone except Visual C++ (cause problems for VC++ - crashes)
368 #if !defined(__VISUALC__)
369 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
372 // Everyone except Visual C++ (cause problems for VC++ - crashes)
373 #if !defined(__VISUALC__)
374 #define _WX_WANT_ARRAY_DELETE_VOID
377 // Only CodeWarrior 6 or higher
378 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
379 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
382 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
384 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
386 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
387 // needed by wxObject::Dump
388 #include "wx/iosfwrap.h"
391 // ----------------------------------------------------------------------------
392 // wxObject: the root class of wxWindows object hierarchy
393 // ----------------------------------------------------------------------------
395 class WXDLLIMPEXP_BASE wxObject
397 DECLARE_ABSTRACT_CLASS(wxObject
)
400 void InitFrom(const wxObject
& other
);
403 wxObject() { m_refData
= NULL
; }
404 virtual ~wxObject() { UnRef(); }
406 wxObject(const wxObject
& other
)
411 wxObject
& operator=(const wxObject
& other
)
413 if ( this != &other
)
421 bool IsKindOf(wxClassInfo
*info
) const;
424 // Turn on the correct set of new and delete operators
426 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
427 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
430 #ifdef _WX_WANT_DELETE_VOID
431 void operator delete ( void * buf
);
434 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
435 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
438 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
439 void operator delete ( void *buf
, const wxChar
*, int );
442 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
443 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
446 #ifdef _WX_WANT_ARRAY_DELETE_VOID
447 void operator delete[] ( void *buf
);
450 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
451 void operator delete[] (void* buf
, const wxChar
*, int );
455 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
456 virtual void Dump(wxSTD ostream
& str
);
459 // ref counted data handling methods
462 wxObjectRefData
*GetRefData() const { return m_refData
; }
463 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
465 // make a 'clone' of the object
466 void Ref(const wxObject
& clone
);
468 // destroy a reference
472 // ensure that our data is not shared with anybody else: if we have no
473 // data, it is created using CreateRefData() below, if we have shared data
474 // it is copied using CloneRefData(), otherwise nothing is done
475 void AllocExclusive();
477 // both methods must be implemented if Unshare() is used, not pure virtual
478 // only because of the backwards compatibility reasons
480 // create a new m_refData
481 virtual wxObjectRefData
*CreateRefData() const;
483 // create a new m_refData initialized with the given one
484 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
486 wxObjectRefData
*m_refData
;
489 // ----------------------------------------------------------------------------
490 // wxObjectRefData: ref counted data meant to be stored in wxObject
491 // ----------------------------------------------------------------------------
493 class WXDLLIMPEXP_BASE wxObjectRefData
495 friend class WXDLLIMPEXP_BASE wxObject
;
498 wxObjectRefData() : m_count(1) { }
499 virtual ~wxObjectRefData() { }
501 int GetRefCount() const { return m_count
; }
508 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
510 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
513 // ----------------------------------------------------------------------------
514 // more debugging macros
515 // ----------------------------------------------------------------------------
519 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
521 #else // !__WXDEBUG__
522 #define WXDEBUG_NEW new
525 // Redefine new to be the debugging version. This doesn't work with all
526 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
527 // to use the debugging version.
529 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
530 #define new new(__TFILE__,__LINE__)
533 #endif // _WX_OBJECTH__