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