1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxRefCounter
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
9 /** @class wxObjectRefData
11 This class is just a typedef to wxRefCounter and is used by wxObject.
13 Derive classes from this to store your own data. When retrieving information
14 from a wxObject's reference data, you will need to cast to your own derived class.
16 @section objectrefdata_example Example
22 class MyCar : public wxObject
28 bool IsOk() const { return m_refData != NULL; }
30 bool operator == ( const MyCar& car ) const;
31 bool operator != (const MyCar& car) const { return !(*this == car); }
33 void SetPrice( int price );
37 virtual wxObjectRefData *CreateRefData() const;
38 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
40 DECLARE_DYNAMIC_CLASS(MyCar)
47 // the reference data class is typically a private class only visible in the
48 // implementation source file of the refcounted class.
49 class MyCarRefData : public wxObjectRefData
57 MyCarRefData( const MyCarRefData& data )
60 // copy refcounted data; this is usually a time- and memory-consuming operation
61 // and is only done when two (or more) MyCar instances need to unshare a
62 // common instance of MyCarRefData
63 m_price = data.m_price;
66 bool operator == (const MyCarRefData& data) const
68 return m_price == data.m_price;
72 // in real world, reference counting is usually used only when
73 // the wxObjectRefData-derived class holds data very memory-consuming;
74 // in this example the various MyCar instances may share a MyCarRefData
75 // instance which however only takes 4 bytes for this integer!
80 #define M_CARDATA ((MyCarRefData *)m_refData)
81 IMPLEMENT_DYNAMIC_CLASS(MyCar,wxObject)
83 MyCar::MyCar( int price )
85 // here we init the MyCar internal data:
86 m_refData = new MyCarRefData();
87 M_CARDATA->m_price = price;
90 wxObjectRefData *MyCar::CreateRefData() const
92 return new MyCarRefData;
95 wxObjectRefData *MyCar::CloneRefData(const wxObjectRefData *data) const
97 return new MyCarRefData(*(MyCarRefData *)data);
100 bool MyCar::operator == ( const MyCar& car ) const
102 if (m_refData == car.m_refData)
104 if (!m_refData || !car.m_refData)
107 // here we use the MyCarRefData::operator==() function.
108 // Note however that this comparison may be very slow if the
109 // reference data contains a lot of data to be compared.
110 return ( *(MyCarRefData*)m_refData == *(MyCarRefData*)car.m_refData );
113 void MyCar::SetPrice( int price )
115 // since this function modifies one of the MyCar internal property,
116 // we need to be sure that the other MyCar instances which share the
117 // same MyCarRefData instance are not affected by this call.
118 // I.e. it's very important to call UnShare() in all setters of
119 // refcounted classes!
122 M_CARDATA->m_price = price;
125 int MyCar::GetPrice() const
127 wxCHECK_MSG( IsOk(), -1, "invalid car" );
129 return M_CARDATA->m_price;
137 @see wxObject, wxObjectDataPtr<T>, @ref overview_refcount
139 typedef wxRefCounter wxObjectRefData
;
145 This class is used to manage reference-counting.
150 @see wxObject, wxObjectRefData, wxObjectDataPtr<T>, @ref overview_refcount
158 It's declared @c protected so that wxRefCounter instances
159 will never be destroyed directly but only as result of a DecRef() call.
161 virtual ~wxRefCounter();
165 Default constructor. Initialises the internal reference count to 1.
170 Decrements the reference count associated with this shared data and, if
171 it reaches zero, destroys this instance of wxRefCounter releasing its
174 Please note that after calling this function, the caller should
175 absolutely avoid to use the pointer to this instance since it may not be
181 Returns the reference count associated with this shared data.
183 When this goes to zero during a DecRef() call, the object will auto-free itself.
185 int GetRefCount() const;
188 Increments the reference count associated with this shared data.
198 This is the root class of many of the wxWidgets classes.
200 It declares a virtual destructor which ensures that destructors get called
201 for all derived class objects where necessary.
203 wxObject is the hub of a dynamic object creation scheme, enabling a program
204 to create instances of a class only knowing its string class name, and to
205 query the class hierarchy.
207 The class contains optional debugging versions of @b new and @b delete, which
208 can help trace memory allocation and deallocation problems.
210 wxObject can be used to implement @ref overview_refcount "reference counted"
211 objects, such as wxPen, wxBitmap and others
212 (see @ref overview_refcount_list "this list").
213 See wxRefCounter and @ref overview_refcount for more info about
219 @see wxClassInfo, @ref overview_debugging, @ref overview_refcount,
220 wxObjectDataRef, wxObjectDataPtr<T>
227 Default ctor; initializes to @NULL the internal reference data.
234 Sets the internal wxObject::m_refData pointer to point to the same
235 instance of the wxObjectRefData-derived class pointed by @c other and
236 increments the refcount of wxObject::m_refData.
238 wxObject(const wxObject
& other
);
243 Performs dereferencing, for those objects that use reference counting.
248 This virtual function is redefined for every class that requires run-time
249 type information, when using the ::DECLARE_CLASS macro (or similar).
251 virtual wxClassInfo
* GetClassInfo() const;
254 Returns the wxObject::m_refData pointer, i.e. the data referenced by this object.
256 @see Ref(), UnRef(), wxObject::m_refData, SetRefData(), wxObjectRefData
258 wxObjectRefData
* GetRefData() const;
261 Determines whether this class is a subclass of (or the same class as)
267 bool tmp = obj->IsKindOf(CLASSINFO(wxFrame));
271 A pointer to a class information object, which may be obtained
272 by using the ::CLASSINFO macro.
274 @return @true if the class represented by info is the same class as this
275 one or is derived from it.
277 bool IsKindOf(const wxClassInfo
* info
) const;
280 Returns @true if this object has the same data pointer as @a obj.
282 Notice that @true is returned if the data pointers are @NULL in both objects.
284 This function only does a @e shallow comparison, i.e. it doesn't compare
285 the objects pointed to by the data pointers of these objects.
287 @see @ref overview_refcount
289 bool IsSameAs(const wxObject
& obj
) const;
292 Makes this object refer to the data in @a clone.
295 The object to 'clone'.
297 @remarks First this function calls UnRef() on itself to decrement
298 (and perhaps free) the data it is currently referring to.
299 It then sets its own wxObject::m_refData to point to that of @a clone,
300 and increments the reference count inside the data.
302 @see UnRef(), SetRefData(), GetRefData(), wxObjectRefData
304 void Ref(const wxObject
& clone
);
307 Sets the wxObject::m_refData pointer.
309 @see Ref(), UnRef(), GetRefData(), wxObjectRefData
311 void SetRefData(wxObjectRefData
* data
);
314 Decrements the reference count in the associated data, and if it is zero,
317 The wxObject::m_refData member is set to @NULL.
319 @see Ref(), SetRefData(), GetRefData(), wxObjectRefData
324 This is the same of AllocExclusive() but this method is public.
329 The @e delete operator is defined for debugging versions of the library only,
330 when the identifier @c __WXDEBUG__ is defined.
332 It takes over memory deallocation, allowing wxDebugContext operations.
334 void operator delete(void *buf
);
337 The @e new operator is defined for debugging versions of the library only, when
338 the identifier @c __WXDEBUG__ is defined.
340 It takes over memory allocation, allowing wxDebugContext operations.
342 void* operator new(size_t size
, const wxString
& filename
= NULL
, int lineNum
= 0);
346 Ensure that this object's data is not shared with any other object.
348 If we have no data, it is created using CreateRefData();
349 if we have shared data (i.e. data with a reference count greater than 1),
350 it is copied using CloneRefData(); otherwise nothing is done (the data
351 is already present and is not shared by other object instances).
353 If you use this function you should make sure that you override the
354 CreateRefData() and CloneRefData() functions in your class otherwise
355 an assertion will fail at runtime.
357 void AllocExclusive();
360 Creates a new instance of the wxObjectRefData-derived class specific to
361 this object and returns it.
363 This is usually implemented as a one-line call:
365 wxObjectRefData *MyObject::CreateRefData() const
367 return new MyObjectRefData;
371 virtual wxObjectRefData
*CreateRefData() const;
374 Creates a new instance of the wxObjectRefData-derived class specific to
375 this object and initializes it copying @a data.
377 This is usually implemented as a one-line call:
379 wxObjectRefData *MyObject::CloneRefData(const wxObjectRefData *data) const
381 // rely on the MyObjectRefData copy ctor:
382 return new MyObjectRefData(*(MyObjectRefData *)data);
386 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
389 Pointer to an object which is the object's reference-counted data.
391 @see Ref(), UnRef(), SetRefData(), GetRefData(), wxObjectRefData
393 wxObjectRefData
* m_refData
;
401 This class stores meta-information about classes.
403 Instances of this class are not generally defined directly by an application,
404 but indirectly through use of macros such as ::DECLARE_DYNAMIC_CLASS and
405 ::IMPLEMENT_DYNAMIC_CLASS.
410 @see @ref overview_rtti_classinfo, wxObject
416 Constructs a wxClassInfo object.
418 The supplied macros implicitly construct objects of this class, so there is no
419 need to create such objects explicitly in an application.
421 wxClassInfo(const wxChar
* className
,
422 const wxClassInfo
* baseClass1
,
423 const wxClassInfo
* baseClass2
,
424 int size
, wxObjectConstructorFn fn
);
427 Creates an object of the appropriate kind.
429 @return @NULL if the class has not been declared dynamically creatable
430 (typically, this happens for abstract classes).
432 wxObject
* CreateObject() const;
435 Finds the wxClassInfo object for a class with the given @a name.
437 static wxClassInfo
* FindClass(const wxString
& className
);
440 Returns the name of the first base class (@NULL if none).
442 const wxChar
* GetBaseClassName1() const;
445 Returns the name of the second base class (@NULL if none).
447 const wxChar
* GetBaseClassName2() const;
450 Returns the string form of the class name.
452 const wxChar
* GetClassName() const;
455 Returns the size of the class.
460 Returns @true if this class info can create objects of the associated class.
462 bool IsDynamic() const;
465 Returns @true if this class is a kind of (inherits from) the given class.
467 bool IsKindOf(const wxClassInfo
* info
) const;
474 This is an helper template class primarily written to avoid memory leaks because of
475 missing calls to wxObjectRefData::DecRef().
477 Despite the name this template can actually be used as a smart pointer for any
478 class implementing the reference counting interface which only consists of the two
479 methods @b T::IncRef() and @b T::DecRef().
481 The difference to wxSharedPtr<T> is that wxObjectDataPtr<T> relies on the reference
482 counting to be in the class pointed to, where instead wxSharedPtr<T> implements the
483 reference counting itself.
485 @section objectdataptr_example Example
488 class MyCarRefData: public wxObjectRefData
491 MyCarRefData() { m_price = 0; }
493 MyCarRefData( const MyCarRefData& data )
496 m_price = data.m_price;
499 void SetPrice( int price ) { m_price = price; }
500 int GetPrice() const { return m_price; }
502 bool operator == ( const MyCarRefData& other ) const
504 return m_price == other.m_price;
514 // initializes this MyCar assigning to the
515 // internal data pointer a new instance of MyCarRefData
516 MyCar( int price ) : m_data( new MyCarRefData )
518 m_data->SetPrice( price );
521 MyCar& operator =( const MyCar& tocopy )
523 // shallow copy: this is just a fast copy of pointers; the real
524 // memory-consuming data which typically is stored inside
525 // MyCarRefData is not copied here!
526 m_data = tocopy.m_data;
530 bool operator == ( const MyCar& other ) const
532 if (m_data.get() == other.m_data.get())
533 return true; // this instance and the 'other' one share the
534 // same MyCarRefData data...
536 // rely on the MyCarRefData::operator==()
537 return (*m_data.get()) == (*other.m_data.get());
540 void SetPrice( int price )
542 // make sure changes to this class do not affect other instances
543 // currently sharing our same refcounted data:
546 m_data->SetPrice( price );
551 return m_data->GetPrice();
554 wxObjectDataPtr<MyCarRefData> m_data;
559 if (m_data->GetRefCount() == 1)
562 m_data.reset( new MyCarRefData( *m_data ) );
569 @category{rtti,smartpointers}
571 @see wxObject, wxObjectRefData, @ref overview_refcount, wxSharedPtr<T>,
572 wxScopedPtr<T>, wxWeakRef<T>
575 class wxObjectDataPtr
<T
>
581 @a ptr is a pointer to the reference counted object to which this class points.
582 If @a ptr is not NULL @b T::IncRef() will be called on the object.
584 wxObjectDataPtr
<T
>(T
* ptr
= NULL
);
587 This copy constructor increases the count of the reference counted object to
588 which @a tocopy points and then this class will point to, as well.
590 wxObjectDataPtr
<T
>(const wxObjectDataPtr
<T
>& tocopy
);
594 Decreases the reference count of the object to which this class points.
596 ~wxObjectDataPtr
<T
>();
599 Gets a pointer to the reference counted object to which this class points.
604 Reset this class to ptr which points to a reference counted object and
605 calls T::DecRef() on the previously owned object.
610 Conversion to a boolean expression (in a variant which is not
611 convertable to anything but a boolean expression).
613 If this class contains a valid pointer it will return @true, if it contains
614 a @NULL pointer it will return @false.
616 operator unspecified_bool_type() const;
619 Returns a reference to the object.
621 If the internal pointer is @NULL this method will cause an assert in debug mode.
623 T
& operator*() const;
626 Returns a pointer to the reference counted object to which this class points.
628 If this the internal pointer is @NULL, this method will assert in debug mode.
630 T
* operator->() const;
636 wxObjectDataPtr
<T
>& operator=(const wxObjectDataPtr
<T
>& tocopy
);
637 wxObjectDataPtr
<T
>& operator=(T
* ptr
);
643 // ============================================================================
644 // Global functions/macros
645 // ============================================================================
647 /** @addtogroup group_funcmacro_rtti */
651 Returns a pointer to the wxClassInfo object associated with this class.
655 #define CLASSINFO( className )
658 Used inside a class declaration to declare that the class should be made
659 known to the class hierarchy, but objects of this class cannot be created
660 dynamically. The same as DECLARE_ABSTRACT_CLASS().
664 #define DECLARE_CLASS( className )
667 Used inside a class declaration to declare that the class should be
668 made known to the class hierarchy, but objects of this class cannot be created
669 dynamically. The same as DECLARE_CLASS().
676 class wxCommand: public wxObject
678 DECLARE_ABSTRACT_CLASS(wxCommand)
687 #define DECLARE_ABSTRACT_CLASS( className )
690 Used inside a class declaration to make the class known to wxWidgets RTTI
691 system and also declare that the objects of this class should be
692 dynamically creatable from run-time type information. Notice that this
693 implies that the class should have a default constructor, if this is not
694 the case consider using DECLARE_CLASS().
701 class wxFrame: public wxWindow
703 DECLARE_DYNAMIC_CLASS(wxFrame)
706 const wxString& frameTitle;
712 #define DECLARE_DYNAMIC_CLASS( className )
715 Used in a C++ implementation file to complete the declaration of a class
716 that has run-time type information. The same as IMPLEMENT_ABSTRACT_CLASS().
720 #define IMPLEMENT_CLASS( className, baseClassName )
723 Used in a C++ implementation file to complete the declaration of a class
724 that has run-time type information and two base classes. The same as
725 IMPLEMENT_ABSTRACT_CLASS2().
729 #define IMPLEMENT_CLASS2( className, baseClassName1, baseClassName2 )
732 Used in a C++ implementation file to complete the declaration of a class
733 that has run-time type information. The same as IMPLEMENT_CLASS().
740 IMPLEMENT_ABSTRACT_CLASS(wxCommand, wxObject)
742 wxCommand::wxCommand(void)
748 #define IMPLEMENT_ABSTRACT_CLASS( className, baseClassName )
751 Used in a C++ implementation file to complete the declaration of a class
752 that has run-time type information and two base classes. The same as
757 #define IMPLEMENT_ABSTRACT_CLASS2( className, baseClassName1, baseClassName2 )
760 Used in a C++ implementation file to complete the declaration of a class
761 that has run-time type information, and whose instances can be created
769 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
771 wxFrame::wxFrame(void)
777 #define IMPLEMENT_DYNAMIC_CLASS( className, baseClassName )
780 Used in a C++ implementation file to complete the declaration of a class
781 that has run-time type information, and whose instances can be created
782 dynamically. Use this for classes derived from two base classes.
786 #define IMPLEMENT_DYNAMIC_CLASS2( className, baseClassName1, baseClassName2 )
789 Same as @c const_cast<T>(x) if the compiler supports const cast or @c (T)x for
790 old compilers. Unlike wxConstCast(), the cast it to the type @c T and not to
791 <tt>T *</tt> and also the order of arguments is the same as for the standard cast.
795 @see wx_reinterpret_cast(), wx_static_cast()
797 #define wx_const_cast(T, x)
800 Same as @c reinterpret_cast<T>(x) if the compiler supports reinterpret cast or
801 @c (T)x for old compilers.
805 @see wx_const_cast(), wx_static_cast()
807 #define wx_reinterpret_cast(T, x)
810 Same as @c static_cast<T>(x) if the compiler supports static cast or @c (T)x for
811 old compilers. Unlike wxStaticCast(), there are no checks being done and
812 the meaning of the macro arguments is exactly the same as for the standard
813 static cast, i.e. @a T is the full type name and star is not appended to it.
817 @see wx_const_cast(), wx_reinterpret_cast(), wx_truncate_cast()
819 #define wx_static_cast(T, x)
822 This case doesn’t correspond to any standard cast but exists solely to make
823 casts which possibly result in a truncation of an integer value more
828 #define wx_truncate_cast(T, x)
831 This macro expands into <tt>const_cast<classname *>(ptr)</tt> if the compiler
832 supports const_cast or into an old, C-style cast, otherwise.
836 @see wx_const_cast(), wxDynamicCast(), wxStaticCast()
838 #define wxConstCast( ptr, classname )
841 This macro returns the pointer @e ptr cast to the type @e classname * if
842 the pointer is of this type (the check is done during the run-time) or
843 @NULL otherwise. Usage of this macro is preferred over obsoleted
844 wxObject::IsKindOf() function.
846 The @e ptr argument may be @NULL, in which case @NULL will be returned.
853 wxWindow *win = wxWindow::FindFocus();
854 wxTextCtrl *text = wxDynamicCast(win, wxTextCtrl);
857 // a text control has the focus...
861 // no window has the focus or it is not a text control
865 @see @ref overview_rtti, wxDynamicCastThis(), wxConstCast(), wxStaticCast()
867 #define wxDynamicCast( ptr, classname )
870 This macro is equivalent to <tt>wxDynamicCast(this, classname)</tt> but the latter provokes
871 spurious compilation warnings from some compilers (because it tests whether
872 @c this pointer is non-@NULL which is always true), so this macro should be
879 #define wxDynamicCastThis( classname )
882 This macro checks that the cast is valid in debug mode (an assert failure
883 will result if wxDynamicCast(ptr, classname) == @NULL) and then returns the
884 result of executing an equivalent of <tt>static_cast<classname *>(ptr)</tt>.
888 @see wx_static_cast(), wxDynamicCast(), wxConstCast()
890 #define wxStaticCast( ptr, classname )
893 Creates and returns an object of the given class, if the class has been
894 registered with the dynamic class system using DECLARE... and IMPLEMENT...
899 wxObject
*wxCreateDynamicObject(const wxString
& className
);
903 /** @addtogroup group_funcmacro_debug */
907 This is defined in debug mode to be call the redefined new operator
908 with filename and line number arguments. The definition is:
911 #define WXDEBUG_NEW new(__FILE__,__LINE__)
914 In non-debug mode, this is defined as the normal new operator.
918 #define WXDEBUG_NEW( arg )