]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_OBJECTH__ | |
13 | #define _WX_OBJECTH__ | |
14 | ||
15 | #ifdef __GNUG__ | |
16 | #pragma interface "object.h" | |
17 | #endif | |
18 | ||
19 | #include "wx/defs.h" | |
20 | #include "wx/memory.h" | |
21 | ||
22 | class WXDLLEXPORT wxObject; | |
23 | ||
24 | #if wxUSE_DYNAMIC_CLASSES | |
25 | ||
26 | // #ifdef __GNUWIN32__ | |
27 | #ifdef GetClassName | |
28 | #undef GetClassName | |
29 | #endif | |
30 | #ifdef GetClassInfo | |
31 | #undef GetClassInfo | |
32 | #endif | |
33 | // #endif | |
34 | ||
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; | |
42 | ||
43 | #if wxUSE_IOSTREAMH | |
44 | // N.B. BC++ doesn't have istream.h, ostream.h | |
45 | # include <iostream.h> | |
46 | #else | |
47 | # include <ostream> | |
48 | # if defined(__VISUALC__) || defined(__MWERKS__) | |
49 | using namespace std; | |
50 | # endif | |
51 | #endif | |
52 | ||
53 | /* | |
54 | * Dynamic object system declarations | |
55 | */ | |
56 | ||
57 | typedef wxObject * (*wxObjectConstructorFn) (void); | |
58 | ||
59 | class WXDLLEXPORT wxClassInfo | |
60 | { | |
61 | public: | |
62 | wxClassInfo(wxChar *cName, wxChar *baseName1, wxChar *baseName2, int sz, wxObjectConstructorFn fn); | |
63 | ||
64 | wxObject *CreateObject(void); | |
65 | ||
66 | inline wxChar *GetClassName(void) const { return m_className; } | |
67 | inline wxChar *GetBaseClassName1(void) const { return m_baseClassName1; } | |
68 | inline wxChar *GetBaseClassName2(void) const { return m_baseClassName2; } | |
69 | inline wxClassInfo* GetBaseClass1() const { return m_baseInfo1; } | |
70 | inline wxClassInfo* GetBaseClass2() const { return m_baseInfo2; } | |
71 | inline int GetSize(void) const { return m_objectSize; } | |
72 | inline wxObjectConstructorFn GetConstructor() const { return m_objectConstructor; } | |
73 | static inline wxClassInfo* GetFirst() { return sm_first; } | |
74 | inline wxClassInfo* GetNext() const { return m_next; } | |
75 | bool IsKindOf(wxClassInfo *info) const; | |
76 | ||
77 | static wxClassInfo *FindClass(wxChar *c); | |
78 | ||
79 | // Initializes parent pointers and hash table for fast searching. | |
80 | static void InitializeClasses(void); | |
81 | ||
82 | // Cleans up hash table used for fast searching. | |
83 | static void CleanUpClasses(void); | |
84 | ||
85 | public: | |
86 | wxChar* m_className; | |
87 | wxChar* m_baseClassName1; | |
88 | wxChar* m_baseClassName2; | |
89 | int m_objectSize; | |
90 | wxObjectConstructorFn m_objectConstructor; | |
91 | ||
92 | // Pointers to base wxClassInfos: set in InitializeClasses | |
93 | // called from wx_main.cc | |
94 | wxClassInfo* m_baseInfo1; | |
95 | wxClassInfo* m_baseInfo2; | |
96 | ||
97 | static wxClassInfo* sm_first; | |
98 | wxClassInfo* m_next; | |
99 | ||
100 | static wxHashTable* sm_classTable; | |
101 | }; | |
102 | ||
103 | WXDLLEXPORT wxObject* wxCreateDynamicObject(const wxChar *name); | |
104 | ||
105 | #if wxUSE_SERIAL | |
106 | WXDLLEXPORT wxObject* wxCreateStoredObject( wxInputStream& stream ); | |
107 | #endif | |
108 | ||
109 | #define DECLARE_DYNAMIC_CLASS(name) \ | |
110 | public:\ | |
111 | static wxClassInfo sm_class##name;\ | |
112 | wxClassInfo *GetClassInfo() const \ | |
113 | { return &name::sm_class##name; } | |
114 | ||
115 | #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name) | |
116 | #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name) | |
117 | ||
118 | ////// | |
119 | ////// for concrete classes | |
120 | ////// | |
121 | ||
122 | // Single inheritance with one base class | |
123 | #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \ | |
124 | wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \ | |
125 | { return new name; }\ | |
126 | wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename), (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name); | |
127 | ||
128 | // Multiple inheritance with two base classes | |
129 | #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \ | |
130 | wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name(void) \ | |
131 | { return new name; }\ | |
132 | wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename1), (wxChar *) _T(#basename2), (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name); | |
133 | ||
134 | ////// | |
135 | ////// for abstract classes | |
136 | ////// | |
137 | ||
138 | // Single inheritance with one base class | |
139 | #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \ | |
140 | wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename), \ | |
141 | (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) NULL); | |
142 | ||
143 | // Multiple inheritance with two base classes | |
144 | #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \ | |
145 | wxClassInfo name::sm_class##name((wxChar *) _T(#name), (wxChar *) _T(#basename1), \ | |
146 | (wxChar *) _T(#basename2), (int) sizeof(name), (wxObjectConstructorFn) NULL); | |
147 | ||
148 | #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS | |
149 | #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2 | |
150 | ||
151 | #define CLASSINFO(name) (&name::sm_class##name) | |
152 | ||
153 | #else // !wxUSE_DYNAMIC_CLASSES | |
154 | ||
155 | // No dynamic class system: so stub out the macros | |
156 | #define DECLARE_DYNAMIC_CLASS(name) | |
157 | #define DECLARE_ABSTRACT_CLASS(name) | |
158 | #define DECLARE_CLASS(name) | |
159 | #define IMPLEMENT_DYNAMIC_CLASS(name, basename) | |
160 | #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) | |
161 | #define IMPLEMENT_ABSTRACT_CLASS(name, basename) | |
162 | #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) | |
163 | #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS | |
164 | #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2 | |
165 | ||
166 | #endif // wxUSE_DYNAMIC_CLASSES/!wxUSE_DYNAMIC_CLASSES | |
167 | ||
168 | #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className) | |
169 | ||
170 | // Just seems a bit nicer-looking (pretend it's not a macro) | |
171 | #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className) | |
172 | ||
173 | // to be replaced by dynamic_cast<> in the future | |
174 | #define wxDynamicCast(obj, className) \ | |
175 | ((obj) && ((obj)->IsKindOf(&className::sm_class##className)) \ | |
176 | ? (className *)(obj) \ | |
177 | : (className *)0) | |
178 | ||
179 | // Unfortunately Borland seems to need this include. | |
180 | #ifdef __BORLANDC__ | |
181 | #if wxUSE_IOSTREAMH | |
182 | #include <iostream.h> | |
183 | #else | |
184 | #include <iostream> | |
185 | #endif | |
186 | #endif | |
187 | ||
188 | class WXDLLEXPORT wxObjectRefData; | |
189 | ||
190 | class WXDLLEXPORT wxObject | |
191 | { | |
192 | public: | |
193 | ||
194 | // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS | |
195 | static wxClassInfo sm_classwxObject; | |
196 | ||
197 | wxObject(void); | |
198 | virtual ~wxObject(void); | |
199 | ||
200 | virtual wxClassInfo *GetClassInfo(void) const { return &sm_classwxObject; } | |
201 | wxObject *Clone(void) const; | |
202 | virtual void CopyObject(wxObject& object_dest) const; | |
203 | ||
204 | bool IsKindOf(wxClassInfo *info) const; | |
205 | ||
206 | #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING | |
207 | void * operator new (size_t size, wxChar * fileName = NULL, int lineNum = 0); | |
208 | void operator delete (void * buf); | |
209 | ||
210 | // VC++ 6.0 | |
211 | #if defined(__VISUALC__) && (__VISUALC__ >= 1200) | |
212 | void operator delete(void *buf, char*, int); | |
213 | #endif | |
214 | ||
215 | // Causes problems for VC++ | |
216 | #if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__) | |
217 | void * operator new[] (size_t size, wxChar * fileName = NULL, int lineNum = 0); | |
218 | void operator delete[] (void * buf); | |
219 | #endif | |
220 | ||
221 | #ifdef __MWERKS__ | |
222 | void * operator new[] (size_t size, wxChar * fileName , int lineNum = 0); | |
223 | void operator delete[] (void * buf); | |
224 | #endif | |
225 | ||
226 | #endif // Debug & memory tracing | |
227 | ||
228 | #if defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT | |
229 | virtual void Dump(ostream& str); | |
230 | #endif | |
231 | ||
232 | #if wxUSE_SERIAL | |
233 | virtual void StoreObject( wxObjectOutputStream &stream ); | |
234 | virtual void LoadObject( wxObjectInputStream &stream ); | |
235 | #endif | |
236 | ||
237 | // make a 'clone' of the object | |
238 | void Ref(const wxObject& clone); | |
239 | ||
240 | // destroy a reference | |
241 | void UnRef(void); | |
242 | ||
243 | inline wxObjectRefData *GetRefData(void) const { return m_refData; } | |
244 | inline void SetRefData(wxObjectRefData *data) { m_refData = data; } | |
245 | ||
246 | protected: | |
247 | wxObjectRefData* m_refData; | |
248 | #if wxUSE_SERIAL | |
249 | wxObject_Serialize* m_serialObj; | |
250 | #endif | |
251 | }; | |
252 | ||
253 | /* | |
254 | * wxObjectData | |
255 | */ | |
256 | ||
257 | class WXDLLEXPORT wxObjectRefData { | |
258 | ||
259 | friend class wxObject; | |
260 | ||
261 | public: | |
262 | wxObjectRefData(void); | |
263 | virtual ~wxObjectRefData(void); | |
264 | ||
265 | inline int GetRefCount(void) const { return m_count; } | |
266 | private: | |
267 | int m_count; | |
268 | }; | |
269 | ||
270 | #ifdef __WXDEBUG__ | |
271 | #ifndef WXDEBUG_NEW | |
272 | #define WXDEBUG_NEW new(__TFILE__,__LINE__) | |
273 | #endif | |
274 | #else | |
275 | #define WXDEBUG_NEW new | |
276 | #endif | |
277 | ||
278 | // Redefine new to be the debugging version. This doesn't | |
279 | // work with all compilers, in which case you need to | |
280 | // use WXDEBUG_NEW explicitly if you wish to use the debugging version. | |
281 | ||
282 | #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS | |
283 | #define new new(__TFILE__,__LINE__) | |
284 | #endif | |
285 | ||
286 | #endif | |
287 | // _WX_OBJECTH__ | |
288 | ||
289 |