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