📄 char_producer.html
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<TITLE>MFC Programmer's SourceBook : STL Programmer's Guide</TITLE>
<META name="description"
content="A freely available implementation
of the C++ Standard Template Library, including
hypertext documentation.">
<META name="keywords"
content="generic programming, STL, standard template library">
</HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
var adcategory = "cpp";
// -->
</SCRIPT>
<body background="../../fancyhome/back.gif" bgcolor="#FFFFFF" >
<SCRIPT LANGUAGE="JavaScript"><!--
var nfrm = location.href.indexOf("_nfrm_");
var validframes = (top.frames.length > 0 && top.frames['ad'] && top.frames['logo'] );
var random = Math.random();
if( !validframes && nfrm == -1 )
{
var dclkPage = "www.codeguru.com/";
if( self.adcategory )
dclkPage += adcategory;
else
dclkPage += "mfc";
document.write('<nolayer><center>');
document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
+ random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
+ 'frameborder=0 scrolling=no bordercolor="#000000">');
document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
+ random + '">');
document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
+ random + '" height=60 width=468>' + '</a>');
document.write('</iframe>');
document.write('</center></nolayer>');
document.write('<layer src="http://ad.doubleclick.net/adl/' + dclkPage +
';ord=' + random + '"></layer>');
document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}
// top.location = "/show.cgi?" + adcategory + "=" + location.pathname;
// -->
</SCRIPT>
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupcHNFCY34AAHqBcNc">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupcHNFCY34AAHqBcNc"></a>
</p>
</noscript>
<BR Clear>
<H1>char_producer</H1>
<Table CellPadding=0 CellSpacing=0 width=100%>
<TR>
<TD Align=left><Img src = "containers.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD>
<TD Align=right><Img src = "type.gif" Alt="" WIDTH = "194" HEIGHT = "39" ></TD>
</TR>
<TR>
<TD Align=left VAlign=top><b>Category</b>: containers</TD>
<TD Align=right VAlign=top><b>Component type</b>: type</TD>
</TR>
</Table>
<h3>Description</h3>
<tt>Char_producter<charT></tt> is a helper class for <tt><A href="Rope.html">rope</A></tt>. Its only
purpose is for the creation of <tt>rope</tt>s that use lazy evaluation. A
<tt>rope</tt> that uses lazy evaluation is one that does not store
the value of the <tt>n</tt>th character until the value of that particular
character is actually needed. This is sometimes a useful
optimization.
<P>
<tt>Char_producer<charT></tt> is an abstract base class; it provides an
empty virtual destructor and a pure virtual <tt>operator()</tt>. Only
subclasses of <tt>char_producer</tt> can be used, not <tt>char_producer</tt>
directly.
<P>
Classes derived from <tt>char_producer</tt> define a method for computing the
characters in a <tt><A href="Rope.html">rope</A></tt> lazily, whenever those characters are
needed. Unlike <A href="functors.html">Function Objects</A>, <tt>char_producer</tt>s can be stored
inside a <tt><A href="Rope.html">rope</A></tt> data structure. All char producers must be derived
from the single base class <tt>char_producer</tt>.
<P>
For performance reasons, the <tt>operator()</tt> inside <tt>char_producer</tt> is
invoked to fill a buffer with a sequence of characters
rather than producing a single character at a time. Its declaration
is
<pre>
virtual void operator()(size_t start_pos, size_t len, charT* buffer) = 0;
</pre>
<P>
An invocation of <tt>operator()</tt> requests that <tt>len</tt> characters starting
at <tt>start_pos</tt> should be deposited into <tt>buffer</tt>. If
the character at the <tt>n</tt>th position is requested twice, the same
character must be returned both times. (That is, a <tt>char_producer</tt>
must represent a specific, deterministic sequence of characters.)
<P>
The following is an example of how to use <tt>char_producer</tt> and lazy
evaluation: it is a (somewhat naive implementation of a)
<tt>char_producer</tt> subclass, which allows an entire file to be treated as
a <tt><A href="Rope.html">rope</A></tt>. A <tt>rope</tt> constructed from <tt>file_char_prod</tt> will contain the
same character sequence as the file specified in the constructor. The
file will be read only when the <tt>rope</tt> is accessed, not when the
<tt>rope</tt> is constructed.
<pre>
void fail(char* s) {
fprintf(stderr, "%s errno = %d\n", s, errno);
exit(1);
}
class file_char_prod : public char_producer<char> {
public:
FILE* f;
file_char_prod(char *file_name) {
if (NULL == (f = fopen(file_name, "rb")))
fail("Open failed");
}
~file_char_prod() { fclose(f); }
virtual void operator()(size_t start_pos, size_t len, char* buffer) {
if (fseek(f, start_pos, SEEK_SET)) fail("Seek failed");
if (fread(buffer, sizeof(char), len, f) < len) fail("Read failed");
}
long len() {
// Return the length of a file; this is the only
// mechanism that the standard C library makes possible.
if (fseek(f, 0, SEEK_END)) fail("Seek failed");
return ftell(f);
}
};
</pre>
<P>
The following program uses the above class to extract and write the
middle 200 characters of a file. Note that even if it is invoked on a
gigabyte file, it will still only read approximately 200 characters
from the file. Likewise, the <tt>rope</tt> will only require a small amount of
memory.
<pre>
int main(int argc, char** argv)
{
if (argc != 2)
fail("wrong number of arguments");
file_char_prod* fcp = new file_char_prod(argv[1]);
<A href="Rope.html">crope</A> s(fcp, fcp -> len(), true);
size_t len = s.size();
<A href="Rope.html">crope</A> middle = s.substr(len/2 - 100, 200) + "\n";
fwrite(middle.c_str(), sizeof(char), middle.size(), stdout);
}
</pre>
<h3>Definition</h3>
Defined in <A href="rope.h">rope.h</A>.
<h3>Template parameters</h3>
<Table border>
<TR>
<TH>
Parameter
</TH>
<TH>
Description
</TH>
<TH>
Default
</TH>
</TR>
<TR>
<TD VAlign=top>
<tt>charT</tt>
</TD>
<TD VAlign=top>
The character type
</TD>
<TD VAlign=top>
</TD>
</tr>
</table>
<h3>Model of</h3>
<A href="Assignable.html">Assignable</A>
<h3>Type requirements</h3>
<tt>charT</tt> is a model of <A href="Assignable.html">Assignable</A>.
<h3>Members</h3>
<Table border>
<TR>
<TH>
Member
</TH>
<TH>
Where defined
</TH>
<TH>
Description
</TH>
</TR>
<TR>
<TD VAlign=top>
<tt>virtual ~char_producer()</tt>
</TD>
<TD VAlign=top>
<tt>char_producer</tt>
</TD>
<TD VAlign=top>
A virtual destructor.
</TD>
</TR>
<TR>
<TD VAlign=top>
</TD>
<TD VAlign=top>
<pre>
virtual void operator()(size_t start_pos,
size_t len,
charT* buffer)
</pre>
</TD>
<TD VAlign=top>
Copy <tt>len</tt> characters starting at position <tt>start_pos</tt> in the
string into <tt>buffer</tt>. If a character at a particular
position is requested more than once, each request must result in
the same character. Note that this is a pure virtual function; it
must be overridden by every subclass of <tt>char_producer</tt>.
</TD>
</tr>
</table>
<h3>Notes</h3>
<h3>See also</h3>
<tt><A href="Rope.html">rope</A></tt>, <A href="functors.html" tppabs="http://www.sgi.com/Technology/STL/functors.shtml">Function object</A>
<HR SIZE="6"> <FONT SIZE="-2"> Copyright © 1996 Silicon Graphics, Inc.
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="index.html" >
STL</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© Copyright 1997-1998 CodeGuru</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact : <A HREF="mailto:webmaster@codeguru.com">webmaster@codeguru.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
<SCRIPT LANGUAGE="JavaScript" ><!--
var adurl = "/cgi-bin/doubleclick.cgi?";
if( self.adcategory )
adurl += adcategory;
else
adurl += "mfc";
if( self.parent.norefreshad )
parent.norefreshad = false;
else if( validframes )
parent.frames['ad'].location = adurl;
if( !validframes && nfrm == -1)
{
var dclkPage = "www.codeguru.com/";
if( self.adcategory )
dclkPage += adcategory;
else
dclkPage += "mfc";
// var random = Math.random();
document.write('<nolayer><center>');
document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
+ random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
+ 'frameborder=0 scrolling=no bordercolor="#000000">');
document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
+ random + '">');
document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
+ random + '" height=60 width=468>' + '</a>');
document.write('</iframe>');
document.write('</center></nolayer>');
document.write('<layer src="http://ad.doubleclick.net/adl/' + dclkPage +
';ord=' + random + '"></layer>');
document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}
// -->
</SCRIPT>
<!-- SCRIPT LANGUAGE="JavaScript" SRC="/global/fscript.js">
//
</SCRIPT -->
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupcHNFCY34AAHqBcNc">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupcHNFCY34AAHqBcNc"></a>
</p>
</noscript>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -