]> git.saurik.com Git - wxWidgets.git/blob - include/wx/object.h
Applied patch [ 1181975 ] [wxMSW] Removes all flicker from wxNotebook
[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(NO_GCC_PRAGMA)
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 #ifndef wxUSE_EXTENDED_RTTI
30 #define wxUSE_EXTENDED_RTTI 0
31 #endif
32
33 #if wxUSE_EXTENDED_RTTI
34 #include "wx/xti.h"
35 #else
36
37 // ----------------------------------------------------------------------------
38 // conditional compilation
39 // ----------------------------------------------------------------------------
40
41 // this shouldn't be needed any longer as <wx/msw/private.h> does it but it
42 // doesn't hurt neither
43 #ifdef GetClassName
44 #undef GetClassName
45 #endif
46 #ifdef GetClassInfo
47 #undef GetClassInfo
48 #endif
49
50 class WXDLLIMPEXP_BASE wxClassInfo;
51 class WXDLLIMPEXP_BASE wxHashTable;
52 class WXDLLIMPEXP_BASE wxObjectRefData;
53
54 // ----------------------------------------------------------------------------
55 // wxClassInfo
56 // ----------------------------------------------------------------------------
57
58 typedef wxObject *(*wxObjectConstructorFn)(void);
59
60 class WXDLLIMPEXP_BASE wxClassInfo
61 {
62 public:
63 wxClassInfo( const wxChar *className,
64 const wxClassInfo *baseInfo1,
65 const wxClassInfo *baseInfo2,
66 int size,
67 wxObjectConstructorFn ctor )
68 : m_className(className)
69 , m_objectSize(size)
70 , m_objectConstructor(ctor)
71 , m_baseInfo1(baseInfo1)
72 , m_baseInfo2(baseInfo2)
73 , m_next(sm_first)
74 {
75 sm_first = this;
76 Register();
77 }
78
79 ~wxClassInfo();
80
81 wxObject *CreateObject() { return m_objectConstructor ? (*m_objectConstructor)() : 0; }
82
83 const wxChar *GetClassName() const { return m_className; }
84 const wxChar *GetBaseClassName1() const
85 { return m_baseInfo1 ? m_baseInfo1->GetClassName() : NULL; }
86 const wxChar *GetBaseClassName2() const
87 { return m_baseInfo2 ? m_baseInfo2->GetClassName() : NULL; }
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 #if 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 #if 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_ABSTRACT_CLASS(name) \
160 public: \
161 static wxClassInfo ms_classInfo; \
162 virtual wxClassInfo *GetClassInfo() const;
163
164 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
165 DECLARE_NO_ASSIGN_CLASS(name) \
166 DECLARE_DYNAMIC_CLASS(name)
167
168 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
169 DECLARE_NO_COPY_CLASS(name) \
170 DECLARE_DYNAMIC_CLASS(name)
171
172 #define DECLARE_DYNAMIC_CLASS(name) \
173 DECLARE_ABSTRACT_CLASS(name) \
174 static wxObject* wxCreateObject();
175
176 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
177
178
179 // common part of the macros below
180 #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \
181 wxClassInfo name::ms_classInfo(wxT(#name), \
182 &basename::ms_classInfo, \
183 baseclsinfo2, \
184 (int) sizeof(name), \
185 (wxObjectConstructorFn) func); \
186 \
187 wxClassInfo *name::GetClassInfo() const \
188 { return &name::ms_classInfo; }
189
190 #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \
191 wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func)
192
193 #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \
194 wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo)
195
196 // -----------------------------------
197 // for concrete classes
198 // -----------------------------------
199
200 // Single inheritance with one base class
201 #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
202 wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \
203 wxObject* name::wxCreateObject() \
204 { return new name; }
205
206 // Multiple inheritance with two base classes
207 #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
208 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \
209 name::wxCreateObject) \
210 wxObject* name::wxCreateObject() \
211 { return new name; }
212
213 // -----------------------------------
214 // for abstract classes
215 // -----------------------------------
216
217 // Single inheritance with one base class
218
219 #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
220 wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL)
221
222 // Multiple inheritance with two base classes
223
224 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
225 wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL)
226
227 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
228 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
229
230 #endif // !wxUSE_EXTENDED_RTTI
231
232
233 // -----------------------------------
234 // for pluggable classes
235 // -----------------------------------
236
237 // NOTE: this should probably be the very first statement
238 // in the class declaration so wxPluginSentinel is
239 // the first member initialised and the last destroyed.
240
241 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
242
243 #if wxUSE_NESTED_CLASSES
244
245 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
246 class exportdecl name##PluginSentinel { \
247 private: \
248 static const wxString sm_className; \
249 public: \
250 name##PluginSentinel(); \
251 ~name##PluginSentinel(); \
252 }; \
253 name##PluginSentinel m_pluginsentinel;
254
255 #define _IMPLEMENT_DL_SENTINEL(name) \
256 const wxString name::name##PluginSentinel::sm_className(#name); \
257 name::name##PluginSentinel::name##PluginSentinel() { \
258 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
259 if( e != 0 ) { e->RefObj(); } \
260 } \
261 name::name##PluginSentinel::~name##PluginSentinel() { \
262 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
263 if( e != 0 ) { e->UnrefObj(); } \
264 }
265 #else
266
267 #define _DECLARE_DL_SENTINEL(name)
268 #define _IMPLEMENT_DL_SENTINEL(name)
269
270 #endif // wxUSE_NESTED_CLASSES
271
272 #define DECLARE_PLUGGABLE_CLASS(name) \
273 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
274 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
275 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
276
277 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
278 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
279 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
280 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
281
282 #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
283 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
284 #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
285 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
286 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
287 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
288 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
289 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
290
291 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
292 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
293 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
294 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
295 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
296 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
297 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
298 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
299
300 #define CLASSINFO(name) (&name::ms_classInfo)
301
302 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
303
304 // Just seems a bit nicer-looking (pretend it's not a macro)
305 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
306
307 // this cast does some more checks at compile time as it uses static_cast
308 // internally
309 //
310 // note that it still has different semantics from dynamic_cast<> and so can't
311 // be replaced by it as long as there are any compilers not supporting it
312 #define wxDynamicCast(obj, className) \
313 ((className *) wxCheckDynamicCast( \
314 wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \
315 wx_const_cast(className *, wx_static_cast(const className *, obj)))), \
316 &className::ms_classInfo))
317
318 // The 'this' pointer is always true, so use this version
319 // to cast the this pointer and avoid compiler warnings.
320 #define wxDynamicCastThis(className) \
321 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
322
323 #ifdef __WXDEBUG__
324 inline void* wxCheckCast(void *ptr)
325 {
326 wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") );
327 return ptr;
328 }
329 #define wxStaticCast(obj, className) \
330 ((className *)wxCheckCast(wxDynamicCast(obj, className)))
331
332 #else // !__WXDEBUG__
333 #define wxStaticCast(obj, className) \
334 wx_const_cast(className *, wx_static_cast(const className *, obj))
335
336 #endif // __WXDEBUG__
337
338 // ----------------------------------------------------------------------------
339 // set up memory debugging macros
340 // ----------------------------------------------------------------------------
341
342 /*
343 Which new/delete operator variants do we want?
344
345 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
346 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
347 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
348 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
349 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
350 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
351 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
352 */
353
354 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
355
356 // All compilers get this one
357 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
358
359 // Everyone except Visage gets the next one
360 #ifndef __VISAGECPP__
361 #define _WX_WANT_DELETE_VOID
362 #endif
363
364 // Only visage gets this one under the correct circumstances
365 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
366 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
367 #endif
368
369 // Only VC++ 6.0 and CodeWarrior compilers get overloaded delete that matches new
370 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
371 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
372 #endif
373
374 // Now see who (if anyone) gets the array memory operators
375 #if wxUSE_ARRAY_MEMORY_OPERATORS
376
377 // Everyone except Visual C++ (cause problems for VC++ - crashes)
378 #if !defined(__VISUALC__)
379 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
380 #endif
381
382 // Everyone except Visual C++ (cause problems for VC++ - crashes)
383 #if !defined(__VISUALC__)
384 #define _WX_WANT_ARRAY_DELETE_VOID
385 #endif
386
387 // Only CodeWarrior 6 or higher
388 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
389 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
390 #endif
391
392 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
393
394 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
395
396 // ----------------------------------------------------------------------------
397 // wxObject: the root class of wxWidgets object hierarchy
398 // ----------------------------------------------------------------------------
399
400 class WXDLLIMPEXP_BASE wxObject
401 {
402 DECLARE_ABSTRACT_CLASS(wxObject)
403
404 private:
405 void InitFrom(const wxObject& other);
406
407 public:
408 wxObject() { m_refData = NULL; }
409 virtual ~wxObject() { UnRef(); }
410
411 wxObject(const wxObject& other)
412 {
413 InitFrom(other);
414 }
415
416 wxObject& operator=(const wxObject& other)
417 {
418 if ( this != &other )
419 {
420 UnRef();
421 InitFrom(other);
422 }
423 return *this;
424 }
425
426 bool IsKindOf(wxClassInfo *info) const;
427
428
429 // Turn on the correct set of new and delete operators
430
431 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
432 void *operator new ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
433 #endif
434
435 #ifdef _WX_WANT_DELETE_VOID
436 void operator delete ( void * buf );
437 #endif
438
439 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
440 void operator delete ( void *buf, const char *_fname, size_t _line );
441 #endif
442
443 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
444 void operator delete ( void *buf, const wxChar*, int );
445 #endif
446
447 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
448 void *operator new[] ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
449 #endif
450
451 #ifdef _WX_WANT_ARRAY_DELETE_VOID
452 void operator delete[] ( void *buf );
453 #endif
454
455 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
456 void operator delete[] (void* buf, const wxChar*, int );
457 #endif
458
459 // ref counted data handling methods
460
461 // get/set
462 wxObjectRefData *GetRefData() const { return m_refData; }
463 void SetRefData(wxObjectRefData *data) { m_refData = data; }
464
465 // make a 'clone' of the object
466 void Ref(const wxObject& clone);
467
468 // destroy a reference
469 void UnRef();
470
471
472 // Reserved for future use
473 virtual void ReservedObjectFunc1() {}
474 virtual void ReservedObjectFunc2() {}
475 virtual void ReservedObjectFunc3() {}
476 virtual void ReservedObjectFunc4() {}
477 virtual void ReservedObjectFunc5() {}
478 virtual void ReservedObjectFunc6() {}
479 virtual void ReservedObjectFunc7() {}
480 virtual void ReservedObjectFunc8() {}
481 virtual void ReservedObjectFunc9() {}
482
483 protected:
484 // ensure that our data is not shared with anybody else: if we have no
485 // data, it is created using CreateRefData() below, if we have shared data
486 // it is copied using CloneRefData(), otherwise nothing is done
487 void AllocExclusive();
488
489 // both methods must be implemented if Unshare() is used, not pure virtual
490 // only because of the backwards compatibility reasons
491
492 // create a new m_refData
493 virtual wxObjectRefData *CreateRefData() const;
494
495 // create a new m_refData initialized with the given one
496 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
497
498 wxObjectRefData *m_refData;
499 };
500
501 // ----------------------------------------------------------------------------
502 // wxObjectRefData: ref counted data meant to be stored in wxObject
503 // ----------------------------------------------------------------------------
504
505 class WXDLLIMPEXP_BASE wxObjectRefData
506 {
507 friend class WXDLLIMPEXP_BASE wxObject;
508
509 public:
510 wxObjectRefData() : m_count(1) { }
511 virtual ~wxObjectRefData() { }
512
513 int GetRefCount() const { return m_count; }
514
515 private:
516 int m_count;
517 };
518
519
520 inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo)
521 {
522 return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : NULL;
523 }
524
525 #if wxUSE_EXTENDED_RTTI
526 class WXDLLIMPEXP_BASE wxDynamicObject : public wxObject
527 {
528 friend class WXDLLIMPEXP_BASE wxDynamicClassInfo ;
529 public:
530 // instantiates this object with an instance of its superclass
531 wxDynamicObject(wxObject* superClassInstance, const wxDynamicClassInfo *info) ;
532 ~wxDynamicObject();
533
534 void SetProperty (const wxChar *propertyName, const wxxVariant &value);
535 wxxVariant GetProperty (const wxChar *propertyName) const ;
536
537 // get the runtime identity of this object
538 wxClassInfo *GetClassInfo() const
539 {
540 #ifdef _MSC_VER
541 return (wxClassInfo*) m_classInfo;
542 #else
543 return wx_const_cast(wxClassInfo *, m_classInfo);
544 #endif
545 }
546
547 wxObject* GetSuperClassInstance() const
548 {
549 return m_superClassInstance ;
550 }
551 private :
552 // removes an existing runtime-property
553 void RemoveProperty( const wxChar *propertyName ) ;
554
555 // renames an existing runtime-property
556 void RenameProperty( const wxChar *oldPropertyName , const wxChar *newPropertyName ) ;
557
558 wxObject *m_superClassInstance ;
559 const wxDynamicClassInfo *m_classInfo;
560 struct wxDynamicObjectInternal;
561 wxDynamicObjectInternal *m_data;
562 };
563 #endif
564
565 // ----------------------------------------------------------------------------
566 // more debugging macros
567 // ----------------------------------------------------------------------------
568
569 #ifdef __WXDEBUG__
570 #ifndef WXDEBUG_NEW
571 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
572 #endif
573 #else // !__WXDEBUG__
574 #define WXDEBUG_NEW new
575 #endif
576
577 // Redefine new to be the debugging version. This doesn't work with all
578 // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish
579 // to use the debugging version.
580
581 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
582 #define new new(__TFILE__,__LINE__)
583 #elif (defined(__WXDEBUG__) && defined(__VISUALC__) && !wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS)
584 // Including this file redefines new and allows leak reports to contain line numbers
585 #include "wx/msw/msvcrt.h"
586 #endif
587
588 #endif // _WX_OBJECTH__
589