⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arc_7049.htm

📁 C++标准库 C++标准库 C++标准库 C++标准库
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<P>This section will give a more detailed description of the iostreams software architecture, including the classes and their inheritance relationship and respective responsibilities.  If you would rather start using iostreams directly, go on to <A HREF="for_5394.htm">Section 2.3</A>.</P><A NAME="2.2.4.1"><H4>2.2.4.1 The Internal Structure of the Formatting Layer</H4></A><P>Classes that belong to the formatting layer are often referred to as the <I>stream</I> <I>classes</I>.  Figure 7 illustrates the class hierarchy of all the stream classes:</P><H4>Figure 7.  Internal class hierarchy of the formatting layer <A HREF="endnote2.htm#fn14">[14]</A></H4><BR><IMG SRC="images/image24.gif"><P>Let us discuss in more detail the components and characteristics of the class hierarchy given in the figure:</P><UL><LI><P><B>The Iostreams Base Class <I>ios_base</I></B>.  This class is the base class of all stream classes.  Independent of character type, it encapsulates information that is needed by all streams.  This information includes:</P><UL><LI><P>Control information for parsing and formatting;</P></LI><LI><P>Additional information for the user's special needs (a way to extend iostreams, as we will see later on);</P></LI><LI><P>The locale imbued on the stream;</P></LI></UL><P>Additionally, <B><I><B><I>ios_base</I></B></B></I> defines several types that are used by all stream classes, such as format flags, status bits, open mode, exception class, etc.</P></LI><LI><P><B>The Iostreams Character Type-Dependent Base Class.</B>  Here is the virtual base class for the stream classes:</P><P><SAMP>basic_ios&#60;class charT, class traits=char_traits&#60;charT> ></SAMP></P><UL><LI><P>The class holds a pointer to the stream buffer, and</P></LI><LI><P>State information that reflects the integrity of the stream buffer;</P></LI></UL><P>Note that <B><I><B><I>basic_ios&#60;></I></B></B></I> is a class template taking two parameters, the type of character handled by the stream, and the <I>character traits</I>.</P><P>The type of character can be type <SAMP>char</SAMP> for single-byte characters, or type <SAMP>wchar_t</SAMP> for wide characters, or any other user-defined character type.  There are instantiations for <SAMP>char</SAMP> and <SAMP>wchar_t</SAMP> provided by the Standard C++ Library.</P><P>For convenience, there are typedefs for these instantiations:</P><P><SAMP>typedef basic_ios&#60;char> </SAMP><B>ios</B> and <SAMP>typedef basic_ios&#60;wchar_t> </SAMP><B>wios</B></P><P>Note that <B>ios</B> is not a class anymore, as it was in the traditional iostreams.  If you have existing programs that use the old iostreams, they may no longer be compilable with the standard iostreams.  (See list of incompatibilities in <A HREF="how_0298.htm">section 2.1</A>4)</P></LI><LI><P><B>Character Traits.</B>  These describe the properties of a character type.  Many things change with the character type, such as:</P><UL><LI><P><B>The end-of-file value</B>.  For type <SAMP>char,</SAMP> the end-of file value is represented by an integral constant called <SAMP>EOF</SAMP>.  For type <SAMP>wchar_t</SAMP>, there is a constant defined that is called <SAMP>WEOF</SAMP>.  For an arbitrary user-defined character type, the associated character traits define what the end-of-file value for this particular character type is.</P></LI><LI><P><B>The type of the </B><SAMP>EOF</SAMP> <B>value</B>.  This needs to be a type that can hold the <SAMP>EOF</SAMP> value.  For example, for single-byte characters, this type is <SAMP>int</SAMP>, different from the actual character type <SAMP>char</SAMP>.</P></LI><LI><P><B>The equality of two characters</B>.  For an exotic user-defined character type, the equality of two characters might mean something different from just bit-wise equality.  Here you can define it.</P></LI></UL><P>A complete list of character traits is given in the string section that explains character traits.</P><P>There are specializations defined for type <SAMP>char</SAMP> and <SAMP>wchar_t</SAMP>.  In general, this class template is not meant to be instantiated for a character type.  You should always define class template specializations.</P><P>Fortunately, the Standard C++ Library is designed to make the most common cases the easiest.  The traits template parameter has a sensible default value, so usually you don't have to bother with character traits at all.</P></LI><LI><P><B>The Input and Output Streams.  </B>The three stream classes for input and output are:</P><PRE>basic_istream &#60;class charT, class traits=char_traits&#60;charT> > basic_ostream &#60;class charT, class traits=char_traits&#60;charT> > basic_iostream&#60;class charT, class traits=char_traits&#60;charT> ></PRE><P>Class <SAMP>istream</SAMP> handles input, class <SAMP>ostream</SAMP> is for output.  Class <SAMP>iostream</SAMP> deals with input <I>and</I> output; such a stream is called a <I>bidirectional</I> stream.</P><P>The three stream classes define functions for parsing and formatting, which are overloaded versions of <SAMP>operator>>()</SAMP> for input, called <I>extractors</I>, and overloaded versions of <SAMP>operator&#60;&#60;()</SAMP> for output, called <I>inserters</I>.</P><P>Additionally, there are member functions for unformatted input and output, like <SAMP>get()</SAMP>, <SAMP>put()</SAMP>, etc.</P></LI><LI><P><B>The File Streams.  </B>The file stream classes support input and output to and from files.  They are: </P><PRE>basic_ifstream&#60;class charT, class traits=char_traits&#60;charT> > basic_ofstream&#60;class charT, class traits=char_traits&#60;charT> > basic_fstream&#60;class charT, class traits=char_traits&#60;charT> ></PRE><P>There are functions for opening and closing files, similar to the C functions <SAMP>fopen()</SAMP> and <SAMP>fclose()</SAMP>.  Internally they use a special kind of stream buffer, called a <I>file buffer,</I> to control the transport of characters to/from the associated file.  The function of the file streams is illustrated in Figure 8:</P><H4>Figure 8.  File I/O</H4><BR><IMG SRC="images/image25.gif"></LI><LI><P><B>The String Streams.</B>  The string stream classes support in-memory I/O; that is, reading and writing to a string held in memory.  They are:</P><PRE>basic_istringstream&#60;class charT, class traits=char_traits&#60;charT> >basic_ostringstream&#60;class charT, class traits=char_traits&#60;charT> >basic_stringstream&#60;class charT, class traits=char_traits&#60;charT> ></PRE><P>There are functions for getting and setting the string to be used as a buffer.  Internally a specialized stream buffer is used.  In this particular case, the buffer and the external device are the same.  Figure 9 below illustrates how the string stream classes work:</P><H4>Figure 9.  In-memory I/O</H4><BR><IMG SRC="images/image26.gif"></LI></UL><A NAME="2.2.4.2"><H4>2.2.4.2 The Transport Layer's Internal Structure</H4></A><P>Classes of the transport layer are often referred to as the stream buffer classes.  Figure 10 gives the class hierarchy of all stream buffer classes:</P><H4>Figure 10.  Hierarchy of the transport layer</H4><BR><IMG SRC="images/image27.gif"><P>The stream buffer classes are responsible for transfer of characters from and to external devices.  </P><UL><LI><P><B>The Stream Buffer.  </B>This class represents an abstract stream buffer:</P><P><SAMP>basic_streambuf&#60;class charT, class traits=char_traits&#60;charT> ></SAMP></P><P>It does not have any knowledge about the external device.  Instead, it defines two virtual functions, <SAMP>overflow()</SAMP> and <SAMP>underflow()</SAMP>, to perform the actual transport.  These two functions have knowledge of the peculiarities of the external device they are connected to.  They have to be overwritten by all concrete stream buffer classes, like file and string buffers.</P><P>The stream buffer class maintains two character sequences:  the <I>get area</I>, which represents the input sequence read from an external device, and the <I>put area</I>, which is the output sequence to be written to the device.  There are functions for providing the next character from the buffer, such as <SAMP>sgetc()</SAMP>, etc.  They are typically called by the formatting layer in order to receive characters for parsing.  Accordingly, there are also functions for placing the next character into the buffer, such as <SAMP>sputc()</SAMP>, etc.</P><P>A stream buffer also carries a locale object.</P></LI><LI><P><B>The File Buffer.</B>  The file buffer classes associate the input and output sequences with a file.  A file buffer takes the form:</P><P><SAMP>basic_filebuf&#60;class charT, class traits=char_traits&#60;charT> ></SAMP></P><P>The file buffer has functions like <SAMP>open()</SAMP> and <SAMP>close()</SAMP>.  The file buffer class inherits a locale object from its stream buffer base class.  It uses the locale's code conversion facet for transforming the external character encoding to the encoding used internally.  Figure 11 shows how the file buffer works:</P><H4>Figure 11.  Character code conversion performed by the file buffer</H4><BR><IMG SRC="images/image28.gif"></LI><LI><P><B>The String Stream Buffer.</B>  These classes implement the in-memory I/O:</P><P><SAMP>basic_stringbuf&#60;class charT, class traits=char_traits&#60;charT> ></SAMP></P><P>With string buffers, the internal buffer and the external device are one and the same.  The internal buffer is dynamic, in that it is extended if necessary to hold all the characters written to it.  You can obtain copies of the internally held buffer, and you can provide a string to be copied into the internal buffer.</P></LI></UL><A NAME="2.2.4.3"><H4>2.2.4.3 Collaboration of Streams and Stream Buffers</H4></A><P>The base class <B><I><B><I>basic_ios&#60;></I></B></B></I> holds a pointer to a stream buffer.  The derived stream classes, like file and string streams, contain a file or string buffer object.  The stream buffer pointer of the base class refers to this embedded object.  This architecture is illustrated in Figure 12 below: </P><H4>Figure 12.  How an input file stream uses a file buffer</H4><BR><IMG SRC="images/image29.gif"><P>Stream buffers can be used independently of streams, as for unformatted I/O, for example.  However, streams always need a stream buffer.</P><A NAME="2.2.4.4"><H4>2.2.4.4 Collaboration of Locales and Iostreams</H4></A><P>The base class <B><I><B><I>ios_base</I></B></B></I> contains a locale object.  The formatting and parsing functions defined by the derived stream classes use the numeric facets of that locale.</P><P>The class <B><I><B><I>basic_ios&#60;charT></I></B></B></I> holds a pointer to the stream buffer.  This stream buffer has a locale object, too, usually a copy of the same locale object used by the functions of the stream classes.  The stream buffer's input and output functions use the code conversion facet of the attached locale.  Figure 13 below illustrates the architecture:</P><H4>Figure 13.  How an input file stream uses locales</H4><BR><IMG SRC="images/image30.gif"><HR><A HREF="how_0298.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc2.htm"><IMG SRC="images/toc.gif"></A><A HREF="for_5394.htm"><IMG SRC="images/next.gif"></A><P>&copy;Copyright 1996, Rogue Wave Software, Inc.</P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -