]>
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$ | |
371a5b4e | 8 | // Copyright: (c) 1997 Julian Smart |
0b9ab0bd | 9 | // (c) 2001 Ron Lee <ron@debian.org> |
65571936 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
34138703 JS |
13 | #ifndef _WX_OBJECTH__ |
14 | #define _WX_OBJECTH__ | |
c801d85f | 15 | |
0b9ab0bd RL |
16 | // ---------------------------------------------------------------------------- |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
e55ad60e | 20 | #include "wx/memory.h" |
c801d85f | 21 | |
bddd7a8d | 22 | class WXDLLIMPEXP_BASE wxObject; |
c801d85f | 23 | |
4393b50c | 24 | #ifndef wxUSE_EXTENDED_RTTI |
6d553b5f | 25 | #define wxUSE_EXTENDED_RTTI 0 |
a095505c SC |
26 | #endif |
27 | ||
644cb537 MB |
28 | #define DECLARE_CLASS_INFO_ITERATORS() \ |
29 | class WXDLLIMPEXP_BASE const_iterator \ | |
30 | { \ | |
31 | typedef wxHashTable_Node Node; \ | |
32 | public: \ | |
33 | typedef const wxClassInfo* value_type; \ | |
34 | typedef const value_type& const_reference; \ | |
35 | typedef const_iterator itor; \ | |
36 | typedef value_type* ptr_type; \ | |
37 | \ | |
38 | Node* m_node; \ | |
39 | wxHashTable* m_table; \ | |
40 | public: \ | |
41 | typedef const_reference reference_type; \ | |
42 | typedef ptr_type pointer_type; \ | |
43 | \ | |
44 | const_iterator(Node* node, wxHashTable* table) \ | |
45 | : m_node(node), m_table(table) { } \ | |
46 | const_iterator() : m_node(NULL), m_table(NULL) { } \ | |
47 | value_type operator*() const; \ | |
48 | itor& operator++(); \ | |
49 | const itor operator++(int); \ | |
50 | bool operator!=(const itor& it) const \ | |
51 | { return it.m_node != m_node; } \ | |
52 | bool operator==(const itor& it) const \ | |
53 | { return it.m_node == m_node; } \ | |
54 | }; \ | |
55 | \ | |
56 | static const_iterator begin_classinfo(); \ | |
57 | static const_iterator end_classinfo(); | |
58 | ||
6d553b5f | 59 | #if wxUSE_EXTENDED_RTTI |
a095505c | 60 | #include "wx/xti.h" |
a095505c SC |
61 | #else |
62 | ||
0b9ab0bd RL |
63 | // ---------------------------------------------------------------------------- |
64 | // conditional compilation | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
bddd7a8d VZ |
67 | class WXDLLIMPEXP_BASE wxClassInfo; |
68 | class WXDLLIMPEXP_BASE wxHashTable; | |
644cb537 MB |
69 | class WXDLLIMPEXP_BASE wxObject; |
70 | class WXDLLIMPEXP_BASE wxPluginLibrary; | |
bddd7a8d | 71 | class WXDLLIMPEXP_BASE wxObjectRefData; |
644cb537 | 72 | class WXDLLIMPEXP_BASE wxHashTable_Node; |
c801d85f | 73 | |
0b9ab0bd RL |
74 | // ---------------------------------------------------------------------------- |
75 | // wxClassInfo | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
78 | typedef wxObject *(*wxObjectConstructorFn)(void); | |
c801d85f | 79 | |
bddd7a8d | 80 | class WXDLLIMPEXP_BASE wxClassInfo |
c801d85f | 81 | { |
644cb537 MB |
82 | friend class WXDLLIMPEXP_BASE wxObject; |
83 | friend wxObject *wxCreateDynamicObject(const wxChar *name); | |
aac65598 | 84 | public: |
38befbee | 85 | wxClassInfo( const wxChar *className, |
d1d738f1 VS |
86 | const wxClassInfo *baseInfo1, |
87 | const wxClassInfo *baseInfo2, | |
38befbee RL |
88 | int size, |
89 | wxObjectConstructorFn ctor ) | |
0b9ab0bd | 90 | : m_className(className) |
0b9ab0bd RL |
91 | , m_objectSize(size) |
92 | , m_objectConstructor(ctor) | |
d1d738f1 VS |
93 | , m_baseInfo1(baseInfo1) |
94 | , m_baseInfo2(baseInfo2) | |
0b9ab0bd | 95 | , m_next(sm_first) |
7e548f6b | 96 | { |
d1d738f1 VS |
97 | sm_first = this; |
98 | Register(); | |
99 | } | |
0b9ab0bd | 100 | |
aa4b7ef9 | 101 | ~wxClassInfo(); |
1d2eddff | 102 | |
4e124582 VZ |
103 | wxObject *CreateObject() const |
104 | { return m_objectConstructor ? (*m_objectConstructor)() : 0; } | |
0c06c983 | 105 | bool IsDynamic() const { return (NULL != m_objectConstructor); } |
0b9ab0bd RL |
106 | |
107 | const wxChar *GetClassName() const { return m_className; } | |
469349b5 MB |
108 | const wxChar *GetBaseClassName1() const |
109 | { return m_baseInfo1 ? m_baseInfo1->GetClassName() : NULL; } | |
110 | const wxChar *GetBaseClassName2() const | |
111 | { return m_baseInfo2 ? m_baseInfo2->GetClassName() : NULL; } | |
0b9ab0bd RL |
112 | const wxClassInfo *GetBaseClass1() const { return m_baseInfo1; } |
113 | const wxClassInfo *GetBaseClass2() const { return m_baseInfo2; } | |
114 | int GetSize() const { return m_objectSize; } | |
115 | ||
4e124582 VZ |
116 | wxObjectConstructorFn GetConstructor() const |
117 | { return m_objectConstructor; } | |
0b9ab0bd RL |
118 | static const wxClassInfo *GetFirst() { return sm_first; } |
119 | const wxClassInfo *GetNext() const { return m_next; } | |
120 | static wxClassInfo *FindClass(const wxChar *className); | |
807d8487 | 121 | |
0b9ab0bd RL |
122 | // Climb upwards through inheritance hierarchy. |
123 | // Dual inheritance is catered for. | |
124 | ||
125 | bool IsKindOf(const wxClassInfo *info) const | |
126 | { | |
127 | return info != 0 && | |
128 | ( info == this || | |
129 | ( m_baseInfo1 && m_baseInfo1->IsKindOf(info) ) || | |
130 | ( m_baseInfo2 && m_baseInfo2->IsKindOf(info) ) ); | |
131 | } | |
3f4a0c5b | 132 | |
644cb537 MB |
133 | DECLARE_CLASS_INFO_ITERATORS() |
134 | private: | |
0b9ab0bd | 135 | const wxChar *m_className; |
0b9ab0bd RL |
136 | int m_objectSize; |
137 | wxObjectConstructorFn m_objectConstructor; | |
138 | ||
1125dc3f | 139 | // Pointers to base wxClassInfos |
0b9ab0bd RL |
140 | |
141 | const wxClassInfo *m_baseInfo1; | |
142 | const wxClassInfo *m_baseInfo2; | |
143 | ||
144 | // class info object live in a linked list: | |
145 | // pointers to its head and the next element in it | |
146 | ||
147 | static wxClassInfo *sm_first; | |
148 | wxClassInfo *m_next; | |
149 | ||
150 | static wxHashTable *sm_classTable; | |
1f428942 | 151 | |
7e548f6b | 152 | protected: |
d1d738f1 VS |
153 | // registers the class |
154 | void Register(); | |
155 | void Unregister(); | |
1125dc3f VS |
156 | |
157 | DECLARE_NO_COPY_CLASS(wxClassInfo) | |
c801d85f KB |
158 | }; |
159 | ||
bddd7a8d | 160 | WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxChar *name); |
c801d85f | 161 | |
0b9ab0bd RL |
162 | // ---------------------------------------------------------------------------- |
163 | // Dynamic class macros | |
164 | // ---------------------------------------------------------------------------- | |
165 | ||
14ca93a0 VZ |
166 | #define DECLARE_ABSTRACT_CLASS(name) \ |
167 | public: \ | |
168 | static wxClassInfo ms_classInfo; \ | |
169 | virtual wxClassInfo *GetClassInfo() const; | |
170 | ||
171 | #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \ | |
172 | DECLARE_NO_ASSIGN_CLASS(name) \ | |
fc7a2a60 VZ |
173 | DECLARE_DYNAMIC_CLASS(name) |
174 | ||
14ca93a0 VZ |
175 | #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \ |
176 | DECLARE_NO_COPY_CLASS(name) \ | |
fc7a2a60 VZ |
177 | DECLARE_DYNAMIC_CLASS(name) |
178 | ||
14ca93a0 VZ |
179 | #define DECLARE_DYNAMIC_CLASS(name) \ |
180 | DECLARE_ABSTRACT_CLASS(name) \ | |
181 | static wxObject* wxCreateObject(); | |
182 | ||
c801d85f KB |
183 | #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name) |
184 | ||
14ca93a0 VZ |
185 | |
186 | // common part of the macros below | |
187 | #define wxIMPLEMENT_CLASS_COMMON(name, basename, baseclsinfo2, func) \ | |
188 | wxClassInfo name::ms_classInfo(wxT(#name), \ | |
189 | &basename::ms_classInfo, \ | |
190 | baseclsinfo2, \ | |
191 | (int) sizeof(name), \ | |
192 | (wxObjectConstructorFn) func); \ | |
193 | \ | |
194 | wxClassInfo *name::GetClassInfo() const \ | |
195 | { return &name::ms_classInfo; } | |
196 | ||
197 | #define wxIMPLEMENT_CLASS_COMMON1(name, basename, func) \ | |
198 | wxIMPLEMENT_CLASS_COMMON(name, basename, NULL, func) | |
199 | ||
200 | #define wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, func) \ | |
2b199a38 | 201 | wxIMPLEMENT_CLASS_COMMON(name, basename1, &basename2::ms_classInfo, func) |
14ca93a0 | 202 | |
0b9ab0bd RL |
203 | // ----------------------------------- |
204 | // for concrete classes | |
205 | // ----------------------------------- | |
206 | ||
207 | // Single inheritance with one base class | |
14ca93a0 VZ |
208 | #define IMPLEMENT_DYNAMIC_CLASS(name, basename) \ |
209 | wxIMPLEMENT_CLASS_COMMON1(name, basename, name::wxCreateObject) \ | |
210 | wxObject* name::wxCreateObject() \ | |
211 | { return new name; } | |
c801d85f | 212 | |
0b9ab0bd | 213 | // Multiple inheritance with two base classes |
14ca93a0 VZ |
214 | #define IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) \ |
215 | wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, \ | |
216 | name::wxCreateObject) \ | |
217 | wxObject* name::wxCreateObject() \ | |
218 | { return new name; } | |
c801d85f | 219 | |
0b9ab0bd RL |
220 | // ----------------------------------- |
221 | // for abstract classes | |
222 | // ----------------------------------- | |
c801d85f | 223 | |
0b9ab0bd RL |
224 | // Single inheritance with one base class |
225 | ||
14ca93a0 VZ |
226 | #define IMPLEMENT_ABSTRACT_CLASS(name, basename) \ |
227 | wxIMPLEMENT_CLASS_COMMON1(name, basename, NULL) | |
0b9ab0bd RL |
228 | |
229 | // Multiple inheritance with two base classes | |
230 | ||
14ca93a0 VZ |
231 | #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \ |
232 | wxIMPLEMENT_CLASS_COMMON2(name, basename1, basename2, NULL) | |
c801d85f KB |
233 | |
234 | #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS | |
235 | #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2 | |
236 | ||
6d553b5f | 237 | #endif // !wxUSE_EXTENDED_RTTI |
a095505c SC |
238 | |
239 | ||
0b9ab0bd RL |
240 | // ----------------------------------- |
241 | // for pluggable classes | |
242 | // ----------------------------------- | |
243 | ||
244 | // NOTE: this should probably be the very first statement | |
245 | // in the class declaration so wxPluginSentinel is | |
246 | // the first member initialised and the last destroyed. | |
247 | ||
248 | // _DECLARE_DL_SENTINEL(name) wxPluginSentinel m_pluginsentinel; | |
249 | ||
250 | #if wxUSE_NESTED_CLASSES | |
251 | ||
60b73526 RL |
252 | #define _DECLARE_DL_SENTINEL(name, exportdecl) \ |
253 | class exportdecl name##PluginSentinel { \ | |
254 | private: \ | |
255 | static const wxString sm_className; \ | |
256 | public: \ | |
257 | name##PluginSentinel(); \ | |
2e0b1b11 | 258 | ~name##PluginSentinel(); \ |
60b73526 | 259 | }; \ |
0b9ab0bd | 260 | name##PluginSentinel m_pluginsentinel; |
0b9ab0bd RL |
261 | |
262 | #define _IMPLEMENT_DL_SENTINEL(name) \ | |
263 | const wxString name::name##PluginSentinel::sm_className(#name); \ | |
264 | name::name##PluginSentinel::name##PluginSentinel() { \ | |
4f89dbc4 | 265 | wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \ |
7c1e2b44 | 266 | if( e != 0 ) { e->RefObj(); } \ |
0b9ab0bd | 267 | } \ |
abad5367 | 268 | name::name##PluginSentinel::~name##PluginSentinel() { \ |
4f89dbc4 | 269 | wxPluginLibrary *e = (wxPluginLibrary*) wxPluginLibrary::ms_classes.Get(#name); \ |
7c1e2b44 | 270 | if( e != 0 ) { e->UnrefObj(); } \ |
0b9ab0bd RL |
271 | } |
272 | #else | |
273 | ||
274 | #define _DECLARE_DL_SENTINEL(name) | |
275 | #define _IMPLEMENT_DL_SENTINEL(name) | |
276 | ||
277 | #endif // wxUSE_NESTED_CLASSES | |
278 | ||
279 | #define DECLARE_PLUGGABLE_CLASS(name) \ | |
60b73526 | 280 | DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT) |
0b9ab0bd | 281 | #define DECLARE_ABSTRACT_PLUGGABLE_CLASS(name) \ |
60b73526 RL |
282 | DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, WXDLLEXPORT) |
283 | ||
284 | #define DECLARE_USER_EXPORTED_PLUGGABLE_CLASS(name, usergoo) \ | |
285 | DECLARE_DYNAMIC_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo) | |
286 | #define DECLARE_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, usergoo) \ | |
287 | DECLARE_ABSTRACT_CLASS(name) _DECLARE_DL_SENTINEL(name, usergoo) | |
0b9ab0bd RL |
288 | |
289 | #define IMPLEMENT_PLUGGABLE_CLASS(name, basename) \ | |
290 | IMPLEMENT_DYNAMIC_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name) | |
291 | #define IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) \ | |
292 | IMPLEMENT_DYNAMIC_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name) | |
0b9ab0bd RL |
293 | #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) \ |
294 | IMPLEMENT_ABSTRACT_CLASS(name, basename) _IMPLEMENT_DL_SENTINEL(name) | |
295 | #define IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \ | |
296 | IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) _IMPLEMENT_DL_SENTINEL(name) | |
297 | ||
60b73526 RL |
298 | #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS(name, basename) \ |
299 | IMPLEMENT_PLUGGABLE_CLASS(name, basename) | |
300 | #define IMPLEMENT_USER_EXPORTED_PLUGGABLE_CLASS2(name, basename1, basename2) \ | |
301 | IMPLEMENT_PLUGGABLE_CLASS2(name, basename1, basename2) | |
302 | #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS(name, basename) \ | |
303 | IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS(name, basename) | |
304 | #define IMPLEMENT_USER_EXPORTED_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) \ | |
305 | IMPLEMENT_ABSTRACT_PLUGGABLE_CLASS2(name, basename1, basename2) | |
306 | ||
c0db9626 | 307 | #define CLASSINFO(name) (&name::ms_classInfo) |
c801d85f | 308 | |
c0db9626 | 309 | #define wxIS_KIND_OF(obj, className) obj->IsKindOf(&className::ms_classInfo) |
3013b6f4 | 310 | |
b2edef6f | 311 | // Just seems a bit nicer-looking (pretend it's not a macro) |
c0db9626 | 312 | #define wxIsKindOf(obj, className) obj->IsKindOf(&className::ms_classInfo) |
c801d85f | 313 | |
73fbb031 VZ |
314 | // this cast does some more checks at compile time as it uses static_cast |
315 | // internally | |
316 | // | |
317 | // note that it still has different semantics from dynamic_cast<> and so can't | |
318 | // be replaced by it as long as there are any compilers not supporting it | |
34636400 | 319 | #define wxDynamicCast(obj, className) \ |
5232d996 VZ |
320 | ((className *) wxCheckDynamicCast( \ |
321 | wx_const_cast(wxObject *, wx_static_cast(const wxObject *, \ | |
322 | wx_const_cast(className *, wx_static_cast(const className *, obj)))), \ | |
323 | &className::ms_classInfo)) | |
0b9ab0bd | 324 | |
b2edef6f VZ |
325 | // The 'this' pointer is always true, so use this version |
326 | // to cast the this pointer and avoid compiler warnings. | |
f7637829 | 327 | #define wxDynamicCastThis(className) \ |
73fbb031 | 328 | (IsKindOf(&className::ms_classInfo) ? (className *)(this) : (className *)0) |
33ac7e6f | 329 | |
f6bcfd97 | 330 | #ifdef __WXDEBUG__ |
e32d4836 | 331 | inline void* wxCheckCast(void *ptr) |
0b9ab0bd RL |
332 | { |
333 | wxASSERT_MSG( ptr, _T("wxStaticCast() used incorrectly") ); | |
e32d4836 | 334 | return ptr; |
0b9ab0bd RL |
335 | } |
336 | #define wxStaticCast(obj, className) \ | |
e32d4836 | 337 | ((className *)wxCheckCast(wxDynamicCast(obj, className))) |
f6bcfd97 | 338 | |
0b9ab0bd | 339 | #else // !__WXDEBUG__ |
ef0aea5a VZ |
340 | #define wxStaticCast(obj, className) \ |
341 | wx_const_cast(className *, wx_static_cast(const className *, obj)) | |
f6bcfd97 | 342 | |
0b9ab0bd | 343 | #endif // __WXDEBUG__ |
f6bcfd97 | 344 | |
b2edef6f VZ |
345 | // ---------------------------------------------------------------------------- |
346 | // set up memory debugging macros | |
347 | // ---------------------------------------------------------------------------- | |
348 | ||
349 | /* | |
350 | Which new/delete operator variants do we want? | |
351 | ||
352 | _WX_WANT_NEW_SIZET_WXCHAR_INT = void *operator new (size_t size, wxChar *fileName = 0, int lineNum = 0) | |
353 | _WX_WANT_DELETE_VOID = void operator delete (void * buf) | |
354 | _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET = void operator delete (void *buf, const char *_fname, size_t _line) | |
355 | _WX_WANT_DELETE_VOID_WXCHAR_INT = void operator delete(void *buf, wxChar*, int) | |
356 | _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT = void *operator new[] (size_t size, wxChar *fileName , int lineNum = 0) | |
357 | _WX_WANT_ARRAY_DELETE_VOID = void operator delete[] (void *buf) | |
358 | _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT = void operator delete[] (void* buf, wxChar*, int ) | |
359 | */ | |
360 | ||
361 | #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING | |
362 | ||
363 | // All compilers get this one | |
364 | #define _WX_WANT_NEW_SIZET_WXCHAR_INT | |
365 | ||
366 | // Everyone except Visage gets the next one | |
367 | #ifndef __VISAGECPP__ | |
368 | #define _WX_WANT_DELETE_VOID | |
c801d85f | 369 | #endif |
b2edef6f VZ |
370 | |
371 | // Only visage gets this one under the correct circumstances | |
372 | #if defined(__VISAGECPP__) && __DEBUG_ALLOC__ | |
373 | #define _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET | |
374 | #endif | |
375 | ||
4e124582 VZ |
376 | // Only VC++ 6 and CodeWarrior get overloaded delete that matches new |
377 | #if (defined(__VISUALC__) && (__VISUALC__ >= 1200)) || \ | |
378 | (defined(__MWERKS__) && (__MWERKS__ >= 0x2400)) | |
b2edef6f | 379 | #define _WX_WANT_DELETE_VOID_WXCHAR_INT |
fd85b064 | 380 | #endif |
c801d85f | 381 | |
b2edef6f VZ |
382 | // Now see who (if anyone) gets the array memory operators |
383 | #if wxUSE_ARRAY_MEMORY_OPERATORS | |
384 | ||
385 | // Everyone except Visual C++ (cause problems for VC++ - crashes) | |
386 | #if !defined(__VISUALC__) | |
387 | #define _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT | |
388 | #endif | |
389 | ||
390 | // Everyone except Visual C++ (cause problems for VC++ - crashes) | |
391 | #if !defined(__VISUALC__) | |
392 | #define _WX_WANT_ARRAY_DELETE_VOID | |
393 | #endif | |
394 | ||
395 | // Only CodeWarrior 6 or higher | |
396 | #if defined(__MWERKS__) && (__MWERKS__ >= 0x2400) | |
397 | #define _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT | |
398 | #endif | |
399 | ||
400 | #endif // wxUSE_ARRAY_MEMORY_OPERATORS | |
401 | ||
e755eb67 | 402 | #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING |
b2edef6f | 403 | |
f8855e47 VZ |
404 | // ---------------------------------------------------------------------------- |
405 | // wxObjectRefData: ref counted data meant to be stored in wxObject | |
406 | // ---------------------------------------------------------------------------- | |
407 | ||
408 | class WXDLLIMPEXP_BASE wxObjectRefData | |
409 | { | |
410 | friend class WXDLLIMPEXP_BASE wxObject; | |
411 | ||
412 | public: | |
413 | wxObjectRefData() : m_count(1) { } | |
f8855e47 VZ |
414 | |
415 | int GetRefCount() const { return m_count; } | |
416 | ||
4a11340a RR |
417 | void IncRef() { m_count++; } |
418 | void DecRef(); | |
419 | ||
420 | protected: | |
421 | // this object should never be destroyed directly but only as a | |
422 | // result of a DecRef() call: | |
423 | virtual ~wxObjectRefData() { } | |
424 | ||
f8855e47 | 425 | private: |
4a11340a | 426 | // our refcount: |
f8855e47 VZ |
427 | int m_count; |
428 | }; | |
429 | ||
4a11340a RR |
430 | // ---------------------------------------------------------------------------- |
431 | // wxObjectDataPtr: helper class to avoid memleaks because of missing calls | |
432 | // to wxObjectRefData::DecRef | |
433 | // ---------------------------------------------------------------------------- | |
434 | ||
435 | template <class T> | |
436 | class wxObjectDataPtr | |
437 | { | |
438 | public: | |
439 | typedef T element_type; | |
440 | ||
441 | wxEXPLICIT wxObjectDataPtr(T *ptr = NULL) : m_ptr(ptr) {} | |
442 | ||
443 | // copy ctor | |
444 | wxObjectDataPtr(const wxObjectDataPtr<T> &tocopy) | |
445 | : m_ptr(tocopy.m_ptr) | |
446 | { | |
447 | if (m_ptr) | |
448 | m_ptr->IncRef(); | |
449 | } | |
450 | ||
451 | ~wxObjectDataPtr() | |
452 | { | |
453 | if (m_ptr) | |
454 | m_ptr->DecRef(); | |
455 | } | |
456 | ||
457 | T *get() const { return m_ptr; } | |
458 | T *operator->() const { return get(); } | |
459 | ||
460 | void reset(T *ptr) | |
461 | { | |
462 | if (m_ptr) | |
463 | m_ptr->DecRef(); | |
464 | m_ptr = ptr; | |
465 | } | |
466 | ||
467 | wxObjectDataPtr& operator=(const wxObjectDataPtr &tocopy) | |
468 | { | |
469 | if (m_ptr) | |
470 | m_ptr->DecRef(); | |
471 | m_ptr = tocopy.m_ptr; | |
472 | if (m_ptr) | |
473 | m_ptr->IncRef(); | |
474 | return *this; | |
475 | } | |
476 | ||
477 | wxObjectDataPtr& operator=(T *ptr) | |
478 | { | |
479 | if (m_ptr) | |
480 | m_ptr->DecRef(); | |
481 | m_ptr = ptr; | |
482 | if (m_ptr) | |
483 | m_ptr->IncRef(); | |
484 | return *this; | |
485 | } | |
486 | ||
487 | private: | |
488 | T *m_ptr; | |
489 | }; | |
490 | ||
0b9ab0bd | 491 | // ---------------------------------------------------------------------------- |
77ffb593 | 492 | // wxObject: the root class of wxWidgets object hierarchy |
0b9ab0bd RL |
493 | // ---------------------------------------------------------------------------- |
494 | ||
bddd7a8d | 495 | class WXDLLIMPEXP_BASE wxObject |
c801d85f | 496 | { |
684242c6 | 497 | DECLARE_ABSTRACT_CLASS(wxObject) |
c801d85f | 498 | |
0b9ab0bd | 499 | public: |
b2edef6f | 500 | wxObject() { m_refData = NULL; } |
0b9ab0bd | 501 | virtual ~wxObject() { UnRef(); } |
4393b50c | 502 | |
a6391d30 | 503 | wxObject(const wxObject& other) |
66e9a9f0 | 504 | { |
f8855e47 VZ |
505 | m_refData = other.m_refData; |
506 | if (m_refData) | |
507 | m_refData->m_count++; | |
66e9a9f0 | 508 | } |
4393b50c | 509 | |
a6391d30 GD |
510 | wxObject& operator=(const wxObject& other) |
511 | { | |
512 | if ( this != &other ) | |
513 | { | |
f8855e47 | 514 | Ref(other); |
a6391d30 GD |
515 | } |
516 | return *this; | |
517 | } | |
518 | ||
0b9ab0bd | 519 | bool IsKindOf(wxClassInfo *info) const; |
c801d85f | 520 | |
0b9ab0bd | 521 | |
b2edef6f VZ |
522 | // Turn on the correct set of new and delete operators |
523 | ||
524 | #ifdef _WX_WANT_NEW_SIZET_WXCHAR_INT | |
cf760e4c | 525 | void *operator new ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 ); |
0b9ab0bd | 526 | #endif |
27198be4 | 527 | |
b2edef6f VZ |
528 | #ifdef _WX_WANT_DELETE_VOID |
529 | void operator delete ( void * buf ); | |
530 | #endif | |
0b9ab0bd | 531 | |
b2edef6f VZ |
532 | #ifdef _WX_WANT_DELETE_VOID_CONSTCHAR_SIZET |
533 | void operator delete ( void *buf, const char *_fname, size_t _line ); | |
0b9ab0bd | 534 | #endif |
76626af2 | 535 | |
b2edef6f | 536 | #ifdef _WX_WANT_DELETE_VOID_WXCHAR_INT |
cf760e4c | 537 | void operator delete ( void *buf, const wxChar*, int ); |
b2edef6f | 538 | #endif |
8cfc5426 | 539 | |
b2edef6f | 540 | #ifdef _WX_WANT_ARRAY_NEW_SIZET_WXCHAR_INT |
cf760e4c | 541 | void *operator new[] ( size_t size, const wxChar *fileName = NULL, int lineNum = 0 ); |
0b9ab0bd RL |
542 | #endif |
543 | ||
b2edef6f VZ |
544 | #ifdef _WX_WANT_ARRAY_DELETE_VOID |
545 | void operator delete[] ( void *buf ); | |
0b9ab0bd | 546 | #endif |
27198be4 | 547 | |
b2edef6f | 548 | #ifdef _WX_WANT_ARRAY_DELETE_VOID_WXCHAR_INT |
cf760e4c | 549 | void operator delete[] (void* buf, const wxChar*, int ); |
b2edef6f | 550 | #endif |
c801d85f | 551 | |
807d8487 VZ |
552 | // ref counted data handling methods |
553 | ||
554 | // get/set | |
555 | wxObjectRefData *GetRefData() const { return m_refData; } | |
556 | void SetRefData(wxObjectRefData *data) { m_refData = data; } | |
557 | ||
558 | // make a 'clone' of the object | |
0b9ab0bd | 559 | void Ref(const wxObject& clone); |
c801d85f | 560 | |
807d8487 | 561 | // destroy a reference |
0b9ab0bd | 562 | void UnRef(); |
c801d85f | 563 | |
93c5f755 PC |
564 | // Make sure this object has only one reference |
565 | void UnShare() { AllocExclusive(); } | |
566 | ||
a3ab1c18 VZ |
567 | // check if this object references the same data as the other one |
568 | bool IsSameAs(const wxObject& o) const { return m_refData == o.m_refData; } | |
55ccdb93 | 569 | |
c801d85f | 570 | protected: |
807d8487 VZ |
571 | // ensure that our data is not shared with anybody else: if we have no |
572 | // data, it is created using CreateRefData() below, if we have shared data | |
573 | // it is copied using CloneRefData(), otherwise nothing is done | |
574 | void AllocExclusive(); | |
575 | ||
4e124582 VZ |
576 | // both methods must be implemented if AllocExclusive() is used, not pure |
577 | // virtual only because of the backwards compatibility reasons | |
807d8487 VZ |
578 | |
579 | // create a new m_refData | |
580 | virtual wxObjectRefData *CreateRefData() const; | |
581 | ||
582 | // create a new m_refData initialized with the given one | |
b8027888 | 583 | virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const; |
807d8487 | 584 | |
0b9ab0bd | 585 | wxObjectRefData *m_refData; |
c801d85f KB |
586 | }; |
587 | ||
ea1e6c4b RL |
588 | inline wxObject *wxCheckDynamicCast(wxObject *obj, wxClassInfo *classInfo) |
589 | { | |
b2edef6f | 590 | return obj && obj->GetClassInfo()->IsKindOf(classInfo) ? obj : NULL; |
ea1e6c4b RL |
591 | } |
592 | ||
2d51f067 SC |
593 | #if wxUSE_EXTENDED_RTTI |
594 | class WXDLLIMPEXP_BASE wxDynamicObject : public wxObject | |
595 | { | |
8f2b1cfd | 596 | friend class WXDLLIMPEXP_BASE wxDynamicClassInfo ; |
2d51f067 SC |
597 | public: |
598 | // instantiates this object with an instance of its superclass | |
599 | wxDynamicObject(wxObject* superClassInstance, const wxDynamicClassInfo *info) ; | |
d3c7fc99 | 600 | virtual ~wxDynamicObject(); |
2d51f067 | 601 | |
8f2b1cfd SC |
602 | void SetProperty (const wxChar *propertyName, const wxxVariant &value); |
603 | wxxVariant GetProperty (const wxChar *propertyName) const ; | |
2d51f067 SC |
604 | |
605 | // get the runtime identity of this object | |
606 | wxClassInfo *GetClassInfo() const | |
607 | { | |
f525dc35 JS |
608 | #ifdef _MSC_VER |
609 | return (wxClassInfo*) m_classInfo; | |
610 | #else | |
4a11340a RR |
611 | wxDynamicClassInfo *nonconst = wx_const_cast(wxDynamicClassInfo *, m_classInfo); |
612 | return wx_static_cast(wxClassInfo *, nonconst); | |
f525dc35 | 613 | #endif |
2d51f067 SC |
614 | } |
615 | ||
616 | wxObject* GetSuperClassInstance() const | |
617 | { | |
618 | return m_superClassInstance ; | |
619 | } | |
620 | private : | |
8f2b1cfd SC |
621 | // removes an existing runtime-property |
622 | void RemoveProperty( const wxChar *propertyName ) ; | |
623 | ||
624 | // renames an existing runtime-property | |
625 | void RenameProperty( const wxChar *oldPropertyName , const wxChar *newPropertyName ) ; | |
626 | ||
2d51f067 SC |
627 | wxObject *m_superClassInstance ; |
628 | const wxDynamicClassInfo *m_classInfo; | |
629 | struct wxDynamicObjectInternal; | |
630 | wxDynamicObjectInternal *m_data; | |
631 | }; | |
632 | #endif | |
633 | ||
b2edef6f VZ |
634 | // ---------------------------------------------------------------------------- |
635 | // more debugging macros | |
636 | // ---------------------------------------------------------------------------- | |
637 | ||
b2edef6f VZ |
638 | // Redefine new to be the debugging version. This doesn't work with all |
639 | // compilers, in which case you need to use WXDEBUG_NEW explicitly if you wish | |
640 | // to use the debugging version. | |
7fe7d506 | 641 | |
70bf6180 VZ |
642 | #ifdef __WXDEBUG__ |
643 | #define WXDEBUG_NEW new(__TFILE__,__LINE__) | |
644 | ||
645 | #if wxUSE_DEBUG_NEW_ALWAYS | |
646 | #if wxUSE_GLOBAL_MEMORY_OPERATORS | |
647 | #define new WXDEBUG_NEW | |
648 | #elif defined(__VISUALC__) | |
649 | // Including this file redefines new and allows leak reports to | |
650 | // contain line numbers | |
651 | #include "wx/msw/msvcrt.h" | |
652 | #endif | |
653 | #endif // wxUSE_DEBUG_NEW_ALWAYS | |
654 | #else // !__WXDEBUG__ | |
655 | #define WXDEBUG_NEW new | |
656 | #endif // __WXDEBUG__/!__WXDEBUG__ | |
c801d85f | 657 | |
c0089c96 | 658 | #endif // _WX_OBJECTH__ |