]>
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 | { | |
55 | public: | |
9d2f3c71 | 56 | wxClassInfo(wxChar *cName, wxChar *baseName1, wxChar *baseName2, int sz, wxObjectConstructorFn fn); |
c801d85f KB |
57 | |
58 | wxObject *CreateObject(void); | |
3f4a0c5b | 59 | |
9d2f3c71 OK |
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; } | |
0c32066b JS |
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; } | |
856d2e52 | 67 | static inline wxClassInfo* GetFirst() { return sm_first; } |
0c32066b | 68 | inline wxClassInfo* GetNext() const { return m_next; } |
8cb50e4b | 69 | bool IsKindOf(wxClassInfo *info) const; |
c801d85f | 70 | |
9d2f3c71 | 71 | static wxClassInfo *FindClass(wxChar *c); |
0c32066b JS |
72 | |
73 | // Initializes parent pointers and hash table for fast searching. | |
c801d85f | 74 | static void InitializeClasses(void); |
0c32066b JS |
75 | |
76 | // Cleans up hash table used for fast searching. | |
77 | static void CleanUpClasses(void); | |
78 | ||
79 | public: | |
9d2f3c71 OK |
80 | wxChar* m_className; |
81 | wxChar* m_baseClassName1; | |
82 | wxChar* m_baseClassName2; | |
0c32066b JS |
83 | int m_objectSize; |
84 | wxObjectConstructorFn m_objectConstructor; | |
3f4a0c5b | 85 | |
0c32066b JS |
86 | // Pointers to base wxClassInfos: set in InitializeClasses |
87 | // called from wx_main.cc | |
88 | wxClassInfo* m_baseInfo1; | |
89 | wxClassInfo* m_baseInfo2; | |
90 | ||
91 | static wxClassInfo* sm_first; | |
92 | wxClassInfo* m_next; | |
93 | ||
94 | static wxHashTable* sm_classTable; | |
c801d85f KB |
95 | }; |
96 | ||
9d2f3c71 | 97 | WXDLLEXPORT wxObject* wxCreateDynamicObject(const wxChar *name); |
c801d85f | 98 | |
9838df2c | 99 | #if wxUSE_SERIAL |
184b5d99 | 100 | WXDLLEXPORT wxObject* wxCreateStoredObject( wxInputStream& stream ); |
c801d85f KB |
101 | #endif |
102 | ||
103 | #define DECLARE_DYNAMIC_CLASS(name) \ | |
104 | public:\ | |
0c32066b | 105 | static wxClassInfo sm_class##name;\ |
8cb50e4b | 106 | wxClassInfo *GetClassInfo() const \ |
0c32066b | 107 | { return &name::sm_class##name; } |
c801d85f KB |
108 | |
109 | #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name) | |
110 | #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name) | |
111 | ||
c801d85f KB |
112 | ////// |
113 | ////// for concrete classes | |
114 | ////// | |
115 | ||
116 | // Single inheritance with one base class | |
117 | #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \ | |
118 | wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \ | |
119 | { return new name; }\ | |
223d09f6 | 120 | wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename), (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name); |
c801d85f KB |
121 | |
122 | // Multiple inheritance with two base classes | |
123 | #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \ | |
124 | wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \ | |
125 | { return new name; }\ | |
223d09f6 | 126 | wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename1), (wxChar *) wxT(#basename2), (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name); |
c801d85f | 127 | |
c801d85f KB |
128 | ////// |
129 | ////// for abstract classes | |
130 | ////// | |
131 | ||
132 | // Single inheritance with one base class | |
133 | #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \ | |
223d09f6 | 134 | wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename), \ |
34636400 | 135 | (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL); |
c801d85f KB |
136 | |
137 | // Multiple inheritance with two base classes | |
138 | #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \ | |
223d09f6 KB |
139 | wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename1), \ |
140 | (wxChar *) wxT(#basename2), (int) sizeof(name), (wxObjectConstructorFn) NULL); | |
c801d85f KB |
141 | |
142 | #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS | |
143 | #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2 | |
144 | ||
0c32066b | 145 | #define CLASSINFO(name) (&name::sm_class##name) |
c801d85f | 146 | |
34636400 | 147 | #else // !wxUSE_DYNAMIC_CLASSES |
c801d85f KB |
148 | |
149 | // No dynamic class system: so stub out the macros | |
150 | #define DECLARE_DYNAMIC_CLASS(name) | |
151 | #define DECLARE_ABSTRACT_CLASS(name) | |
152 | #define DECLARE_CLASS(name) | |
153 | #define IMPLEMENT_DYNAMIC_CLASS(name, basename) | |
154 | #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) | |
155 | #define IMPLEMENT_ABSTRACT_CLASS(name, basename) | |
156 | #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) | |
157 | #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS | |
158 | #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2 | |
159 | ||
34636400 | 160 | #endif // wxUSE_DYNAMIC_CLASSES/!wxUSE_DYNAMIC_CLASSES |
c801d85f | 161 | |
3013b6f4 JS |
162 | #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className) |
163 | ||
164 | // Just seems a bit nicer-looking (pretend it's not a macro) | |
165 | #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className) | |
c801d85f | 166 | |
34636400 VZ |
167 | // to be replaced by dynamic_cast<> in the future |
168 | #define wxDynamicCast(obj, className) \ | |
169 | ((obj) && ((obj)->IsKindOf(&className::sm_class##className)) \ | |
170 | ? (className *)(obj) \ | |
171 | : (className *)0) | |
172 | ||
f6bcfd97 BP |
173 | #define wxConstCast(obj, className) ((className *)(obj)) |
174 | ||
175 | #ifdef __WXDEBUG__ | |
176 | inline void wxCheckCast(void *ptr) | |
177 | { | |
178 | wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") ); | |
179 | } | |
180 | ||
181 | #define wxStaticCast(obj, className) \ | |
182 | (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj))) | |
183 | ||
184 | #else // !Debug | |
185 | #define wxStaticCast(obj, className) ((className *)(obj)) | |
186 | #endif // Debug/!Debug | |
187 | ||
c801d85f KB |
188 | // Unfortunately Borland seems to need this include. |
189 | #ifdef __BORLANDC__ | |
34636400 VZ |
190 | #if wxUSE_IOSTREAMH |
191 | #include <iostream.h> | |
192 | #else | |
193 | #include <iostream> | |
194 | #endif | |
c801d85f KB |
195 | #endif |
196 | ||
197 | class WXDLLEXPORT wxObjectRefData; | |
198 | ||
199 | class WXDLLEXPORT wxObject | |
200 | { | |
201 | public: | |
202 | ||
203 | // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS | |
0c32066b | 204 | static wxClassInfo sm_classwxObject; |
c801d85f KB |
205 | |
206 | wxObject(void); | |
207 | virtual ~wxObject(void); | |
208 | ||
8cb50e4b | 209 | virtual wxClassInfo *GetClassInfo(void) const { return &sm_classwxObject; } |
aadbdf11 GL |
210 | wxObject *Clone(void) const; |
211 | virtual void CopyObject(wxObject& object_dest) const; | |
c801d85f | 212 | |
8cb50e4b | 213 | bool IsKindOf(wxClassInfo *info) const; |
c801d85f | 214 | |
ea57084d | 215 | #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING |
4de6207a | 216 | void * operator new (size_t size, wxChar * fileName = NULL, int lineNum = 0); |
4720388f DW |
217 | |
218 | #if defined(__VISAGECPP__) | |
219 | #if __DEBUG_ALLOC__ | |
220 | void operator delete (void * buf,const char * _fname, size_t _line); | |
221 | #endif //__DEBUG_ALLOC__ | |
222 | #else | |
c801d85f | 223 | void operator delete (void * buf); |
4720388f DW |
224 | #endif |
225 | // defined(__VISAGECPP__) | |
27198be4 | 226 | |
76626af2 | 227 | // VC++ 6.0 |
3f4a0c5b | 228 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) |
f6bcfd97 | 229 | void operator delete(void *buf, wxChar*, int); |
76626af2 JS |
230 | #endif |
231 | ||
3f4a0c5b | 232 | // Causes problems for VC++ |
921b3c17 | 233 | #if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__) |
4de6207a | 234 | void * operator new[] (size_t size, wxChar * fileName = NULL, int lineNum = 0); |
c801d85f KB |
235 | void operator delete[] (void * buf); |
236 | #endif | |
237 | ||
27198be4 | 238 | #ifdef __MWERKS__ |
7c74e7fe SC |
239 | void * operator new[] (size_t size, wxChar * fileName , int lineNum = 0); |
240 | void * operator new[] (size_t size) { return operator new[] ( size , NULL , 0 ) ; } | |
27198be4 SC |
241 | void operator delete[] (void * buf); |
242 | #endif | |
243 | ||
3f4a0c5b | 244 | #endif // Debug & memory tracing |
c801d85f | 245 | |
38830220 | 246 | #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) |
c801d85f KB |
247 | virtual void Dump(ostream& str); |
248 | #endif | |
249 | ||
9838df2c | 250 | #if wxUSE_SERIAL |
7a4b9130 GL |
251 | virtual void StoreObject( wxObjectOutputStream &stream ); |
252 | virtual void LoadObject( wxObjectInputStream &stream ); | |
c801d85f KB |
253 | #endif |
254 | ||
255 | // make a 'clone' of the object | |
256 | void Ref(const wxObject& clone); | |
257 | ||
258 | // destroy a reference | |
259 | void UnRef(void); | |
260 | ||
261 | inline wxObjectRefData *GetRefData(void) const { return m_refData; } | |
262 | inline void SetRefData(wxObjectRefData *data) { m_refData = data; } | |
263 | ||
4720388f DW |
264 | //EK |
265 | #if defined(__WXDEBUG__) && defined(__VISAGECPP__) | |
266 | public: | |
267 | static int N; | |
268 | static int Nid; // total number of objects and serial counter | |
269 | int id; // serial number for current object | |
270 | #endif // __WXDEBUG__ | |
271 | ||
c801d85f | 272 | protected: |
0c32066b | 273 | wxObjectRefData* m_refData; |
9838df2c | 274 | #if wxUSE_SERIAL |
0c32066b | 275 | wxObject_Serialize* m_serialObj; |
f4a8c29f | 276 | #endif |
c801d85f KB |
277 | }; |
278 | ||
279 | /* | |
280 | * wxObjectData | |
281 | */ | |
282 | ||
283 | class WXDLLEXPORT wxObjectRefData { | |
284 | ||
285 | friend class wxObject; | |
286 | ||
287 | public: | |
288 | wxObjectRefData(void); | |
289 | virtual ~wxObjectRefData(void); | |
290 | ||
291 | inline int GetRefCount(void) const { return m_count; } | |
292 | private: | |
293 | int m_count; | |
294 | }; | |
295 | ||
7fe7d506 JS |
296 | #ifdef __WXDEBUG__ |
297 | #ifndef WXDEBUG_NEW | |
4de6207a | 298 | #define WXDEBUG_NEW new(__TFILE__,__LINE__) |
7fe7d506 JS |
299 | #endif |
300 | #else | |
301 | #define WXDEBUG_NEW new | |
302 | #endif | |
303 | ||
304 | // Redefine new to be the debugging version. This doesn't | |
305 | // work with all compilers, in which case you need to | |
306 | // use WXDEBUG_NEW explicitly if you wish to use the debugging version. | |
307 | ||
308 | #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS | |
4de6207a | 309 | #define new new(__TFILE__,__LINE__) |
c801d85f KB |
310 | #endif |
311 | ||
312 | #endif | |
34138703 | 313 | // _WX_OBJECTH__ |
c801d85f KB |
314 | |
315 |