]>
git.saurik.com Git - wxWidgets.git/blob - interface/object.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxObjectRefData
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 @class wxObjectRefData
13 This class is used to store reference-counted data.
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.
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)
47 class MyCarRefData: public wxObjectRefData
55 MyCarRefData( const MyCarRefData& data )
58 m_price = data.m_price;
61 bool operator == (const MyCarRefData& data) const
63 return m_price == data.m_price;
70 #define M_CARDATA ((MyCarRefData *)m_refData)
72 IMPLEMENT_DYNAMIC_CLASS(MyCar,wxObject)
74 MyCar::MyCar( int price )
76 m_refData = new MyCarRefData();
77 M_CARDATA->m_price = price;
80 wxObjectRefData *MyCar::CreateRefData() const
82 return new MyCarRefData;
85 wxObjectRefData *MyCar::CloneRefData(const wxObjectRefData *data) const
87 return new MyCarRefData(*(MyCarRefData *)data);
90 bool MyCar::operator == ( const MyCar& car ) const
92 if (m_refData == car.m_refData) return true;
94 if (!m_refData || !car.m_refData) return false;
96 return ( *(MyCarRefData*)m_refData == *(MyCarRefData*)car.m_refData );
99 void MyCar::SetPrice( int price )
103 M_CARDATA->m_price = price;
106 int MyCar::GetPrice() const
108 wxCHECK_MSG( IsOk(), -1, "invalid car" );
110 return (M_CARDATA->m_price);
118 @see wxObject, wxObjectDataPtr<T>, @ref overview_refcount
120 class wxObjectRefData
126 It's declared @c protected so that wxObjectRefData instances
127 will never be destroyed directly but only as result of a DecRef() call.
133 Default constructor. Initialises the internal reference count to 1.
138 Decrements the reference count associated with this shared data and, if
139 it reaches zero, destroys this instance of wxObjectRefData releasing its
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
149 Returns the reference count associated with this shared data.
151 When this goes to zero during a DecRef() call, the object will auto-free itself.
153 int GetRefCount() const;
156 Increments the reference count associated with this shared data.
167 This is the root class of many of the wxWidgets classes.
169 It declares a virtual destructor which ensures that destructors get called
170 for all derived class objects where necessary.
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.
176 The class contains optional debugging versions of @b new and @b delete, which
177 can help trace memory allocation and deallocation problems.
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").
186 @see wxClassInfo, @ref overview_debugging, wxObjectRefData
197 wxObject(const wxObject
& other
);
203 Performs dereferencing, for those objects that use reference counting.
208 A virtual function that may be redefined by derived classes to allow dumping of
211 This function is only defined in debug build and exists only if @c __WXDEBUG__
215 Stream on which to output dump information.
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.
224 void Dump(ostream
& stream
);
227 This virtual function is redefined for every class that requires run-time
228 type information, when using the ::DECLARE_CLASS macro (or similar).
230 wxClassInfo
* GetClassInfo();
233 Returns the wxObject::m_refData pointer, i.e. the data referenced by this object.
235 @see Ref(), UnRef(), wxObject::m_refData, SetRefData(), wxObjectRefData
237 wxObjectRefData
* GetRefData() const;
240 Determines whether this class is a subclass of (or the same class as)
246 bool tmp = obj->IsKindOf(CLASSINFO(wxFrame));
250 A pointer to a class information object, which may be obtained
251 by using the ::CLASSINFO macro.
253 @return @true if the class represented by info is the same class as this
254 one or is derived from it.
256 bool IsKindOf(wxClassInfo
* info
);
259 Returns @true if this object has the same data pointer as @a obj.
261 Notice that @true is returned if the data pointers are @NULL in both objects.
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.
266 @see @ref overview_refcount
268 bool IsSameAs(const wxObject
& obj
);
271 Makes this object refer to the data in @a clone.
274 The object to 'clone'.
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.
281 @see UnRef(), SetRefData(), GetRefData(), wxObjectRefData
283 void Ref(const wxObject
& clone
);
286 Sets the wxObject::m_refData pointer.
288 @see Ref(), UnRef(), GetRefData(), wxObjectRefData
290 void SetRefData(wxObjectRefData
* data
);
293 Decrements the reference count in the associated data, and if it is zero,
296 The wxObject::m_refData member is set to @NULL.
298 @see Ref(), SetRefData(), GetRefData(), wxObjectRefData
303 Ensure that this object's data is not shared with any other object.
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.
312 The @e delete operator is defined for debugging versions of the library only,
313 when the identifier @c __WXDEBUG__ is defined.
315 It takes over memory deallocation, allowing wxDebugContext operations.
317 void operator delete(void *buf
);
320 The @e new operator is defined for debugging versions of the library only, when
321 the identifier @c __WXDEBUG__ is defined.
323 It takes over memory allocation, allowing wxDebugContext operations.
325 void* operator new(size_t size
, const wxString
& filename
= NULL
, int lineNum
= 0);
330 Pointer to an object which is the object's reference-counted data.
332 @see Ref(), UnRef(), SetRefData(), GetRefData(), wxObjectRefData
334 wxObjectRefData
* m_refData
;
343 This class stores meta-information about classes.
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.
352 @see @ref overview_rtti_classinfo, wxObject
358 Constructs a wxClassInfo object.
360 The supplied macros implicitly construct objects of this class, so there is no
361 need to create such objects explicitly in an application.
363 wxClassInfo(const wxChar
* className
,
364 const wxClassInfo
* baseClass1
,
365 const wxClassInfo
* baseClass2
,
366 int size
, wxObjectConstructorFn fn
);
369 Creates an object of the appropriate kind.
371 @return @NULL if the class has not been declared dynamically creatable
372 (typically, this happens for abstract classes).
374 wxObject
* CreateObject() const;
377 Finds the wxClassInfo object for a class with the given @a name.
379 static wxClassInfo
* FindClass(wxChar
* name
);
382 Returns the name of the first base class (@NULL if none).
384 wxChar
* GetBaseClassName1() const;
387 Returns the name of the second base class (@NULL if none).
389 wxChar
* GetBaseClassName2() const;
392 Returns the string form of the class name.
394 wxChar
* GetClassName() const;
397 Returns the size of the class.
402 Initializes pointers in the wxClassInfo objects for fast execution of IsKindOf().
403 Called in base wxWidgets library initialization.
405 static void InitializeClasses();
408 Returns @true if this class info can create objects of the associated class.
410 bool IsDynamic() const;
413 Returns @true if this class is a kind of (inherits from) the given class.
415 bool IsKindOf(wxClassInfo
* info
);
423 This is helper template class primarily written to avoid memory leaks because of
424 missing calls to wxObjectRefData::DecRef().
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().
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.
438 class MyCarRefData: public wxObjectRefData
441 MyCarRefData() { m_price = 0; }
443 MyCarRefData( const MyCarRefData& data )
446 m_price = data.m_price;
449 void SetPrice( int price ) { m_price = price; }
450 int GetPrice() { return m_price; }
459 MyCar( int price ) : m_data( new MyCarRefData )
461 m_data->SetPrice( price );
464 MyCar& operator =( const MyCar& tocopy )
466 m_data = tocopy.m_data;
470 bool operator == ( const MyCar& other ) const
472 if (m_data.get() == other.m_data.get()) return true;
473 return (m_data->GetPrice() == other.m_data->GetPrice());
476 void SetPrice( int price )
479 m_data->SetPrice( price );
484 return m_data->GetPrice();
487 wxObjectDataPtr<MyCarRefData> m_data;
492 if (m_data->GetRefCount() == 1)
495 m_data.reset( new MyCarRefData( *m_data ) );
502 @category{rtti,smartpointers}
504 @see wxObject, wxObjectRefData, @ref overview_refcount, wxSharedPtr<T>,
505 wxScopedPtr<T>, wxWeakRef<T>
507 class wxObjectDataPtr
<T
>
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.
516 wxObjectDataPtr
<T
>(T
* ptr
= NULL
);
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.
522 wxObjectDataPtr
<T
>(const wxObjectDataPtr
<T
>& tocopy
);
526 Decreases the reference count of the object to which this class points.
528 ~wxObjectDataPtr
<T
>();
531 Gets a pointer to the reference counted object to which this class points.
536 Reset this class to ptr which points to a reference counted object and
537 calls T::DecRef() on the previously owned object.
542 Conversion to a boolean expression (in a variant which is not
543 convertable to anything but a boolean expression).
545 If this class contains a valid pointer it will return @true, if it contains
546 a @NULL pointer it will return @false.
548 operator unspecified_bool_type() const;
551 Returns a reference to the object.
553 If the internal pointer is @NULL this method will cause an assert in debug mode.
555 T
& operator*() const;
558 Returns a pointer to the reference counted object to which this class points.
560 If this the internal pointer is @NULL, this method will assert in debug mode.
562 T
* operator->() const;
568 wxObjectDataPtr
<T
>& operator=(const wxObjectDataPtr
<T
>& tocopy
);
569 wxObjectDataPtr
<T
>& operator=(T
* ptr
);
575 // ============================================================================
576 // Global functions/macros
577 // ============================================================================
579 /** @ingroup group_funcmacro_rtti */
583 Returns a pointer to the wxClassInfo object associated with this class.
587 #define CLASSINFO( className )
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().
596 #define DECLARE_CLASS( className )
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().
608 class wxCommand: public wxObject
610 DECLARE_ABSTRACT_CLASS(wxCommand)
619 #define DECLARE_ABSTRACT_CLASS( className )
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().
633 class wxFrame: public wxWindow
635 DECLARE_DYNAMIC_CLASS(wxFrame)
638 const wxString& frameTitle;
644 #define DECLARE_DYNAMIC_CLASS( className )
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().
652 #define IMPLEMENT_CLASS( className, baseClassName )
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().
661 #define IMPLEMENT_CLASS2( className, baseClassName1, baseClassName2 )
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().
672 IMPLEMENT_ABSTRACT_CLASS(wxCommand, wxObject)
674 wxCommand::wxCommand(void)
680 #define IMPLEMENT_ABSTRACT_CLASS( className, baseClassName )
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
689 #define IMPLEMENT_ABSTRACT_CLASS2( className, baseClassName1, baseClassName2 )
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
701 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
703 wxFrame::wxFrame(void)
709 #define IMPLEMENT_DYNAMIC_CLASS( className, baseClassName )
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.
718 #define IMPLEMENT_DYNAMIC_CLASS2( className, baseClassName1, baseClassName2 )
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.
727 @see wx_reinterpret_cast(), wx_static_cast()
729 #define wx_const_cast(T, x)
732 Same as @c reinterpret_cast<T>(x) if the compiler supports reinterpret cast or
733 @c (T)x for old compilers.
737 @see wx_const_cast(), wx_static_cast()
739 #define wx_reinterpret_cast(T, x)
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.
749 @see wx_const_cast(), wx_reinterpret_cast(), wx_truncate_cast()
751 #define wx_static_cast(T, x)
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
760 #define wx_truncate_cast(T, x)
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.
768 @see wx_const_cast(), wxDynamicCast(), wxStaticCast()
770 #define wxConstCast( ptr, classname )
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.
778 The @e ptr argument may be @NULL, in which case @NULL will be returned.
785 wxWindow *win = wxWindow::FindFocus();
786 wxTextCtrl *text = wxDynamicCast(win, wxTextCtrl);
789 // a text control has the focus...
793 // no window has the focus or it is not a text control
797 @see @ref overview_rtti, wxDynamicCastThis(), wxConstCast(), wxStaticCast()
799 #define wxDynamicCast( ptr, classname )
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
811 #define wxDynamicCastThis( classname )
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>.
820 @see wx_static_cast(), wxDynamicCast(), wxConstCast()
822 #define wxStaticCast( ptr, classname )
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...
831 wxObject
*wxCreateDynamicObject(const wxString
& className
);
835 /** @ingroup group_funcmacro_debug */
839 This is defined in debug mode to be call the redefined new operator
840 with filename and line number arguments. The definition is:
843 #define WXDEBUG_NEW new(__FILE__,__LINE__)
846 In non-debug mode, this is defined as the normal new operator.
850 #define WXDEBUG_NEW( arg )