]> git.saurik.com Git - wxWidgets.git/blame - include/wx/object.h
Added wchar handling for EMX (gcc on OS/2).
[wxWidgets.git] / include / wx / object.h
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: object.h
3// Purpose: wxObject class, plus run-time type information macros
4// Author: Julian Smart
0b9ab0bd 5// Modified by: Ron Lee
c801d85f
KB
6// Created: 01/02/97
7// RCS-ID: $Id$
0b9ab0bd
RL
8// Copyright: (c) 1997 Julian Smart and Markus Holzem
9// (c) 2001 Ron Lee <ron@debian.org>
3f4a0c5b 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
34138703
JS
13#ifndef _WX_OBJECTH__
14#define _WX_OBJECTH__
c801d85f
KB
15
16#ifdef __GNUG__
0d3820b3 17#pragma interface "object.h"
c801d85f
KB
18#endif
19
0b9ab0bd
RL
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
c801d85f 24#include "wx/defs.h"
e55ad60e 25#include "wx/memory.h"
c801d85f
KB
26
27class WXDLLEXPORT wxObject;
28
47d67540 29#if wxUSE_DYNAMIC_CLASSES
c801d85f 30
0b9ab0bd
RL
31// ----------------------------------------------------------------------------
32// conditional compilation
33// ----------------------------------------------------------------------------
34
c801d85f
KB
35#ifdef GetClassName
36#undef GetClassName
37#endif
3f1af920
JS
38#ifdef GetClassInfo
39#undef GetClassInfo
c801d85f
KB
40#endif
41
42class WXDLLEXPORT wxClassInfo;
f4a8c29f 43class WXDLLEXPORT wxHashTable;
c801d85f 44
38830220 45#if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
0b9ab0bd 46#include "wx/ioswrap.h"
fbc535ff
JS
47#endif
48
c801d85f 49
0b9ab0bd
RL
50// ----------------------------------------------------------------------------
51// wxClassInfo
52// ----------------------------------------------------------------------------
53
54typedef wxObject *(*wxObjectConstructorFn)(void);
c801d85f 55
c801d85f
KB
56class WXDLLEXPORT wxClassInfo
57{
aac65598 58public:
38befbee
RL
59 wxClassInfo( const wxChar *className,
60 const wxChar *baseName1,
61 const wxChar *baseName2,
62 int size,
63 wxObjectConstructorFn ctor )
0b9ab0bd
RL
64 : m_className(className)
65 , m_baseClassName1(baseName1)
66 , m_baseClassName2(baseName2)
67 , m_objectSize(size)
68 , m_objectConstructor(ctor)
69 , m_baseInfo1(0)
70 , m_baseInfo2(0)
71 , m_next(sm_first)
72 { sm_first = this; }
73
74 wxObject *CreateObject() { return m_objectConstructor ? (*m_objectConstructor)() : 0; }
75
76 const wxChar *GetClassName() const { return m_className; }
77 const wxChar *GetBaseClassName1() const { return m_baseClassName1; }
78 const wxChar *GetBaseClassName2() const { return m_baseClassName2; }
79 const wxClassInfo *GetBaseClass1() const { return m_baseInfo1; }
80 const wxClassInfo *GetBaseClass2() const { return m_baseInfo2; }
81 int GetSize() const { return m_objectSize; }
82
83 wxObjectConstructorFn GetConstructor() const { return m_objectConstructor; }
84 static const wxClassInfo *GetFirst() { return sm_first; }
85 const wxClassInfo *GetNext() const { return m_next; }
86 static wxClassInfo *FindClass(const wxChar *className);
87
88 // Climb upwards through inheritance hierarchy.
89 // Dual inheritance is catered for.
90
91 bool IsKindOf(const wxClassInfo *info) const
92 {
93 return info != 0 &&
94 ( info == this ||
95 ( m_baseInfo1 && m_baseInfo1->IsKindOf(info) ) ||
96 ( m_baseInfo2 && m_baseInfo2->IsKindOf(info) ) );
97 }
3f4a0c5b 98
0b9ab0bd 99 // Initializes parent pointers and hash table for fast searching.
c801d85f 100
0b9ab0bd 101 static void InitializeClasses();
0c32066b 102
0b9ab0bd 103 // Cleans up hash table used for fast searching.
0c32066b 104
0b9ab0bd 105 static void CleanUpClasses();
0c32066b
JS
106
107public:
0b9ab0bd
RL
108 const wxChar *m_className;
109 const wxChar *m_baseClassName1;
110 const wxChar *m_baseClassName2;
111 int m_objectSize;
112 wxObjectConstructorFn m_objectConstructor;
113
114 // Pointers to base wxClassInfos: set in InitializeClasses
115
116 const wxClassInfo *m_baseInfo1;
117 const wxClassInfo *m_baseInfo2;
118
119 // class info object live in a linked list:
120 // pointers to its head and the next element in it
121
122 static wxClassInfo *sm_first;
123 wxClassInfo *m_next;
124
125 static wxHashTable *sm_classTable;
c801d85f
KB
126};
127
0b9ab0bd 128WXDLLEXPORT wxObject *wxCreateDynamicObject(const wxChar *name);
c801d85f 129
c801d85f 130
0b9ab0bd
RL
131// ----------------------------------------------------------------------------
132// Dynamic class macros
133// ----------------------------------------------------------------------------
134
135#define DECLARE_DYNAMIC_CLASS(name) \
136 public: \
137 static wxClassInfo sm_class##name; \
138 virtual wxClassInfo *GetClassInfo() const \
0c32066b 139 { return &name::sm_class##name; }
c801d85f
KB
140
141#define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
142#define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
143
0b9ab0bd
RL
144// -----------------------------------
145// for concrete classes
146// -----------------------------------
147
148 // Single inheritance with one base class
c801d85f 149
0b9ab0bd
RL
150#define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
151 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
152 { return new name; } \
153 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
154 0, (int) sizeof(name), \
155 (wxObjectConstructorFn) wxConstructorFor##name);
c801d85f 156
0b9ab0bd 157 // Multiple inheritance with two base classes
c801d85f 158
0b9ab0bd
RL
159#define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
160 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
161 { return new name; } \
162 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
163 wxT(#basename2), (int) sizeof(name), \
164 (wxObjectConstructorFn) wxConstructorFor##name);
c801d85f 165
0b9ab0bd
RL
166// -----------------------------------
167// for abstract classes
168// -----------------------------------
c801d85f 169
0b9ab0bd
RL
170 // Single inheritance with one base class
171
172#define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
173 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
174 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
175
176 // Multiple inheritance with two base classes
177
178#define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
179 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
180 wxT(#basename2), (int) sizeof(name), \
181 (wxObjectConstructorFn) 0);
c801d85f
KB
182
183#define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
184#define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
185
0b9ab0bd
RL
186// -----------------------------------
187// for pluggable classes
188// -----------------------------------
189
190 // NOTE: this should probably be the very first statement
191 // in the class declaration so wxPluginSentinel is
192 // the first member initialised and the last destroyed.
193
194// _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
195
196#if wxUSE_NESTED_CLASSES
197
60b73526
RL
198#define _DECLARE_DL_SENTINEL(name, exportdecl) \
199class exportdecl name##PluginSentinel { \
200private: \
201 static const wxString sm_className; \
202public: \
203 name##PluginSentinel(); \
2e0b1b11 204 ~name##PluginSentinel(); \
60b73526 205}; \
0b9ab0bd 206name##PluginSentinel m_pluginsentinel;
0b9ab0bd
RL
207
208#define _IMPLEMENT_DL_SENTINEL(name) \
209 const wxString name::name##PluginSentinel::sm_className(#name); \
210 name::name##PluginSentinel::name##PluginSentinel() { \
4f89dbc4 211 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
7c1e2b44 212 if( e != 0 ) { e->RefObj(); } \
0b9ab0bd 213 } \
abad5367 214 name::name##PluginSentinel::~name##PluginSentinel() { \
4f89dbc4 215 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
7c1e2b44 216 if( e != 0 ) { e->UnrefObj(); } \
0b9ab0bd
RL
217 }
218#else
219
220#define _DECLARE_DL_SENTINEL(name)
221#define _IMPLEMENT_DL_SENTINEL(name)
222
223#endif // wxUSE_NESTED_CLASSES
224
225#define DECLARE_PLUGGABLE_CLASS(name) \
60b73526 226 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
0b9ab0bd 227#define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
60b73526
RL
228 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
229
230#define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
231 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
232#define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
233 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
0b9ab0bd
RL
234
235#define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
236 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
237#define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
238 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
0b9ab0bd
RL
239#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
240 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
241#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
242 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
243
60b73526
RL
244#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
245 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
246#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
247 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
248#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
249 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
250#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
251 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
252
0b9ab0bd 253
0c32066b 254#define CLASSINFO(name) (&name::sm_class##name)
c801d85f 255
34636400 256#else // !wxUSE_DYNAMIC_CLASSES
c801d85f 257
0b9ab0bd
RL
258 // No dynamic class system: so stub out the macros
259
c801d85f
KB
260#define DECLARE_DYNAMIC_CLASS(name)
261#define DECLARE_ABSTRACT_CLASS(name)
262#define DECLARE_CLASS(name)
263#define IMPLEMENT_DYNAMIC_CLASS(name, basename)
264#define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
265#define IMPLEMENT_ABSTRACT_CLASS(name, basename)
266#define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
267#define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
268#define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
269
0b9ab0bd
RL
270#define DECLARE_PLUGGABLE_CLASS(name)
271#define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
272#define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
273#define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
274#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
275#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
276
60b73526
RL
277#define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
278#define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
279#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
280#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
281#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
282#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
283
0b9ab0bd
RL
284#endif // wxUSE_DYNAMIC_CLASSES
285
c801d85f 286
3013b6f4
JS
287#define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
288
0b9ab0bd
RL
289 // Just seems a bit nicer-looking (pretend it's not a macro)
290
3013b6f4 291#define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
c801d85f 292
0b9ab0bd
RL
293 // to be replaced by dynamic_cast<> in the future
294
34636400 295#define wxDynamicCast(obj, className) \
0b9ab0bd
RL
296 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
297
298 // The 'this' pointer is always true, so use this version
299 // to cast the this pointer and avoid compiler warnings.
34636400 300
f7637829 301#define wxDynamicCastThis(className) \
0b9ab0bd 302 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
33ac7e6f 303
f6bcfd97
BP
304#define wxConstCast(obj, className) ((className *)(obj))
305
0b9ab0bd 306
f6bcfd97 307#ifdef __WXDEBUG__
0b9ab0bd
RL
308inline void wxCheckCast(void *ptr)
309{
310 wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") );
311}
312#define wxStaticCast(obj, className) \
313 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
f6bcfd97 314
0b9ab0bd
RL
315#else // !__WXDEBUG__
316#define wxStaticCast(obj, className) ((className *)(obj))
f6bcfd97 317
0b9ab0bd 318#endif // __WXDEBUG__
f6bcfd97 319
0b9ab0bd
RL
320
321 // Unfortunately Borland seems to need this include.
322
323#if wxUSE_STD_IOSTREAM \
324 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
325 && defined(__BORLANDC__)
326#if wxUSE_IOSTREAMH
327#include <iostream.h>
328#else
329#include <iostream>
c801d85f 330#endif
fd85b064 331#endif
c801d85f 332
0b9ab0bd
RL
333
334// ----------------------------------------------------------------------------
335// wxObject
336// ----------------------------------------------------------------------------
337
c801d85f
KB
338class WXDLLEXPORT wxObjectRefData;
339
340class WXDLLEXPORT wxObject
341{
0b9ab0bd 342DECLARE_ABSTRACT_CLASS(wxObject)
c801d85f 343
0b9ab0bd
RL
344public:
345 wxObject() : m_refData(0) {}
346 virtual ~wxObject() { UnRef(); }
c801d85f 347
0b9ab0bd 348 bool IsKindOf(wxClassInfo *info) const;
c801d85f 349
ea57084d 350#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
0b9ab0bd
RL
351 void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0);
352
353#ifndef __VISAGECPP__
8cfc5426 354 void operator delete (void * buf);
0b9ab0bd
RL
355#elif __DEBUG_ALLOC__
356 void operator delete (void *buf, const char *_fname, size_t _line);
357#endif
27198be4 358
0b9ab0bd
RL
359 // VC++ 6.0
360
361#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
8cfc5426 362 void operator delete(void *buf, wxChar*, int);
0b9ab0bd 363#endif
76626af2 364
3f4a0c5b 365 // Causes problems for VC++
8cfc5426 366
0b9ab0bd
RL
367#if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__)
368 void *operator new[] (size_t size, wxChar *fileName = 0, int lineNum = 0);
369 void operator delete[] (void *buf);
370#endif
371
372#ifdef __MWERKS__
373 void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0);
374 void *operator new[] (size_t size) { return operator new[] ( size, 0, 0 ) ; }
375 void operator delete[] (void *buf);
376#endif
27198be4 377
3f4a0c5b 378#endif // Debug & memory tracing
c801d85f 379
c801d85f 380
0b9ab0bd
RL
381#if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
382 virtual void Dump(wxSTD ostream& str);
c801d85f
KB
383#endif
384
0b9ab0bd
RL
385 // make a 'clone' of the object
386
387 void Ref(const wxObject& clone);
c801d85f 388
0b9ab0bd
RL
389 // destroy a reference
390
391 void UnRef();
c801d85f 392
0b9ab0bd
RL
393 inline wxObjectRefData *GetRefData() const { return m_refData; }
394 inline void SetRefData(wxObjectRefData *data) { m_refData = data; }
c801d85f
KB
395
396protected:
0b9ab0bd 397 wxObjectRefData *m_refData;
c801d85f
KB
398};
399
c801d85f 400
0b9ab0bd
RL
401class WXDLLEXPORT wxObjectRefData
402{
c801d85f
KB
403 friend class wxObject;
404
405public:
0b9ab0bd
RL
406 wxObjectRefData() : m_count(1) {}
407 virtual ~wxObjectRefData() {}
c801d85f 408
0b9ab0bd 409 inline int GetRefCount() const { return m_count; }
c801d85f
KB
410private:
411 int m_count;
412};
413
0b9ab0bd 414
ea1e6c4b
RL
415inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo)
416{
417 return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : 0;
418}
419
7fe7d506
JS
420#ifdef __WXDEBUG__
421#ifndef WXDEBUG_NEW
4de6207a 422#define WXDEBUG_NEW new(__TFILE__,__LINE__)
7fe7d506
JS
423#endif
424#else
425#define WXDEBUG_NEW new
426#endif
427
0b9ab0bd
RL
428 // Redefine new to be the debugging version. This doesn't
429 // work with all compilers, in which case you need to
430 // use WXDEBUG_NEW explicitly if you wish to use the debugging version.
7fe7d506
JS
431
432#if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
4de6207a 433#define new new(__TFILE__,__LINE__)
c801d85f
KB
434#endif
435
0b9ab0bd 436#endif // _WX_OBJECTH__
c801d85f 437
0b9ab0bd 438// vi:sts=4:sw=4:et