implemented lazy binding detection (broken RTTI classinfo detection)
[wxWidgets.git] / include / wx / object.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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 // this shouldn't be needed any longer as <wx/msw/private.h> does it but it
36 // doesn't hurt neither
37 #ifdef GetClassName
38 #undef GetClassName
39 #endif
40 #ifdef GetClassInfo
41 #undef GetClassInfo
42 #endif
43
44 class WXDLLEXPORT wxClassInfo;
45 class WXDLLEXPORT wxHashTable;
46 class WXDLLEXPORT wxObjectRefData;
47
48 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
49 #include "wx/ioswrap.h"
50 #endif
51
52
53 // ----------------------------------------------------------------------------
54 // wxClassInfo
55 // ----------------------------------------------------------------------------
56
57 typedef wxObject *(*wxObjectConstructorFn)(void);
58
59 class WXDLLEXPORT wxClassInfo
60 {
61 public:
62 wxClassInfo( const wxChar *className,
63 const wxChar *baseName1,
64 const wxChar *baseName2,
65 int size,
66 wxObjectConstructorFn ctor )
67 : m_className(className)
68 , m_baseClassName1(baseName1)
69 , m_baseClassName2(baseName2)
70 , m_objectSize(size)
71 , m_objectConstructor(ctor)
72 , m_baseInfo1(0)
73 , m_baseInfo2(0)
74 , m_next(sm_first)
75 {
76 #ifdef __WXDEBUG__
77 if (sm_classTable != NULL) {
78 wxString msg(_T("too late binding of class info (lazy binding) for "));
79 msg += className;
80 wxFAIL_MSG(msg);
81 }
82 #endif
83 sm_first = this;
84 }
85
86 wxObject *CreateObject() { return m_objectConstructor ? (*m_objectConstructor)() : 0; }
87
88 const wxChar *GetClassName() const { return m_className; }
89 const wxChar *GetBaseClassName1() const { return m_baseClassName1; }
90 const wxChar *GetBaseClassName2() const { return m_baseClassName2; }
91 const wxClassInfo *GetBaseClass1() const { return m_baseInfo1; }
92 const wxClassInfo *GetBaseClass2() const { return m_baseInfo2; }
93 int GetSize() const { return m_objectSize; }
94
95 wxObjectConstructorFn GetConstructor() const { return m_objectConstructor; }
96 static const wxClassInfo *GetFirst() { return sm_first; }
97 const wxClassInfo *GetNext() const { return m_next; }
98 static wxClassInfo *FindClass(const wxChar *className);
99
100 // Climb upwards through inheritance hierarchy.
101 // Dual inheritance is catered for.
102
103 bool IsKindOf(const wxClassInfo *info) const
104 {
105 return info != 0 &&
106 ( info == this ||
107 ( m_baseInfo1 && m_baseInfo1->IsKindOf(info) ) ||
108 ( m_baseInfo2 && m_baseInfo2->IsKindOf(info) ) );
109 }
110
111 // Initializes parent pointers and hash table for fast searching.
112
113 static void InitializeClasses();
114
115 // Cleans up hash table used for fast searching.
116
117 static void CleanUpClasses();
118
119 public:
120 const wxChar *m_className;
121 const wxChar *m_baseClassName1;
122 const wxChar *m_baseClassName2;
123 int m_objectSize;
124 wxObjectConstructorFn m_objectConstructor;
125
126 // Pointers to base wxClassInfos: set in InitializeClasses
127
128 const wxClassInfo *m_baseInfo1;
129 const wxClassInfo *m_baseInfo2;
130
131 // class info object live in a linked list:
132 // pointers to its head and the next element in it
133
134 static wxClassInfo *sm_first;
135 wxClassInfo *m_next;
136
137 // FIXME: this should be private (currently used directly by way too
138 // many clients)
139 static wxHashTable *sm_classTable;
140
141 private:
142 // InitializeClasses() helper
143 static wxClassInfo *GetBaseByName(const wxChar *name);
144
145 DECLARE_NO_COPY_CLASS(wxClassInfo)
146 };
147
148 WXDLLEXPORT wxObject *wxCreateDynamicObject(const wxChar *name);
149
150 // ----------------------------------------------------------------------------
151 // Dynamic class macros
152 // ----------------------------------------------------------------------------
153
154 #define DECLARE_DYNAMIC_CLASS(name) \
155 public: \
156 static wxClassInfo sm_class##name; \
157 virtual wxClassInfo *GetClassInfo() const \
158 { return &name::sm_class##name; }
159
160 #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
161 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
162
163 // -----------------------------------
164 // for concrete classes
165 // -----------------------------------
166
167 // Single inheritance with one base class
168
169 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
170 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
171 { return new name; } \
172 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
173 0, (int) sizeof(name), \
174 (wxObjectConstructorFn) wxConstructorFor##name);
175
176 // Multiple inheritance with two base classes
177
178 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
179 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
180 { return new name; } \
181 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
182 wxT(#basename2), (int) sizeof(name), \
183 (wxObjectConstructorFn) wxConstructorFor##name);
184
185 // -----------------------------------
186 // for abstract classes
187 // -----------------------------------
188
189 // Single inheritance with one base class
190
191 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
192 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
193 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
194
195 // Multiple inheritance with two base classes
196
197 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
198 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
199 wxT(#basename2), (int) sizeof(name), \
200 (wxObjectConstructorFn) 0);
201
202 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
203 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
204
205 // -----------------------------------
206 // for pluggable classes
207 // -----------------------------------
208
209 // NOTE: this should probably be the very first statement
210 // in the class declaration so wxPluginSentinel is
211 // the first member initialised and the last destroyed.
212
213 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
214
215 #if wxUSE_NESTED_CLASSES
216
217 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
218 class exportdecl name##PluginSentinel { \
219 private: \
220 static const wxString sm_className; \
221 public: \
222 name##PluginSentinel(); \
223 ~name##PluginSentinel(); \
224 }; \
225 name##PluginSentinel m_pluginsentinel;
226
227 #define _IMPLEMENT_DL_SENTINEL(name) \
228 const wxString name::name##PluginSentinel::sm_className(#name); \
229 name::name##PluginSentinel::name##PluginSentinel() { \
230 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
231 if( e != 0 ) { e->RefObj(); } \
232 } \
233 name::name##PluginSentinel::~name##PluginSentinel() { \
234 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
235 if( e != 0 ) { e->UnrefObj(); } \
236 }
237 #else
238
239 #define _DECLARE_DL_SENTINEL(name)
240 #define _IMPLEMENT_DL_SENTINEL(name)
241
242 #endif // wxUSE_NESTED_CLASSES
243
244 #define DECLARE_PLUGGABLE_CLASS(name) \
245 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
246 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
247 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
248
249 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
250 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
251 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
252 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
253
254 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
255 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
256 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
257 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
258 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
259 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
260 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
261 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
262
263 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
264 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
265 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
266 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
267 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
268 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
269 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
270 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
271
272
273 #define CLASSINFO(name) (&name::sm_class##name)
274
275 #else // !wxUSE_DYNAMIC_CLASSES
276
277 // No dynamic class system: so stub out the macros
278
279 #define DECLARE_DYNAMIC_CLASS(name)
280 #define DECLARE_ABSTRACT_CLASS(name)
281 #define DECLARE_CLASS(name)
282 #define IMPLEMENT_DYNAMIC_CLASS(name, basename)
283 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
284 #define IMPLEMENT_ABSTRACT_CLASS(name, basename)
285 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
286 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
287 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
288
289 #define DECLARE_PLUGGABLE_CLASS(name)
290 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
291 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
292 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
293 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
294 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
295
296 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
297 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
298 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
299 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
300 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
301 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
302
303 #endif // wxUSE_DYNAMIC_CLASSES
304
305
306 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
307
308 // Just seems a bit nicer-looking (pretend it's not a macro)
309 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
310
311 // to be replaced by dynamic_cast<> in the future
312 #define wxDynamicCast(obj, className) \
313 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
314
315 // The 'this' pointer is always true, so use this version
316 // to cast the this pointer and avoid compiler warnings.
317 #define wxDynamicCastThis(className) \
318 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
319
320 #define wxConstCast(obj, className) ((className *)(obj))
321
322
323 #ifdef __WXDEBUG__
324 inline void wxCheckCast(void *ptr)
325 {
326 wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") );
327 }
328 #define wxStaticCast(obj, className) \
329 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
330
331 #else // !__WXDEBUG__
332 #define wxStaticCast(obj, className) ((className *)(obj))
333
334 #endif // __WXDEBUG__
335
336
337 // for some reason Borland seems to need this include.
338 #if wxUSE_STD_IOSTREAM \
339 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
340 && defined(__BORLANDC__)
341 #if wxUSE_IOSTREAMH
342 #include <iostream.h>
343 #else
344 #include <iostream>
345 #endif
346 #endif // wxUSE_IOSTREAMH
347
348 // ----------------------------------------------------------------------------
349 // set up memory debugging macros
350 // ----------------------------------------------------------------------------
351
352 /*
353 Which new/delete operator variants do we want?
354
355 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
356 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
357 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
358 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
359 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
360 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
361 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
362 */
363
364 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
365
366 // All compilers get this one
367 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
368
369 // Everyone except Visage gets the next one
370 #ifndef __VISAGECPP__
371 #define _WX_WANT_DELETE_VOID
372 #endif
373
374 // Only visage gets this one under the correct circumstances
375 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
376 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
377 #endif
378
379 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
380 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
381 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
382 #endif
383
384 // Now see who (if anyone) gets the array memory operators
385 #if wxUSE_ARRAY_MEMORY_OPERATORS
386
387 // Everyone except Visual C++ (cause problems for VC++ - crashes)
388 #if !defined(__VISUALC__)
389 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
390 #endif
391
392 // Everyone except Visual C++ (cause problems for VC++ - crashes)
393 #if !defined(__VISUALC__)
394 #define _WX_WANT_ARRAY_DELETE_VOID
395 #endif
396
397 // Only CodeWarrior 6 or higher
398 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
399 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
400 #endif
401
402 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
403
404 #endif // WXDEBUG && wxUSE_MEMORY_TRACING
405
406
407 // ----------------------------------------------------------------------------
408 // wxObject: the root class of wxWindows object hierarchy
409 // ----------------------------------------------------------------------------
410
411 class WXDLLEXPORT wxObject
412 {
413 DECLARE_ABSTRACT_CLASS(wxObject)
414 DECLARE_NO_COPY_CLASS(wxObject)
415
416 public:
417 wxObject() { m_refData = NULL; }
418 virtual ~wxObject() { UnRef(); }
419
420 bool IsKindOf(wxClassInfo *info) const;
421
422
423 // Turn on the correct set of new and delete operators
424
425 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
426 void *operator new ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
427 #endif
428
429 #ifdef _WX_WANT_DELETE_VOID
430 void operator delete ( void * buf );
431 #endif
432
433 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
434 void operator delete ( void *buf, const char *_fname, size_t _line );
435 #endif
436
437 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
438 void operator delete ( void *buf, const wxChar*, int );
439 #endif
440
441 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
442 void *operator new[] ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
443 #endif
444
445 #ifdef _WX_WANT_ARRAY_DELETE_VOID
446 void operator delete[] ( void *buf );
447 #endif
448
449 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
450 void operator delete[] (void* buf, const wxChar*, int );
451 #endif
452
453
454 #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
455 virtual void Dump(wxSTD ostream& str);
456 #endif
457
458 // ref counted data handling methods
459
460 // get/set
461 wxObjectRefData *GetRefData() const { return m_refData; }
462 void SetRefData(wxObjectRefData *data) { m_refData = data; }
463
464 // make a 'clone' of the object
465 void Ref(const wxObject& clone);
466
467 // destroy a reference
468 void UnRef();
469
470 protected:
471 // ensure that our data is not shared with anybody else: if we have no
472 // data, it is created using CreateRefData() below, if we have shared data
473 // it is copied using CloneRefData(), otherwise nothing is done
474 void AllocExclusive();
475
476 // both methods must be implemented if Unshare() is used, not pure virtual
477 // only because of the backwards compatibility reasons
478
479 // create a new m_refData
480 virtual wxObjectRefData *CreateRefData() const;
481
482 // create a new m_refData initialized with the given one
483 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
484
485 wxObjectRefData *m_refData;
486 };
487
488 // ----------------------------------------------------------------------------
489 // wxObjectRefData: ref counted data meant to be stored in wxObject
490 // ----------------------------------------------------------------------------
491
492 class WXDLLEXPORT wxObjectRefData
493 {
494 friend class wxObject;
495
496 public:
497 wxObjectRefData() : m_count(1) { }
498 virtual ~wxObjectRefData() { }
499
500 int GetRefCount() const { return m_count; }
501
502 private:
503 int m_count;
504 };
505
506
507 inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo)
508 {
509 return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : NULL;
510 }
511
512 // ----------------------------------------------------------------------------
513 // more debugging macros
514 // ----------------------------------------------------------------------------
515
516 #ifdef __WXDEBUG__
517 #ifndef WXDEBUG_NEW
518 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
519 #endif
520 #else // !__WXDEBUG__
521 #define WXDEBUG_NEW new
522 #endif
523
524 // Redefine new to be the debugging version. This doesn't work with all
525 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
526 // to use the debugging version.
527
528 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
529 #define new new(__TFILE__,__LINE__)
530 #endif
531
532 #endif // _WX_OBJECTH__
533