container.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: container.h
  3. // Purpose: topic overview
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @page overview_container Container Classes
  9. @tableofcontents
  10. For historical reasons, wxWidgets uses custom container classes internally.
  11. This was unfortunately unavoidable during a long time when the standard library
  12. wasn't widely available and can't be easily changed even now that it is for
  13. compatibility reasons. If you are building your own version of the library and
  14. don't care about compatibility nor slight (less than 5%) size penalty imposed
  15. by the use of STL classes, you may choose to use the "STL" build of wxWidgets
  16. in which these custom classes are replaced with their standard counterparts and
  17. only read the section @ref overview_container_std explaining how to do it.
  18. Otherwise you will need to know about the custom wxWidgets container classes
  19. such as wxList<T> and wxArray<T> if only to use wxWidgets functions that work
  20. with them, e.g. wxWindow::GetChildren(), and you should find the information
  21. about using these classes below useful.
  22. Notice that we recommend that you use standard classes directly in your own
  23. code instead of the container classes provided by wxWidgets in any case as the
  24. standard classes are easier to use and may also be safer because of extra
  25. run-time checks they may perform as well as more efficient.
  26. Finally notice that recent versions of wxWidgets also provide standard-like
  27. classes such as wxVector<T>, wxStack<T> or wxDList which can be used exactly
  28. like the std::vector<T>, std::stack<T> and std::list<T*>, respectively, and
  29. actually are just typedefs for the corresponding types if wxWidgets is compiled
  30. in STL mode. These classes could be useful if you wish to avoid the use of the
  31. standard library in your code for some reason.
  32. To summarize, you should use the standard container classes such as
  33. std::vector<T> and std::list<T> if possible and wxVector<T> or wxDList<T> if
  34. it isn't and only use legacy wxWidgets containers such as wxArray<T> and
  35. wxList<T> when you must, i.e. when you use a wxWidgets function taking or
  36. returning a container of such type.
  37. @see @ref group_class_containers
  38. @section overview_container_legacy Legacy Classes
  39. The list classes in wxWidgets are doubly-linked lists which may either own the
  40. objects they contain (meaning that the list deletes the object when it is
  41. removed from the list or the list itself is destroyed) or just store the
  42. pointers depending on whether or not you called wxList<T>::DeleteContents()
  43. method.
  44. Dynamic arrays resemble C arrays but with two important differences: they
  45. provide run-time range checking in debug builds and they automatically expand
  46. the allocated memory when there is no more space for new items. They come in
  47. two sorts: the "plain" arrays which store either built-in types such as "char",
  48. "int" or "bool" or the pointers to arbitrary objects, or "object arrays" which
  49. own the object pointers to which they store.
  50. For the same portability reasons, the container classes implementation in
  51. wxWidgets don't use templates, but are rather based on C preprocessor i.e. are
  52. implemented using the macros: WX_DECLARE_LIST() and WX_DEFINE_LIST() for the
  53. linked lists and WX_DECLARE_ARRAY(), WX_DECLARE_OBJARRAY() and
  54. WX_DEFINE_OBJARRAY() for the dynamic arrays.
  55. The "DECLARE" macro declares a new container class containing the elements of
  56. given type and is needed for all three types of container classes: lists,
  57. arrays and objarrays. The "DEFINE" classes must be inserted in your program in
  58. a place where the @e full declaration of container element class is in scope
  59. (i.e. not just forward declaration), otherwise destructors of the container
  60. elements will not be called!
  61. As array classes never delete the items they contain anyhow, there is no
  62. WX_DEFINE_ARRAY() macro for them.
  63. Examples of usage of these macros may be found in wxList<T> and wxArray<T>
  64. documentation.
  65. Finally, wxWidgets predefines several commonly used container classes. wxList
  66. is defined for compatibility with previous versions as a list containing
  67. wxObjects and wxStringList as a list of C-style strings (char *), both of these
  68. classes are deprecated and should not be used in new programs. The following
  69. array classes are defined: wxArrayInt, wxArrayLong, wxArrayPtrVoid and
  70. wxArrayString. The first three store elements of corresponding types, but
  71. wxArrayString is somewhat special: it is an optimized version of wxArray which
  72. uses its knowledge about wxString reference counting schema.
  73. @section overview_container_std STL Build
  74. To build wxWidgets with the standard containers you need to set
  75. wxUSE_STD_CONTAINERS option to 1 in @c wx/msw/setup.h for wxMSW builds or
  76. specify @c --enable-std_containers option to configure (which is also
  77. implicitly enabled by @c --enable-stl option) in Unix builds.
  78. The standard container build is mostly, but not quite, compatible with the
  79. default one. Here are the most important differences:
  80. - wxList::compatibility_iterator must be used instead of wxList::Node* when
  81. iterating over the list contents. The compatibility_iterator class has the
  82. same semantics as a Node pointer but it is an object and not a pointer, so
  83. you need to write
  84. @code
  85. for ( wxWindowList::compatibility_iterator it = list.GetFirst();
  86. it;
  87. it = it->GetNext() )
  88. ...
  89. @endcode
  90. instead of the old
  91. @code
  92. for ( wxWindowList::Node *n = list.GetFirst(); n; n = n->GetNext() )
  93. ...
  94. @endcode
  95. - wxSortedArrayString and wxArrayString are separate classes now and the
  96. former doesn't derive from the latter. If you need to convert a sorted array
  97. to a normal one, you must copy all the elements. Alternatively, you may
  98. avoid the use of wxSortedArrayString by using a normal array and calling its
  99. Sort() method when needed.
  100. - WX_DEFINE_ARRAY_INT(bool) cannot be used because of the differences in
  101. std::vector<bool> specialization compared with the generic std::vector<>
  102. class. Please either use std::vector<bool> directly or use an integer array
  103. instead.
  104. */