]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/object.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxObjectRefData
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 @class wxObjectRefData
12 This class is used to store reference-counted data.
14 Derive classes from this to store your own data. When retrieving information
15 from a wxObject's reference data, you will need to cast to your own derived class.
17 @section objectrefdata_example Example
23 class MyCar : public wxObject
29 bool IsOk() const { return m_refData != NULL; }
31 bool operator == ( const MyCar& car ) const;
32 bool operator != (const MyCar& car) const { return !(*this == car); }
34 void SetPrice( int price );
38 virtual wxObjectRefData *CreateRefData() const;
39 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
41 DECLARE_DYNAMIC_CLASS(MyCar)
48 // the reference data class is typically a private class only visible in the
49 // implementation source file of the refcounted class.
50 class MyCarRefData : public wxObjectRefData
58 MyCarRefData( const MyCarRefData& data )
61 // copy refcounted data; this is usually a time- and memory-consuming operation
62 // and is only done when two (or more) MyCar instances need to unshare a
63 // common instance of MyCarRefData
64 m_price = data.m_price;
67 bool operator == (const MyCarRefData& data) const
69 return m_price == data.m_price;
73 // in real world, reference counting is usually used only when
74 // the wxObjectRefData-derived class holds data very memory-consuming;
75 // in this example the various MyCar instances may share a MyCarRefData
76 // instance which however only takes 4 bytes for this integer!
81 #define M_CARDATA ((MyCarRefData *)m_refData)
82 IMPLEMENT_DYNAMIC_CLASS(MyCar,wxObject)
84 MyCar::MyCar( int price )
86 // here we init the MyCar internal data:
87 m_refData = new MyCarRefData();
88 M_CARDATA->m_price = price;
91 wxObjectRefData *MyCar::CreateRefData() const
93 return new MyCarRefData;
96 wxObjectRefData *MyCar::CloneRefData(const wxObjectRefData *data) const
98 return new MyCarRefData(*(MyCarRefData *)data);
101 bool MyCar::operator == ( const MyCar& car ) const
103 if (m_refData == car.m_refData)
105 if (!m_refData || !car.m_refData)
108 // here we use the MyCarRefData::operator==() function.
109 // Note however that this comparison may be very slow if the
110 // reference data contains a lot of data to be compared.
111 return ( *(MyCarRefData*)m_refData == *(MyCarRefData*)car.m_refData );
114 void MyCar::SetPrice( int price )
116 // since this function modifies one of the MyCar internal property,
117 // we need to be sure that the other MyCar instances which share the
118 // same MyCarRefData instance are not affected by this call.
119 // I.e. it's very important to call UnShare() in all setters of
120 // refcounted classes!
123 M_CARDATA->m_price = price;
126 int MyCar::GetPrice() const
128 wxCHECK_MSG( IsOk(), -1, "invalid car" );
130 return M_CARDATA->m_price;
138 @see wxObject, wxObjectDataPtr<T>, @ref overview_refcount
140 class wxObjectRefData
146 It's declared @c protected so that wxObjectRefData instances
147 will never be destroyed directly but only as result of a DecRef() call.
149 virtual ~wxObjectRefData();
153 Default constructor. Initialises the internal reference count to 1.
158 Decrements the reference count associated with this shared data and, if
159 it reaches zero, destroys this instance of wxObjectRefData releasing its
162 Please note that after calling this function, the caller should
163 absolutely avoid to use the pointer to this instance since it may not be
169 Returns the reference count associated with this shared data.
171 When this goes to zero during a DecRef() call, the object will auto-free itself.
173 int GetRefCount() const;
176 Increments the reference count associated with this shared data.
186 This is the root class of many of the wxWidgets classes.
188 It declares a virtual destructor which ensures that destructors get called
189 for all derived class objects where necessary.
191 wxObject is the hub of a dynamic object creation scheme, enabling a program
192 to create instances of a class only knowing its string class name, and to
193 query the class hierarchy.
195 The class contains optional debugging versions of @b new and @b delete, which
196 can help trace memory allocation and deallocation problems.
198 wxObject can be used to implement @ref overview_refcount "reference counted"
199 objects, such as wxPen, wxBitmap and others
200 (see @ref overview_refcount_list "this list").
201 See wxObjectRefData and @ref overview_refcount for more info about
207 @see wxClassInfo, @ref overview_debugging, @ref overview_refcount,
208 wxObjectRefData, wxObjectDataPtr<T>
215 Default ctor; initializes to @NULL the internal reference data.
222 Sets the internal wxObject::m_refData pointer to point to the same
223 instance of the wxObjectRefData-derived class pointed by @c other and
224 increments the refcount of wxObject::m_refData.
226 wxObject(const wxObject
& other
);
231 Performs dereferencing, for those objects that use reference counting.
236 This virtual function is redefined for every class that requires run-time
237 type information, when using the ::DECLARE_CLASS macro (or similar).
239 virtual wxClassInfo
* GetClassInfo() const;
242 Returns the wxObject::m_refData pointer, i.e. the data referenced by this object.
244 @see Ref(), UnRef(), wxObject::m_refData, SetRefData(), wxObjectRefData
246 wxObjectRefData
* GetRefData() const;
249 Determines whether this class is a subclass of (or the same class as)
255 bool tmp = obj->IsKindOf(CLASSINFO(wxFrame));
259 A pointer to a class information object, which may be obtained
260 by using the ::CLASSINFO macro.
262 @return @true if the class represented by info is the same class as this
263 one or is derived from it.
265 bool IsKindOf(const wxClassInfo
* info
) const;
268 Returns @true if this object has the same data pointer as @a obj.
270 Notice that @true is returned if the data pointers are @NULL in both objects.
272 This function only does a @e shallow comparison, i.e. it doesn't compare
273 the objects pointed to by the data pointers of these objects.
275 @see @ref overview_refcount
277 bool IsSameAs(const wxObject
& obj
) const;
280 Makes this object refer to the data in @a clone.
283 The object to 'clone'.
285 @remarks First this function calls UnRef() on itself to decrement
286 (and perhaps free) the data it is currently referring to.
287 It then sets its own wxObject::m_refData to point to that of @a clone,
288 and increments the reference count inside the data.
290 @see UnRef(), SetRefData(), GetRefData(), wxObjectRefData
292 void Ref(const wxObject
& clone
);
295 Sets the wxObject::m_refData pointer.
297 @see Ref(), UnRef(), GetRefData(), wxObjectRefData
299 void SetRefData(wxObjectRefData
* data
);
302 Decrements the reference count in the associated data, and if it is zero,
305 The wxObject::m_refData member is set to @NULL.
307 @see Ref(), SetRefData(), GetRefData(), wxObjectRefData
312 This is the same of AllocExclusive() but this method is public.
317 The @e delete operator is defined for debugging versions of the library only,
318 when the identifier @c __WXDEBUG__ is defined.
320 It takes over memory deallocation, allowing wxDebugContext operations.
322 void operator delete(void *buf
);
325 The @e new operator is defined for debugging versions of the library only, when
326 the identifier @c __WXDEBUG__ is defined.
328 It takes over memory allocation, allowing wxDebugContext operations.
330 void* operator new(size_t size
, const wxString
& filename
= NULL
, int lineNum
= 0);
334 Ensure that this object's data is not shared with any other object.
336 If we have no data, it is created using CreateRefData();
337 if we have shared data (i.e. data with a reference count greater than 1),
338 it is copied using CloneRefData(); otherwise nothing is done (the data
339 is already present and is not shared by other object instances).
341 If you use this function you should make sure that you override the
342 CreateRefData() and CloneRefData() functions in your class otherwise
343 an assertion will fail at runtime.
345 void AllocExclusive();
348 Creates a new instance of the wxObjectRefData-derived class specific to
349 this object and returns it.
351 This is usually implemented as a one-line call:
353 wxObjectRefData *MyObject::CreateRefData() const
355 return new MyObjectRefData;
359 virtual wxObjectRefData
*CreateRefData() const;
362 Creates a new instance of the wxObjectRefData-derived class specific to
363 this object and initializes it copying @a data.
365 This is usually implemented as a one-line call:
367 wxObjectRefData *MyObject::CloneRefData(const wxObjectRefData *data) const
369 // rely on the MyObjectRefData copy ctor:
370 return new MyObjectRefData(*(MyObjectRefData *)data);
374 virtual wxObjectRefData
*CloneRefData(const wxObjectRefData
*data
) const;
377 Pointer to an object which is the object's reference-counted data.
379 @see Ref(), UnRef(), SetRefData(), GetRefData(), wxObjectRefData
381 wxObjectRefData
* m_refData
;
389 This class stores meta-information about classes.
391 Instances of this class are not generally defined directly by an application,
392 but indirectly through use of macros such as ::DECLARE_DYNAMIC_CLASS and
393 ::IMPLEMENT_DYNAMIC_CLASS.
398 @see @ref overview_rtti_classinfo, wxObject
404 Constructs a wxClassInfo object.
406 The supplied macros implicitly construct objects of this class, so there is no
407 need to create such objects explicitly in an application.
409 wxClassInfo(const wxChar
* className
,
410 const wxClassInfo
* baseClass1
,
411 const wxClassInfo
* baseClass2
,
412 int size
, wxObjectConstructorFn fn
);
415 Creates an object of the appropriate kind.
417 @return @NULL if the class has not been declared dynamically creatable
418 (typically, this happens for abstract classes).
420 wxObject
* CreateObject() const;
423 Finds the wxClassInfo object for a class with the given @a name.
425 static wxClassInfo
* FindClass(const wxString
& className
);
428 Returns the name of the first base class (@NULL if none).
430 const wxChar
* GetBaseClassName1() const;
433 Returns the name of the second base class (@NULL if none).
435 const wxChar
* GetBaseClassName2() const;
438 Returns the string form of the class name.
440 const wxChar
* GetClassName() const;
443 Returns the size of the class.
448 Returns @true if this class info can create objects of the associated class.
450 bool IsDynamic() const;
453 Returns @true if this class is a kind of (inherits from) the given class.
455 bool IsKindOf(const wxClassInfo
* info
) const;
462 This is an helper template class primarily written to avoid memory leaks because of
463 missing calls to wxObjectRefData::DecRef().
465 Despite the name this template can actually be used as a smart pointer for any
466 class implementing the reference counting interface which only consists of the two
467 methods @b T::IncRef() and @b T::DecRef().
469 The difference to wxSharedPtr<T> is that wxObjectDataPtr<T> relies on the reference
470 counting to be in the class pointed to, where instead wxSharedPtr<T> implements the
471 reference counting itself.
473 @section objectdataptr_example Example
476 class MyCarRefData: public wxObjectRefData
479 MyCarRefData() { m_price = 0; }
481 MyCarRefData( const MyCarRefData& data )
484 m_price = data.m_price;
487 void SetPrice( int price ) { m_price = price; }
488 int GetPrice() const { return m_price; }
490 bool operator == ( const MyCarRefData& other ) const
492 return m_price == other.m_price;
502 // initializes this MyCar assigning to the
503 // internal data pointer a new instance of MyCarRefData
504 MyCar( int price ) : m_data( new MyCarRefData )
506 m_data->SetPrice( price );
509 MyCar& operator =( const MyCar& tocopy )
511 // shallow copy: this is just a fast copy of pointers; the real
512 // memory-consuming data which typically is stored inside
513 // MyCarRefData is not copied here!
514 m_data = tocopy.m_data;
518 bool operator == ( const MyCar& other ) const
520 if (m_data.get() == other.m_data.get())
521 return true; // this instance and the 'other' one share the
522 // same MyCarRefData data...
524 // rely on the MyCarRefData::operator==()
525 return (*m_data.get()) == (*other.m_data.get());
528 void SetPrice( int price )
530 // make sure changes to this class do not affect other instances
531 // currently sharing our same refcounted data:
534 m_data->SetPrice( price );
539 return m_data->GetPrice();
542 wxObjectDataPtr<MyCarRefData> m_data;
547 if (m_data->GetRefCount() == 1)
550 m_data.reset( new MyCarRefData( *m_data ) );
557 @category{rtti,smartpointers}
559 @see wxObject, wxObjectRefData, @ref overview_refcount, wxSharedPtr<T>,
560 wxScopedPtr<T>, wxWeakRef<T>
563 class wxObjectDataPtr
<T
>
569 @a ptr is a pointer to the reference counted object to which this class points.
570 If @a ptr is not NULL @b T::IncRef() will be called on the object.
572 wxObjectDataPtr
<T
>(T
* ptr
= NULL
);
575 This copy constructor increases the count of the reference counted object to
576 which @a tocopy points and then this class will point to, as well.
578 wxObjectDataPtr
<T
>(const wxObjectDataPtr
<T
>& tocopy
);
582 Decreases the reference count of the object to which this class points.
584 ~wxObjectDataPtr
<T
>();
587 Gets a pointer to the reference counted object to which this class points.
592 Reset this class to ptr which points to a reference counted object and
593 calls T::DecRef() on the previously owned object.
598 Conversion to a boolean expression (in a variant which is not
599 convertable to anything but a boolean expression).
601 If this class contains a valid pointer it will return @true, if it contains
602 a @NULL pointer it will return @false.
604 operator unspecified_bool_type() const;
607 Returns a reference to the object.
609 If the internal pointer is @NULL this method will cause an assert in debug mode.
611 T
& operator*() const;
614 Returns a pointer to the reference counted object to which this class points.
616 If this the internal pointer is @NULL, this method will assert in debug mode.
618 T
* operator->() const;
624 wxObjectDataPtr
<T
>& operator=(const wxObjectDataPtr
<T
>& tocopy
);
625 wxObjectDataPtr
<T
>& operator=(T
* ptr
);
631 // ============================================================================
632 // Global functions/macros
633 // ============================================================================
635 /** @addtogroup group_funcmacro_rtti */
639 Returns a pointer to the wxClassInfo object associated with this class.
643 #define CLASSINFO( className )
646 Used inside a class declaration to declare that the class should be made
647 known to the class hierarchy, but objects of this class cannot be created
648 dynamically. The same as DECLARE_ABSTRACT_CLASS().
652 #define DECLARE_CLASS( className )
655 Used inside a class declaration to declare that the class should be
656 made known to the class hierarchy, but objects of this class cannot be created
657 dynamically. The same as DECLARE_CLASS().
664 class wxCommand: public wxObject
666 DECLARE_ABSTRACT_CLASS(wxCommand)
675 #define DECLARE_ABSTRACT_CLASS( className )
678 Used inside a class declaration to make the class known to wxWidgets RTTI
679 system and also declare that the objects of this class should be
680 dynamically creatable from run-time type information. Notice that this
681 implies that the class should have a default constructor, if this is not
682 the case consider using DECLARE_CLASS().
689 class wxFrame: public wxWindow
691 DECLARE_DYNAMIC_CLASS(wxFrame)
694 const wxString& frameTitle;
700 #define DECLARE_DYNAMIC_CLASS( className )
703 Used in a C++ implementation file to complete the declaration of a class
704 that has run-time type information. The same as IMPLEMENT_ABSTRACT_CLASS().
708 #define IMPLEMENT_CLASS( className, baseClassName )
711 Used in a C++ implementation file to complete the declaration of a class
712 that has run-time type information and two base classes. The same as
713 IMPLEMENT_ABSTRACT_CLASS2().
717 #define IMPLEMENT_CLASS2( className, baseClassName1, baseClassName2 )
720 Used in a C++ implementation file to complete the declaration of a class
721 that has run-time type information. The same as IMPLEMENT_CLASS().
728 IMPLEMENT_ABSTRACT_CLASS(wxCommand, wxObject)
730 wxCommand::wxCommand(void)
736 #define IMPLEMENT_ABSTRACT_CLASS( className, baseClassName )
739 Used in a C++ implementation file to complete the declaration of a class
740 that has run-time type information and two base classes. The same as
745 #define IMPLEMENT_ABSTRACT_CLASS2( className, baseClassName1, baseClassName2 )
748 Used in a C++ implementation file to complete the declaration of a class
749 that has run-time type information, and whose instances can be created
757 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
759 wxFrame::wxFrame(void)
765 #define IMPLEMENT_DYNAMIC_CLASS( className, baseClassName )
768 Used in a C++ implementation file to complete the declaration of a class
769 that has run-time type information, and whose instances can be created
770 dynamically. Use this for classes derived from two base classes.
774 #define IMPLEMENT_DYNAMIC_CLASS2( className, baseClassName1, baseClassName2 )
777 Same as @c const_cast<T>(x) if the compiler supports const cast or @c (T)x for
778 old compilers. Unlike wxConstCast(), the cast it to the type @c T and not to
779 <tt>T *</tt> and also the order of arguments is the same as for the standard cast.
783 @see wx_reinterpret_cast(), wx_static_cast()
785 #define wx_const_cast(T, x)
788 Same as @c reinterpret_cast<T>(x) if the compiler supports reinterpret cast or
789 @c (T)x for old compilers.
793 @see wx_const_cast(), wx_static_cast()
795 #define wx_reinterpret_cast(T, x)
798 Same as @c static_cast<T>(x) if the compiler supports static cast or @c (T)x for
799 old compilers. Unlike wxStaticCast(), there are no checks being done and
800 the meaning of the macro arguments is exactly the same as for the standard
801 static cast, i.e. @a T is the full type name and star is not appended to it.
805 @see wx_const_cast(), wx_reinterpret_cast(), wx_truncate_cast()
807 #define wx_static_cast(T, x)
810 This case doesn’t correspond to any standard cast but exists solely to make
811 casts which possibly result in a truncation of an integer value more
816 #define wx_truncate_cast(T, x)
819 This macro expands into <tt>const_cast<classname *>(ptr)</tt> if the compiler
820 supports const_cast or into an old, C-style cast, otherwise.
824 @see wx_const_cast(), wxDynamicCast(), wxStaticCast()
826 #define wxConstCast( ptr, classname )
829 This macro returns the pointer @e ptr cast to the type @e classname * if
830 the pointer is of this type (the check is done during the run-time) or
831 @NULL otherwise. Usage of this macro is preferred over obsoleted
832 wxObject::IsKindOf() function.
834 The @e ptr argument may be @NULL, in which case @NULL will be returned.
841 wxWindow *win = wxWindow::FindFocus();
842 wxTextCtrl *text = wxDynamicCast(win, wxTextCtrl);
845 // a text control has the focus...
849 // no window has the focus or it is not a text control
853 @see @ref overview_rtti, wxDynamicCastThis(), wxConstCast(), wxStaticCast()
855 #define wxDynamicCast( ptr, classname )
858 This macro is equivalent to <tt>wxDynamicCast(this, classname)</tt> but the latter provokes
859 spurious compilation warnings from some compilers (because it tests whether
860 @c this pointer is non-@NULL which is always true), so this macro should be
867 #define wxDynamicCastThis( classname )
870 This macro checks that the cast is valid in debug mode (an assert failure
871 will result if wxDynamicCast(ptr, classname) == @NULL) and then returns the
872 result of executing an equivalent of <tt>static_cast<classname *>(ptr)</tt>.
876 @see wx_static_cast(), wxDynamicCast(), wxConstCast()
878 #define wxStaticCast( ptr, classname )
881 Creates and returns an object of the given class, if the class has been
882 registered with the dynamic class system using DECLARE... and IMPLEMENT...
887 wxObject
*wxCreateDynamicObject(const wxString
& className
);
891 /** @addtogroup group_funcmacro_debug */
895 This is defined in debug mode to be call the redefined new operator
896 with filename and line number arguments. The definition is:
899 #define WXDEBUG_NEW new(__FILE__,__LINE__)
902 In non-debug mode, this is defined as the normal new operator.
906 #define WXDEBUG_NEW( arg )