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