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