]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/object.h
Doc and comment cleanup, fixes, tweaks
[wxWidgets.git] / interface / wx / 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
12 This class is used to store reference-counted data.
13
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.
16
17 @b Example:
18
19 @code
20 // include file
21
22 class MyCar: public wxObject
23 {
24 public:
25 MyCar() { }
26 MyCar( int price );
27
28 bool IsOk() const { return m_refData != NULL; }
29
30 bool operator == ( const MyCar& car ) const;
31 bool operator != (const MyCar& car) const { return !(*this == car); }
32
33 void SetPrice( int price );
34 int GetPrice() const;
35
36 protected:
37 virtual wxObjectRefData *CreateRefData() const;
38 virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
39
40 DECLARE_DYNAMIC_CLASS(MyCar)
41 };
42
43
44 // implementation
45
46 class MyCarRefData: public wxObjectRefData
47 {
48 public:
49 MyCarRefData()
50 {
51 m_price = 0;
52 }
53
54 MyCarRefData( const MyCarRefData& data )
55 : wxObjectRefData()
56 {
57 m_price = data.m_price;
58 }
59
60 bool operator == (const MyCarRefData& data) const
61 {
62 return m_price == data.m_price;
63 }
64
65 int m_price;
66 };
67
68
69 #define M_CARDATA ((MyCarRefData *)m_refData)
70
71 IMPLEMENT_DYNAMIC_CLASS(MyCar,wxObject)
72
73 MyCar::MyCar( int price )
74 {
75 m_refData = new MyCarRefData();
76 M_CARDATA->m_price = price;
77 }
78
79 wxObjectRefData *MyCar::CreateRefData() const
80 {
81 return new MyCarRefData;
82 }
83
84 wxObjectRefData *MyCar::CloneRefData(const wxObjectRefData *data) const
85 {
86 return new MyCarRefData(*(MyCarRefData *)data);
87 }
88
89 bool MyCar::operator == ( const MyCar& car ) const
90 {
91 if (m_refData == car.m_refData) return true;
92
93 if (!m_refData || !car.m_refData) return false;
94
95 return ( *(MyCarRefData*)m_refData == *(MyCarRefData*)car.m_refData );
96 }
97
98 void MyCar::SetPrice( int price )
99 {
100 UnShare();
101
102 M_CARDATA->m_price = price;
103 }
104
105 int MyCar::GetPrice() const
106 {
107 wxCHECK_MSG( IsOk(), -1, "invalid car" );
108
109 return (M_CARDATA->m_price);
110 }
111 @endcode
112
113
114 @library{wxbase}
115 @category{rtti}
116
117 @see wxObject, wxObjectDataPtr<T>, @ref overview_refcount
118 */
119 class wxObjectRefData
120 {
121 protected:
122 /**
123 Destructor.
124
125 It's declared @c protected so that wxObjectRefData instances
126 will never be destroyed directly but only as result of a DecRef() call.
127 */
128 ~wxObjectRefData();
129
130 public:
131 /**
132 Default constructor. Initialises the internal reference count to 1.
133 */
134 wxObjectRefData();
135
136 /**
137 Decrements the reference count associated with this shared data and, if
138 it reaches zero, destroys this instance of wxObjectRefData releasing its
139 memory.
140
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
143 valid anymore.
144 */
145 void DecRef();
146
147 /**
148 Returns the reference count associated with this shared data.
149
150 When this goes to zero during a DecRef() call, the object will auto-free itself.
151 */
152 int GetRefCount() const;
153
154 /**
155 Increments the reference count associated with this shared data.
156 */
157 void IncRef();
158 };
159
160
161
162 /**
163 @class wxObject
164
165 This is the root class of many of the wxWidgets classes.
166
167 It declares a virtual destructor which ensures that destructors get called
168 for all derived class objects where necessary.
169
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.
173
174 The class contains optional debugging versions of @b new and @b delete, which
175 can help trace memory allocation and deallocation problems.
176
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").
180
181 @library{wxbase}
182 @category{rtti}
183
184 @see wxClassInfo, @ref overview_debugging, wxObjectRefData
185 */
186 class wxObject
187 {
188 public:
189
190 wxObject();
191
192 /**
193 Copy ctor.
194 */
195 wxObject(const wxObject& other);
196
197
198 /**
199 Destructor.
200
201 Performs dereferencing, for those objects that use reference counting.
202 */
203 virtual ~wxObject();
204
205 /**
206 A virtual function that may be redefined by derived classes to allow dumping of
207 memory states.
208
209 This function is only defined in debug build and exists only if @c __WXDEBUG__
210 is defined.
211
212 @param stream
213 Stream on which to output dump information.
214
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.
221 */
222 void Dump(ostream& stream);
223
224 /**
225 This virtual function is redefined for every class that requires run-time
226 type information, when using the ::DECLARE_CLASS macro (or similar).
227 */
228 virtual wxClassInfo* GetClassInfo() const;
229
230 /**
231 Returns the wxObject::m_refData pointer, i.e. the data referenced by this object.
232
233 @see Ref(), UnRef(), wxObject::m_refData, SetRefData(), 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 Example:
242
243 @code
244 bool tmp = obj->IsKindOf(CLASSINFO(wxFrame));
245 @endcode
246
247 @param info
248 A pointer to a class information object, which may be obtained
249 by using the ::CLASSINFO macro.
250
251 @return @true if the class represented by info is the same class as this
252 one or is derived from it.
253 */
254 bool IsKindOf(const wxClassInfo* info) const;
255
256 /**
257 Returns @true if this object has the same data pointer as @a obj.
258
259 Notice that @true is returned if the data pointers are @NULL in both objects.
260
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.
263
264 @see @ref overview_refcount
265 */
266 bool IsSameAs(const wxObject& obj) const;
267
268 /**
269 Makes this object refer to the data in @a clone.
270
271 @param clone
272 The object to 'clone'.
273
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.
278
279 @see UnRef(), SetRefData(), GetRefData(), wxObjectRefData
280 */
281 void Ref(const wxObject& clone);
282
283 /**
284 Sets the wxObject::m_refData pointer.
285
286 @see Ref(), UnRef(), GetRefData(), 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
294 The wxObject::m_refData member is set to @NULL.
295
296 @see Ref(), SetRefData(), 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
313 It takes over memory deallocation, allowing wxDebugContext operations.
314 */
315 void operator delete(void *buf);
316
317 /**
318 The @e new operator is defined for debugging versions of the library only, when
319 the identifier @c __WXDEBUG__ is defined.
320
321 It takes over memory allocation, allowing wxDebugContext operations.
322 */
323 void* operator new(size_t size, const wxString& filename = NULL, int lineNum = 0);
324
325 protected:
326
327 /**
328 Pointer to an object which is the object's reference-counted data.
329
330 @see Ref(), UnRef(), SetRefData(), GetRefData(), wxObjectRefData
331 */
332 wxObjectRefData* m_refData;
333 };
334
335
336
337 /**
338 @class wxClassInfo
339
340 This class stores meta-information about classes.
341
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.
345
346 @library{wxbase}
347 @category{rtti}
348
349 @see @ref overview_rtti_classinfo, wxObject
350 */
351 class wxClassInfo
352 {
353 public:
354 /**
355 Constructs a wxClassInfo object.
356
357 The supplied macros implicitly construct objects of this class, so there is no
358 need to create such objects explicitly in an application.
359 */
360 wxClassInfo(const wxChar* className,
361 const wxClassInfo* baseClass1,
362 const wxClassInfo* baseClass2,
363 int size, wxObjectConstructorFn fn);
364
365 /**
366 Creates an object of the appropriate kind.
367
368 @return @NULL if the class has not been declared dynamically creatable
369 (typically, this happens for abstract classes).
370 */
371 wxObject* CreateObject() const;
372
373 /**
374 Finds the wxClassInfo object for a class with the given @a name.
375 */
376 static wxClassInfo* FindClass(wxChar* name);
377
378 /**
379 Returns the name of the first base class (@NULL if none).
380 */
381 wxChar* GetBaseClassName1() const;
382
383 /**
384 Returns the name of the second base class (@NULL if none).
385 */
386 wxChar* GetBaseClassName2() const;
387
388 /**
389 Returns the string form of the class name.
390 */
391 wxChar* GetClassName() const;
392
393 /**
394 Returns the size of the class.
395 */
396 int GetSize() const;
397
398 /**
399 Initializes pointers in the wxClassInfo objects for fast execution of IsKindOf().
400 Called in base wxWidgets library initialization.
401 */
402 static void InitializeClasses();
403
404 /**
405 Returns @true if this class info can create objects of the associated class.
406 */
407 bool IsDynamic() const;
408
409 /**
410 Returns @true if this class is a kind of (inherits from) the given class.
411 */
412 bool IsKindOf(wxClassInfo* info);
413 };
414
415
416
417 /**
418
419 This is helper template class primarily written to avoid memory leaks because of
420 missing calls to wxObjectRefData::DecRef().
421
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().
425
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.
429
430
431 @b Example:
432
433 @code
434 class MyCarRefData: public wxObjectRefData
435 {
436 public:
437 MyCarRefData() { m_price = 0; }
438
439 MyCarRefData( const MyCarRefData& data )
440 : wxObjectRefData()
441 {
442 m_price = data.m_price;
443 }
444
445 void SetPrice( int price ) { m_price = price; }
446 int GetPrice() { return m_price; }
447
448 protected:
449 int m_price;
450 };
451
452 class MyCar
453 {
454 public:
455 MyCar( int price ) : m_data( new MyCarRefData )
456 {
457 m_data->SetPrice( price );
458 }
459
460 MyCar& operator =( const MyCar& tocopy )
461 {
462 m_data = tocopy.m_data;
463 return *this;
464 }
465
466 bool operator == ( const MyCar& other ) const
467 {
468 if (m_data.get() == other.m_data.get()) return true;
469 return (m_data->GetPrice() == other.m_data->GetPrice());
470 }
471
472 void SetPrice( int price )
473 {
474 UnShare();
475 m_data->SetPrice( price );
476 }
477
478 int GetPrice() const
479 {
480 return m_data->GetPrice();
481 }
482
483 wxObjectDataPtr<MyCarRefData> m_data;
484
485 protected:
486 void UnShare()
487 {
488 if (m_data->GetRefCount() == 1)
489 return;
490
491 m_data.reset( new MyCarRefData( *m_data ) );
492 }
493 };
494 @endcode
495
496
497 @library{wxbase}
498 @category{rtti,smartpointers}
499
500 @see wxObject, wxObjectRefData, @ref overview_refcount, wxSharedPtr<T>,
501 wxScopedPtr<T>, wxWeakRef<T>
502 */
503 class wxObjectDataPtr<T>
504 {
505 public:
506 /**
507 Constructor.
508
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.
511 */
512 wxObjectDataPtr<T>(T* ptr = NULL);
513
514 /**
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.
517 */
518 wxObjectDataPtr<T>(const wxObjectDataPtr<T>& tocopy);
519
520
521 /**
522 Decreases the reference count of the object to which this class points.
523 */
524 ~wxObjectDataPtr<T>();
525
526 /**
527 Gets a pointer to the reference counted object to which this class points.
528 */
529 T* get() const;
530
531 /**
532 Reset this class to ptr which points to a reference counted object and
533 calls T::DecRef() on the previously owned object.
534 */
535 void reset(T *ptr);
536
537 /**
538 Conversion to a boolean expression (in a variant which is not
539 convertable to anything but a boolean expression).
540
541 If this class contains a valid pointer it will return @true, if it contains
542 a @NULL pointer it will return @false.
543 */
544 operator unspecified_bool_type() const;
545
546 /**
547 Returns a reference to the object.
548
549 If the internal pointer is @NULL this method will cause an assert in debug mode.
550 */
551 T& operator*() const;
552
553 /**
554 Returns a pointer to the reference counted object to which this class points.
555
556 If this the internal pointer is @NULL, this method will assert in debug mode.
557 */
558 T* operator->() const;
559
560 //@{
561 /**
562 Assignment operator.
563 */
564 wxObjectDataPtr<T>& operator=(const wxObjectDataPtr<T>& tocopy);
565 wxObjectDataPtr<T>& operator=(T* ptr);
566 //@}
567 };
568
569
570
571 // ============================================================================
572 // Global functions/macros
573 // ============================================================================
574
575 /** @ingroup group_funcmacro_rtti */
576 //@{
577
578 /**
579 Returns a pointer to the wxClassInfo object associated with this class.
580
581 @header{wx/object.h}
582 */
583 #define CLASSINFO( className )
584
585 /**
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().
589
590 @header{wx/object.h}
591 */
592 #define DECLARE_CLASS( className )
593
594 /**
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().
598
599 @header{wx/object.h}
600
601 Example:
602
603 @code
604 class wxCommand: public wxObject
605 {
606 DECLARE_ABSTRACT_CLASS(wxCommand)
607
608 private:
609 ...
610 public:
611 ...
612 };
613 @endcode
614 */
615 #define DECLARE_ABSTRACT_CLASS( className )
616
617 /**
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().
623
624 @header{wx/object.h}
625
626 Example:
627
628 @code
629 class wxFrame: public wxWindow
630 {
631 DECLARE_DYNAMIC_CLASS(wxFrame)
632
633 private:
634 const wxString& frameTitle;
635 public:
636 ...
637 };
638 @endcode
639 */
640 #define DECLARE_DYNAMIC_CLASS( className )
641
642 /**
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().
645
646 @header{wx/object.h}
647 */
648 #define IMPLEMENT_CLASS( className, baseClassName )
649
650 /**
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().
654
655 @header{wx/object.h}
656 */
657 #define IMPLEMENT_CLASS2( className, baseClassName1, baseClassName2 )
658
659 /**
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().
662
663 @header{wx/object.h}
664
665 Example:
666
667 @code
668 IMPLEMENT_ABSTRACT_CLASS(wxCommand, wxObject)
669
670 wxCommand::wxCommand(void)
671 {
672 ...
673 }
674 @endcode
675 */
676 #define IMPLEMENT_ABSTRACT_CLASS( className, baseClassName )
677
678 /**
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
681 IMPLEMENT_CLASS2().
682
683 @header{wx/object.h}
684 */
685 #define IMPLEMENT_ABSTRACT_CLASS2( className, baseClassName1, baseClassName2 )
686
687 /**
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
690 dynamically.
691
692 @header{wx/object.h}
693
694 Example:
695
696 @code
697 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
698
699 wxFrame::wxFrame(void)
700 {
701 ...
702 }
703 @endcode
704 */
705 #define IMPLEMENT_DYNAMIC_CLASS( className, baseClassName )
706
707 /**
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.
711
712 @header{wx/object.h}
713 */
714 #define IMPLEMENT_DYNAMIC_CLASS2( className, baseClassName1, baseClassName2 )
715
716 /**
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.
720
721 @header{wx/defs.h}
722
723 @see wx_reinterpret_cast(), wx_static_cast()
724 */
725 #define wx_const_cast(T, x)
726
727 /**
728 Same as @c reinterpret_cast<T>(x) if the compiler supports reinterpret cast or
729 @c (T)x for old compilers.
730
731 @header{wx/defs.h}
732
733 @see wx_const_cast(), wx_static_cast()
734 */
735 #define wx_reinterpret_cast(T, x)
736
737 /**
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.
742
743 @header{wx/defs.h}
744
745 @see wx_const_cast(), wx_reinterpret_cast(), wx_truncate_cast()
746 */
747 #define wx_static_cast(T, x)
748
749 /**
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
752 readable.
753
754 @header{wx/defs.h}
755 */
756 #define wx_truncate_cast(T, x)
757
758 /**
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.
761
762 @header{wx/defs.h}
763
764 @see wx_const_cast(), wxDynamicCast(), wxStaticCast()
765 */
766 #define wxConstCast( ptr, classname )
767
768 /**
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.
773
774 The @e ptr argument may be @NULL, in which case @NULL will be returned.
775
776 @header{wx/object.h}
777
778 Example:
779
780 @code
781 wxWindow *win = wxWindow::FindFocus();
782 wxTextCtrl *text = wxDynamicCast(win, wxTextCtrl);
783 if ( text )
784 {
785 // a text control has the focus...
786 }
787 else
788 {
789 // no window has the focus or it is not a text control
790 }
791 @endcode
792
793 @see @ref overview_rtti, wxDynamicCastThis(), wxConstCast(), wxStaticCast()
794 */
795 #define wxDynamicCast( ptr, classname )
796
797 /**
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
801 used to avoid them.
802
803 @header{wx/object.h}
804
805 @see wxDynamicCast()
806 */
807 #define wxDynamicCastThis( classname )
808
809 /**
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>.
813
814 @header{wx/object.h}
815
816 @see wx_static_cast(), wxDynamicCast(), wxConstCast()
817 */
818 #define wxStaticCast( ptr, classname )
819
820 /**
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...
823 macros.
824
825 @header{wx/object.h}
826 */
827 wxObject *wxCreateDynamicObject(const wxString& className);
828
829 //@}
830
831 /** @ingroup group_funcmacro_debug */
832 //@{
833
834 /**
835 This is defined in debug mode to be call the redefined new operator
836 with filename and line number arguments. The definition is:
837
838 @code
839 #define WXDEBUG_NEW new(__FILE__,__LINE__)
840 @endcode
841
842 In non-debug mode, this is defined as the normal new operator.
843
844 @header{wx/object.h}
845 */
846 #define WXDEBUG_NEW( arg )
847
848 //@}
849