]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: object.h | |
0e497a9d | 3 | // Purpose: interface of wxRefCounter |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
0e497a9d | 9 | /** @class wxObjectRefData |
7c913512 | 10 | |
0e497a9d | 11 | This class is just a typedef to wxRefCounter and is used by wxObject. |
7c913512 | 12 | |
b64d83f5 FM |
13 | Derive classes from this to store your own data. When retrieving information |
14 | from a wxObject's reference data, you will need to cast to your own derived class. | |
cfa9866b | 15 | |
1729acd8 | 16 | @section objectrefdata_example Example |
cfa9866b FM |
17 | |
18 | @code | |
19 | // include file | |
1729acd8 | 20 | // ------------ |
cfa9866b | 21 | |
1729acd8 | 22 | class MyCar : public wxObject |
cfa9866b FM |
23 | { |
24 | public: | |
25 | MyCar() { } | |
26 | MyCar( int price ); | |
27 | ||
28 | bool IsOk() const { return m_refData != NULL; } | |
29 | ||
30 | bool operator == ( const MyCar& car ) const; | |
31 | bool operator != (const MyCar& car) const { return !(*this == car); } | |
32 | ||
33 | void SetPrice( int price ); | |
34 | int GetPrice() const; | |
35 | ||
36 | protected: | |
37 | virtual wxObjectRefData *CreateRefData() const; | |
38 | virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const; | |
39 | ||
40 | DECLARE_DYNAMIC_CLASS(MyCar) | |
41 | }; | |
42 | ||
43 | ||
44 | // implementation | |
1729acd8 | 45 | // -------------- |
cfa9866b | 46 | |
1729acd8 FM |
47 | // the reference data class is typically a private class only visible in the |
48 | // implementation source file of the refcounted class. | |
49 | class MyCarRefData : public wxObjectRefData | |
cfa9866b FM |
50 | { |
51 | public: | |
52 | MyCarRefData() | |
53 | { | |
54 | m_price = 0; | |
55 | } | |
56 | ||
57 | MyCarRefData( const MyCarRefData& data ) | |
58 | : wxObjectRefData() | |
59 | { | |
1729acd8 FM |
60 | // copy refcounted data; this is usually a time- and memory-consuming operation |
61 | // and is only done when two (or more) MyCar instances need to unshare a | |
62 | // common instance of MyCarRefData | |
cfa9866b FM |
63 | m_price = data.m_price; |
64 | } | |
65 | ||
66 | bool operator == (const MyCarRefData& data) const | |
67 | { | |
68 | return m_price == data.m_price; | |
69 | } | |
70 | ||
1729acd8 FM |
71 | private: |
72 | // in real world, reference counting is usually used only when | |
73 | // the wxObjectRefData-derived class holds data very memory-consuming; | |
74 | // in this example the various MyCar instances may share a MyCarRefData | |
75 | // instance which however only takes 4 bytes for this integer! | |
cfa9866b FM |
76 | int m_price; |
77 | }; | |
78 | ||
79 | ||
80 | #define M_CARDATA ((MyCarRefData *)m_refData) | |
cfa9866b FM |
81 | IMPLEMENT_DYNAMIC_CLASS(MyCar,wxObject) |
82 | ||
83 | MyCar::MyCar( int price ) | |
84 | { | |
1729acd8 | 85 | // here we init the MyCar internal data: |
cfa9866b FM |
86 | m_refData = new MyCarRefData(); |
87 | M_CARDATA->m_price = price; | |
88 | } | |
1729acd8 | 89 | |
cfa9866b FM |
90 | wxObjectRefData *MyCar::CreateRefData() const |
91 | { | |
92 | return new MyCarRefData; | |
93 | } | |
94 | ||
95 | wxObjectRefData *MyCar::CloneRefData(const wxObjectRefData *data) const | |
96 | { | |
97 | return new MyCarRefData(*(MyCarRefData *)data); | |
98 | } | |
99 | ||
100 | bool MyCar::operator == ( const MyCar& car ) const | |
101 | { | |
1729acd8 FM |
102 | if (m_refData == car.m_refData) |
103 | return true; | |
104 | if (!m_refData || !car.m_refData) | |
105 | return false; | |
106 | ||
107 | // here we use the MyCarRefData::operator==() function. | |
108 | // Note however that this comparison may be very slow if the | |
109 | // reference data contains a lot of data to be compared. | |
cfa9866b FM |
110 | return ( *(MyCarRefData*)m_refData == *(MyCarRefData*)car.m_refData ); |
111 | } | |
112 | ||
113 | void MyCar::SetPrice( int price ) | |
114 | { | |
1729acd8 FM |
115 | // since this function modifies one of the MyCar internal property, |
116 | // we need to be sure that the other MyCar instances which share the | |
117 | // same MyCarRefData instance are not affected by this call. | |
118 | // I.e. it's very important to call UnShare() in all setters of | |
119 | // refcounted classes! | |
cfa9866b FM |
120 | UnShare(); |
121 | ||
122 | M_CARDATA->m_price = price; | |
123 | } | |
124 | ||
125 | int MyCar::GetPrice() const | |
126 | { | |
127 | wxCHECK_MSG( IsOk(), -1, "invalid car" ); | |
128 | ||
1729acd8 | 129 | return M_CARDATA->m_price; |
cfa9866b FM |
130 | } |
131 | @endcode | |
7c913512 | 132 | |
0e497a9d | 133 | |
b64d83f5 FM |
134 | @library{wxbase} |
135 | @category{rtti} | |
136 | ||
137 | @see wxObject, wxObjectDataPtr<T>, @ref overview_refcount | |
23324ae1 | 138 | */ |
0e497a9d RR |
139 | typedef wxRefCounter wxObjectRefData; |
140 | ||
141 | ||
142 | /** | |
143 | @class wxRefCounter | |
144 | ||
145 | This class is used to manage reference-counting. | |
146 | ||
147 | @library{wxbase} | |
148 | @category{rtti} | |
149 | ||
150 | @see wxObject, wxObjectRefData, wxObjectDataPtr<T>, @ref overview_refcount | |
151 | */ | |
152 | class wxRefCounter | |
23324ae1 | 153 | { |
b64d83f5 | 154 | protected: |
23324ae1 | 155 | /** |
b64d83f5 FM |
156 | Destructor. |
157 | ||
0e497a9d | 158 | It's declared @c protected so that wxRefCounter instances |
b64d83f5 | 159 | will never be destroyed directly but only as result of a DecRef() call. |
23324ae1 | 160 | */ |
0e497a9d | 161 | virtual ~wxRefCounter(); |
23324ae1 | 162 | |
b64d83f5 | 163 | public: |
23324ae1 | 164 | /** |
b64d83f5 | 165 | Default constructor. Initialises the internal reference count to 1. |
23324ae1 | 166 | */ |
0e497a9d | 167 | wxRefCounter(); |
23324ae1 FM |
168 | |
169 | /** | |
cfa9866b | 170 | Decrements the reference count associated with this shared data and, if |
0e497a9d | 171 | it reaches zero, destroys this instance of wxRefCounter releasing its |
cfa9866b FM |
172 | memory. |
173 | ||
174 | Please note that after calling this function, the caller should | |
175 | absolutely avoid to use the pointer to this instance since it may not be | |
176 | valid anymore. | |
23324ae1 FM |
177 | */ |
178 | void DecRef(); | |
179 | ||
180 | /** | |
181 | Returns the reference count associated with this shared data. | |
b64d83f5 FM |
182 | |
183 | When this goes to zero during a DecRef() call, the object will auto-free itself. | |
23324ae1 | 184 | */ |
328f5751 | 185 | int GetRefCount() const; |
23324ae1 FM |
186 | |
187 | /** | |
188 | Increments the reference count associated with this shared data. | |
189 | */ | |
190 | void IncRef(); | |
191 | }; | |
192 | ||
193 | ||
e54c96f1 | 194 | |
23324ae1 FM |
195 | /** |
196 | @class wxObject | |
7c913512 | 197 | |
23324ae1 | 198 | This is the root class of many of the wxWidgets classes. |
b64d83f5 | 199 | |
cfa9866b FM |
200 | It declares a virtual destructor which ensures that destructors get called |
201 | for all derived class objects where necessary. | |
7c913512 | 202 | |
cfa9866b FM |
203 | wxObject is the hub of a dynamic object creation scheme, enabling a program |
204 | to create instances of a class only knowing its string class name, and to | |
205 | query the class hierarchy. | |
7c913512 | 206 | |
cfa9866b FM |
207 | The class contains optional debugging versions of @b new and @b delete, which |
208 | can help trace memory allocation and deallocation problems. | |
7c913512 | 209 | |
cfa9866b | 210 | wxObject can be used to implement @ref overview_refcount "reference counted" |
b64d83f5 FM |
211 | objects, such as wxPen, wxBitmap and others |
212 | (see @ref overview_refcount_list "this list"). | |
0e497a9d | 213 | See wxRefCounter and @ref overview_refcount for more info about |
1729acd8 | 214 | reference counting. |
7c913512 | 215 | |
23324ae1 FM |
216 | @library{wxbase} |
217 | @category{rtti} | |
7c913512 | 218 | |
1729acd8 | 219 | @see wxClassInfo, @ref overview_debugging, @ref overview_refcount, |
0e497a9d | 220 | wxObjectDataRef, wxObjectDataPtr<T> |
23324ae1 | 221 | */ |
7c913512 | 222 | class wxObject |
23324ae1 FM |
223 | { |
224 | public: | |
cfa9866b | 225 | |
1729acd8 FM |
226 | /** |
227 | Default ctor; initializes to @NULL the internal reference data. | |
228 | */ | |
cfa9866b | 229 | wxObject(); |
b64d83f5 | 230 | |
23324ae1 | 231 | /** |
b64d83f5 | 232 | Copy ctor. |
1729acd8 FM |
233 | |
234 | Sets the internal wxObject::m_refData pointer to point to the same | |
235 | instance of the wxObjectRefData-derived class pointed by @c other and | |
236 | increments the refcount of wxObject::m_refData. | |
23324ae1 | 237 | */ |
7c913512 | 238 | wxObject(const wxObject& other); |
cfa9866b | 239 | |
23324ae1 | 240 | /** |
b64d83f5 FM |
241 | Destructor. |
242 | ||
243 | Performs dereferencing, for those objects that use reference counting. | |
23324ae1 | 244 | */ |
adaaa686 | 245 | virtual ~wxObject(); |
23324ae1 | 246 | |
23324ae1 FM |
247 | /** |
248 | This virtual function is redefined for every class that requires run-time | |
b64d83f5 | 249 | type information, when using the ::DECLARE_CLASS macro (or similar). |
23324ae1 | 250 | */ |
adaaa686 | 251 | virtual wxClassInfo* GetClassInfo() const; |
23324ae1 FM |
252 | |
253 | /** | |
b64d83f5 | 254 | Returns the wxObject::m_refData pointer, i.e. the data referenced by this object. |
3c4f71cc | 255 | |
b64d83f5 | 256 | @see Ref(), UnRef(), wxObject::m_refData, SetRefData(), wxObjectRefData |
23324ae1 | 257 | */ |
328f5751 | 258 | wxObjectRefData* GetRefData() const; |
23324ae1 FM |
259 | |
260 | /** | |
261 | Determines whether this class is a subclass of (or the same class as) | |
262 | the given class. | |
3c4f71cc | 263 | |
b64d83f5 FM |
264 | Example: |
265 | ||
266 | @code | |
267 | bool tmp = obj->IsKindOf(CLASSINFO(wxFrame)); | |
268 | @endcode | |
269 | ||
7c913512 | 270 | @param info |
4cc4bfaf | 271 | A pointer to a class information object, which may be obtained |
b64d83f5 | 272 | by using the ::CLASSINFO macro. |
3c4f71cc | 273 | |
d29a9a8a | 274 | @return @true if the class represented by info is the same class as this |
4cc4bfaf | 275 | one or is derived from it. |
23324ae1 | 276 | */ |
b7e94bd7 | 277 | bool IsKindOf(const wxClassInfo* info) const; |
23324ae1 FM |
278 | |
279 | /** | |
b64d83f5 FM |
280 | Returns @true if this object has the same data pointer as @a obj. |
281 | ||
282 | Notice that @true is returned if the data pointers are @NULL in both objects. | |
cfa9866b | 283 | |
b64d83f5 | 284 | This function only does a @e shallow comparison, i.e. it doesn't compare |
23324ae1 | 285 | the objects pointed to by the data pointers of these objects. |
b64d83f5 FM |
286 | |
287 | @see @ref overview_refcount | |
23324ae1 | 288 | */ |
b7e94bd7 | 289 | bool IsSameAs(const wxObject& obj) const; |
23324ae1 FM |
290 | |
291 | /** | |
b64d83f5 | 292 | Makes this object refer to the data in @a clone. |
3c4f71cc | 293 | |
7c913512 | 294 | @param clone |
4cc4bfaf | 295 | The object to 'clone'. |
3c4f71cc | 296 | |
23324ae1 | 297 | @remarks First this function calls UnRef() on itself to decrement |
b64d83f5 FM |
298 | (and perhaps free) the data it is currently referring to. |
299 | It then sets its own wxObject::m_refData to point to that of @a clone, | |
cfa9866b FM |
300 | and increments the reference count inside the data. |
301 | ||
b64d83f5 | 302 | @see UnRef(), SetRefData(), GetRefData(), wxObjectRefData |
23324ae1 | 303 | */ |
4cc4bfaf | 304 | void Ref(const wxObject& clone); |
23324ae1 FM |
305 | |
306 | /** | |
b64d83f5 | 307 | Sets the wxObject::m_refData pointer. |
3c4f71cc | 308 | |
b64d83f5 | 309 | @see Ref(), UnRef(), GetRefData(), wxObjectRefData |
23324ae1 FM |
310 | */ |
311 | void SetRefData(wxObjectRefData* data); | |
312 | ||
313 | /** | |
314 | Decrements the reference count in the associated data, and if it is zero, | |
315 | deletes the data. | |
3c4f71cc | 316 | |
b64d83f5 FM |
317 | The wxObject::m_refData member is set to @NULL. |
318 | ||
319 | @see Ref(), SetRefData(), GetRefData(), wxObjectRefData | |
23324ae1 FM |
320 | */ |
321 | void UnRef(); | |
322 | ||
323 | /** | |
1729acd8 | 324 | This is the same of AllocExclusive() but this method is public. |
23324ae1 | 325 | */ |
cfa9866b | 326 | void UnShare(); |
23324ae1 FM |
327 | |
328 | /** | |
329 | The @e delete operator is defined for debugging versions of the library only, | |
cfa9866b | 330 | when the identifier @c __WXDEBUG__ is defined. |
b64d83f5 | 331 | |
cfa9866b | 332 | It takes over memory deallocation, allowing wxDebugContext operations. |
23324ae1 | 333 | */ |
cfa9866b | 334 | void operator delete(void *buf); |
23324ae1 FM |
335 | |
336 | /** | |
337 | The @e new operator is defined for debugging versions of the library only, when | |
b64d83f5 FM |
338 | the identifier @c __WXDEBUG__ is defined. |
339 | ||
340 | It takes over memory allocation, allowing wxDebugContext operations. | |
23324ae1 | 341 | */ |
b64d83f5 | 342 | void* operator new(size_t size, const wxString& filename = NULL, int lineNum = 0); |
cfa9866b | 343 | |
b64d83f5 | 344 | protected: |
1729acd8 FM |
345 | /** |
346 | Ensure that this object's data is not shared with any other object. | |
347 | ||
348 | If we have no data, it is created using CreateRefData(); | |
349 | if we have shared data (i.e. data with a reference count greater than 1), | |
350 | it is copied using CloneRefData(); otherwise nothing is done (the data | |
351 | is already present and is not shared by other object instances). | |
352 | ||
353 | If you use this function you should make sure that you override the | |
354 | CreateRefData() and CloneRefData() functions in your class otherwise | |
355 | an assertion will fail at runtime. | |
356 | */ | |
357 | void AllocExclusive(); | |
358 | ||
359 | /** | |
360 | Creates a new instance of the wxObjectRefData-derived class specific to | |
361 | this object and returns it. | |
362 | ||
363 | This is usually implemented as a one-line call: | |
364 | @code | |
365 | wxObjectRefData *MyObject::CreateRefData() const | |
366 | { | |
367 | return new MyObjectRefData; | |
368 | } | |
369 | @endcode | |
370 | */ | |
371 | virtual wxObjectRefData *CreateRefData() const; | |
372 | ||
373 | /** | |
374 | Creates a new instance of the wxObjectRefData-derived class specific to | |
375 | this object and initializes it copying @a data. | |
376 | ||
377 | This is usually implemented as a one-line call: | |
378 | @code | |
379 | wxObjectRefData *MyObject::CloneRefData(const wxObjectRefData *data) const | |
380 | { | |
381 | // rely on the MyObjectRefData copy ctor: | |
382 | return new MyObjectRefData(*(MyObjectRefData *)data); | |
383 | } | |
384 | @endcode | |
385 | */ | |
386 | virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const; | |
cfa9866b FM |
387 | |
388 | /** | |
389 | Pointer to an object which is the object's reference-counted data. | |
390 | ||
b64d83f5 | 391 | @see Ref(), UnRef(), SetRefData(), GetRefData(), wxObjectRefData |
cfa9866b | 392 | */ |
1729acd8 | 393 | wxObjectRefData* m_refData; |
23324ae1 FM |
394 | }; |
395 | ||
396 | ||
e54c96f1 | 397 | |
23324ae1 FM |
398 | /** |
399 | @class wxClassInfo | |
7c913512 | 400 | |
b64d83f5 FM |
401 | This class stores meta-information about classes. |
402 | ||
403 | Instances of this class are not generally defined directly by an application, | |
404 | but indirectly through use of macros such as ::DECLARE_DYNAMIC_CLASS and | |
405 | ::IMPLEMENT_DYNAMIC_CLASS. | |
7c913512 | 406 | |
23324ae1 FM |
407 | @library{wxbase} |
408 | @category{rtti} | |
7c913512 | 409 | |
b64d83f5 | 410 | @see @ref overview_rtti_classinfo, wxObject |
23324ae1 | 411 | */ |
7c913512 | 412 | class wxClassInfo |
23324ae1 FM |
413 | { |
414 | public: | |
415 | /** | |
b64d83f5 FM |
416 | Constructs a wxClassInfo object. |
417 | ||
418 | The supplied macros implicitly construct objects of this class, so there is no | |
419 | need to create such objects explicitly in an application. | |
23324ae1 | 420 | */ |
4cc4bfaf FM |
421 | wxClassInfo(const wxChar* className, |
422 | const wxClassInfo* baseClass1, | |
423 | const wxClassInfo* baseClass2, | |
23324ae1 FM |
424 | int size, wxObjectConstructorFn fn); |
425 | ||
426 | /** | |
b64d83f5 FM |
427 | Creates an object of the appropriate kind. |
428 | ||
d29a9a8a | 429 | @return @NULL if the class has not been declared dynamically creatable |
b64d83f5 | 430 | (typically, this happens for abstract classes). |
23324ae1 | 431 | */ |
328f5751 | 432 | wxObject* CreateObject() const; |
23324ae1 FM |
433 | |
434 | /** | |
b64d83f5 | 435 | Finds the wxClassInfo object for a class with the given @a name. |
23324ae1 | 436 | */ |
382f12e4 | 437 | static wxClassInfo* FindClass(const wxString& className); |
23324ae1 FM |
438 | |
439 | /** | |
440 | Returns the name of the first base class (@NULL if none). | |
441 | */ | |
7323ff1a | 442 | const wxChar* GetBaseClassName1() const; |
23324ae1 FM |
443 | |
444 | /** | |
445 | Returns the name of the second base class (@NULL if none). | |
446 | */ | |
7323ff1a | 447 | const wxChar* GetBaseClassName2() const; |
23324ae1 FM |
448 | |
449 | /** | |
450 | Returns the string form of the class name. | |
451 | */ | |
7323ff1a | 452 | const wxChar* GetClassName() const; |
23324ae1 FM |
453 | |
454 | /** | |
455 | Returns the size of the class. | |
456 | */ | |
328f5751 | 457 | int GetSize() const; |
23324ae1 | 458 | |
23324ae1 FM |
459 | /** |
460 | Returns @true if this class info can create objects of the associated class. | |
461 | */ | |
328f5751 | 462 | bool IsDynamic() const; |
23324ae1 FM |
463 | |
464 | /** | |
465 | Returns @true if this class is a kind of (inherits from) the given class. | |
466 | */ | |
50ec54b6 | 467 | bool IsKindOf(const wxClassInfo* info) const; |
23324ae1 FM |
468 | }; |
469 | ||
470 | ||
e54c96f1 | 471 | |
23324ae1 | 472 | /** |
7c913512 | 473 | |
1729acd8 | 474 | This is an helper template class primarily written to avoid memory leaks because of |
b64d83f5 | 475 | missing calls to wxObjectRefData::DecRef(). |
7c913512 | 476 | |
b64d83f5 FM |
477 | Despite the name this template can actually be used as a smart pointer for any |
478 | class implementing the reference counting interface which only consists of the two | |
479 | methods @b T::IncRef() and @b T::DecRef(). | |
7c913512 | 480 | |
b64d83f5 | 481 | The difference to wxSharedPtr<T> is that wxObjectDataPtr<T> relies on the reference |
1729acd8 | 482 | counting to be in the class pointed to, where instead wxSharedPtr<T> implements the |
23324ae1 | 483 | reference counting itself. |
7c913512 | 484 | |
1729acd8 | 485 | @section objectdataptr_example Example |
cfa9866b FM |
486 | |
487 | @code | |
488 | class MyCarRefData: public wxObjectRefData | |
489 | { | |
490 | public: | |
491 | MyCarRefData() { m_price = 0; } | |
492 | ||
493 | MyCarRefData( const MyCarRefData& data ) | |
494 | : wxObjectRefData() | |
495 | { | |
496 | m_price = data.m_price; | |
497 | } | |
498 | ||
499 | void SetPrice( int price ) { m_price = price; } | |
1729acd8 | 500 | int GetPrice() const { return m_price; } |
cfa9866b | 501 | |
1729acd8 FM |
502 | bool operator == ( const MyCarRefData& other ) const |
503 | { | |
504 | return m_price == other.m_price; | |
505 | } | |
506 | ||
cfa9866b FM |
507 | protected: |
508 | int m_price; | |
509 | }; | |
510 | ||
511 | class MyCar | |
512 | { | |
513 | public: | |
1729acd8 FM |
514 | // initializes this MyCar assigning to the |
515 | // internal data pointer a new instance of MyCarRefData | |
cfa9866b FM |
516 | MyCar( int price ) : m_data( new MyCarRefData ) |
517 | { | |
518 | m_data->SetPrice( price ); | |
519 | } | |
520 | ||
521 | MyCar& operator =( const MyCar& tocopy ) | |
522 | { | |
1729acd8 FM |
523 | // shallow copy: this is just a fast copy of pointers; the real |
524 | // memory-consuming data which typically is stored inside | |
525 | // MyCarRefData is not copied here! | |
cfa9866b FM |
526 | m_data = tocopy.m_data; |
527 | return *this; | |
528 | } | |
529 | ||
530 | bool operator == ( const MyCar& other ) const | |
531 | { | |
1729acd8 FM |
532 | if (m_data.get() == other.m_data.get()) |
533 | return true; // this instance and the 'other' one share the | |
534 | // same MyCarRefData data... | |
535 | ||
536 | // rely on the MyCarRefData::operator==() | |
537 | return (*m_data.get()) == (*other.m_data.get()); | |
cfa9866b FM |
538 | } |
539 | ||
540 | void SetPrice( int price ) | |
541 | { | |
1729acd8 FM |
542 | // make sure changes to this class do not affect other instances |
543 | // currently sharing our same refcounted data: | |
544 | UnShare(); | |
545 | ||
546 | m_data->SetPrice( price ); | |
cfa9866b FM |
547 | } |
548 | ||
549 | int GetPrice() const | |
550 | { | |
551 | return m_data->GetPrice(); | |
552 | } | |
553 | ||
554 | wxObjectDataPtr<MyCarRefData> m_data; | |
555 | ||
556 | protected: | |
557 | void UnShare() | |
558 | { | |
559 | if (m_data->GetRefCount() == 1) | |
560 | return; | |
561 | ||
562 | m_data.reset( new MyCarRefData( *m_data ) ); | |
563 | } | |
564 | }; | |
cfa9866b | 565 | @endcode |
7c913512 | 566 | |
b64d83f5 FM |
567 | |
568 | @library{wxbase} | |
569 | @category{rtti,smartpointers} | |
570 | ||
571 | @see wxObject, wxObjectRefData, @ref overview_refcount, wxSharedPtr<T>, | |
572 | wxScopedPtr<T>, wxWeakRef<T> | |
23324ae1 | 573 | */ |
1729acd8 | 574 | template <class T> |
7c913512 | 575 | class wxObjectDataPtr<T> |
23324ae1 FM |
576 | { |
577 | public: | |
cfa9866b | 578 | /** |
b64d83f5 FM |
579 | Constructor. |
580 | ||
581 | @a ptr is a pointer to the reference counted object to which this class points. | |
cfa9866b FM |
582 | If @a ptr is not NULL @b T::IncRef() will be called on the object. |
583 | */ | |
584 | wxObjectDataPtr<T>(T* ptr = NULL); | |
585 | ||
23324ae1 | 586 | /** |
b64d83f5 FM |
587 | This copy constructor increases the count of the reference counted object to |
588 | which @a tocopy points and then this class will point to, as well. | |
23324ae1 | 589 | */ |
cfa9866b FM |
590 | wxObjectDataPtr<T>(const wxObjectDataPtr<T>& tocopy); |
591 | ||
23324ae1 FM |
592 | |
593 | /** | |
b64d83f5 | 594 | Decreases the reference count of the object to which this class points. |
23324ae1 | 595 | */ |
b64d83f5 | 596 | ~wxObjectDataPtr<T>(); |
23324ae1 FM |
597 | |
598 | /** | |
b64d83f5 | 599 | Gets a pointer to the reference counted object to which this class points. |
23324ae1 | 600 | */ |
328f5751 | 601 | T* get() const; |
23324ae1 | 602 | |
b64d83f5 FM |
603 | /** |
604 | Reset this class to ptr which points to a reference counted object and | |
605 | calls T::DecRef() on the previously owned object. | |
606 | */ | |
d3338a5a | 607 | void reset(T *ptr); |
b64d83f5 | 608 | |
23324ae1 | 609 | /** |
7c913512 | 610 | Conversion to a boolean expression (in a variant which is not |
b64d83f5 FM |
611 | convertable to anything but a boolean expression). |
612 | ||
613 | If this class contains a valid pointer it will return @true, if it contains | |
614 | a @NULL pointer it will return @false. | |
23324ae1 | 615 | */ |
328f5751 | 616 | operator unspecified_bool_type() const; |
23324ae1 FM |
617 | |
618 | /** | |
b64d83f5 FM |
619 | Returns a reference to the object. |
620 | ||
621 | If the internal pointer is @NULL this method will cause an assert in debug mode. | |
23324ae1 | 622 | */ |
cfa9866b | 623 | T& operator*() const; |
23324ae1 FM |
624 | |
625 | /** | |
b64d83f5 FM |
626 | Returns a pointer to the reference counted object to which this class points. |
627 | ||
628 | If this the internal pointer is @NULL, this method will assert in debug mode. | |
23324ae1 | 629 | */ |
b64d83f5 | 630 | T* operator->() const; |
23324ae1 FM |
631 | |
632 | //@{ | |
633 | /** | |
b64d83f5 | 634 | Assignment operator. |
23324ae1 | 635 | */ |
cfa9866b FM |
636 | wxObjectDataPtr<T>& operator=(const wxObjectDataPtr<T>& tocopy); |
637 | wxObjectDataPtr<T>& operator=(T* ptr); | |
23324ae1 FM |
638 | //@} |
639 | }; | |
640 | ||
641 | ||
e54c96f1 | 642 | |
23324ae1 FM |
643 | // ============================================================================ |
644 | // Global functions/macros | |
645 | // ============================================================================ | |
646 | ||
b21126db | 647 | /** @addtogroup group_funcmacro_rtti */ |
8af7f7c1 BP |
648 | //@{ |
649 | ||
23324ae1 | 650 | /** |
8af7f7c1 BP |
651 | Returns a pointer to the wxClassInfo object associated with this class. |
652 | ||
653 | @header{wx/object.h} | |
654 | */ | |
655 | #define CLASSINFO( className ) | |
656 | ||
657 | /** | |
658 | Used inside a class declaration to declare that the class should be made | |
659 | known to the class hierarchy, but objects of this class cannot be created | |
660 | dynamically. The same as DECLARE_ABSTRACT_CLASS(). | |
661 | ||
662 | @header{wx/object.h} | |
23324ae1 | 663 | */ |
8af7f7c1 | 664 | #define DECLARE_CLASS( className ) |
23324ae1 FM |
665 | |
666 | /** | |
667 | Used inside a class declaration to declare that the class should be | |
668 | made known to the class hierarchy, but objects of this class cannot be created | |
8af7f7c1 BP |
669 | dynamically. The same as DECLARE_CLASS(). |
670 | ||
671 | @header{wx/object.h} | |
672 | ||
23324ae1 | 673 | Example: |
4cc4bfaf | 674 | |
23324ae1 FM |
675 | @code |
676 | class wxCommand: public wxObject | |
677 | { | |
8af7f7c1 | 678 | DECLARE_ABSTRACT_CLASS(wxCommand) |
7c913512 | 679 | |
8af7f7c1 BP |
680 | private: |
681 | ... | |
682 | public: | |
683 | ... | |
23324ae1 FM |
684 | }; |
685 | @endcode | |
686 | */ | |
8af7f7c1 | 687 | #define DECLARE_ABSTRACT_CLASS( className ) |
23324ae1 FM |
688 | |
689 | /** | |
8af7f7c1 BP |
690 | Used inside a class declaration to make the class known to wxWidgets RTTI |
691 | system and also declare that the objects of this class should be | |
692 | dynamically creatable from run-time type information. Notice that this | |
693 | implies that the class should have a default constructor, if this is not | |
694 | the case consider using DECLARE_CLASS(). | |
23324ae1 | 695 | |
8af7f7c1 | 696 | @header{wx/object.h} |
7c913512 | 697 | |
8af7f7c1 BP |
698 | Example: |
699 | ||
700 | @code | |
701 | class wxFrame: public wxWindow | |
702 | { | |
703 | DECLARE_DYNAMIC_CLASS(wxFrame) | |
704 | ||
705 | private: | |
706 | const wxString& frameTitle; | |
707 | public: | |
708 | ... | |
709 | }; | |
710 | @endcode | |
23324ae1 | 711 | */ |
7baebf86 | 712 | #define DECLARE_DYNAMIC_CLASS( className ) |
23324ae1 FM |
713 | |
714 | /** | |
8af7f7c1 BP |
715 | Used in a C++ implementation file to complete the declaration of a class |
716 | that has run-time type information. The same as IMPLEMENT_ABSTRACT_CLASS(). | |
717 | ||
718 | @header{wx/object.h} | |
23324ae1 | 719 | */ |
8af7f7c1 | 720 | #define IMPLEMENT_CLASS( className, baseClassName ) |
23324ae1 FM |
721 | |
722 | /** | |
8af7f7c1 BP |
723 | Used in a C++ implementation file to complete the declaration of a class |
724 | that has run-time type information and two base classes. The same as | |
725 | IMPLEMENT_ABSTRACT_CLASS2(). | |
7c913512 | 726 | |
8af7f7c1 | 727 | @header{wx/object.h} |
23324ae1 | 728 | */ |
8af7f7c1 | 729 | #define IMPLEMENT_CLASS2( className, baseClassName1, baseClassName2 ) |
23324ae1 FM |
730 | |
731 | /** | |
8af7f7c1 BP |
732 | Used in a C++ implementation file to complete the declaration of a class |
733 | that has run-time type information. The same as IMPLEMENT_CLASS(). | |
734 | ||
735 | @header{wx/object.h} | |
736 | ||
23324ae1 | 737 | Example: |
4cc4bfaf | 738 | |
23324ae1 FM |
739 | @code |
740 | IMPLEMENT_ABSTRACT_CLASS(wxCommand, wxObject) | |
7c913512 | 741 | |
23324ae1 FM |
742 | wxCommand::wxCommand(void) |
743 | { | |
8af7f7c1 | 744 | ... |
23324ae1 FM |
745 | } |
746 | @endcode | |
747 | */ | |
8af7f7c1 | 748 | #define IMPLEMENT_ABSTRACT_CLASS( className, baseClassName ) |
23324ae1 FM |
749 | |
750 | /** | |
8af7f7c1 BP |
751 | Used in a C++ implementation file to complete the declaration of a class |
752 | that has run-time type information and two base classes. The same as | |
753 | IMPLEMENT_CLASS2(). | |
754 | ||
755 | @header{wx/object.h} | |
23324ae1 | 756 | */ |
8af7f7c1 | 757 | #define IMPLEMENT_ABSTRACT_CLASS2( className, baseClassName1, baseClassName2 ) |
23324ae1 FM |
758 | |
759 | /** | |
8af7f7c1 BP |
760 | Used in a C++ implementation file to complete the declaration of a class |
761 | that has run-time type information, and whose instances can be created | |
762 | dynamically. | |
7c913512 | 763 | |
8af7f7c1 BP |
764 | @header{wx/object.h} |
765 | ||
766 | Example: | |
767 | ||
768 | @code | |
769 | IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow) | |
770 | ||
771 | wxFrame::wxFrame(void) | |
772 | { | |
773 | ... | |
774 | } | |
775 | @endcode | |
23324ae1 | 776 | */ |
8af7f7c1 | 777 | #define IMPLEMENT_DYNAMIC_CLASS( className, baseClassName ) |
23324ae1 FM |
778 | |
779 | /** | |
8af7f7c1 BP |
780 | Used in a C++ implementation file to complete the declaration of a class |
781 | that has run-time type information, and whose instances can be created | |
782 | dynamically. Use this for classes derived from two base classes. | |
783 | ||
784 | @header{wx/object.h} | |
23324ae1 | 785 | */ |
8af7f7c1 | 786 | #define IMPLEMENT_DYNAMIC_CLASS2( className, baseClassName1, baseClassName2 ) |
23324ae1 FM |
787 | |
788 | /** | |
cfa9866b FM |
789 | Same as @c const_cast<T>(x) if the compiler supports const cast or @c (T)x for |
790 | old compilers. Unlike wxConstCast(), the cast it to the type @c T and not to | |
791 | <tt>T *</tt> and also the order of arguments is the same as for the standard cast. | |
8af7f7c1 BP |
792 | |
793 | @header{wx/defs.h} | |
794 | ||
795 | @see wx_reinterpret_cast(), wx_static_cast() | |
23324ae1 | 796 | */ |
8af7f7c1 | 797 | #define wx_const_cast(T, x) |
23324ae1 FM |
798 | |
799 | /** | |
cfa9866b FM |
800 | Same as @c reinterpret_cast<T>(x) if the compiler supports reinterpret cast or |
801 | @c (T)x for old compilers. | |
4cc4bfaf | 802 | |
8af7f7c1 | 803 | @header{wx/defs.h} |
7c913512 | 804 | |
8af7f7c1 | 805 | @see wx_const_cast(), wx_static_cast() |
23324ae1 | 806 | */ |
8af7f7c1 | 807 | #define wx_reinterpret_cast(T, x) |
23324ae1 FM |
808 | |
809 | /** | |
cfa9866b | 810 | Same as @c static_cast<T>(x) if the compiler supports static cast or @c (T)x for |
8af7f7c1 BP |
811 | old compilers. Unlike wxStaticCast(), there are no checks being done and |
812 | the meaning of the macro arguments is exactly the same as for the standard | |
cfa9866b | 813 | static cast, i.e. @a T is the full type name and star is not appended to it. |
7c913512 | 814 | |
8af7f7c1 BP |
815 | @header{wx/defs.h} |
816 | ||
817 | @see wx_const_cast(), wx_reinterpret_cast(), wx_truncate_cast() | |
23324ae1 | 818 | */ |
8af7f7c1 | 819 | #define wx_static_cast(T, x) |
23324ae1 FM |
820 | |
821 | /** | |
8af7f7c1 BP |
822 | This case doesn’t correspond to any standard cast but exists solely to make |
823 | casts which possibly result in a truncation of an integer value more | |
824 | readable. | |
825 | ||
826 | @header{wx/defs.h} | |
23324ae1 | 827 | */ |
8af7f7c1 | 828 | #define wx_truncate_cast(T, x) |
23324ae1 FM |
829 | |
830 | /** | |
cfa9866b | 831 | This macro expands into <tt>const_cast<classname *>(ptr)</tt> if the compiler |
8af7f7c1 | 832 | supports const_cast or into an old, C-style cast, otherwise. |
4cc4bfaf | 833 | |
8af7f7c1 | 834 | @header{wx/defs.h} |
7c913512 | 835 | |
8af7f7c1 | 836 | @see wx_const_cast(), wxDynamicCast(), wxStaticCast() |
23324ae1 | 837 | */ |
7baebf86 | 838 | #define wxConstCast( ptr, classname ) |
23324ae1 | 839 | |
23324ae1 | 840 | /** |
8af7f7c1 BP |
841 | This macro returns the pointer @e ptr cast to the type @e classname * if |
842 | the pointer is of this type (the check is done during the run-time) or | |
843 | @NULL otherwise. Usage of this macro is preferred over obsoleted | |
844 | wxObject::IsKindOf() function. | |
7c913512 | 845 | |
8af7f7c1 BP |
846 | The @e ptr argument may be @NULL, in which case @NULL will be returned. |
847 | ||
848 | @header{wx/object.h} | |
849 | ||
850 | Example: | |
851 | ||
852 | @code | |
853 | wxWindow *win = wxWindow::FindFocus(); | |
854 | wxTextCtrl *text = wxDynamicCast(win, wxTextCtrl); | |
855 | if ( text ) | |
856 | { | |
857 | // a text control has the focus... | |
858 | } | |
859 | else | |
860 | { | |
861 | // no window has the focus or it is not a text control | |
862 | } | |
863 | @endcode | |
864 | ||
b64d83f5 | 865 | @see @ref overview_rtti, wxDynamicCastThis(), wxConstCast(), wxStaticCast() |
23324ae1 | 866 | */ |
7baebf86 | 867 | #define wxDynamicCast( ptr, classname ) |
23324ae1 FM |
868 | |
869 | /** | |
cfa9866b | 870 | This macro is equivalent to <tt>wxDynamicCast(this, classname)</tt> but the latter provokes |
8af7f7c1 BP |
871 | spurious compilation warnings from some compilers (because it tests whether |
872 | @c this pointer is non-@NULL which is always true), so this macro should be | |
873 | used to avoid them. | |
7c913512 | 874 | |
8af7f7c1 BP |
875 | @header{wx/object.h} |
876 | ||
877 | @see wxDynamicCast() | |
23324ae1 | 878 | */ |
7baebf86 | 879 | #define wxDynamicCastThis( classname ) |
23324ae1 FM |
880 | |
881 | /** | |
8af7f7c1 BP |
882 | This macro checks that the cast is valid in debug mode (an assert failure |
883 | will result if wxDynamicCast(ptr, classname) == @NULL) and then returns the | |
cfa9866b | 884 | result of executing an equivalent of <tt>static_cast<classname *>(ptr)</tt>. |
4cc4bfaf | 885 | |
8af7f7c1 | 886 | @header{wx/object.h} |
7c913512 | 887 | |
8af7f7c1 | 888 | @see wx_static_cast(), wxDynamicCast(), wxConstCast() |
23324ae1 | 889 | */ |
7baebf86 | 890 | #define wxStaticCast( ptr, classname ) |
8af7f7c1 BP |
891 | |
892 | /** | |
893 | Creates and returns an object of the given class, if the class has been | |
894 | registered with the dynamic class system using DECLARE... and IMPLEMENT... | |
895 | macros. | |
896 | ||
897 | @header{wx/object.h} | |
898 | */ | |
7baebf86 | 899 | wxObject *wxCreateDynamicObject(const wxString& className); |
8af7f7c1 BP |
900 | |
901 | //@} | |
23324ae1 | 902 | |
b21126db | 903 | /** @addtogroup group_funcmacro_debug */ |
29f86fc1 BP |
904 | //@{ |
905 | ||
906 | /** | |
907 | This is defined in debug mode to be call the redefined new operator | |
908 | with filename and line number arguments. The definition is: | |
909 | ||
910 | @code | |
911 | #define WXDEBUG_NEW new(__FILE__,__LINE__) | |
912 | @endcode | |
913 | ||
914 | In non-debug mode, this is defined as the normal new operator. | |
915 | ||
916 | @header{wx/object.h} | |
917 | */ | |
918 | #define WXDEBUG_NEW( arg ) | |
919 | ||
920 | //@} | |
921 |