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 // ----------------------------------------------------------------------------
42 class WXDLLEXPORT wxClassInfo
;
43 class WXDLLEXPORT wxHashTable
;
45 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
46 #include "wx/ioswrap.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 typedef wxObject
*(*wxObjectConstructorFn
)(void);
56 class WXDLLEXPORT wxClassInfo
59 wxClassInfo( const wxChar
*className
,
60 const wxChar
*baseName1
,
61 const wxChar
*baseName2
,
63 wxObjectConstructorFn ctor
)
64 : m_className(className
)
65 , m_baseClassName1(baseName1
)
66 , m_baseClassName2(baseName2
)
68 , 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 static wxHashTable
*sm_classTable
;
128 WXDLLEXPORT wxObject
*wxCreateDynamicObject(const wxChar
*name
);
131 // ----------------------------------------------------------------------------
132 // Dynamic class macros
133 // ----------------------------------------------------------------------------
135 #define DECLARE_DYNAMIC_CLASS(name) \
137 static wxClassInfo sm_class##name; \
138 virtual wxClassInfo *GetClassInfo() const \
139 { return &name::sm_class##name; }
141 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
142 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
144 // -----------------------------------
145 // for concrete classes
146 // -----------------------------------
148 // Single inheritance with one base class
150 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
151 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
152 { return new name; } \
153 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
154 0, (int) sizeof(name), \
155 (wxObjectConstructorFn) wxConstructorFor##name);
157 // Multiple inheritance with two base classes
159 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
160 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
161 { return new name; } \
162 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
163 wxT(#basename2), (int) sizeof(name), \
164 (wxObjectConstructorFn) wxConstructorFor##name);
166 // -----------------------------------
167 // for abstract classes
168 // -----------------------------------
170 // Single inheritance with one base class
172 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
173 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
174 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
176 // Multiple inheritance with two base classes
178 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
179 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
180 wxT(#basename2), (int) sizeof(name), \
181 (wxObjectConstructorFn) 0);
183 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
184 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
186 // -----------------------------------
187 // for pluggable classes
188 // -----------------------------------
190 // NOTE: this should probably be the very first statement
191 // in the class declaration so wxPluginSentinel is
192 // the first member initialised and the last destroyed.
194 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
196 #if wxUSE_NESTED_CLASSES
199 #define _DECLARE_DL_SENTINEL(name) \
200 wxPluginSentinel m_pluginsentinel;
204 #define _DECLARE_DL_SENTINEL(name) \
205 class name##PluginSentinel { \
207 static const wxString sm_className; \
209 name##PluginSentinel(); \
210 ~##name##PluginSentinel(); \
212 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 wxDLManifestEntry *e = (wxDLManifestEntry*) wxDLManifestEntry::ms_classes.Get(#name); \
219 if( e != 0 ) { e->RefObj(); } \
221 name::name##PluginSentinel::~##name##PluginSentinel() { \
222 wxDLManifestEntry *e = (wxDLManifestEntry*) wxDLManifestEntry::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)
235 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
236 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name)
238 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
239 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
240 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
241 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
243 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
244 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
245 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
246 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
249 #define CLASSINFO(name) (&name::sm_class##name)
251 #else // !wxUSE_DYNAMIC_CLASSES
253 // No dynamic class system: so stub out the macros
255 #define DECLARE_DYNAMIC_CLASS(name)
256 #define DECLARE_ABSTRACT_CLASS(name)
257 #define DECLARE_CLASS(name)
258 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
259 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
260 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
261 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
262 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
263 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
265 #define DECLARE_PLUGGABLE_CLASS(name)
266 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
267 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
268 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
269 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
270 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
272 #endif // wxUSE_DYNAMIC_CLASSES
275 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
277 // Just seems a bit nicer-looking (pretend it's not a macro)
279 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
281 // to be replaced by dynamic_cast<> in the future
283 #define wxDynamicCast(obj, className) \
284 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
286 // The 'this' pointer is always true, so use this version
287 // to cast the this pointer and avoid compiler warnings.
289 #define wxDynamicCastThis(className) \
290 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
292 #define wxConstCast(obj, className) ((className *)(obj))
296 inline void wxCheckCast(void *ptr
)
298 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
300 #define wxStaticCast(obj, className) \
301 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
303 #else // !__WXDEBUG__
304 #define wxStaticCast(obj, className) ((className *)(obj))
306 #endif // __WXDEBUG__
309 // Unfortunately Borland seems to need this include.
311 #if wxUSE_STD_IOSTREAM \
312 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
313 && defined(__BORLANDC__)
315 #include <iostream.h>
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 class WXDLLEXPORT wxObjectRefData
;
328 class WXDLLEXPORT wxObject
330 DECLARE_ABSTRACT_CLASS(wxObject
)
333 wxObject() : m_refData(0) {}
334 virtual ~wxObject() { UnRef(); }
336 bool IsKindOf(wxClassInfo
*info
) const;
338 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
339 void *operator new (size_t size
, wxChar
*fileName
= 0, int lineNum
= 0);
341 #ifndef __VISAGECPP__
342 void operator delete (void * buf
);
343 #elif __DEBUG_ALLOC__
344 void operator delete (void *buf
, const char *_fname
, size_t _line
);
349 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
350 void operator delete(void *buf
, wxChar
*, int);
353 // Causes problems for VC++
355 #if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__)
356 void *operator new[] (size_t size
, wxChar
*fileName
= 0, int lineNum
= 0);
357 void operator delete[] (void *buf
);
361 void *operator new[] (size_t size
, wxChar
*fileName
, int lineNum
= 0);
362 void *operator new[] (size_t size
) { return operator new[] ( size
, 0, 0 ) ; }
363 void operator delete[] (void *buf
);
366 #endif // Debug & memory tracing
369 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
370 virtual void Dump(wxSTD ostream
& str
);
373 // make a 'clone' of the object
375 void Ref(const wxObject
& clone
);
377 // destroy a reference
381 inline wxObjectRefData
*GetRefData() const { return m_refData
; }
382 inline void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
385 wxObjectRefData
*m_refData
;
389 class WXDLLEXPORT wxObjectRefData
391 friend class wxObject
;
394 wxObjectRefData() : m_count(1) {}
395 virtual ~wxObjectRefData() {}
397 inline int GetRefCount() const { return m_count
; }
403 inline wxObject
*wxCheckDynamicCast(wxObject
*obj
, wxClassInfo
*classInfo
)
405 return obj
&& obj
->GetClassInfo()->IsKindOf(classInfo
) ? obj
: 0;
410 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
413 #define WXDEBUG_NEW new
416 // Redefine new to be the debugging version. This doesn't
417 // work with all compilers, in which case you need to
418 // use WXDEBUG_NEW explicitly if you wish to use the debugging version.
420 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
421 #define new new(__TFILE__,__LINE__)
424 #endif // _WX_OBJECTH__