1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxObject class, plus run-time type information macros
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "object.h"
20 #include "wx/memory.h"
22 class WXDLLEXPORT wxObject
;
24 #if wxUSE_DYNAMIC_CLASSES
26 // #ifdef __GNUWIN32__
35 class WXDLLEXPORT wxClassInfo
;
36 class WXDLLEXPORT wxInputStream
;
37 class WXDLLEXPORT wxOutputStream
;
38 class WXDLLEXPORT wxObjectInputStream
;
39 class WXDLLEXPORT wxObjectOutputStream
;
40 class WXDLLEXPORT wxHashTable
;
41 class WXDLLEXPORT wxObject_Serialize
;
43 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
44 #include "wx/ioswrap.h"
48 * Dynamic object system declarations
51 typedef wxObject
* (*wxObjectConstructorFn
) (void);
53 class WXDLLEXPORT wxClassInfo
56 wxClassInfo(const wxChar
*cName
,
57 const wxChar
*baseName1
,
58 const wxChar
*baseName2
,
60 wxObjectConstructorFn fn
);
62 wxObject
*CreateObject(void);
64 const wxChar
*GetClassName() const { return m_className
; }
65 const wxChar
*GetBaseClassName1() const { return m_baseClassName1
; }
66 const wxChar
*GetBaseClassName2() const { return m_baseClassName2
; }
67 const wxClassInfo
* GetBaseClass1() const { return m_baseInfo1
; }
68 const wxClassInfo
* GetBaseClass2() const { return m_baseInfo2
; }
69 int GetSize() const { return m_objectSize
; }
70 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
71 static const wxClassInfo
* GetFirst() { return sm_first
; }
72 const wxClassInfo
* GetNext() const { return m_next
; }
73 bool IsKindOf(const wxClassInfo
*info
) const;
75 static wxClassInfo
*FindClass(const wxChar
*c
);
77 // Initializes parent pointers and hash table for fast searching.
78 static void InitializeClasses();
80 // Cleans up hash table used for fast searching.
81 static void CleanUpClasses();
84 const wxChar
* m_className
;
85 const wxChar
* m_baseClassName1
;
86 const wxChar
* m_baseClassName2
;
88 wxObjectConstructorFn m_objectConstructor
;
90 // Pointers to base wxClassInfos: set in InitializeClasses
91 const wxClassInfo
* m_baseInfo1
;
92 const wxClassInfo
* m_baseInfo2
;
94 // class info object live in a linked list: pointers to its head and the
96 static wxClassInfo
* sm_first
;
99 static wxHashTable
* sm_classTable
;
102 WXDLLEXPORT wxObject
* wxCreateDynamicObject(const wxChar
*name
);
105 WXDLLEXPORT wxObject
* wxCreateStoredObject( wxInputStream
& stream
);
108 #define DECLARE_DYNAMIC_CLASS(name) \
110 static wxClassInfo sm_class##name;\
111 wxClassInfo *GetClassInfo() const \
112 { return &name::sm_class##name; }
114 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
115 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
118 ////// for concrete classes
121 // Single inheritance with one base class
122 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
123 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
124 { return new name; }\
125 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename), (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
127 // Multiple inheritance with two base classes
128 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
129 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
130 { return new name; }\
131 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename1), (wxChar *) wxT(#basename2), (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
134 ////// for abstract classes
137 // Single inheritance with one base class
138 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
139 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename), \
140 (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL);
142 // Multiple inheritance with two base classes
143 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
144 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename1), \
145 (wxChar *) wxT(#basename2), (int) sizeof(name), (wxObjectConstructorFn) NULL);
147 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
148 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
150 #define CLASSINFO(name) (&name::sm_class##name)
152 #else // !wxUSE_DYNAMIC_CLASSES
154 // No dynamic class system: so stub out the macros
155 #define DECLARE_DYNAMIC_CLASS(name)
156 #define DECLARE_ABSTRACT_CLASS(name)
157 #define DECLARE_CLASS(name)
158 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
159 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
160 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
161 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
162 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
163 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
165 #endif // wxUSE_DYNAMIC_CLASSES/!wxUSE_DYNAMIC_CLASSES
167 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
169 // Just seems a bit nicer-looking (pretend it's not a macro)
170 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
172 // to be replaced by dynamic_cast<> in the future
173 #define wxDynamicCast(obj, className) \
174 ((obj) && ((obj)->IsKindOf(&className::sm_class##className)) \
175 ? (className *)(obj) \
178 // The 'this' pointer is always true, so use this version to cast the this
179 // pointer and avoid compiler warnings.
180 #define wxDynamicCastThis(className) \
181 (IsKindOf(&className::sm_class##className) \
182 ? (className *)(this) \
185 #define wxConstCast(obj, className) ((className *)(obj))
188 inline void wxCheckCast(void *ptr
)
190 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
193 #define wxStaticCast(obj, className) \
194 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
197 #define wxStaticCast(obj, className) ((className *)(obj))
198 #endif // Debug/!Debug
200 // Unfortunately Borland seems to need this include.
201 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
204 #include <iostream.h>
211 class WXDLLEXPORT wxObjectRefData
;
213 class WXDLLEXPORT wxObject
217 // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS
218 static wxClassInfo sm_classwxObject
;
221 virtual ~wxObject(void);
223 virtual wxClassInfo
*GetClassInfo(void) const { return &sm_classwxObject
; }
225 bool IsKindOf(wxClassInfo
*info
) const;
227 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
228 void * operator new (size_t size
, wxChar
* fileName
= NULL
, int lineNum
= 0);
229 # if defined(__VISAGECPP__)
231 void operator delete (void * buf
,const char * _fname
, size_t _line
);
232 # endif //__DEBUG_ALLOC__
233 # else // Everybody else
234 void operator delete (void * buf
);
235 # endif // end of VISAGECPP
238 # if defined(__VISUALC__) && (__VISUALC__ >= 1200)
239 void operator delete(void *buf
, wxChar
*, int);
242 // Causes problems for VC++
243 # if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__)
244 void * operator new[] (size_t size
, wxChar
* fileName
= NULL
, int lineNum
= 0);
245 void operator delete[] (void * buf
);
249 void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
= 0);
250 void * operator new[] (size_t size
) { return operator new[] ( size
, NULL
, 0 ) ; }
251 void operator delete[] (void * buf
);
254 #endif // Debug & memory tracing
256 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
257 virtual void Dump(wxSTD ostream
& str
);
261 virtual void StoreObject( wxObjectOutputStream
&stream
);
262 virtual void LoadObject( wxObjectInputStream
&stream
);
265 // make a 'clone' of the object
266 void Ref(const wxObject
& clone
);
268 // destroy a reference
271 inline wxObjectRefData
*GetRefData(void) const { return m_refData
; }
272 inline void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
275 wxObjectRefData
* m_refData
;
277 wxObject_Serialize
* m_serialObj
;
285 class WXDLLEXPORT wxObjectRefData
{
287 friend class wxObject
;
290 wxObjectRefData(void);
291 virtual ~wxObjectRefData(void);
293 inline int GetRefCount(void) const { return m_count
; }
300 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
303 #define WXDEBUG_NEW new
306 // Redefine new to be the debugging version. This doesn't
307 // work with all compilers, in which case you need to
308 // use WXDEBUG_NEW explicitly if you wish to use the debugging version.
310 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
311 #define new new(__TFILE__,__LINE__)