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