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