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