]> git.saurik.com Git - wxWidgets.git/blob - include/wx/object.h
added wxEncodingToCodepage() and implemented it properly (using in32 API instead...
[wxWidgets.git] / include / wx / object.h
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_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
44 #include "wx/ioswrap.h"
45 #endif
46
47 /*
48 * Dynamic object system declarations
49 */
50
51 typedef wxObject * (*wxObjectConstructorFn) (void);
52
53 class WXDLLEXPORT wxClassInfo
54 {
55 public:
56 wxClassInfo(const wxChar *cName,
57 const wxChar *baseName1,
58 const wxChar *baseName2,
59 int sz,
60 wxObjectConstructorFn fn);
61
62 wxObject *CreateObject(void);
63
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;
74
75 static wxClassInfo *FindClass(const wxChar *c);
76
77 // Initializes parent pointers and hash table for fast searching.
78 static void InitializeClasses();
79
80 // Cleans up hash table used for fast searching.
81 static void CleanUpClasses();
82
83 public:
84 const wxChar* m_className;
85 const wxChar* m_baseClassName1;
86 const wxChar* m_baseClassName2;
87 int m_objectSize;
88 wxObjectConstructorFn m_objectConstructor;
89
90 // Pointers to base wxClassInfos: set in InitializeClasses
91 const wxClassInfo* m_baseInfo1;
92 const wxClassInfo* m_baseInfo2;
93
94 // class info object live in a linked list: pointers to its head and the
95 // next element in it
96 static wxClassInfo* sm_first;
97 wxClassInfo* m_next;
98
99 static wxHashTable* sm_classTable;
100 };
101
102 WXDLLEXPORT wxObject* wxCreateDynamicObject(const wxChar *name);
103
104 #if wxUSE_SERIAL
105 WXDLLEXPORT wxObject* wxCreateStoredObject( wxInputStream& stream );
106 #endif
107
108 #define DECLARE_DYNAMIC_CLASS(name) \
109 public:\
110 static wxClassInfo sm_class##name;\
111 wxClassInfo *GetClassInfo() const \
112 { return &name::sm_class##name; }
113
114 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
115 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
116
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; }\
125 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename), (wxChar *) NULL, (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
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; }\
131 wxClassInfo name::sm_class##name((wxChar *) wxT(#name), (wxChar *) wxT(#basename1), (wxChar *) wxT(#basename2), (int) sizeof(name), (wxObjectConstructorFn) wxConstructorFor##name);
132
133 //////
134 ////// for abstract classes
135 //////
136
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);
141
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);
146
147 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
148 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
149
150 #define CLASSINFO(name) (&name::sm_class##name)
151
152 #else // !wxUSE_DYNAMIC_CLASSES
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
165 #endif // wxUSE_DYNAMIC_CLASSES/!wxUSE_DYNAMIC_CLASSES
166
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)
171
172 // to be replaced by dynamic_cast<> in the future
173 #define wxDynamicCast(obj, className) \
174 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
175
176 // The 'this' pointer is always true, so use this version to cast the this
177 // pointer and avoid compiler warnings.
178 #define wxDynamicCastThis(className) \
179 (IsKindOf(&className::sm_class##className) \
180 ? (className *)(this) \
181 : (className *)0)
182
183 #define wxConstCast(obj, className) ((className *)(obj))
184
185 #ifdef __WXDEBUG__
186 inline void wxCheckCast(void *ptr)
187 {
188 wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") );
189 }
190
191 #define wxStaticCast(obj, className) \
192 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
193
194 #else // !Debug
195 #define wxStaticCast(obj, className) ((className *)(obj))
196 #endif // Debug/!Debug
197
198 // Unfortunately Borland seems to need this include.
199 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
200 #ifdef __BORLANDC__
201 #if wxUSE_IOSTREAMH
202 #include <iostream.h>
203 #else
204 #include <iostream>
205 #endif
206 #endif
207 #endif
208
209 class WXDLLEXPORT wxObjectRefData;
210
211 class WXDLLEXPORT wxObject
212 {
213 public:
214
215 // This is equivalent to using the macro DECLARE_ABSTRACT_CLASS
216 static wxClassInfo sm_classwxObject;
217
218 wxObject(void);
219 virtual ~wxObject(void);
220
221 virtual wxClassInfo *GetClassInfo(void) const { return &sm_classwxObject; }
222
223 bool IsKindOf(wxClassInfo *info) const;
224
225 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
226 void * operator new (size_t size, wxChar * fileName = NULL, int lineNum = 0);
227 # if defined(__VISAGECPP__)
228 # if __DEBUG_ALLOC__
229 void operator delete (void * buf,const char * _fname, size_t _line);
230 # endif //__DEBUG_ALLOC__
231 # else // Everybody else
232 void operator delete (void * buf);
233 # endif // end of VISAGECPP
234
235 // VC++ 6.0
236 # if defined(__VISUALC__) && (__VISUALC__ >= 1200)
237 void operator delete(void *buf, wxChar*, int);
238 # endif
239
240 // Causes problems for VC++
241 # if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__)
242 void * operator new[] (size_t size, wxChar * fileName = NULL, int lineNum = 0);
243 void operator delete[] (void * buf);
244 # endif
245
246 # ifdef __MWERKS__
247 void * operator new[] (size_t size, wxChar * fileName , int lineNum = 0);
248 void * operator new[] (size_t size) { return operator new[] ( size , NULL , 0 ) ; }
249 void operator delete[] (void * buf);
250 # endif
251
252 #endif // Debug & memory tracing
253
254 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
255 virtual void Dump(wxSTD ostream& str);
256 #endif
257
258 #if wxUSE_SERIAL
259 virtual void StoreObject( wxObjectOutputStream &stream );
260 virtual void LoadObject( wxObjectInputStream &stream );
261 #endif
262
263 // make a 'clone' of the object
264 void Ref(const wxObject& clone);
265
266 // destroy a reference
267 void UnRef(void);
268
269 inline wxObjectRefData *GetRefData(void) const { return m_refData; }
270 inline void SetRefData(wxObjectRefData *data) { m_refData = data; }
271
272 protected:
273 wxObjectRefData* m_refData;
274 #if wxUSE_SERIAL
275 wxObject_Serialize* m_serialObj;
276 #endif
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
296 inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo)
297 {
298 return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : 0;
299 }
300
301 #ifdef __WXDEBUG__
302 #ifndef WXDEBUG_NEW
303 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
304 #endif
305 #else
306 #define WXDEBUG_NEW new
307 #endif
308
309 // Redefine new to be the debugging version. This doesn't
310 // work with all compilers, in which case you need to
311 // use WXDEBUG_NEW explicitly if you wish to use the debugging version.
312
313 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
314 #define new new(__TFILE__,__LINE__)
315 #endif
316
317 #endif
318 // _WX_OBJECTH__
319
320