]>
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 | |
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 | ||
45 | DECLARE_DYNAMIC_CLASS(MyCar) | |
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) | |
cfa9866b FM |
86 | IMPLEMENT_DYNAMIC_CLASS(MyCar,wxObject) |
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 | |
b64d83f5 | 260 | type information, when using the ::DECLARE_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 | |
278 | bool tmp = obj->IsKindOf(CLASSINFO(wxFrame)); | |
279 | @endcode | |
280 | ||
7c913512 | 281 | @param info |
4cc4bfaf | 282 | A pointer to a class information object, which may be obtained |
b64d83f5 | 283 | by using the ::CLASSINFO 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, | |
415 | but indirectly through use of macros such as ::DECLARE_DYNAMIC_CLASS and | |
416 | ::IMPLEMENT_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 | */ | |
658 | #define CLASSINFO( className ) | |
659 | ||
660 | /** | |
661 | Used inside a class declaration to declare that the class should be made | |
662 | known to the class hierarchy, but objects of this class cannot be created | |
663 | dynamically. The same as DECLARE_ABSTRACT_CLASS(). | |
664 | ||
665 | @header{wx/object.h} | |
23324ae1 | 666 | */ |
8af7f7c1 | 667 | #define DECLARE_CLASS( className ) |
23324ae1 FM |
668 | |
669 | /** | |
670 | Used inside a class declaration to declare that the class should be | |
671 | made known to the class hierarchy, but objects of this class cannot be created | |
8af7f7c1 BP |
672 | dynamically. The same as DECLARE_CLASS(). |
673 | ||
674 | @header{wx/object.h} | |
675 | ||
23324ae1 | 676 | Example: |
4cc4bfaf | 677 | |
23324ae1 FM |
678 | @code |
679 | class wxCommand: public wxObject | |
680 | { | |
8af7f7c1 | 681 | DECLARE_ABSTRACT_CLASS(wxCommand) |
7c913512 | 682 | |
8af7f7c1 BP |
683 | private: |
684 | ... | |
685 | public: | |
686 | ... | |
23324ae1 FM |
687 | }; |
688 | @endcode | |
689 | */ | |
8af7f7c1 | 690 | #define DECLARE_ABSTRACT_CLASS( className ) |
23324ae1 FM |
691 | |
692 | /** | |
8af7f7c1 BP |
693 | Used inside a class declaration to make the class known to wxWidgets RTTI |
694 | system and also declare that the objects of this class should be | |
695 | dynamically creatable from run-time type information. Notice that this | |
696 | implies that the class should have a default constructor, if this is not | |
697 | the case consider using DECLARE_CLASS(). | |
23324ae1 | 698 | |
8af7f7c1 | 699 | @header{wx/object.h} |
7c913512 | 700 | |
8af7f7c1 BP |
701 | Example: |
702 | ||
703 | @code | |
704 | class wxFrame: public wxWindow | |
705 | { | |
706 | DECLARE_DYNAMIC_CLASS(wxFrame) | |
707 | ||
708 | private: | |
709 | const wxString& frameTitle; | |
710 | public: | |
711 | ... | |
712 | }; | |
713 | @endcode | |
23324ae1 | 714 | */ |
7baebf86 | 715 | #define DECLARE_DYNAMIC_CLASS( className ) |
23324ae1 FM |
716 | |
717 | /** | |
8af7f7c1 BP |
718 | Used in a C++ implementation file to complete the declaration of a class |
719 | that has run-time type information. The same as IMPLEMENT_ABSTRACT_CLASS(). | |
720 | ||
721 | @header{wx/object.h} | |
23324ae1 | 722 | */ |
8af7f7c1 | 723 | #define IMPLEMENT_CLASS( className, baseClassName ) |
23324ae1 FM |
724 | |
725 | /** | |
8af7f7c1 BP |
726 | Used in a C++ implementation file to complete the declaration of a class |
727 | that has run-time type information and two base classes. The same as | |
728 | IMPLEMENT_ABSTRACT_CLASS2(). | |
7c913512 | 729 | |
8af7f7c1 | 730 | @header{wx/object.h} |
23324ae1 | 731 | */ |
8af7f7c1 | 732 | #define IMPLEMENT_CLASS2( className, baseClassName1, baseClassName2 ) |
23324ae1 FM |
733 | |
734 | /** | |
8af7f7c1 BP |
735 | Used in a C++ implementation file to complete the declaration of a class |
736 | that has run-time type information. The same as IMPLEMENT_CLASS(). | |
737 | ||
738 | @header{wx/object.h} | |
739 | ||
23324ae1 | 740 | Example: |
4cc4bfaf | 741 | |
23324ae1 FM |
742 | @code |
743 | IMPLEMENT_ABSTRACT_CLASS(wxCommand, wxObject) | |
7c913512 | 744 | |
23324ae1 FM |
745 | wxCommand::wxCommand(void) |
746 | { | |
8af7f7c1 | 747 | ... |
23324ae1 FM |
748 | } |
749 | @endcode | |
750 | */ | |
8af7f7c1 | 751 | #define IMPLEMENT_ABSTRACT_CLASS( className, baseClassName ) |
23324ae1 FM |
752 | |
753 | /** | |
8af7f7c1 BP |
754 | Used in a C++ implementation file to complete the declaration of a class |
755 | that has run-time type information and two base classes. The same as | |
756 | IMPLEMENT_CLASS2(). | |
757 | ||
758 | @header{wx/object.h} | |
23324ae1 | 759 | */ |
8af7f7c1 | 760 | #define IMPLEMENT_ABSTRACT_CLASS2( className, baseClassName1, baseClassName2 ) |
23324ae1 FM |
761 | |
762 | /** | |
8af7f7c1 BP |
763 | Used in a C++ implementation file to complete the declaration of a class |
764 | that has run-time type information, and whose instances can be created | |
765 | dynamically. | |
7c913512 | 766 | |
8af7f7c1 BP |
767 | @header{wx/object.h} |
768 | ||
769 | Example: | |
770 | ||
771 | @code | |
772 | IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow) | |
773 | ||
774 | wxFrame::wxFrame(void) | |
775 | { | |
776 | ... | |
777 | } | |
778 | @endcode | |
23324ae1 | 779 | */ |
8af7f7c1 | 780 | #define IMPLEMENT_DYNAMIC_CLASS( className, baseClassName ) |
23324ae1 FM |
781 | |
782 | /** | |
8af7f7c1 BP |
783 | Used in a C++ implementation file to complete the declaration of a class |
784 | that has run-time type information, and whose instances can be created | |
785 | dynamically. Use this for classes derived from two base classes. | |
786 | ||
787 | @header{wx/object.h} | |
23324ae1 | 788 | */ |
8af7f7c1 | 789 | #define IMPLEMENT_DYNAMIC_CLASS2( className, baseClassName1, baseClassName2 ) |
23324ae1 FM |
790 | |
791 | /** | |
cfa9866b FM |
792 | Same as @c const_cast<T>(x) if the compiler supports const cast or @c (T)x for |
793 | old compilers. Unlike wxConstCast(), the cast it to the type @c T and not to | |
794 | <tt>T *</tt> and also the order of arguments is the same as for the standard cast. | |
8af7f7c1 BP |
795 | |
796 | @header{wx/defs.h} | |
797 | ||
798 | @see wx_reinterpret_cast(), wx_static_cast() | |
23324ae1 | 799 | */ |
8af7f7c1 | 800 | #define wx_const_cast(T, x) |
23324ae1 FM |
801 | |
802 | /** | |
cfa9866b FM |
803 | Same as @c reinterpret_cast<T>(x) if the compiler supports reinterpret cast or |
804 | @c (T)x for old compilers. | |
4cc4bfaf | 805 | |
8af7f7c1 | 806 | @header{wx/defs.h} |
7c913512 | 807 | |
8af7f7c1 | 808 | @see wx_const_cast(), wx_static_cast() |
23324ae1 | 809 | */ |
8af7f7c1 | 810 | #define wx_reinterpret_cast(T, x) |
23324ae1 FM |
811 | |
812 | /** | |
cfa9866b | 813 | Same as @c static_cast<T>(x) if the compiler supports static cast or @c (T)x for |
8af7f7c1 BP |
814 | old compilers. Unlike wxStaticCast(), there are no checks being done and |
815 | the meaning of the macro arguments is exactly the same as for the standard | |
cfa9866b | 816 | static cast, i.e. @a T is the full type name and star is not appended to it. |
7c913512 | 817 | |
8af7f7c1 BP |
818 | @header{wx/defs.h} |
819 | ||
820 | @see wx_const_cast(), wx_reinterpret_cast(), wx_truncate_cast() | |
23324ae1 | 821 | */ |
8af7f7c1 | 822 | #define wx_static_cast(T, x) |
23324ae1 FM |
823 | |
824 | /** | |
8af7f7c1 BP |
825 | This case doesn’t correspond to any standard cast but exists solely to make |
826 | casts which possibly result in a truncation of an integer value more | |
827 | readable. | |
828 | ||
829 | @header{wx/defs.h} | |
23324ae1 | 830 | */ |
8af7f7c1 | 831 | #define wx_truncate_cast(T, x) |
23324ae1 FM |
832 | |
833 | /** | |
cfa9866b | 834 | This macro expands into <tt>const_cast<classname *>(ptr)</tt> if the compiler |
8af7f7c1 | 835 | supports const_cast or into an old, C-style cast, otherwise. |
4cc4bfaf | 836 | |
8af7f7c1 | 837 | @header{wx/defs.h} |
7c913512 | 838 | |
8af7f7c1 | 839 | @see wx_const_cast(), wxDynamicCast(), wxStaticCast() |
23324ae1 | 840 | */ |
7baebf86 | 841 | #define wxConstCast( ptr, classname ) |
23324ae1 | 842 | |
23324ae1 | 843 | /** |
8af7f7c1 BP |
844 | This macro returns the pointer @e ptr cast to the type @e classname * if |
845 | the pointer is of this type (the check is done during the run-time) or | |
846 | @NULL otherwise. Usage of this macro is preferred over obsoleted | |
847 | wxObject::IsKindOf() function. | |
7c913512 | 848 | |
8af7f7c1 BP |
849 | The @e ptr argument may be @NULL, in which case @NULL will be returned. |
850 | ||
851 | @header{wx/object.h} | |
852 | ||
853 | Example: | |
854 | ||
855 | @code | |
856 | wxWindow *win = wxWindow::FindFocus(); | |
857 | wxTextCtrl *text = wxDynamicCast(win, wxTextCtrl); | |
858 | if ( text ) | |
859 | { | |
860 | // a text control has the focus... | |
861 | } | |
862 | else | |
863 | { | |
864 | // no window has the focus or it is not a text control | |
865 | } | |
866 | @endcode | |
867 | ||
b64d83f5 | 868 | @see @ref overview_rtti, wxDynamicCastThis(), wxConstCast(), wxStaticCast() |
23324ae1 | 869 | */ |
7baebf86 | 870 | #define wxDynamicCast( ptr, classname ) |
23324ae1 FM |
871 | |
872 | /** | |
cfa9866b | 873 | This macro is equivalent to <tt>wxDynamicCast(this, classname)</tt> but the latter provokes |
8af7f7c1 BP |
874 | spurious compilation warnings from some compilers (because it tests whether |
875 | @c this pointer is non-@NULL which is always true), so this macro should be | |
876 | used to avoid them. | |
7c913512 | 877 | |
8af7f7c1 BP |
878 | @header{wx/object.h} |
879 | ||
880 | @see wxDynamicCast() | |
23324ae1 | 881 | */ |
7baebf86 | 882 | #define wxDynamicCastThis( classname ) |
23324ae1 FM |
883 | |
884 | /** | |
8af7f7c1 BP |
885 | This macro checks that the cast is valid in debug mode (an assert failure |
886 | will result if wxDynamicCast(ptr, classname) == @NULL) and then returns the | |
cfa9866b | 887 | result of executing an equivalent of <tt>static_cast<classname *>(ptr)</tt>. |
4cc4bfaf | 888 | |
8af7f7c1 | 889 | @header{wx/object.h} |
7c913512 | 890 | |
8af7f7c1 | 891 | @see wx_static_cast(), wxDynamicCast(), wxConstCast() |
23324ae1 | 892 | */ |
7baebf86 | 893 | #define wxStaticCast( ptr, classname ) |
8af7f7c1 BP |
894 | |
895 | /** | |
896 | Creates and returns an object of the given class, if the class has been | |
897 | registered with the dynamic class system using DECLARE... and IMPLEMENT... | |
898 | macros. | |
899 | ||
900 | @header{wx/object.h} | |
901 | */ | |
7baebf86 | 902 | wxObject *wxCreateDynamicObject(const wxString& className); |
8af7f7c1 BP |
903 | |
904 | //@} | |
23324ae1 | 905 | |
b21126db | 906 | /** @addtogroup group_funcmacro_debug */ |
29f86fc1 BP |
907 | //@{ |
908 | ||
909 | /** | |
910 | This is defined in debug mode to be call the redefined new operator | |
911 | with filename and line number arguments. The definition is: | |
912 | ||
913 | @code | |
914 | #define WXDEBUG_NEW new(__FILE__,__LINE__) | |
915 | @endcode | |
916 | ||
917 | In non-debug mode, this is defined as the normal new operator. | |
918 | ||
919 | @header{wx/object.h} | |
920 | */ | |
921 | #define WXDEBUG_NEW( arg ) | |
922 | ||
923 | //@} | |
924 |