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