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_DYNAMIC_CLASS_NO_ASSIGN(name) \
149 DECLARE_NO_ASSIGN_CLASS(name) \
150 DECLARE_DYNAMIC_CLASS(name)
152 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
153 DECLARE_NO_COPY_CLASS(name) \
154 DECLARE_DYNAMIC_CLASS(name)
156 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
157 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
159 // -----------------------------------
160 // for concrete classes
161 // -----------------------------------
163 // Single inheritance with one base class
165 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
166 wxObject* wxConstructorFor##name() \
167 { return new name; } \
168 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
169 0, (int) sizeof(name), \
170 (wxObjectConstructorFn) wxConstructorFor##name);
172 // Multiple inheritance with two base classes
174 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
175 wxObject* wxConstructorFor##name() \
176 { return new name; } \
177 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
178 wxT(#basename2), (int) sizeof(name), \
179 (wxObjectConstructorFn) wxConstructorFor##name);
181 // -----------------------------------
182 // for abstract classes
183 // -----------------------------------
185 // Single inheritance with one base class
187 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
188 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
189 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
191 // Multiple inheritance with two base classes
193 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
194 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
195 wxT(#basename2), (int) sizeof(name), \
196 (wxObjectConstructorFn) 0);
198 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
199 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
201 // -----------------------------------
202 // for pluggable classes
203 // -----------------------------------
205 // NOTE: this should probably be the very first statement
206 // in the class declaration so wxPluginSentinel is
207 // the first member initialised and the last destroyed.
209 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
211 #if wxUSE_NESTED_CLASSES
213 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
214 class exportdecl name##PluginSentinel { \
216 static const wxString sm_className; \
218 name##PluginSentinel(); \
219 ~name##PluginSentinel(); \
221 name##PluginSentinel m_pluginsentinel;
223 #define _IMPLEMENT_DL_SENTINEL(name) \
224 const wxString name::name##PluginSentinel::sm_className(#name); \
225 name::name##PluginSentinel::name##PluginSentinel() { \
226 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
227 if( e != 0 ) { e->RefObj(); } \
229 name::name##PluginSentinel::~name##PluginSentinel() { \
230 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
231 if( e != 0 ) { e->UnrefObj(); } \
235 #define _DECLARE_DL_SENTINEL(name)
236 #define _IMPLEMENT_DL_SENTINEL(name)
238 #endif // wxUSE_NESTED_CLASSES
240 #define DECLARE_PLUGGABLE_CLASS(name) \
241 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
242 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
243 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
245 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
246 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
247 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
248 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
250 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
251 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
252 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
253 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
254 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
255 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
256 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
257 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
259 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
260 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
261 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
262 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
263 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
264 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
265 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
266 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
269 #define CLASSINFO(name) (&name::sm_class##name)
271 #else // !wxUSE_DYNAMIC_CLASSES
273 // No dynamic class system: so stub out the macros
275 #define DECLARE_DYNAMIC_CLASS(name)
276 #define DECLARE_ABSTRACT_CLASS(name)
277 #define DECLARE_CLASS(name)
278 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
279 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
280 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
281 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
282 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
283 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
285 #define DECLARE_PLUGGABLE_CLASS(name)
286 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
287 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
288 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
289 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
290 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
292 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
293 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
294 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
295 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
296 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
297 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
299 #endif // wxUSE_DYNAMIC_CLASSES
302 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
304 // Just seems a bit nicer-looking (pretend it's not a macro)
305 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
307 // to be replaced by dynamic_cast<> in the future
308 #define wxDynamicCast(obj, className) \
309 ((className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className))
311 // The 'this' pointer is always true, so use this version
312 // to cast the this pointer and avoid compiler warnings.
313 #define wxDynamicCastThis(className) \
314 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
316 #ifdef HAVE_CONST_CAST
317 #define wxConstCast(obj, className) const_cast<className *>(obj)
319 #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__
336 // ----------------------------------------------------------------------------
337 // set up memory debugging macros
338 // ----------------------------------------------------------------------------
341 Which new/delete operator variants do we want?
343 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
344 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
345 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
346 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
347 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
348 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
349 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
352 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
354 // All compilers get this one
355 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
357 // Everyone except Visage gets the next one
358 #ifndef __VISAGECPP__
359 #define _WX_WANT_DELETE_VOID
362 // Only visage gets this one under the correct circumstances
363 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
364 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
367 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
368 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
369 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
372 // Now see who (if anyone) gets the array memory operators
373 #if wxUSE_ARRAY_MEMORY_OPERATORS
375 // Everyone except Visual C++ (cause problems for VC++ - crashes)
376 #if !defined(__VISUALC__)
377 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
380 // Everyone except Visual C++ (cause problems for VC++ - crashes)
381 #if !defined(__VISUALC__)
382 #define _WX_WANT_ARRAY_DELETE_VOID
385 // Only CodeWarrior 6 or higher
386 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
387 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
390 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
392 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
394 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
395 // needed by wxObject::Dump
396 #include "wx/iosfwrap.h"
399 // ----------------------------------------------------------------------------
400 // wxObject: the root class of wxWindows object hierarchy
401 // ----------------------------------------------------------------------------
403 class WXDLLIMPEXP_BASE wxObject
405 DECLARE_ABSTRACT_CLASS(wxObject
)
408 void InitFrom(const wxObject
& other
);
411 wxObject() { m_refData
= NULL
; }
412 virtual ~wxObject() { UnRef(); }
414 wxObject(const wxObject
& other
)
419 wxObject
& operator=(const wxObject
& other
)
421 if ( this != &other
)
429 bool IsKindOf(wxClassInfo
*info
) const;
432 // Turn on the correct set of new and delete operators
434 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
435 void *operator new ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
438 #ifdef _WX_WANT_DELETE_VOID
439 void operator delete ( void * buf
);
442 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
443 void operator delete ( void *buf
, const char *_fname
, size_t _line
);
446 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
447 void operator delete ( void *buf
, const wxChar
*, int );
450 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
451 void *operator new[] ( size_t size
, const wxChar
*fileName
= NULL
, int lineNum
= 0 );
454 #ifdef _WX_WANT_ARRAY_DELETE_VOID
455 void operator delete[] ( void *buf
);
458 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
459 void operator delete[] (void* buf
, const wxChar
*, int );
463 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
464 virtual void Dump(wxSTD ostream
& str
);
467 // ref counted data handling methods
470 wxObjectRefData
*GetRefData() const { return m_refData
; }
471 void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
473 // make a 'clone' of the object
474 void Ref(const wxObject
& clone
);
476 // destroy a reference
480 // ensure that our data is not shared with anybody else: if we have no
481 // data, it is created using CreateRefData() below, if we have shared data
482 // it is copied using CloneRefData(), otherwise nothing is done
483 void AllocExclusive();
485 // both methods must be implemented if Unshare() is used, not pure virtual
486 // only because of the backwards compatibility reasons
488 // create a new m_refData
489 virtual wxObjectRefData
*CreateRefData() const;
491 // create a new m_refData initialized with the given one
492 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
494 wxObjectRefData
*m_refData
;
497 // ----------------------------------------------------------------------------
498 // wxObjectRefData: ref counted data meant to be stored in wxObject
499 // ----------------------------------------------------------------------------
501 class WXDLLIMPEXP_BASE wxObjectRefData
503 friend class WXDLLIMPEXP_BASE wxObject
;
506 wxObjectRefData() : m_count(1) { }
507 virtual ~wxObjectRefData() { }
509 int GetRefCount() const { return m_count
; }
516 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
518 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: NULL
;
521 // ----------------------------------------------------------------------------
522 // more debugging macros
523 // ----------------------------------------------------------------------------
527 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
529 #else // !__WXDEBUG__
530 #define WXDEBUG_NEW new
533 // Redefine new to be the debugging version. This doesn't work with all
534 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
535 // to use the debugging version.
537 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
538 #define new new(__TFILE__,__LINE__)
541 #endif // _WX_OBJECTH__