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(wxChar
*cName
, wxChar
*baseName1
, wxChar
*baseName2
, int sz
, wxObjectConstructorFn fn
);
58 wxObject
*CreateObject(void);
60 inline wxChar
*GetClassName(void) const { return m_className
; }
61 inline wxChar
*GetBaseClassName1(void) const { return m_baseClassName1
; }
62 inline wxChar
*GetBaseClassName2(void) const { return m_baseClassName2
; }
63 inline wxClassInfo
* GetBaseClass1() const { return m_baseInfo1
; }
64 inline wxClassInfo
* GetBaseClass2() const { return m_baseInfo2
; }
65 inline int GetSize(void) const { return m_objectSize
; }
66 inline wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
67 static inline wxClassInfo
* GetFirst() { return sm_first
; }
68 inline wxClassInfo
* GetNext() const { return m_next
; }
69 bool IsKindOf(wxClassInfo
*info
) const;
71 static wxClassInfo
*FindClass(wxChar
*c
);
73 // Initializes parent pointers and hash table for fast searching.
74 static void InitializeClasses(void);
76 // Cleans up hash table used for fast searching.
77 static void CleanUpClasses(void);
81 wxChar
* m_baseClassName1
;
82 wxChar
* m_baseClassName2
;
84 wxObjectConstructorFn m_objectConstructor
;
86 // Pointers to base wxClassInfos: set in InitializeClasses
87 // called from wx_main.cc
88 wxClassInfo
* m_baseInfo1
;
89 wxClassInfo
* m_baseInfo2
;
91 static wxClassInfo
* sm_first
;
94 static wxHashTable
* sm_classTable
;
97 WXDLLEXPORT wxObject
* wxCreateDynamicObject(const wxChar
*name
);
100 WXDLLEXPORT wxObject
* wxCreateStoredObject( wxInputStream
& stream
);
103 #define DECLARE_DYNAMIC_CLASS(name) \
105 static wxClassInfo sm_class##name;\
106 wxClassInfo *GetClassInfo() const \
107 { return &name::sm_class##name; }
109 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
110 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
113 ////// for concrete classes
116 // Single inheritance with one base class
117 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
118 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void); \
119 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
120 { return new name; }\
121 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename), (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
123 // Multiple inheritance with two base classes
124 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
125 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \
126 { return new name; }\
127 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename1), (wxChar *) wxT(#basename2), (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
130 ////// for abstract classes
133 // Single inheritance with one base class
134 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
135 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename), \
136 (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL);
138 // Multiple inheritance with two base classes
139 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
140 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename1), \
141 (wxChar *) wxT(#basename2), (int) sizeof(name), (wxObjectConstructorFn) NULL);
143 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
144 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
146 #define CLASSINFO(name) (&name::sm_class##name)
148 #else // !wxUSE_DYNAMIC_CLASSES
150 // No dynamic class system: so stub out the macros
151 #define DECLARE_DYNAMIC_CLASS(name)
152 #define DECLARE_ABSTRACT_CLASS(name)
153 #define DECLARE_CLASS(name)
154 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
155 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
156 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
157 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
158 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
159 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
161 #endif // wxUSE_DYNAMIC_CLASSES/!wxUSE_DYNAMIC_CLASSES
163 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
165 // Just seems a bit nicer-looking (pretend it's not a macro)
166 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
168 // to be replaced by dynamic_cast<> in the future
169 #define wxDynamicCast(obj, className) \
170 ((obj) && ((obj)->IsKindOf(&className::sm_class##className)) \
171 ? (className *)(obj) \
174 #define wxConstCast(obj, className) ((className *)(obj))
177 inline void wxCheckCast(void *ptr
)
179 wxASSERT_MSG( ptr
, _T("wxStaticCast() used incorrectly") );
182 #define wxStaticCast(obj, className) \
183 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
186 #define wxStaticCast(obj, className) ((className *)(obj))
187 #endif // Debug/!Debug
189 // Unfortunately Borland seems to need this include.
192 #include <iostream.h>
198 class WXDLLEXPORT wxObjectRefData
;
200 class WXDLLEXPORT wxObject
204 // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS
205 static wxClassInfo sm_classwxObject
;
208 virtual ~wxObject(void);
210 virtual wxClassInfo
*GetClassInfo(void) const { return &sm_classwxObject
; }
211 wxObject
*Clone(void) const;
212 virtual void CopyObject(wxObject
& object_dest
) const;
214 bool IsKindOf(wxClassInfo
*info
) const;
216 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
217 void * operator new (size_t size
, wxChar
* fileName
= NULL
, int lineNum
= 0);
219 #if defined(__VISAGECPP__)
221 void operator delete (void * buf
,const char * _fname
, size_t _line
);
222 #endif //__DEBUG_ALLOC__
224 void operator delete (void * buf
);
226 // defined(__VISAGECPP__)
229 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
230 void operator delete(void *buf
, wxChar
*, int);
233 // Causes problems for VC++
234 #if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__)
235 void * operator new[] (size_t size
, wxChar
* fileName
= NULL
, int lineNum
= 0);
236 void operator delete[] (void * buf
);
240 void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
= 0);
241 void * operator new[] (size_t size
) { return operator new[] ( size
, NULL
, 0 ) ; }
242 void operator delete[] (void * buf
);
245 #endif // Debug & memory tracing
247 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
248 virtual void Dump(ostream
& str
);
252 virtual void StoreObject( wxObjectOutputStream
&stream
);
253 virtual void LoadObject( wxObjectInputStream
&stream
);
256 // make a 'clone' of the object
257 void Ref(const wxObject
& clone
);
259 // destroy a reference
262 inline wxObjectRefData
*GetRefData(void) const { return m_refData
; }
263 inline void SetRefData(wxObjectRefData
*data
) { m_refData
= data
; }
266 #if defined(__WXDEBUG__) && defined(__VISAGECPP__)
269 static int Nid
; // total number of objects and serial counter
270 int id
; // serial number for current object
271 #endif // __WXDEBUG__
274 wxObjectRefData
* m_refData
;
276 wxObject_Serialize
* m_serialObj
;
284 class WXDLLEXPORT wxObjectRefData
{
286 friend class wxObject
;
289 wxObjectRefData(void);
290 virtual ~wxObjectRefData(void);
292 inline int GetRefCount(void) const { return m_count
; }
299 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
302 #define WXDEBUG_NEW new
305 // Redefine new to be the debugging version. This doesn't
306 // work with all compilers, in which case you need to
307 // use WXDEBUG_NEW explicitly if you wish to use the debugging version.
309 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
310 #define new new(__TFILE__,__LINE__)