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