]>
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.
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)
46 class MyCarRefData: public wxObjectRefData
54 MyCarRefData( const MyCarRefData& data )
57 m_price = data.m_price;
60 bool operator == (const MyCarRefData& data) const
62 return m_price == data.m_price;
69 #define M_CARDATA ((MyCarRefData *)m_refData)
71 IMPLEMENT_DYNAMIC_CLASS(MyCar,wxObject)
73 MyCar::MyCar( int price )
75 m_refData = new MyCarRefData();
76 M_CARDATA->m_price = price;
79 wxObjectRefData *MyCar::CreateRefData() const
81 return new MyCarRefData;
84 wxObjectRefData *MyCar::CloneRefData(const wxObjectRefData *data) const
86 return new MyCarRefData(*(MyCarRefData *)data);
89 bool MyCar::operator == ( const MyCar& car ) const
91 if (m_refData == car.m_refData) return true;
93 if (!m_refData || !car.m_refData) return false;
95 return ( *(MyCarRefData*)m_refData == *(MyCarRefData*)car.m_refData );
98 void MyCar::SetPrice( int price )
102 M_CARDATA->m_price = price;
105 int MyCar::GetPrice() const
107 wxCHECK_MSG( IsOk(), -1, "invalid car" );
109 return (M_CARDATA->m_price);
117 @see wxObject, wxObjectDataPtr<T>, @ref overview_refcount
119 class wxObjectRefData
125 It's declared @c protected so that wxObjectRefData instances
126 will never be destroyed directly but only as result of a DecRef() call.
132 Default constructor. Initialises the internal reference count to 1.
137 Decrements the reference count associated with this shared data and, if
138 it reaches zero, destroys this instance of wxObjectRefData releasing its
141 Please note that after calling this function, the caller should
142 absolutely avoid to use the pointer to this instance since it may not be
148 Returns the reference count associated with this shared data.
150 When this goes to zero during a DecRef() call, the object will auto-free itself.
152 int GetRefCount() const;
155 Increments the reference count associated with this shared data.
165 This is the root class of many of the wxWidgets classes.
167 It declares a virtual destructor which ensures that destructors get called
168 for all derived class objects where necessary.
170 wxObject is the hub of a dynamic object creation scheme, enabling a program
171 to create instances of a class only knowing its string class name, and to
172 query the class hierarchy.
174 The class contains optional debugging versions of @b new and @b delete, which
175 can help trace memory allocation and deallocation problems.
177 wxObject can be used to implement @ref overview_refcount "reference counted"
178 objects, such as wxPen, wxBitmap and others
179 (see @ref overview_refcount_list "this list").
184 @see wxClassInfo, @ref overview_debugging, wxObjectRefData
195 wxObject(const wxObject
& other
);
201 Performs dereferencing, for those objects that use reference counting.
206 A virtual function that may be redefined by derived classes to allow dumping of
209 This function is only defined in debug build and exists only if @c __WXDEBUG__
213 Stream on which to output dump information.
215 @remarks Currently wxWidgets does not define Dump() for derived classes,
216 but programmers may wish to use it for their own applications.
217 Be sure to call the Dump member of the class's base class to allow all
218 information to be dumped.
219 The implementation of this function in wxObject just writes
220 the class name of the object.
222 void Dump(ostream
& stream
);
225 This virtual function is redefined for every class that requires run-time
226 type information, when using the ::DECLARE_CLASS macro (or similar).
228 wxClassInfo
* GetClassInfo();
231 Returns the wxObject::m_refData pointer, i.e. the data referenced by this object.
233 @see Ref(), UnRef(), wxObject::m_refData, SetRefData(), wxObjectRefData
235 wxObjectRefData
* GetRefData() const;
238 Determines whether this class is a subclass of (or the same class as)
244 bool tmp = obj->IsKindOf(CLASSINFO(wxFrame));
248 A pointer to a class information object, which may be obtained
249 by using the ::CLASSINFO macro.
251 @return @true if the class represented by info is the same class as this
252 one or is derived from it.
254 bool IsKindOf(const wxClassInfo
* info
) const;
257 Returns @true if this object has the same data pointer as @a obj.
259 Notice that @true is returned if the data pointers are @NULL in both objects.
261 This function only does a @e shallow comparison, i.e. it doesn't compare
262 the objects pointed to by the data pointers of these objects.
264 @see @ref overview_refcount
266 bool IsSameAs(const wxObject
& obj
) const;
269 Makes this object refer to the data in @a clone.
272 The object to 'clone'.
274 @remarks First this function calls UnRef() on itself to decrement
275 (and perhaps free) the data it is currently referring to.
276 It then sets its own wxObject::m_refData to point to that of @a clone,
277 and increments the reference count inside the data.
279 @see UnRef(), SetRefData(), GetRefData(), wxObjectRefData
281 void Ref(const wxObject
& clone
);
284 Sets the wxObject::m_refData pointer.
286 @see Ref(), UnRef(), GetRefData(), wxObjectRefData
288 void SetRefData(wxObjectRefData
* data
);
291 Decrements the reference count in the associated data, and if it is zero,
294 The wxObject::m_refData member is set to @NULL.
296 @see Ref(), SetRefData(), GetRefData(), wxObjectRefData
301 Ensure that this object's data is not shared with any other object.
303 If we have no data, it is created using CreateRefData() below,
304 if we have shared data, it is copied using CloneRefData(),
305 otherwise nothing is done.
310 The @e delete operator is defined for debugging versions of the library only,
311 when the identifier @c __WXDEBUG__ is defined.
313 It takes over memory deallocation, allowing wxDebugContext operations.
315 void operator delete(void *buf
);
318 The @e new operator is defined for debugging versions of the library only, when
319 the identifier @c __WXDEBUG__ is defined.
321 It takes over memory allocation, allowing wxDebugContext operations.
323 void* operator new(size_t size
, const wxString
& filename
= NULL
, int lineNum
= 0);
328 Pointer to an object which is the object's reference-counted data.
330 @see Ref(), UnRef(), SetRefData(), GetRefData(), wxObjectRefData
332 wxObjectRefData
* m_refData
;
340 This class stores meta-information about classes.
342 Instances of this class are not generally defined directly by an application,
343 but indirectly through use of macros such as ::DECLARE_DYNAMIC_CLASS and
344 ::IMPLEMENT_DYNAMIC_CLASS.
349 @see @ref overview_rtti_classinfo, wxObject
355 Constructs a wxClassInfo object.
357 The supplied macros implicitly construct objects of this class, so there is no
358 need to create such objects explicitly in an application.
360 wxClassInfo(const wxChar
* className
,
361 const wxClassInfo
* baseClass1
,
362 const wxClassInfo
* baseClass2
,
363 int size
, wxObjectConstructorFn fn
);
366 Creates an object of the appropriate kind.
368 @return @NULL if the class has not been declared dynamically creatable
369 (typically, this happens for abstract classes).
371 wxObject
* CreateObject() const;
374 Finds the wxClassInfo object for a class with the given @a name.
376 static wxClassInfo
* FindClass(wxChar
* name
);
379 Returns the name of the first base class (@NULL if none).
381 wxChar
* GetBaseClassName1() const;
384 Returns the name of the second base class (@NULL if none).
386 wxChar
* GetBaseClassName2() const;
389 Returns the string form of the class name.
391 wxChar
* GetClassName() const;
394 Returns the size of the class.
399 Initializes pointers in the wxClassInfo objects for fast execution of IsKindOf().
400 Called in base wxWidgets library initialization.
402 static void InitializeClasses();
405 Returns @true if this class info can create objects of the associated class.
407 bool IsDynamic() const;
410 Returns @true if this class is a kind of (inherits from) the given class.
412 bool IsKindOf(wxClassInfo
* info
);
419 This is helper template class primarily written to avoid memory leaks because of
420 missing calls to wxObjectRefData::DecRef().
422 Despite the name this template can actually be used as a smart pointer for any
423 class implementing the reference counting interface which only consists of the two
424 methods @b T::IncRef() and @b T::DecRef().
426 The difference to wxSharedPtr<T> is that wxObjectDataPtr<T> relies on the reference
427 counting to be in the class pointed to where instead wxSharedPtr<T> implements the
428 reference counting itself.
434 class MyCarRefData: public wxObjectRefData
437 MyCarRefData() { m_price = 0; }
439 MyCarRefData( const MyCarRefData& data )
442 m_price = data.m_price;
445 void SetPrice( int price ) { m_price = price; }
446 int GetPrice() { return m_price; }
455 MyCar( int price ) : m_data( new MyCarRefData )
457 m_data->SetPrice( price );
460 MyCar& operator =( const MyCar& tocopy )
462 m_data = tocopy.m_data;
466 bool operator == ( const MyCar& other ) const
468 if (m_data.get() == other.m_data.get()) return true;
469 return (m_data->GetPrice() == other.m_data->GetPrice());
472 void SetPrice( int price )
475 m_data->SetPrice( price );
480 return m_data->GetPrice();
483 wxObjectDataPtr<MyCarRefData> m_data;
488 if (m_data->GetRefCount() == 1)
491 m_data.reset( new MyCarRefData( *m_data ) );
498 @category{rtti,smartpointers}
500 @see wxObject, wxObjectRefData, @ref overview_refcount, wxSharedPtr<T>,
501 wxScopedPtr<T>, wxWeakRef<T>
503 class wxObjectDataPtr
<T
>
509 @a ptr is a pointer to the reference counted object to which this class points.
510 If @a ptr is not NULL @b T::IncRef() will be called on the object.
512 wxObjectDataPtr
<T
>(T
* ptr
= NULL
);
515 This copy constructor increases the count of the reference counted object to
516 which @a tocopy points and then this class will point to, as well.
518 wxObjectDataPtr
<T
>(const wxObjectDataPtr
<T
>& tocopy
);
522 Decreases the reference count of the object to which this class points.
524 ~wxObjectDataPtr
<T
>();
527 Gets a pointer to the reference counted object to which this class points.
532 Reset this class to ptr which points to a reference counted object and
533 calls T::DecRef() on the previously owned object.
538 Conversion to a boolean expression (in a variant which is not
539 convertable to anything but a boolean expression).
541 If this class contains a valid pointer it will return @true, if it contains
542 a @NULL pointer it will return @false.
544 operator unspecified_bool_type() const;
547 Returns a reference to the object.
549 If the internal pointer is @NULL this method will cause an assert in debug mode.
551 T
& operator*() const;
554 Returns a pointer to the reference counted object to which this class points.
556 If this the internal pointer is @NULL, this method will assert in debug mode.
558 T
* operator->() const;
564 wxObjectDataPtr
<T
>& operator=(const wxObjectDataPtr
<T
>& tocopy
);
565 wxObjectDataPtr
<T
>& operator=(T
* ptr
);
571 // ============================================================================
572 // Global functions/macros
573 // ============================================================================
575 /** @ingroup group_funcmacro_rtti */
579 Returns a pointer to the wxClassInfo object associated with this class.
583 #define CLASSINFO( className )
586 Used inside a class declaration to declare that the class should be made
587 known to the class hierarchy, but objects of this class cannot be created
588 dynamically. The same as DECLARE_ABSTRACT_CLASS().
592 #define DECLARE_CLASS( className )
595 Used inside a class declaration to declare that the class should be
596 made known to the class hierarchy, but objects of this class cannot be created
597 dynamically. The same as DECLARE_CLASS().
604 class wxCommand: public wxObject
606 DECLARE_ABSTRACT_CLASS(wxCommand)
615 #define DECLARE_ABSTRACT_CLASS( className )
618 Used inside a class declaration to make the class known to wxWidgets RTTI
619 system and also declare that the objects of this class should be
620 dynamically creatable from run-time type information. Notice that this
621 implies that the class should have a default constructor, if this is not
622 the case consider using DECLARE_CLASS().
629 class wxFrame: public wxWindow
631 DECLARE_DYNAMIC_CLASS(wxFrame)
634 const wxString& frameTitle;
640 #define DECLARE_DYNAMIC_CLASS( className )
643 Used in a C++ implementation file to complete the declaration of a class
644 that has run-time type information. The same as IMPLEMENT_ABSTRACT_CLASS().
648 #define IMPLEMENT_CLASS( className, baseClassName )
651 Used in a C++ implementation file to complete the declaration of a class
652 that has run-time type information and two base classes. The same as
653 IMPLEMENT_ABSTRACT_CLASS2().
657 #define IMPLEMENT_CLASS2( className, baseClassName1, baseClassName2 )
660 Used in a C++ implementation file to complete the declaration of a class
661 that has run-time type information. The same as IMPLEMENT_CLASS().
668 IMPLEMENT_ABSTRACT_CLASS(wxCommand, wxObject)
670 wxCommand::wxCommand(void)
676 #define IMPLEMENT_ABSTRACT_CLASS( className, baseClassName )
679 Used in a C++ implementation file to complete the declaration of a class
680 that has run-time type information and two base classes. The same as
685 #define IMPLEMENT_ABSTRACT_CLASS2( className, baseClassName1, baseClassName2 )
688 Used in a C++ implementation file to complete the declaration of a class
689 that has run-time type information, and whose instances can be created
697 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
699 wxFrame::wxFrame(void)
705 #define IMPLEMENT_DYNAMIC_CLASS( className, baseClassName )
708 Used in a C++ implementation file to complete the declaration of a class
709 that has run-time type information, and whose instances can be created
710 dynamically. Use this for classes derived from two base classes.
714 #define IMPLEMENT_DYNAMIC_CLASS2( className, baseClassName1, baseClassName2 )
717 Same as @c const_cast<T>(x) if the compiler supports const cast or @c (T)x for
718 old compilers. Unlike wxConstCast(), the cast it to the type @c T and not to
719 <tt>T *</tt> and also the order of arguments is the same as for the standard cast.
723 @see wx_reinterpret_cast(), wx_static_cast()
725 #define wx_const_cast(T, x)
728 Same as @c reinterpret_cast<T>(x) if the compiler supports reinterpret cast or
729 @c (T)x for old compilers.
733 @see wx_const_cast(), wx_static_cast()
735 #define wx_reinterpret_cast(T, x)
738 Same as @c static_cast<T>(x) if the compiler supports static cast or @c (T)x for
739 old compilers. Unlike wxStaticCast(), there are no checks being done and
740 the meaning of the macro arguments is exactly the same as for the standard
741 static cast, i.e. @a T is the full type name and star is not appended to it.
745 @see wx_const_cast(), wx_reinterpret_cast(), wx_truncate_cast()
747 #define wx_static_cast(T, x)
750 This case doesn’t correspond to any standard cast but exists solely to make
751 casts which possibly result in a truncation of an integer value more
756 #define wx_truncate_cast(T, x)
759 This macro expands into <tt>const_cast<classname *>(ptr)</tt> if the compiler
760 supports const_cast or into an old, C-style cast, otherwise.
764 @see wx_const_cast(), wxDynamicCast(), wxStaticCast()
766 #define wxConstCast( ptr, classname )
769 This macro returns the pointer @e ptr cast to the type @e classname * if
770 the pointer is of this type (the check is done during the run-time) or
771 @NULL otherwise. Usage of this macro is preferred over obsoleted
772 wxObject::IsKindOf() function.
774 The @e ptr argument may be @NULL, in which case @NULL will be returned.
781 wxWindow *win = wxWindow::FindFocus();
782 wxTextCtrl *text = wxDynamicCast(win, wxTextCtrl);
785 // a text control has the focus...
789 // no window has the focus or it is not a text control
793 @see @ref overview_rtti, wxDynamicCastThis(), wxConstCast(), wxStaticCast()
795 #define wxDynamicCast( ptr, classname )
798 This macro is equivalent to <tt>wxDynamicCast(this, classname)</tt> but the latter provokes
799 spurious compilation warnings from some compilers (because it tests whether
800 @c this pointer is non-@NULL which is always true), so this macro should be
807 #define wxDynamicCastThis( classname )
810 This macro checks that the cast is valid in debug mode (an assert failure
811 will result if wxDynamicCast(ptr, classname) == @NULL) and then returns the
812 result of executing an equivalent of <tt>static_cast<classname *>(ptr)</tt>.
816 @see wx_static_cast(), wxDynamicCast(), wxConstCast()
818 #define wxStaticCast( ptr, classname )
821 Creates and returns an object of the given class, if the class has been
822 registered with the dynamic class system using DECLARE... and IMPLEMENT...
827 wxObject
*wxCreateDynamicObject(const wxString
& className
);
831 /** @ingroup group_funcmacro_debug */
835 This is defined in debug mode to be call the redefined new operator
836 with filename and line number arguments. The definition is:
839 #define WXDEBUG_NEW new(__FILE__,__LINE__)
842 In non-debug mode, this is defined as the normal new operator.
846 #define WXDEBUG_NEW( arg )