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