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