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