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