]>
Commit | Line | Data |
---|---|---|
55b7bba1 GL |
1 | \section{Streams in wxWindows overview}\label{wxstreamoverview} |
2 | ||
3 | Classes: \helpref{wxStreamBase}{wxstreambase}, | |
4 | \helpref{wxStreamBuffer}{wxstreambuffer}, \helpref{wxInputStream}{wxinputstream}, | |
5 | \helpref{wxOutputStream}{wxoutputstream}, | |
6 | \helpref{wxFilterInputStream}{wxfilterinputstream}, | |
7 | \helpref{wxFilterOutputStream}{wxfilteroutputstream} | |
8 | ||
9 | \wxheading{Purpose of wxStream} | |
10 | ||
4130b487 | 11 | We went into troubles with C++ std streams on several platforms: |
55b7bba1 | 12 | they react quite well in most cases, but in multi-threaded case, for example, |
4130b487 RR |
13 | they have many problems. Some Borland Compilers refuse to work at all |
14 | with them and using iostreams on Linux makes writing programs, that are | |
15 | binary compatible across different Linux distributions, impossible. | |
55b7bba1 | 16 | |
4130b487 RR |
17 | Therefore, wxStreams have been added to wxWindows because an application should |
18 | compile and run on all supported platforms and we don't want users depend on release | |
55b7bba1 GL |
19 | X.XX of libg++ or some other compiler to run the program. |
20 | ||
21 | wxStreams is divided in two main parts: | |
22 | \begin{enumerate}\itemsep=0pt | |
23 | \item the core: wxStreamBase, wxStreamBuffer, wxInputStream, wxOutputStream, | |
24 | wxFilterIn/OutputStream | |
25 | \item the "IO" classes: wxSocketIn/OutputStream, wxDataIn/OutputStream, wxFileIn/OutputStream, ... | |
26 | \end{enumerate} | |
27 | ||
28 | wxStreamBase is the base definition of a stream. It defines, for example, | |
29 | the API of OnSysRead, OnSysWrite, OnSysSeek and OnSysTell. These functions are | |
30 | are really implemented by the "IO" classes. | |
31 | wxInputStream and wxOutputStream inherit from it. | |
32 | ||
33 | wxStreamBuffer is a cache manager for wxStreamBase (it manages a stream buffer | |
34 | linked to a stream). One stream can have multiple stream buffers but one stream | |
35 | have always one autoinitialized stream buffer. | |
36 | ||
37 | wxInputStream is the base class for read-only streams. It implements Read, | |
38 | SeekI (I for Input), and all read or IO generic related functions. | |
39 | wxOutputStream does the same thing but it is for write-only streams. | |
40 | ||
41 | wxFilterIn/OutputStream is base class definition for stream filtering. | |
42 | I mean by stream filtering, a stream which does no syscall but filter datas | |
43 | which are passed to it and then pass them to another stream. | |
44 | For example, wxZLibInputStream is an inline stream decompressor. | |
45 | ||
46 | The "IO" classes implements the specific parts of the stream. This could be | |
47 | nothing in the case of wxMemoryIn/OutputStream which bases itself on | |
48 | wxStreamBuffer. This could also be a simple link to the a true syscall | |
49 | (for example read(...), write(...)). | |
50 | ||
51 | \wxheading{Generic usage: an example} | |
52 | ||
53 | About its usage, it's simple. We can take the example of wxFileInputStream and here is a sample | |
54 | code: | |
55 | ||
56 | \begin{verbatim} | |
57 | ... | |
58 | // The constructor initializes the stream buffer and open the file descriptor | |
59 | // associated to the name of the file. | |
4130b487 | 60 | wxFileInputStream in_stream("the_file_to_be_read"); |
55b7bba1 | 61 | |
4130b487 RR |
62 | // Ok, read some bytes ... nb_datas is expressed in bytes. |
63 | in_stream.Read(data, nb_datas); | |
64 | if (in_stream.LastError() != wxStream_NOERROR) { | |
55b7bba1 GL |
65 | // Oh oh, something bad happens. |
66 | // For a complete list, look into the documentation at wxStreamBase. | |
67 | } | |
68 | ||
69 | // You can also inline all like this. | |
4130b487 | 70 | if (in_stream.Read(data, nb_datas).LastError() != wxStream_NOERROR) { |
55b7bba1 GL |
71 | // Do something. |
72 | } | |
73 | ||
74 | // You can also get the last number of bytes REALLY put into the buffer. | |
4130b487 | 75 | size_t really_read = in_stream.LastRead(); |
55b7bba1 GL |
76 | |
77 | // Ok, moves to the beginning of the stream. SeekI returns the last position | |
78 | // in the stream counted from the beginning. | |
4130b487 | 79 | off_t old_position = in_stream.SeekI(0, wxFromBeginning); |
55b7bba1 GL |
80 | |
81 | // What is my current position ? | |
4130b487 | 82 | off_t position = in_stream.TellI(); |
55b7bba1 GL |
83 | |
84 | // wxFileInputStream will close the file descriptor on the destruction. | |
85 | \end{verbatim} | |
86 | ||
87 | \wxheading{Compatibility with c++ stream} | |
88 | ||
89 | As I said previously, we could add a filter stream so it takes an istream | |
90 | argument and builds a wxInputStream from it: I don't think it should | |
91 | be difficult to implement it and it may be available in the fix of wxWindows 2.0. | |
22d6efa8 | 92 |