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