]> git.saurik.com Git - wxWidgets.git/blame - include/wx/object.h
Added new stream tests to typetest.
[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);
807d8487 87
0b9ab0bd
RL
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
1f428942
VZ
125 // FIXME: this should be private (currently used directly by way too
126 // many clients)
0b9ab0bd 127 static wxHashTable *sm_classTable;
1f428942
VZ
128
129private:
130 // InitializeClasses() helper
131 static wxClassInfo *GetBaseByName(const wxChar *name);
c801d85f
KB
132};
133
0b9ab0bd 134WXDLLEXPORT wxObject *wxCreateDynamicObject(const wxChar *name);
c801d85f 135
0b9ab0bd
RL
136// ----------------------------------------------------------------------------
137// Dynamic class macros
138// ----------------------------------------------------------------------------
139
140#define DECLARE_DYNAMIC_CLASS(name) \
141 public: \
142 static wxClassInfo sm_class##name; \
143 virtual wxClassInfo *GetClassInfo() const \
0c32066b 144 { return &name::sm_class##name; }
c801d85f
KB
145
146#define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
147#define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
148
0b9ab0bd
RL
149// -----------------------------------
150// for concrete classes
151// -----------------------------------
152
153 // Single inheritance with one base class
c801d85f 154
0b9ab0bd
RL
155#define IMPLEMENT_DYNAMIC_CLASS(name, basename) \
156 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
157 { return new name; } \
158 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
159 0, (int) sizeof(name), \
160 (wxObjectConstructorFn) wxConstructorFor##name);
c801d85f 161
0b9ab0bd 162 // Multiple inheritance with two base classes
c801d85f 163
0b9ab0bd
RL
164#define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \
165 wxObject* WXDLLEXPORT_CTORFN wxConstructorFor##name() \
166 { return new name; } \
167 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
168 wxT(#basename2), (int) sizeof(name), \
169 (wxObjectConstructorFn) wxConstructorFor##name);
c801d85f 170
0b9ab0bd
RL
171// -----------------------------------
172// for abstract classes
173// -----------------------------------
c801d85f 174
0b9ab0bd
RL
175 // Single inheritance with one base class
176
177#define IMPLEMENT_ABSTRACT_CLASS(name, basename) \
178 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename), \
179 0, (int) sizeof(name), (wxObjectConstructorFn) 0);
180
181 // Multiple inheritance with two base classes
182
183#define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
184 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
185 wxT(#basename2), (int) sizeof(name), \
186 (wxObjectConstructorFn) 0);
c801d85f
KB
187
188#define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
189#define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
190
0b9ab0bd
RL
191// -----------------------------------
192// for pluggable classes
193// -----------------------------------
194
195 // NOTE: this should probably be the very first statement
196 // in the class declaration so wxPluginSentinel is
197 // the first member initialised and the last destroyed.
198
199// _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel;
200
201#if wxUSE_NESTED_CLASSES
202
60b73526
RL
203#define _DECLARE_DL_SENTINEL(name, exportdecl) \
204class exportdecl name##PluginSentinel { \
205private: \
206 static const wxString sm_className; \
207public: \
208 name##PluginSentinel(); \
2e0b1b11 209 ~name##PluginSentinel(); \
60b73526 210}; \
0b9ab0bd 211name##PluginSentinel m_pluginsentinel;
0b9ab0bd
RL
212
213#define _IMPLEMENT_DL_SENTINEL(name) \
214 const wxString name::name##PluginSentinel::sm_className(#name); \
215 name::name##PluginSentinel::name##PluginSentinel() { \
4f89dbc4 216 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
7c1e2b44 217 if( e != 0 ) { e->RefObj(); } \
0b9ab0bd 218 } \
abad5367 219 name::name##PluginSentinel::~name##PluginSentinel() { \
4f89dbc4 220 wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \
7c1e2b44 221 if( e != 0 ) { e->UnrefObj(); } \
0b9ab0bd
RL
222 }
223#else
224
225#define _DECLARE_DL_SENTINEL(name)
226#define _IMPLEMENT_DL_SENTINEL(name)
227
228#endif // wxUSE_NESTED_CLASSES
229
230#define DECLARE_PLUGGABLE_CLASS(name) \
60b73526 231 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
0b9ab0bd 232#define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \
60b73526
RL
233 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT)
234
235#define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \
236 DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
237#define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \
238 DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo)
0b9ab0bd
RL
239
240#define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \
241 IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
242#define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \
243 IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
0b9ab0bd
RL
244#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
245 IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name)
246#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
247 IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name)
248
60b73526
RL
249#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \
250 IMPLEMENT_PLUGGABLE_CLASS(name, basename)
251#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \
252 IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
253#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \
254 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
255#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \
256 IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
257
0b9ab0bd 258
0c32066b 259#define CLASSINFO(name) (&name::sm_class##name)
c801d85f 260
34636400 261#else // !wxUSE_DYNAMIC_CLASSES
c801d85f 262
0b9ab0bd
RL
263 // No dynamic class system: so stub out the macros
264
c801d85f
KB
265#define DECLARE_DYNAMIC_CLASS(name)
266#define DECLARE_ABSTRACT_CLASS(name)
267#define DECLARE_CLASS(name)
268#define IMPLEMENT_DYNAMIC_CLASS(name, basename)
269#define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2)
270#define IMPLEMENT_ABSTRACT_CLASS(name, basename)
271#define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2)
272#define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
273#define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2
274
0b9ab0bd
RL
275#define DECLARE_PLUGGABLE_CLASS(name)
276#define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name)
277#define IMPLEMENT_PLUGGABLE_CLASS(name, basename)
278#define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2)
279#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename)
280#define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
281
60b73526
RL
282#define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo)
283#define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo)
284#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename)
285#define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2)
286#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename)
287#define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2)
288
0b9ab0bd
RL
289#endif // wxUSE_DYNAMIC_CLASSES
290
c801d85f 291
3013b6f4
JS
292#define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className)
293
0b9ab0bd
RL
294 // Just seems a bit nicer-looking (pretend it's not a macro)
295
3013b6f4 296#define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className)
c801d85f 297
0b9ab0bd
RL
298 // to be replaced by dynamic_cast<> in the future
299
34636400 300#define wxDynamicCast(obj, className) \
0b9ab0bd
RL
301 (className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)
302
303 // The 'this' pointer is always true, so use this version
304 // to cast the this pointer and avoid compiler warnings.
34636400 305
f7637829 306#define wxDynamicCastThis(className) \
0b9ab0bd 307 (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0)
33ac7e6f 308
f6bcfd97
BP
309#define wxConstCast(obj, className) ((className *)(obj))
310
0b9ab0bd 311
f6bcfd97 312#ifdef __WXDEBUG__
0b9ab0bd
RL
313inline void wxCheckCast(void *ptr)
314{
315 wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") );
316}
317#define wxStaticCast(obj, className) \
318 (wxCheckCast(wxDynamicCast(obj, className)), ((className *)(obj)))
f6bcfd97 319
0b9ab0bd
RL
320#else // !__WXDEBUG__
321#define wxStaticCast(obj, className) ((className *)(obj))
f6bcfd97 322
0b9ab0bd 323#endif // __WXDEBUG__
f6bcfd97 324
0b9ab0bd
RL
325
326 // Unfortunately Borland seems to need this include.
327
328#if wxUSE_STD_IOSTREAM \
329 && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \
330 && defined(__BORLANDC__)
331#if wxUSE_IOSTREAMH
332#include <iostream.h>
333#else
334#include <iostream>
c801d85f 335#endif
fd85b064 336#endif
c801d85f 337
0b9ab0bd
RL
338
339// ----------------------------------------------------------------------------
340// wxObject
341// ----------------------------------------------------------------------------
342
c801d85f
KB
343class WXDLLEXPORT wxObjectRefData;
344
345class WXDLLEXPORT wxObject
346{
0b9ab0bd 347DECLARE_ABSTRACT_CLASS(wxObject)
c801d85f 348
0b9ab0bd
RL
349public:
350 wxObject() : m_refData(0) {}
351 virtual ~wxObject() { UnRef(); }
c801d85f 352
0b9ab0bd 353 bool IsKindOf(wxClassInfo *info) const;
c801d85f 354
ea57084d 355#if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
0b9ab0bd
RL
356 void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0);
357
358#ifndef __VISAGECPP__
8cfc5426 359 void operator delete (void * buf);
0b9ab0bd
RL
360#elif __DEBUG_ALLOC__
361 void operator delete (void *buf, const char *_fname, size_t _line);
362#endif
27198be4 363
0b9ab0bd
RL
364 // VC++ 6.0
365
366#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
8cfc5426 367 void operator delete(void *buf, wxChar*, int);
0b9ab0bd 368#endif
76626af2 369
3f4a0c5b 370 // Causes problems for VC++
8cfc5426 371
0b9ab0bd
RL
372#if wxUSE_ARRAY_MEMORY_OPERATORS && !defined(__VISUALC__) && !defined( __MWERKS__)
373 void *operator new[] (size_t size, wxChar *fileName = 0, int lineNum = 0);
374 void operator delete[] (void *buf);
375#endif
376
377#ifdef __MWERKS__
378 void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0);
379 void *operator new[] (size_t size) { return operator new[] ( size, 0, 0 ) ; }
380 void operator delete[] (void *buf);
381#endif
27198be4 382
3f4a0c5b 383#endif // Debug & memory tracing
c801d85f 384
c801d85f 385
0b9ab0bd
RL
386#if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT)
387 virtual void Dump(wxSTD ostream& str);
c801d85f
KB
388#endif
389
807d8487
VZ
390 // ref counted data handling methods
391
392 // get/set
393 wxObjectRefData *GetRefData() const { return m_refData; }
394 void SetRefData(wxObjectRefData *data) { m_refData = data; }
395
396 // make a 'clone' of the object
0b9ab0bd 397 void Ref(const wxObject& clone);
c801d85f 398
807d8487 399 // destroy a reference
0b9ab0bd 400 void UnRef();
c801d85f 401
c801d85f 402protected:
807d8487
VZ
403 // ensure that our data is not shared with anybody else: if we have no
404 // data, it is created using CreateRefData() below, if we have shared data
405 // it is copied using CloneRefData(), otherwise nothing is done
406 void AllocExclusive();
407
408 // both methods must be implemented if Unshare() is used, not pure virtual
409 // only because of the backwards compatibility reasons
410
411 // create a new m_refData
412 virtual wxObjectRefData *CreateRefData() const;
413
414 // create a new m_refData initialized with the given one
b8027888 415 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
807d8487 416
0b9ab0bd 417 wxObjectRefData *m_refData;
c801d85f
KB
418};
419
c801d85f 420
0b9ab0bd
RL
421class WXDLLEXPORT wxObjectRefData
422{
c801d85f
KB
423 friend class wxObject;
424
425public:
0b9ab0bd
RL
426 wxObjectRefData() : m_count(1) {}
427 virtual ~wxObjectRefData() {}
c801d85f 428
0b9ab0bd 429 inline int GetRefCount() const { return m_count; }
c801d85f
KB
430private:
431 int m_count;
432};
433
0b9ab0bd 434
ea1e6c4b
RL
435inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo)
436{
437 return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : 0;
438}
439
7fe7d506
JS
440#ifdef __WXDEBUG__
441#ifndef WXDEBUG_NEW
4de6207a 442#define WXDEBUG_NEW new(__TFILE__,__LINE__)
7fe7d506
JS
443#endif
444#else
445#define WXDEBUG_NEW new
446#endif
447
0b9ab0bd
RL
448 // Redefine new to be the debugging version. This doesn't
449 // work with all compilers, in which case you need to
450 // use WXDEBUG_NEW explicitly if you wish to use the debugging version.
7fe7d506
JS
451
452#if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
4de6207a 453#define new new(__TFILE__,__LINE__)
c801d85f
KB
454#endif
455
0b9ab0bd 456#endif // _WX_OBJECTH__
c801d85f 457
0b9ab0bd 458// vi:sts=4:sw=4:et