]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
b2edef6f | 2 | // Name: wx/object.h |
c801d85f KB |
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 | 15 | |
af49c4b8 | 16 | #if defined(__GNUG__) && !defined(__APPLE__) |
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 | |
27 | class WXDLLEXPORT wxObject; | |
28 | ||
47d67540 | 29 | #if wxUSE_DYNAMIC_CLASSES |
c801d85f | 30 | |
0b9ab0bd RL |
31 | // ---------------------------------------------------------------------------- |
32 | // conditional compilation | |
33 | // ---------------------------------------------------------------------------- | |
34 | ||
b2edef6f VZ |
35 | // this shouldn't be needed any longer as <wx/msw/private.h> does it but it |
36 | // doesn't hurt neither | |
c801d85f KB |
37 | #ifdef GetClassName |
38 | #undef GetClassName | |
39 | #endif | |
3f1af920 JS |
40 | #ifdef GetClassInfo |
41 | #undef GetClassInfo | |
c801d85f KB |
42 | #endif |
43 | ||
44 | class WXDLLEXPORT wxClassInfo; | |
f4a8c29f | 45 | class WXDLLEXPORT wxHashTable; |
b2edef6f | 46 | class WXDLLEXPORT wxObjectRefData; |
c801d85f | 47 | |
38830220 | 48 | #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) |
b2edef6f | 49 | #include "wx/ioswrap.h" |
fbc535ff JS |
50 | #endif |
51 | ||
c801d85f | 52 | |
0b9ab0bd RL |
53 | // ---------------------------------------------------------------------------- |
54 | // wxClassInfo | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
57 | typedef wxObject *(*wxObjectConstructorFn)(void); | |
c801d85f | 58 | |
c801d85f KB |
59 | class WXDLLEXPORT wxClassInfo |
60 | { | |
aac65598 | 61 | public: |
38befbee RL |
62 | wxClassInfo( const wxChar *className, |
63 | const wxChar *baseName1, | |
64 | const wxChar *baseName2, | |
65 | int size, | |
66 | wxObjectConstructorFn ctor ) | |
0b9ab0bd RL |
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) | |
6d1b9112 | 75 | { sm_first = this; } |
0b9ab0bd | 76 | |
aa4b7ef9 | 77 | ~wxClassInfo(); |
1d2eddff | 78 | |
0b9ab0bd RL |
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); | |
807d8487 | 92 | |
0b9ab0bd RL |
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 | } | |
3f4a0c5b | 103 | |
0b9ab0bd | 104 | // Initializes parent pointers and hash table for fast searching. |
c801d85f | 105 | |
0b9ab0bd | 106 | static void InitializeClasses(); |
0c32066b | 107 | |
0b9ab0bd | 108 | // Cleans up hash table used for fast searching. |
0c32066b | 109 | |
0b9ab0bd | 110 | static void CleanUpClasses(); |
0c32066b JS |
111 | |
112 | public: | |
0b9ab0bd RL |
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 | ||
1f428942 VZ |
130 | // FIXME: this should be private (currently used directly by way too |
131 | // many clients) | |
0b9ab0bd | 132 | static wxHashTable *sm_classTable; |
1f428942 VZ |
133 | |
134 | private: | |
135 | // InitializeClasses() helper | |
136 | static wxClassInfo *GetBaseByName(const wxChar *name); | |
684242c6 GD |
137 | |
138 | DECLARE_NO_COPY_CLASS(wxClassInfo) | |
c801d85f KB |
139 | }; |
140 | ||
0b9ab0bd | 141 | WXDLLEXPORT wxObject *wxCreateDynamicObject(const wxChar *name); |
c801d85f | 142 | |
0b9ab0bd RL |
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 \ | |
0c32066b | 151 | { return &name::sm_class##name; } |
c801d85f KB |
152 | |
153 | #define DECLARE_ABSTRACT_CLASS(name) DECLARE_DYNAMIC_CLASS(name) | |
154 | #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name) | |
155 | ||
0b9ab0bd RL |
156 | // ----------------------------------- |
157 | // for concrete classes | |
158 | // ----------------------------------- | |
159 | ||
160 | // Single inheritance with one base class | |
c801d85f | 161 | |
0b9ab0bd RL |
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); | |
c801d85f | 168 | |
0b9ab0bd | 169 | // Multiple inheritance with two base classes |
c801d85f | 170 | |
0b9ab0bd RL |
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); | |
c801d85f | 177 | |
0b9ab0bd RL |
178 | // ----------------------------------- |
179 | // for abstract classes | |
180 | // ----------------------------------- | |
c801d85f | 181 | |
0b9ab0bd RL |
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); | |
c801d85f KB |
194 | |
195 | #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS | |
196 | #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2 | |
197 | ||
0b9ab0bd RL |
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 | ||
60b73526 RL |
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(); \ | |
2e0b1b11 | 216 | ~name##PluginSentinel(); \ |
60b73526 | 217 | }; \ |
0b9ab0bd | 218 | name##PluginSentinel m_pluginsentinel; |
0b9ab0bd RL |
219 | |
220 | #define _IMPLEMENT_DL_SENTINEL(name) \ | |
221 | const wxString name::name##PluginSentinel::sm_className(#name); \ | |
222 | name::name##PluginSentinel::name##PluginSentinel() { \ | |
4f89dbc4 | 223 | wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \ |
7c1e2b44 | 224 | if( e != 0 ) { e->RefObj(); } \ |
0b9ab0bd | 225 | } \ |
abad5367 | 226 | name::name##PluginSentinel::~name##PluginSentinel() { \ |
4f89dbc4 | 227 | wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \ |
7c1e2b44 | 228 | if( e != 0 ) { e->UnrefObj(); } \ |
0b9ab0bd RL |
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) \ | |
60b73526 | 238 | DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT) |
0b9ab0bd | 239 | #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \ |
60b73526 RL |
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) | |
0b9ab0bd RL |
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) | |
0b9ab0bd RL |
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 | ||
60b73526 RL |
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 | ||
0b9ab0bd | 265 | |
0c32066b | 266 | #define CLASSINFO(name) (&name::sm_class##name) |
c801d85f | 267 | |
34636400 | 268 | #else // !wxUSE_DYNAMIC_CLASSES |
c801d85f | 269 | |
0b9ab0bd RL |
270 | // No dynamic class system: so stub out the macros |
271 | ||
c801d85f KB |
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 | ||
0b9ab0bd RL |
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 | ||
60b73526 RL |
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 | ||
0b9ab0bd RL |
296 | #endif // wxUSE_DYNAMIC_CLASSES |
297 | ||
c801d85f | 298 | |
3013b6f4 JS |
299 | #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::sm_class##className) |
300 | ||
b2edef6f | 301 | // Just seems a bit nicer-looking (pretend it's not a macro) |
3013b6f4 | 302 | #define wxIsKindOf(obj, className) obj->IsKindOf(&className::sm_class##className) |
c801d85f | 303 | |
b2edef6f | 304 | // to be replaced by dynamic_cast<> in the future |
34636400 | 305 | #define wxDynamicCast(obj, className) \ |
768f1af6 | 306 | ((className *) wxCheckDynamicCast((wxObject*)(obj), &className::sm_class##className)) |
0b9ab0bd | 307 | |
b2edef6f VZ |
308 | // The 'this' pointer is always true, so use this version |
309 | // to cast the this pointer and avoid compiler warnings. | |
f7637829 | 310 | #define wxDynamicCastThis(className) \ |
0b9ab0bd | 311 | (IsKindOf(&className::sm_class##className) ? (className *)(this) : (className *)0) |
33ac7e6f | 312 | |
f6bcfd97 BP |
313 | #define wxConstCast(obj, className) ((className *)(obj)) |
314 | ||
0b9ab0bd | 315 | |
f6bcfd97 | 316 | #ifdef __WXDEBUG__ |
0b9ab0bd RL |
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))) | |
f6bcfd97 | 323 | |
0b9ab0bd RL |
324 | #else // !__WXDEBUG__ |
325 | #define wxStaticCast(obj, className) ((className *)(obj)) | |
f6bcfd97 | 326 | |
0b9ab0bd | 327 | #endif // __WXDEBUG__ |
f6bcfd97 | 328 | |
0b9ab0bd | 329 | |
b2edef6f | 330 | // for some reason Borland seems to need this include. |
0b9ab0bd RL |
331 | #if wxUSE_STD_IOSTREAM \ |
332 | && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) \ | |
333 | && defined(__BORLANDC__) | |
b2edef6f VZ |
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 | |
c801d85f | 365 | #endif |
b2edef6f VZ |
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 | |
684242c6 | 373 | #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || (defined(__MWERKS__) && (__MWERKS__ >= 0x2400)) |
b2edef6f | 374 | #define _WX_WANT_DELETE_VOID_WXCHAR_INT |
fd85b064 | 375 | #endif |
c801d85f | 376 | |
b2edef6f VZ |
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 | ||
0b9ab0bd RL |
399 | |
400 | // ---------------------------------------------------------------------------- | |
b2edef6f | 401 | // wxObject: the root class of wxWindows object hierarchy |
0b9ab0bd RL |
402 | // ---------------------------------------------------------------------------- |
403 | ||
c801d85f KB |
404 | class WXDLLEXPORT wxObject |
405 | { | |
684242c6 | 406 | DECLARE_ABSTRACT_CLASS(wxObject) |
c801d85f | 407 | |
a6391d30 GD |
408 | private: |
409 | void InitFrom(const wxObject& other); | |
410 | ||
0b9ab0bd | 411 | public: |
b2edef6f | 412 | wxObject() { m_refData = NULL; } |
0b9ab0bd | 413 | virtual ~wxObject() { UnRef(); } |
684242c6 | 414 | |
a6391d30 GD |
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 | ||
0b9ab0bd | 430 | bool IsKindOf(wxClassInfo *info) const; |
c801d85f | 431 | |
0b9ab0bd | 432 | |
b2edef6f VZ |
433 | // Turn on the correct set of new and delete operators |
434 | ||
435 | #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT | |
cf760e4c | 436 | void *operator new ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 ); |
0b9ab0bd | 437 | #endif |
27198be4 | 438 | |
b2edef6f VZ |
439 | #ifdef _WX_WANT_DELETE_VOID |
440 | void operator delete ( void * buf ); | |
441 | #endif | |
0b9ab0bd | 442 | |
b2edef6f VZ |
443 | #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET |
444 | void operator delete ( void *buf, const char *_fname, size_t _line ); | |
0b9ab0bd | 445 | #endif |
76626af2 | 446 | |
b2edef6f | 447 | #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT |
cf760e4c | 448 | void operator delete ( void *buf, const wxChar*, int ); |
b2edef6f | 449 | #endif |
8cfc5426 | 450 | |
b2edef6f | 451 | #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT |
cf760e4c | 452 | void *operator new[] ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 ); |
0b9ab0bd RL |
453 | #endif |
454 | ||
b2edef6f VZ |
455 | #ifdef _WX_WANT_ARRAY_DELETE_VOID |
456 | void operator delete[] ( void *buf ); | |
0b9ab0bd | 457 | #endif |
27198be4 | 458 | |
b2edef6f | 459 | #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT |
cf760e4c | 460 | void operator delete[] (void* buf, const wxChar*, int ); |
b2edef6f | 461 | #endif |
c801d85f | 462 | |
c801d85f | 463 | |
0b9ab0bd RL |
464 | #if wxUSE_STD_IOSTREAM && (defined(__WXDEBUG__) || wxUSE_DEBUG_CONTEXT) |
465 | virtual void Dump(wxSTD ostream& str); | |
c801d85f KB |
466 | #endif |
467 | ||
807d8487 VZ |
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 | |
0b9ab0bd | 475 | void Ref(const wxObject& clone); |
c801d85f | 476 | |
807d8487 | 477 | // destroy a reference |
0b9ab0bd | 478 | void UnRef(); |
c801d85f | 479 | |
c801d85f | 480 | protected: |
807d8487 VZ |
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 | |
b8027888 | 493 | virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const; |
807d8487 | 494 | |
0b9ab0bd | 495 | wxObjectRefData *m_refData; |
c801d85f KB |
496 | }; |
497 | ||
b2edef6f VZ |
498 | // ---------------------------------------------------------------------------- |
499 | // wxObjectRefData: ref counted data meant to be stored in wxObject | |
500 | // ---------------------------------------------------------------------------- | |
c801d85f | 501 | |
0b9ab0bd RL |
502 | class WXDLLEXPORT wxObjectRefData |
503 | { | |
c801d85f KB |
504 | friend class wxObject; |
505 | ||
506 | public: | |
b2edef6f VZ |
507 | wxObjectRefData() : m_count(1) { } |
508 | virtual ~wxObjectRefData() { } | |
509 | ||
510 | int GetRefCount() const { return m_count; } | |
c801d85f | 511 | |
c801d85f KB |
512 | private: |
513 | int m_count; | |
514 | }; | |
515 | ||
0b9ab0bd | 516 | |
ea1e6c4b RL |
517 | inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo) |
518 | { | |
b2edef6f | 519 | return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : NULL; |
ea1e6c4b RL |
520 | } |
521 | ||
b2edef6f VZ |
522 | // ---------------------------------------------------------------------------- |
523 | // more debugging macros | |
524 | // ---------------------------------------------------------------------------- | |
525 | ||
7fe7d506 | 526 | #ifdef __WXDEBUG__ |
b2edef6f VZ |
527 | #ifndef WXDEBUG_NEW |
528 | #define WXDEBUG_NEW new(__TFILE__,__LINE__) | |
529 | #endif | |
530 | #else // !__WXDEBUG__ | |
531 | #define WXDEBUG_NEW new | |
7fe7d506 JS |
532 | #endif |
533 | ||
b2edef6f VZ |
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. | |
7fe7d506 JS |
537 | |
538 | #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS | |
b2edef6f | 539 | #define new new(__TFILE__,__LINE__) |
c801d85f KB |
540 | #endif |
541 | ||
0b9ab0bd | 542 | #endif // _WX_OBJECTH__ |
c801d85f | 543 |