reimplemented sanity checks that were lost/broken in the regrettably
[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: Ron Lee
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Julian Smart and Markus Holzem
9 // (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_OBJECTH__
14 #define _WX_OBJECTH__
15
16 #ifdef __GNUG__
17 #pragma interface "object.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 #include "wx/defs.h"
25 #include "wx/memory.h"
26
27 class WXDLLEXPORT wxObject;
28
29 #if wxUSE_DYNAMIC_CLASSES
30
31 // ----------------------------------------------------------------------------
32 // conditional compilation
33 // ----------------------------------------------------------------------------
34
35 #ifdef GetClassName
36 #undef GetClassName
37 #endif
38 #ifdef GetClassInfo
39 #undef GetClassInfo
40 #endif
41
42 class WXDLLEXPORT wxClassInfo;
43 class WXDLLEXPORT wxHashTable;
44
45 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
46 #include "wx/ioswrap.h"
47 #endif
48
49
50 // ----------------------------------------------------------------------------
51 // wxClassInfo
52 // ----------------------------------------------------------------------------
53
54 typedef wxObject *(*wxObjectConstructorFn)(void);
55
56 class WXDLLEXPORT wxClassInfo
57 {
58 public:
59 wxClassInfo( const wxChar *className,
60 const wxChar *baseName1,
61 const wxChar *baseName2,
62 int size,
63 wxObjectConstructorFn ctor )
64 : m_className(className)
65 , m_baseClassName1(baseName1)
66 , m_baseClassName2(baseName2)
67 , m_objectSize(size)
68 , m_objectConstructor(ctor)
69 , m_baseInfo1(0)
70 , m_baseInfo2(0)
71 , m_next(sm_first)
72 { sm_first = this; }
73
74 wxObject *CreateObject() { return m_objectConstructor ? (*m_objectConstructor)() : 0; }
75
76 const wxChar *GetClassName() const { return m_className; }
77 const wxChar *GetBaseClassName1() const { return m_baseClassName1; }
78 const wxChar *GetBaseClassName2() const { return m_baseClassName2; }
79 const wxClassInfo *GetBaseClass1() const { return m_baseInfo1; }
80 const wxClassInfo *GetBaseClass2() const { return m_baseInfo2; }
81 int GetSize() const { return m_objectSize; }
82
83 wxObjectConstructorFn GetConstructor() const { return m_objectConstructor; }
84 static const wxClassInfo *GetFirst() { return sm_first; }
85 const wxClassInfo *GetNext() const { return m_next; }
86 static wxClassInfo *FindClass(const wxChar *className);
87
88 // Climb upwards through inheritance hierarchy.
89 // Dual inheritance is catered for.
90
91 bool IsKindOf(const wxClassInfo *info) const
92 {
93 return info != 0 &&
94 ( info == this ||
95 ( m_baseInfo1 && m_baseInfo1->IsKindOf(info) ) ||
96 ( m_baseInfo2 && m_baseInfo2->IsKindOf(info) ) );
97 }
98
99 // Initializes parent pointers and hash table for fast searching.
100
101 static void InitializeClasses();
102
103 // Cleans up hash table used for fast searching.
104
105 static void CleanUpClasses();
106
107 public:
108 const wxChar *m_className;
109 const wxChar *m_baseClassName1;
110 const wxChar *m_baseClassName2;
111 int m_objectSize;
112 wxObjectConstructorFn m_objectConstructor;
113
114 // Pointers to base wxClassInfos: set in InitializeClasses
115
116 const wxClassInfo *m_baseInfo1;
117 const wxClassInfo *m_baseInfo2;
118
119 // class info object live in a linked list:
120 // pointers to its head and the next element in it
121
122 static wxClassInfo *sm_first;
123 wxClassInfo *m_next;
124
125 static wxHashTable *sm_classTable;
126 };
127
128 WXDLLEXPORT wxObject *wxCreateDynamicObject(const wxChar *name);
129
130
131 // ----------------------------------------------------------------------------
132 // Dynamic class macros
133 // ----------------------------------------------------------------------------
134
135 #define DECLARE_DYNAMIC_CLASS(name) \
136 public: \
137 static wxClassInfo sm_class##name; \
138 virtual wxClassInfo *GetClassInfo() const \
139 { return &name::sm_class##name; }
140
141 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
142 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
143
144 // -----------------------------------
145 // for concrete classes
146 // -----------------------------------
147
148 // Single inheritance with one base class
149
150 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
151 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
152 { return new name; } \
153 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
154 0, (int) sizeof(name), \
155 (wxObjectConstructorFn) wxConstructorFor##name);
156
157 // Multiple inheritance with two base classes
158
159 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
160 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
161 { return new name; } \
162 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
163 wxT(#basename2), (int) sizeof(name), \
164 (wxObjectConstructorFn) wxConstructorFor##name);
165
166 // -----------------------------------
167 // for abstract classes
168 // -----------------------------------
169
170 // Single inheritance with one base class
171
172 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
173 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
174 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
175
176 // Multiple inheritance with two base classes
177
178 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
179 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
180 wxT(#basename2), (int) sizeof(name), \
181 (wxObjectConstructorFn) 0);
182
183 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
184 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
185
186 // -----------------------------------
187 // for pluggable classes
188 // -----------------------------------
189
190 // NOTE: this should probably be the very first statement
191 // in the class declaration so wxPluginSentinel is
192 // the first member initialised and the last destroyed.
193
194 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
195
196 #if wxUSE_NESTED_CLASSES
197
198 #if 0
199 #define _DECLARE_DL_SENTINEL(name) \
200 wxPluginSentinel m_pluginsentinel;
201
202 #else
203
204 #define _DECLARE_DL_SENTINEL(name) \
205 class name##PluginSentinel { \
206 private: \
207 static const wxString sm_className; \
208 public: \
209 name##PluginSentinel(); \
210 ~##name##PluginSentinel(); \
211 }; \
212 name##PluginSentinel m_pluginsentinel;
213 #endif
214
215 #define _IMPLEMENT_DL_SENTINEL(name) \
216 const wxString name::name##PluginSentinel::sm_className(#name); \
217 name::name##PluginSentinel::name##PluginSentinel() { \
218 wxDLManifestEntry *e = (wxDLManifestEntry*) wxDLManifestEntry::ms_classes.Get(#name); \
219 if( e != 0 ) { e->RefObj(); } \
220 } \
221 name::name##PluginSentinel::~##name##PluginSentinel() { \
222 wxDLManifestEntry *e = (wxDLManifestEntry*) wxDLManifestEntry::ms_classes.Get(#name); \
223 if( e != 0 ) { e->UnrefObj(); } \
224 }
225 #else
226
227 #define _DECLARE_DL_SENTINEL(name)
228 #define _IMPLEMENT_DL_SENTINEL(name)
229
230 #endif // wxUSE_NESTED_CLASSES
231
232 #define DECLARE_PLUGGABLE_CLASS(name) \
233 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name)
234
235 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
236 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name)
237
238 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
239 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
240 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
241 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
242
243 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
244 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
245 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
246 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
247
248
249 #define CLASSINFO(name) (&name::sm_class##name)
250
251 #else // !wxUSE_DYNAMIC_CLASSES
252
253 // No dynamic class system: so stub out the macros
254
255 #define DECLARE_DYNAMIC_CLASS(name)
256 #define DECLARE_ABSTRACT_CLASS(name)
257 #define DECLARE_CLASS(name)
258 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
259 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
260 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
261 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
262 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
263 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
264
265 #define DECLARE_PLUGGABLE_CLASS(name)
266 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
267 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
268 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
269 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
270 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
271
272 #endif // wxUSE_DYNAMIC_CLASSES
273
274
275 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
276
277 // Just seems a bit nicer-looking (pretend it's not a macro)
278
279 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
280
281 // to be replaced by dynamic_cast<> in the future
282
283 #define wxDynamicCast(obj, className) \
284 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
285
286 // The 'this' pointer is always true, so use this version
287 // to cast the this pointer and avoid compiler warnings.
288
289 #define wxDynamicCastThis(className) \
290 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
291
292 #define wxConstCast(obj, className) ((className *)(obj))
293
294
295 #ifdef __WXDEBUG__
296 inline void wxCheckCast(void *ptr)
297 {
298 wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") );
299 }
300 #define wxStaticCast(obj, className) \
301 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
302
303 #else // !__WXDEBUG__
304 #define wxStaticCast(obj, className) ((className *)(obj))
305
306 #endif // __WXDEBUG__
307
308
309 // Unfortunately Borland seems to need this include.
310
311 #if wxUSE_STD_IOSTREAM \
312 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
313 && defined(__BORLANDC__)
314 #if wxUSE_IOSTREAMH
315 #include <iostream.h>
316 #else
317 #include <iostream>
318 #endif
319 #endif
320
321
322 // ----------------------------------------------------------------------------
323 // wxObject
324 // ----------------------------------------------------------------------------
325
326 class WXDLLEXPORT wxObjectRefData;
327
328 class WXDLLEXPORT wxObject
329 {
330 DECLARE_ABSTRACT_CLASS(wxObject)
331
332 public:
333 wxObject() : m_refData(0) {}
334 virtual ~wxObject() { UnRef(); }
335
336 bool IsKindOf(wxClassInfo *info) const;
337
338 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
339 void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0);
340
341 #ifndef __VISAGECPP__
342 void operator delete (void * buf);
343 #elif __DEBUG_ALLOC__
344 void operator delete (void *buf, const char *_fname, size_t _line);
345 #endif
346
347 // VC++ 6.0
348
349 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
350 void operator delete(void *buf, wxChar*, int);
351 #endif
352
353 // Causes problems for VC++
354
355 #if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__)
356 void *operator new[] (size_t size, wxChar *fileName = 0, int lineNum = 0);
357 void operator delete[] (void *buf);
358 #endif
359
360 #ifdef __MWERKS__
361 void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0);
362 void *operator new[] (size_t size) { return operator new[] ( size, 0, 0 ) ; }
363 void operator delete[] (void *buf);
364 #endif
365
366 #endif // Debug & memory tracing
367
368
369 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
370 virtual void Dump(wxSTD ostream& str);
371 #endif
372
373 // make a 'clone' of the object
374
375 void Ref(const wxObject& clone);
376
377 // destroy a reference
378
379 void UnRef();
380
381 inline wxObjectRefData *GetRefData() const { return m_refData; }
382 inline void SetRefData(wxObjectRefData *data) { m_refData = data; }
383
384 protected:
385 wxObjectRefData *m_refData;
386 };
387
388
389 class WXDLLEXPORT wxObjectRefData
390 {
391 friend class wxObject;
392
393 public:
394 wxObjectRefData() : m_count(1) {}
395 virtual ~wxObjectRefData() {}
396
397 inline int GetRefCount() const { return m_count; }
398 private:
399 int m_count;
400 };
401
402
403 inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo)
404 {
405 return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : 0;
406 }
407
408 #ifdef __WXDEBUG__
409 #ifndef WXDEBUG_NEW
410 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
411 #endif
412 #else
413 #define WXDEBUG_NEW new
414 #endif
415
416 // Redefine new to be the debugging version. This doesn't
417 // work with all compilers, in which case you need to
418 // use WXDEBUG_NEW explicitly if you wish to use the debugging version.
419
420 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
421 #define new new(__TFILE__,__LINE__)
422 #endif
423
424 #endif // _WX_OBJECTH__
425
426 // vi:sts=4:sw=4:et