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 /////////////////////////////////////////////////////////////////////////////
16 #if defined(__GNUG__) && !defined(__APPLE__)
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
)
79 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
81 const wxChar
*GetClassName() const { return m_className
; }
82 const wxChar
*GetBaseClassName1() const { return m_baseClassName1
; }
83 const wxChar
*GetBaseClassName2() const { return m_baseClassName2
; }
84 const wxClassInfo
*GetBaseClass1() const { return m_baseInfo1
; }
85 const wxClassInfo
*GetBaseClass2() const { return m_baseInfo2
; }
86 int GetSize() const { return m_objectSize
; }
88 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
89 static const wxClassInfo
*GetFirst() { return sm_first
; }
90 const wxClassInfo
*GetNext() const { return m_next
; }
91 static wxClassInfo
*FindClass(const wxChar
*className
);
93 // Climb upwards through inheritance hierarchy.
94 // Dual inheritance is catered for.
96 bool IsKindOf(const wxClassInfo
*info
) const
100 ( m_baseInfo1
&& m_baseInfo1
->IsKindOf(info
) ) ||
101 ( m_baseInfo2
&& m_baseInfo2
->IsKindOf(info
) ) );
104 // Initializes parent pointers and hash table for fast searching.
106 static void InitializeClasses();
108 // Cleans up hash table used for fast searching.
110 static void CleanUpClasses();
113 const wxChar
*m_className
;
114 const wxChar
*m_baseClassName1
;
115 const wxChar
*m_baseClassName2
;
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 WXDLLEXPORT wxObject
*wxCreateDynamicObject(const wxChar
*name
);
143 // ----------------------------------------------------------------------------
144 // Dynamic class macros
145 // ----------------------------------------------------------------------------
147 #define DECLARE_DYNAMIC_CLASS(name) \
149 static wxClassInfo sm_class##name; \
150 virtual wxClassInfo *GetClassInfo() const \
151 { return &name::sm_class##name; }
153 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
154 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
156 // -----------------------------------
157 // for concrete classes
158 // -----------------------------------
160 // Single inheritance with one base class
162 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
163 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
164 { return new name; } \
165 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
166 0, (int) sizeof(name), \
167 (wxObjectConstructorFn) wxConstructorFor##name);
169 // Multiple inheritance with two base classes
171 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
172 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
173 { return new name; } \
174 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
175 wxT(#basename2), (int) sizeof(name), \
176 (wxObjectConstructorFn) wxConstructorFor##name);
178 // -----------------------------------
179 // for abstract classes
180 // -----------------------------------
182 // Single inheritance with one base class
184 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
185 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
186 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
188 // Multiple inheritance with two base classes
190 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
191 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
192 wxT(#basename2), (int) sizeof(name), \
193 (wxObjectConstructorFn) 0);
195 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
196 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
198 // -----------------------------------
199 // for pluggable classes
200 // -----------------------------------
202 // NOTE: this should probably be the very first statement
203 // in the class declaration so wxPluginSentinel is
204 // the first member initialised and the last destroyed.
206 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
208 #if wxUSE_NESTED_CLASSES
210 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
211 class exportdecl name##PluginSentinel { \
213 static const wxString sm_className; \
215 name##PluginSentinel(); \
216 ~name##PluginSentinel(); \
218 name##PluginSentinel m_pluginsentinel;
220 #define _IMPLEMENT_DL_SENTINEL(name) \
221 const wxString name::name##PluginSentinel::sm_className(#name); \
222 name::name##PluginSentinel::name##PluginSentinel() { \
223 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
224 if( e != 0 ) { e->RefObj(); } \
226 name::name##PluginSentinel::~name##PluginSentinel() { \
227 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
228 if( e != 0 ) { e->UnrefObj(); } \
232 #define _DECLARE_DL_SENTINEL(name)
233 #define _IMPLEMENT_DL_SENTINEL(name)
235 #endif // wxUSE_NESTED_CLASSES
237 #define DECLARE_PLUGGABLE_CLASS(name) \
238 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
239 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
240 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
242 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
243 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
244 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
245 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
247 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
248 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
249 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
250 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
251 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
252 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
253 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
254 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
256 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
257 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
258 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
259 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
260 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
261 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
262 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
263 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
266 #define CLASSINFO(name) (&name::sm_class##name)
268 #else // !wxUSE_DYNAMIC_CLASSES
270 // No dynamic class system: so stub out the macros
272 #define DECLARE_DYNAMIC_CLASS(name)
273 #define DECLARE_ABSTRACT_CLASS(name)
274 #define DECLARE_CLASS(name)
275 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
276 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
277 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
278 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
279 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
280 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
282 #define DECLARE_PLUGGABLE_CLASS(name)
283 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
284 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
285 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
286 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
287 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
289 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
290 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
291 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
292 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
293 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
294 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
296 #endif // wxUSE_DYNAMIC_CLASSES
299 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
301 // Just seems a bit nicer-looking (pretend it's not a macro)
302 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
304 // to be replaced by dynamic_cast<> in the future
305 #define wxDynamicCast(obj, className) \
306 ((className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className))
308 // The 'this' pointer is always true, so use this version
309 // to cast the this pointer and avoid compiler warnings.
310 #define wxDynamicCastThis(className) \
311 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
313 #define wxConstCast(obj, className) ((className *)(obj))
317 inline void wxCheckCast(void *ptr
)
319 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
321 #define wxStaticCast(obj, className) \
322 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
324 #else // !__WXDEBUG__
325 #define wxStaticCast(obj, className) ((className *)(obj))
327 #endif // __WXDEBUG__
330 // for some reason Borland seems to need this include.
331 #if wxUSE_STD_IOSTREAM \
332 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
333 && defined(__BORLANDC__)
335 #include <iostream.h>
339 #endif // wxUSE_IOSTREAMH
341 // ----------------------------------------------------------------------------
342 // set up memory debugging macros
343 // ----------------------------------------------------------------------------
346 Which new/delete operator variants do we want?
348 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
349 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
350 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
351 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
352 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
353 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
354 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
357 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
359 // All compilers get this one
360 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
362 // Everyone except Visage gets the next one
363 #ifndef __VISAGECPP__
364 #define _WX_WANT_DELETE_VOID
367 // Only visage gets this one under the correct circumstances
368 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
369 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
372 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
373 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
374 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
377 // Now see who (if anyone) gets the array memory operators
378 #if wxUSE_ARRAY_MEMORY_OPERATORS
380 // Everyone except Visual C++ (cause problems for VC++ - crashes)
381 #if !defined(__VISUALC__)
382 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
385 // Everyone except Visual C++ (cause problems for VC++ - crashes)
386 #if !defined(__VISUALC__)
387 #define _WX_WANT_ARRAY_DELETE_VOID
390 // Only CodeWarrior 6 or higher
391 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
392 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
395 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
397 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
400 // ----------------------------------------------------------------------------
401 // wxObject: the root class of wxWindows object hierarchy
402 // ----------------------------------------------------------------------------
404 class WXDLLEXPORT wxObject
406 DECLARE_ABSTRACT_CLASS(wxObject
)
409 void InitFrom(const wxObject
& other
);
412 wxObject() { m_refData
= NULL
; }
413 virtual ~wxObject() { UnRef(); }
415 wxObject(const wxObject
& other
)
420 wxObject
& operator=(const wxObject
& other
)
422 if ( this != &other
)
430 bool IsKindOf(wxClassInfo
*info
) const;
433 // Turn on the correct set of new and delete operators
435 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
436 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
439 #ifdef _WX_WANT_DELETE_VOID
440 void operator delete ( void * buf
);
443 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
444 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
447 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
448 void operator delete ( void *buf
, const wxChar
*, int );
451 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
452 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
455 #ifdef _WX_WANT_ARRAY_DELETE_VOID
456 void operator delete[] ( void *buf
);
459 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
460 void operator delete[] (void* buf
, const wxChar
*, int );
464 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
465 virtual void Dump(wxSTD ostream
& str
);
468 // ref counted data handling methods
471 wxObjectRefData
*GetRefData() const { return m_refData
; }
472 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
474 // make a 'clone' of the object
475 void Ref(const wxObject
& clone
);
477 // destroy a reference
481 // ensure that our data is not shared with anybody else: if we have no
482 // data, it is created using CreateRefData() below, if we have shared data
483 // it is copied using CloneRefData(), otherwise nothing is done
484 void AllocExclusive();
486 // both methods must be implemented if Unshare() is used, not pure virtual
487 // only because of the backwards compatibility reasons
489 // create a new m_refData
490 virtual wxObjectRefData
*CreateRefData() const;
492 // create a new m_refData initialized with the given one
493 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
495 wxObjectRefData
*m_refData
;
498 // ----------------------------------------------------------------------------
499 // wxObjectRefData: ref counted data meant to be stored in wxObject
500 // ----------------------------------------------------------------------------
502 class WXDLLEXPORT wxObjectRefData
504 friend class wxObject
;
507 wxObjectRefData() : m_count(1) { }
508 virtual ~wxObjectRefData() { }
510 int GetRefCount() const { return m_count
; }
517 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
519 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
522 // ----------------------------------------------------------------------------
523 // more debugging macros
524 // ----------------------------------------------------------------------------
528 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
530 #else // !__WXDEBUG__
531 #define WXDEBUG_NEW new
534 // Redefine new to be the debugging version. This doesn't work with all
535 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
536 // to use the debugging version.
538 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
539 #define new new(__TFILE__,__LINE__)
542 #endif // _WX_OBJECTH__