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

📄 char_producer.html

📁 ISO_C++:C++_STL开发文档
💻 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 BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" 	ALINK="#ff0000"> <IMG SRC="CorpID.gif"      ALT="SGI" HEIGHT="43" WIDTH="151"> <!--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 = "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&lt;charT&gt;</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&lt;charT&gt;</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, &quot;%s errno = %d\n&quot;, s, errno);   exit(1); }class file_char_prod : public char_producer&lt;char&gt; {  public:    FILE* f;    file_char_prod(char *file_name) {      if (NULL == (f = fopen(file_name, &quot;rb&quot;)))         fail(&quot;Open failed&quot;);    }    ~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(&quot;Seek failed&quot;);      if (fread(buffer, sizeof(char), len, f) &lt; len) fail(&quot;Read failed&quot;);    }    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(&quot;Seek failed&quot;);      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(&quot;wrong number of arguments&quot;);  file_char_prod* fcp = new file_char_prod(argv[1]);  <A href="Rope.html">crope</A> s(fcp, fcp -&gt; len(), true);  size_t len = s.size();  <A href="Rope.html">crope</A> middle = s.substr(len/2 - 100, 200) + &quot;\n&quot;;  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>&nbsp;</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>&nbsp;</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--> <HR SIZE="6"><A href="http://www.sgi.com/"><IMG SRC="surf.gif" HEIGHT="54" WIDTH="54"         ALT="[Silicon Surf]"></A><A HREF="index.html"><IMG SRC="stl_home.gif"         HEIGHT="54" WIDTH="54" ALT="[STL Home]"></A><BR><FONT SIZE="-2"><A href="http://www.sgi.com/Misc/sgi_info.html" TARGET="_top">Copyright &copy; 1999 Silicon Graphics, Inc.</A> All Rights Reserved.</FONT><FONT SIZE="-3"><a href="http://www.sgi.com/Misc/external.list.html" TARGET="_top">TrademarkInformation</A></FONT><P></BODY></HTML> 

⌨️ 快捷键说明

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