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