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