📄 char_producer.html
字号:
<HTML><!-- -- Copyright (c) 1996-1999 -- Silicon Graphics Computer Systems, Inc. -- -- Permission to use, copy, modify, distribute and sell this software -- and its documentation for any purpose is hereby granted without fee, -- provided that the above copyright notice appears in all copies and -- that both that copyright notice and this permission notice appear -- in supporting documentation. Silicon Graphics makes no -- representations about the suitability of this software for any -- purpose. It is provided "as is" without express or implied warranty. -- -- Copyright (c) 1994 -- Hewlett-Packard Company -- -- Permission to use, copy, modify, distribute and sell this software -- and its documentation for any purpose is hereby granted without fee, -- provided that the above copyright notice appears in all copies and -- that both that copyright notice and this permission notice appear -- in supporting documentation. Hewlett-Packard Company makes no -- representations about the suitability of this software for any -- purpose. It is provided "as is" without express or implied warranty. -- --><Head><Title>char_producer</Title><!-- Generated by htmldoc --></HEAD><BODY TEXT="#000000" LINK="#006600" ALINK="#003300" VLINK="#7C7F87" BGCOLOR="#FFFFFF"><A HREF="/"><IMG SRC="/images/common/sgilogo_small.gif" ALT="SGI Logo" WIDTH="80" HEIGHT="72" BORDER="0"></A><P><!--end header--><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 = "38" ></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 onlypurpose 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 storethe value of the <tt>n</tt>th character until the value of that particularcharacter is actually needed. This is sometimes a useful optimization.<P><tt>Char_producer<charT></tt> is an abstract base class; it provides anempty 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 thecharacters in a <tt><A href="Rope.html">rope</A></tt> lazily, whenever those characters areneeded. Unlike <A href="functors.html">Function Objects</A>, <tt>char_producer</tt>s can be storedinside a <tt><A href="Rope.html">rope</A></tt> data structure. All char producers must be derivedfrom the single base class <tt>char_producer</tt>.<P>For performance reasons, the <tt>operator()</tt> inside <tt>char_producer</tt> isinvoked to fill a buffer with a sequence of characters rather than producing a single character at a time. Its declarationis<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 startingat <tt>start_pos</tt> should be deposited into <tt>buffer</tt>. Ifthe character at the <tt>n</tt>th position is requested twice, the samecharacter 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 lazyevaluation: it is a (somewhat naive implementation of a)<tt>char_producer</tt> subclass, which allows an entire file to be treated asa <tt><A href="Rope.html">rope</A></tt>. A <tt>rope</tt> constructed from <tt>file_char_prod</tt> will contain thesame character sequence as the file specified in the constructor. Thefile 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 themiddle 200 characters of a file. Note that even if it is invoked on agigabyte file, it will still only read approximately 200 charactersfrom the file. Likewise, the <tt>rope</tt> will only require a small amount ofmemory.<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">rope</A>, and in the backward-compatibility header <A href="rope.h">rope.h</A>.The <tt>char_producer</tt> class is an SGI extension; it is not part of the C++standard.<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">Function object</A><!-- start footer --><!-- Footer Begins --><STYLE TYPE="text/css"><!--TD.footer, TD.footer A{ font-family: Arial, helvetica, sans-serif; font-size: 8pt;}A.home {font-family: Arial, helvetica, sans-serif;}--></STYLE><P><A CLASS="home" HREF="index.html">STL Home</A><P><TABLE WIDTH="600" CELLPADDING="0" CELLPADDING="0" BORDER="0"> <TR> <TD ALIGN="RIGHT" CLASS="footer"><A HREF="/company_info/terms.html" TARGET="_top">terms of use</A> | <A HREF="/company_info/privacy.html" TARGET="_top">privacy policy</A></TD> <TD ALIGN="CENTER" CLASS="footer"> | </TD> <TD ALIGN="LEFT" CLASS="footer"><A HREF="/cgi-bin/feedback/" TARGET="_top">contact us</A></TD> </TR><TR> <TD ALIGN="RIGHT" CLASS="footer">Copyright © 1993-2003 Silicon Graphics, Inc. All rights reserved.</TD> <TD ALIGN="CENTER" CLASS="footer"> | </TD> <TD ALIGN="LEFT" CLASS="footer"><A HREF="/company_info/trademarks/" TARGET="_top">Trademark Information</A></TD> </TR></TABLE><!-- Footer Ends --><!-- end footer --><P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -