]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: object.h | |
3 | // Purpose: wxObject class, plus run-time type information macros | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
3f4a0c5b | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
34138703 JS |
12 | #ifndef _WX_OBJECTH__ |
13 | #define _WX_OBJECTH__ | |
c801d85f KB |
14 | |
15 | #ifdef __GNUG__ | |
0d3820b3 | 16 | #pragma interface "object.h" |
c801d85f KB |
17 | #endif |
18 | ||
19 | #include "wx/defs.h" | |
e55ad60e | 20 | #include "wx/memory.h" |
c801d85f KB |
21 | |
22 | class WXDLLEXPORT wxObject; | |
23 | ||
47d67540 | 24 | #if wxUSE_DYNAMIC_CLASSES |
c801d85f | 25 | |
3f1af920 | 26 | // #ifdef __GNUWIN32__ |
c801d85f KB |
27 | #ifdef GetClassName |
28 | #undef GetClassName | |
29 | #endif | |
3f1af920 JS |
30 | #ifdef GetClassInfo |
31 | #undef GetClassInfo | |
c801d85f | 32 | #endif |
3f1af920 | 33 | // #endif |
c801d85f KB |
34 | |
35 | class WXDLLEXPORT wxClassInfo; | |
1678ad78 | 36 | class WXDLLEXPORT wxInputStream; |
858f7d66 | 37 | class WXDLLEXPORT wxOutputStream; |
f4a8c29f GL |
38 | class WXDLLEXPORT wxObjectInputStream; |
39 | class WXDLLEXPORT wxObjectOutputStream; | |
40 | class WXDLLEXPORT wxHashTable; | |
41 | class WXDLLEXPORT wxObject_Serialize; | |
c801d85f | 42 | |
38830220 RR |
43 | #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) |
44 | #include "wx/ioswrap.h" | |
fbc535ff JS |
45 | #endif |
46 | ||
c801d85f KB |
47 | /* |
48 | * Dynamic object system declarations | |
49 | */ | |
50 | ||
51 | typedef wxObject * (*wxObjectConstructorFn) (void); | |
52 | ||
c801d85f KB |
53 | class WXDLLEXPORT wxClassInfo |
54 | { | |
aac65598 VZ |
55 | public: |
56 | wxClassInfo(const wxChar *cName, | |
57 | const wxChar *baseName1, | |
58 | const wxChar *baseName2, | |
59 | int sz, | |
60 | wxObjectConstructorFn fn); | |
c801d85f KB |
61 | |
62 | wxObject *CreateObject(void); | |
3f4a0c5b | 63 | |
aac65598 VZ |
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; | |
c801d85f | 74 | |
aac65598 | 75 | static wxClassInfo *FindClass(const wxChar *c); |
0c32066b JS |
76 | |
77 | // Initializes parent pointers and hash table for fast searching. | |
aac65598 | 78 | static void InitializeClasses(); |
0c32066b JS |
79 | |
80 | // Cleans up hash table used for fast searching. | |
aac65598 | 81 | static void CleanUpClasses(); |
0c32066b JS |
82 | |
83 | public: | |
aac65598 VZ |
84 | const wxChar* m_className; |
85 | const wxChar* m_baseClassName1; | |
86 | const wxChar* m_baseClassName2; | |
0c32066b JS |
87 | int m_objectSize; |
88 | wxObjectConstructorFn m_objectConstructor; | |
3f4a0c5b | 89 | |
0c32066b | 90 | // Pointers to base wxClassInfos: set in InitializeClasses |
aac65598 VZ |
91 | const wxClassInfo* m_baseInfo1; |
92 | const wxClassInfo* m_baseInfo2; | |
0c32066b | 93 | |
aac65598 VZ |
94 | // class info object live in a linked list: pointers to its head and the |
95 | // next element in it | |
0c32066b JS |
96 | static wxClassInfo* sm_first; |
97 | wxClassInfo* m_next; | |
98 | ||
99 | static wxHashTable* sm_classTable; | |
c801d85f KB |
100 | }; |
101 | ||
9d2f3c71 | 102 | WXDLLEXPORT wxObject* wxCreateDynamicObject(const wxChar *name); |
c801d85f | 103 | |
9838df2c | 104 | #if wxUSE_SERIAL |
184b5d99 | 105 | WXDLLEXPORT wxObject* wxCreateStoredObject( wxInputStream& stream ); |
c801d85f KB |
106 | #endif |
107 | ||
108 | #define DECLARE_DYNAMIC_CLASS(name) \ | |
109 | public:\ | |
0c32066b | 110 | static wxClassInfo sm_class##name;\ |
8cb50e4b | 111 | wxClassInfo *GetClassInfo() const \ |
0c32066b | 112 | { return &name::sm_class##name; } |
c801d85f KB |
113 | |
114 | #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name) | |
115 | #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name) | |
116 | ||
c801d85f KB |
117 | ////// |
118 | ////// for concrete classes | |
119 | ////// | |
120 | ||
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; }\ | |
223d09f6 | 125 | wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename), (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name); |
c801d85f KB |
126 | |
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; }\ | |
223d09f6 | 131 | wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename1), (wxChar *) wxT(#basename2), (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name); |
c801d85f | 132 | |
c801d85f KB |
133 | ////// |
134 | ////// for abstract classes | |
135 | ////// | |
136 | ||
137 | // Single inheritance with one base class | |
138 | #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \ | |
223d09f6 | 139 | wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename), \ |
34636400 | 140 | (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL); |
c801d85f KB |
141 | |
142 | // Multiple inheritance with two base classes | |
143 | #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \ | |
223d09f6 KB |
144 | wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename1), \ |
145 | (wxChar *) wxT(#basename2), (int) sizeof(name), (wxObjectConstructorFn) NULL); | |
c801d85f KB |
146 | |
147 | #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS | |
148 | #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2 | |
149 | ||
0c32066b | 150 | #define CLASSINFO(name) (&name::sm_class##name) |
c801d85f | 151 | |
34636400 | 152 | #else // !wxUSE_DYNAMIC_CLASSES |
c801d85f KB |
153 | |
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 | |
164 | ||
34636400 | 165 | #endif // wxUSE_DYNAMIC_CLASSES/!wxUSE_DYNAMIC_CLASSES |
c801d85f | 166 | |
3013b6f4 JS |
167 | #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className) |
168 | ||
169 | // Just seems a bit nicer-looking (pretend it's not a macro) | |
170 | #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className) | |
c801d85f | 171 | |
34636400 VZ |
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) \ | |
176 | : (className *)0) | |
177 | ||
33ac7e6f KB |
178 | // The 'this' pointer is always true, so use this version to cast the this |
179 | // pointer and avoid compiler warnings. | |
180 | #define wxDynamicThisCast(obj, className) \ | |
181 | (((obj)->IsKindOf(&className::sm_class##className)) \ | |
182 | ? (className *)(obj) \ | |
183 | : (className *)0) | |
184 | ||
f6bcfd97 BP |
185 | #define wxConstCast(obj, className) ((className *)(obj)) |
186 | ||
187 | #ifdef __WXDEBUG__ | |
188 | inline void wxCheckCast(void *ptr) | |
189 | { | |
190 | wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") ); | |
191 | } | |
192 | ||
193 | #define wxStaticCast(obj, className) \ | |
194 | (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj))) | |
195 | ||
196 | #else // !Debug | |
197 | #define wxStaticCast(obj, className) ((className *)(obj)) | |
198 | #endif // Debug/!Debug | |
199 | ||
c801d85f | 200 | // Unfortunately Borland seems to need this include. |
fd85b064 | 201 | #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) |
c801d85f | 202 | #ifdef __BORLANDC__ |
34636400 VZ |
203 | #if wxUSE_IOSTREAMH |
204 | #include <iostream.h> | |
205 | #else | |
206 | #include <iostream> | |
207 | #endif | |
c801d85f | 208 | #endif |
fd85b064 | 209 | #endif |
c801d85f KB |
210 | |
211 | class WXDLLEXPORT wxObjectRefData; | |
212 | ||
213 | class WXDLLEXPORT wxObject | |
214 | { | |
215 | public: | |
216 | ||
217 | // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS | |
0c32066b | 218 | static wxClassInfo sm_classwxObject; |
c801d85f KB |
219 | |
220 | wxObject(void); | |
221 | virtual ~wxObject(void); | |
222 | ||
8cb50e4b | 223 | virtual wxClassInfo *GetClassInfo(void) const { return &sm_classwxObject; } |
aadbdf11 GL |
224 | wxObject *Clone(void) const; |
225 | virtual void CopyObject(wxObject& object_dest) const; | |
c801d85f | 226 | |
8cb50e4b | 227 | bool IsKindOf(wxClassInfo *info) const; |
c801d85f | 228 | |
ea57084d | 229 | #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING |
4de6207a | 230 | void * operator new (size_t size, wxChar * fileName = NULL, int lineNum = 0); |
8cfc5426 DW |
231 | # if defined(__VISAGECPP__) |
232 | # if __DEBUG_ALLOC__ | |
233 | void operator delete (void * buf,const char * _fname, size_t _line); | |
234 | # endif //__DEBUG_ALLOC__ | |
235 | # else // Everybody else | |
236 | void operator delete (void * buf); | |
237 | # endif // end of VISAGECPP | |
27198be4 | 238 | |
76626af2 | 239 | // VC++ 6.0 |
8cfc5426 DW |
240 | # if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
241 | void operator delete(void *buf, wxChar*, int); | |
242 | # endif | |
76626af2 | 243 | |
3f4a0c5b | 244 | // Causes problems for VC++ |
8cfc5426 DW |
245 | # if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__) |
246 | void * operator new[] (size_t size, wxChar * fileName = NULL, int lineNum = 0); | |
247 | void operator delete[] (void * buf); | |
248 | # endif | |
249 | ||
250 | # ifdef __MWERKS__ | |
251 | void * operator new[] (size_t size, wxChar * fileName , int lineNum = 0); | |
252 | void * operator new[] (size_t size) { return operator new[] ( size , NULL , 0 ) ; } | |
253 | void operator delete[] (void * buf); | |
254 | # endif | |
27198be4 | 255 | |
3f4a0c5b | 256 | #endif // Debug & memory tracing |
c801d85f | 257 | |
38830220 | 258 | #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) |
dd107c50 | 259 | virtual void Dump(wxSTD ostream& str); |
c801d85f KB |
260 | #endif |
261 | ||
9838df2c | 262 | #if wxUSE_SERIAL |
7a4b9130 GL |
263 | virtual void StoreObject( wxObjectOutputStream &stream ); |
264 | virtual void LoadObject( wxObjectInputStream &stream ); | |
c801d85f KB |
265 | #endif |
266 | ||
267 | // make a 'clone' of the object | |
268 | void Ref(const wxObject& clone); | |
269 | ||
270 | // destroy a reference | |
271 | void UnRef(void); | |
272 | ||
273 | inline wxObjectRefData *GetRefData(void) const { return m_refData; } | |
274 | inline void SetRefData(wxObjectRefData *data) { m_refData = data; } | |
275 | ||
276 | protected: | |
0c32066b | 277 | wxObjectRefData* m_refData; |
9838df2c | 278 | #if wxUSE_SERIAL |
0c32066b | 279 | wxObject_Serialize* m_serialObj; |
f4a8c29f | 280 | #endif |
c801d85f KB |
281 | }; |
282 | ||
283 | /* | |
284 | * wxObjectData | |
285 | */ | |
286 | ||
287 | class WXDLLEXPORT wxObjectRefData { | |
288 | ||
289 | friend class wxObject; | |
290 | ||
291 | public: | |
292 | wxObjectRefData(void); | |
293 | virtual ~wxObjectRefData(void); | |
294 | ||
295 | inline int GetRefCount(void) const { return m_count; } | |
296 | private: | |
297 | int m_count; | |
298 | }; | |
299 | ||
7fe7d506 JS |
300 | #ifdef __WXDEBUG__ |
301 | #ifndef WXDEBUG_NEW | |
4de6207a | 302 | #define WXDEBUG_NEW new(__TFILE__,__LINE__) |
7fe7d506 JS |
303 | #endif |
304 | #else | |
305 | #define WXDEBUG_NEW new | |
306 | #endif | |
307 | ||
308 | // Redefine new to be the debugging version. This doesn't | |
309 | // work with all compilers, in which case you need to | |
310 | // use WXDEBUG_NEW explicitly if you wish to use the debugging version. | |
311 | ||
312 | #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS | |
4de6207a | 313 | #define new new(__TFILE__,__LINE__) |
c801d85f KB |
314 | #endif |
315 | ||
316 | #endif | |
34138703 | 317 | // _WX_OBJECTH__ |
c801d85f KB |
318 | |
319 |