XTI updates
[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 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/memory.h"
21
22 // based on the value of wxUSE_EXTENDED_RTTI symbol,
23 // only one of the RTTI system will be compiled:
24 // - the "old" one (defined by rtti.h) or
25 // - the "new" one (defined by xti.h)
26 #include "wx/xti.h"
27 #include "wx/rtti.h"
28
29 #define wxIMPLEMENT_CLASS(name, basename) \
30 wxIMPLEMENT_ABSTRACT_CLASS(name, basename)
31
32 #define wxIMPLEMENT_CLASS2(name, basename1, basename2) \
33 wxIMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
34
35 // -----------------------------------
36 // for pluggable classes
37 // -----------------------------------
38
39 // NOTE: this should probably be the very first statement
40 // in the class declaration so wxPluginSentinel is
41 // the first member initialised and the last destroyed.
42
43 // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
44
45 #if wxUSE_NESTED_CLASSES
46
47 #define _DECLARE_DL_SENTINEL(name, exportdecl) \
48 class exportdecl name##PluginSentinel { \
49 private: \
50 static const wxString sm_className; \
51 public: \
52 name##PluginSentinel(); \
53 ~name##PluginSentinel(); \
54 }; \
55 name##PluginSentinel m_pluginsentinel
56
57 #define _IMPLEMENT_DL_SENTINEL(name) \
58 const wxString name::name##PluginSentinel::sm_className(#name); \
59 name::name##PluginSentinel::name##PluginSentinel() { \
60 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
61 if( e != 0 ) { e->RefObj(); } \
62 } \
63 name::name##PluginSentinel::~name##PluginSentinel() { \
64 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
65 if( e != 0 ) { e->UnrefObj(); } \
66 }
67 #else
68
69 #define _DECLARE_DL_SENTINEL(name)
70 #define _IMPLEMENT_DL_SENTINEL(name)
71
72 #endif // wxUSE_NESTED_CLASSES
73
74 #define wxDECLARE_PLUGGABLE_CLASS(name) \
75 wxDECLARE_DYNAMIC_CLASS(name); _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE)
76 #define wxDECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
77 wxDECLARE_ABSTRACT_CLASS(name); _DECLARE_DL_SENTINEL(name, WXDLLIMPEXP_CORE)
78
79 #define wxDECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
80 wxDECLARE_DYNAMIC_CLASS(name); _DECLARE_DL_SENTINEL(name, usergoo)
81 #define wxDECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
82 wxDECLARE_ABSTRACT_CLASS(name); _DECLARE_DL_SENTINEL(name, usergoo)
83
84 #define wxIMPLEMENT_PLUGGABLE_CLASS(name, basename) \
85 wxIMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
86 #define wxIMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
87 wxIMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
88 #define wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
89 wxIMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
90 #define wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
91 wxIMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
92
93 #define wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
94 wxIMPLEMENT_PLUGGABLE_CLASS(name, basename)
95 #define wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
96 wxIMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
97 #define wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
98 wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
99 #define wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
100 wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
101
102 #define wxCLASSINFO(name) (&name::ms_classInfo)
103
104 #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo)
105
106 // Just seems a bit nicer-looking (pretend it's not a macro)
107 #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo)
108
109 // this cast does some more checks at compile time as it uses static_cast
110 // internally
111 //
112 // note that it still has different semantics from dynamic_cast<> and so can't
113 // be replaced by it as long as there are any compilers not supporting it
114 #define wxDynamicCast(obj, className) \
115 ((className *) wxCheckDynamicCast( \
116 const_cast<wxObject *>(static_cast<const wxObject *>(\
117 const_cast<className *>(static_cast<const className *>(obj)))), \
118 &className::ms_classInfo))
119
120 // The 'this' pointer is always true, so use this version
121 // to cast the this pointer and avoid compiler warnings.
122 #define wxDynamicCastThis(className) \
123 (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0)
124
125 // FIXME-VC6: dummy argument needed because VC6 doesn't support explicitly
126 // choosing the template function to call
127 template <class T>
128 inline T *wxCheckCast(const void *ptr, T * = NULL)
129 {
130 wxASSERT_MSG( wxDynamicCast(ptr, T), "wxStaticCast() used incorrectly" );
131 return const_cast<T *>(static_cast<const T *>(ptr));
132 }
133
134 #define wxStaticCast(obj, className) wxCheckCast((obj), (className *)NULL)
135
136 // ----------------------------------------------------------------------------
137 // set up memory debugging macros
138 // ----------------------------------------------------------------------------
139
140 /*
141 Which new/delete operator variants do we want?
142
143 _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0)
144 _WX_WANT_DELETE_VOID = void operator delete (void * buf)
145 _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line)
146 _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int)
147 _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0)
148 _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf)
149 _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int )
150 */
151
152 #if wxUSE_MEMORY_TRACING
153
154 // All compilers get this one
155 #define _WX_WANT_NEW_SIZET_WXCHAR_INT
156
157 // Everyone except Visage gets the next one
158 #ifndef __VISAGECPP__
159 #define _WX_WANT_DELETE_VOID
160 #endif
161
162 // Only visage gets this one under the correct circumstances
163 #if defined(__VISAGECPP__) && __DEBUG_ALLOC__
164 #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
165 #endif
166
167 // Only VC++ 6 and CodeWarrior get overloaded delete that matches new
168 #if (defined(__VISUALC__) && (__VISUALC__ >= 1200)) || \
169 (defined(__MWERKS__) && (__MWERKS__ >= 0x2400))
170 #define _WX_WANT_DELETE_VOID_WXCHAR_INT
171 #endif
172
173 // Now see who (if anyone) gets the array memory operators
174 #if wxUSE_ARRAY_MEMORY_OPERATORS
175
176 // Everyone except Visual C++ (cause problems for VC++ - crashes)
177 #if !defined(__VISUALC__)
178 #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
179 #endif
180
181 // Everyone except Visual C++ (cause problems for VC++ - crashes)
182 #if !defined(__VISUALC__)
183 #define _WX_WANT_ARRAY_DELETE_VOID
184 #endif
185
186 // Only CodeWarrior 6 or higher
187 #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400)
188 #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
189 #endif
190
191 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
192
193 #endif // wxUSE_MEMORY_TRACING
194
195 // ----------------------------------------------------------------------------
196 // Compatibility macro aliases DECLARE group
197 // ----------------------------------------------------------------------------
198 // deprecated variants _not_ requiring a semicolon after them and without wx prefix.
199 // (note that also some wx-prefixed macro do _not_ require a semicolon because
200 // it's not always possible to force the compire to require it)
201
202 #define DECLARE_CLASS_INFO_ITERATORS() wxDECLARE_CLASS_INFO_ITERATORS();
203 #define DECLARE_ABSTRACT_CLASS(n) wxDECLARE_ABSTRACT_CLASS(n);
204 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(n) wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(n);
205 #define DECLARE_DYNAMIC_CLASS_NO_COPY(n) wxDECLARE_DYNAMIC_CLASS_NO_COPY(n);
206 #define DECLARE_DYNAMIC_CLASS(n) wxDECLARE_DYNAMIC_CLASS(n);
207 #define DECLARE_CLASS(n) wxDECLARE_CLASS(n);
208
209 #define DECLARE_PLUGGABLE_CLASS(n) wxDECLARE_PLUGGABLE_CLASS(n);
210 #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(n) wxDECLARE_ABSTRACT_PLUGGABLE_CLASS(n);
211 #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(n,u) wxDECLARE_USER_EXPORTED_PLUGGABLE_CLASS(n,u);
212 #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,u) wxDECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,u);
213
214 // ----------------------------------------------------------------------------
215 // wxRefCounter: ref counted data "manager"
216 // ----------------------------------------------------------------------------
217
218 class WXDLLIMPEXP_BASE wxRefCounter
219 {
220 public:
221 wxRefCounter() { m_count = 1; }
222
223 int GetRefCount() const { return m_count; }
224
225 void IncRef() { m_count++; }
226 void DecRef();
227
228 protected:
229 // this object should never be destroyed directly but only as a
230 // result of a DecRef() call:
231 virtual ~wxRefCounter() { }
232
233 private:
234 // our refcount:
235 int m_count;
236
237 // It doesn't make sense to copy the reference counted objects, a new ref
238 // counter should be created for a new object instead and compilation
239 // errors in the code using wxRefCounter due to the lack of copy ctor often
240 // indicate a problem, e.g. a forgotten copy ctor implementation somewhere.
241 wxDECLARE_NO_COPY_CLASS(wxRefCounter);
242 };
243
244 // ----------------------------------------------------------------------------
245 // wxObjectRefData: ref counted data meant to be stored in wxObject
246 // ----------------------------------------------------------------------------
247
248 typedef wxRefCounter wxObjectRefData;
249
250 // ----------------------------------------------------------------------------
251 // wxObjectDataPtr: helper class to avoid memleaks because of missing calls
252 // to wxObjectRefData::DecRef
253 // ----------------------------------------------------------------------------
254
255 template <class T>
256 class wxObjectDataPtr
257 {
258 public:
259 typedef T element_type;
260
261 wxEXPLICIT wxObjectDataPtr(T *ptr = NULL) : m_ptr(ptr) {}
262
263 // copy ctor
264 wxObjectDataPtr(const wxObjectDataPtr<T> &tocopy)
265 : m_ptr(tocopy.m_ptr)
266 {
267 if (m_ptr)
268 m_ptr->IncRef();
269 }
270
271 ~wxObjectDataPtr()
272 {
273 if (m_ptr)
274 m_ptr->DecRef();
275 }
276
277 T *get() const { return m_ptr; }
278
279 // test for pointer validity: defining conversion to unspecified_bool_type
280 // and not more obvious bool to avoid implicit conversions to integer types
281 typedef T *(wxObjectDataPtr<T>::*unspecified_bool_type)() const;
282 operator unspecified_bool_type() const
283 {
284 return m_ptr ? &wxObjectDataPtr<T>::get : NULL;
285 }
286
287 T& operator*() const
288 {
289 wxASSERT(m_ptr != NULL);
290 return *(m_ptr);
291 }
292
293 T *operator->() const
294 {
295 wxASSERT(m_ptr != NULL);
296 return get();
297 }
298
299 void reset(T *ptr)
300 {
301 if (m_ptr)
302 m_ptr->DecRef();
303 m_ptr = ptr;
304 }
305
306 wxObjectDataPtr& operator=(const wxObjectDataPtr &tocopy)
307 {
308 if (m_ptr)
309 m_ptr->DecRef();
310 m_ptr = tocopy.m_ptr;
311 if (m_ptr)
312 m_ptr->IncRef();
313 return *this;
314 }
315
316 wxObjectDataPtr& operator=(T *ptr)
317 {
318 if (m_ptr)
319 m_ptr->DecRef();
320 m_ptr = ptr;
321 return *this;
322 }
323
324 private:
325 T *m_ptr;
326 };
327
328 // ----------------------------------------------------------------------------
329 // wxObject: the root class of wxWidgets object hierarchy
330 // ----------------------------------------------------------------------------
331
332 class WXDLLIMPEXP_BASE wxObject
333 {
334 wxDECLARE_ABSTRACT_CLASS(wxObject);
335
336 public:
337 wxObject() { m_refData = NULL; }
338 virtual ~wxObject() { UnRef(); }
339
340 wxObject(const wxObject& other)
341 {
342 m_refData = other.m_refData;
343 if (m_refData)
344 m_refData->IncRef();
345 }
346
347 wxObject& operator=(const wxObject& other)
348 {
349 if ( this != &other )
350 {
351 Ref(other);
352 }
353 return *this;
354 }
355
356 bool IsKindOf(const wxClassInfo *info) const;
357
358
359 // Turn on the correct set of new and delete operators
360
361 #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT
362 void *operator new ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
363 #endif
364
365 #ifdef _WX_WANT_DELETE_VOID
366 void operator delete ( void * buf );
367 #endif
368
369 #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET
370 void operator delete ( void *buf, const char *_fname, size_t _line );
371 #endif
372
373 #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT
374 void operator delete ( void *buf, const wxChar*, int );
375 #endif
376
377 #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT
378 void *operator new[] ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 );
379 #endif
380
381 #ifdef _WX_WANT_ARRAY_DELETE_VOID
382 void operator delete[] ( void *buf );
383 #endif
384
385 #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT
386 void operator delete[] (void* buf, const wxChar*, int );
387 #endif
388
389 // ref counted data handling methods
390
391 // get/set
392 wxObjectRefData *GetRefData() const { return m_refData; }
393 void SetRefData(wxObjectRefData *data) { m_refData = data; }
394
395 // make a 'clone' of the object
396 void Ref(const wxObject& clone);
397
398 // destroy a reference
399 void UnRef();
400
401 // Make sure this object has only one reference
402 void UnShare() { AllocExclusive(); }
403
404 // check if this object references the same data as the other one
405 bool IsSameAs(const wxObject& o) const { return m_refData == o.m_refData; }
406
407 protected:
408 // ensure that our data is not shared with anybody else: if we have no
409 // data, it is created using CreateRefData() below, if we have shared data
410 // it is copied using CloneRefData(), otherwise nothing is done
411 void AllocExclusive();
412
413 // both methods must be implemented if AllocExclusive() is used, not pure
414 // virtual only because of the backwards compatibility reasons
415
416 // create a new m_refData
417 virtual wxObjectRefData *CreateRefData() const;
418
419 // create a new m_refData initialized with the given one
420 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
421
422 wxObjectRefData *m_refData;
423 };
424
425 inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo)
426 {
427 return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : NULL;
428 }
429
430 #include "wx/xti2.h"
431
432 // ----------------------------------------------------------------------------
433 // more debugging macros
434 // ----------------------------------------------------------------------------
435
436 #if wxUSE_DEBUG_NEW_ALWAYS
437 #define WXDEBUG_NEW new(__TFILE__,__LINE__)
438
439 #if wxUSE_GLOBAL_MEMORY_OPERATORS
440 #define new WXDEBUG_NEW
441 #elif defined(__VISUALC__)
442 // Including this file redefines new and allows leak reports to
443 // contain line numbers
444 #include "wx/msw/msvcrt.h"
445 #endif
446 #endif // wxUSE_DEBUG_NEW_ALWAYS
447
448 // ----------------------------------------------------------------------------
449 // Compatibility macro aliases IMPLEMENT group
450 // ----------------------------------------------------------------------------
451
452 // deprecated variants _not_ requiring a semicolon after them and without wx prefix.
453 // (note that also some wx-prefixed macro do _not_ require a semicolon because
454 // it's not always possible to force the compire to require it)
455
456 #define IMPLEMENT_DYNAMIC_CLASS(n,b) wxIMPLEMENT_DYNAMIC_CLASS(n,b)
457 #define IMPLEMENT_DYNAMIC_CLASS2(n,b1,b2) wxIMPLEMENT_DYNAMIC_CLASS2(n,b1,b2)
458 #define IMPLEMENT_ABSTRACT_CLASS(n,b) wxIMPLEMENT_ABSTRACT_CLASS(n,b)
459 #define IMPLEMENT_ABSTRACT_CLASS2(n,b1,b2) wxIMPLEMENT_ABSTRACT_CLASS2(n,b1,b2)
460 #define IMPLEMENT_CLASS(n,b) wxIMPLEMENT_CLASS(n,b)
461 #define IMPLEMENT_CLASS2(n,b1,b2) wxIMPLEMENT_CLASS2(n,b1,b2)
462
463 #define IMPLEMENT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_PLUGGABLE_CLASS(n,b)
464 #define IMPLEMENT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_PLUGGABLE_CLASS2(n,b,b2)
465 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(n,b)
466 #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2)
467 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(n,b)
468 #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(n,b,b2)
469 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,b) wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(n,b)
470 #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2) wxIMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(n,b,b2)
471
472 #define CLASSINFO(n) wxCLASSINFO(n)
473
474 #endif // _WX_OBJECTH__