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