[boost-doc-zh] r291 committed - [No log message]

  • From: codesite-noreply@xxxxxxxxxx
  • To: boost-doc-zh-notify@xxxxxxxxxxxxx
  • Date: Tue, 25 Aug 2009 03:07:29 +0000

Revision: 291
Author: totti19851101
Date: Mon Aug 24 20:07:05 2009
Log: [No log message]
http://code.google.com/p/boost-doc-zh/source/detail?r=291

Modified:
 /trunk/doc/html/boost_asio/tutorial/tuttimer3.html

=======================================
--- /trunk/doc/html/boost_asio/tutorial/tuttimer3.html Wed May 27 03:09:50 2009 +++ /trunk/doc/html/boost_asio/tutorial/tuttimer3.html Mon Aug 24 20:07:05 2009
@@ -1,6 +1,6 @@
 <html>
 <head>
-<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Timer.3 - Binding arguments to a handler</title>
 <link rel="stylesheet" href="../../boostbook.css" type="text/css">
 <meta name="generator" content="DocBook XSL Stylesheets V1.74.3">
@@ -24,13 +24,10 @@
 </div>
 <div class="section" lang="en">
 <div class="titlepage"><div><div><h3 class="title">
-<a name="boost_asio.tutorial.tuttimer3"></a><a class="link" href="tuttimer3.html" title="Timer.3 - Binding arguments to a handler"> Timer.3 - Binding arguments
-      to a handler</a>
+<a name="boost_asio.tutorial.tuttimer3"></a><a class="link" href="tuttimer3.html" title="Timer.3 - Binding arguments to a handler"> Timer.3 - 回调函数绑定参数</a>
 </h3></div></div></div>
 <p>
- In this tutorial we will modify the program from tutorial Timer.2 so that - the timer fires once a second. This will show how to pass additional parameters
-        to your handler function.
+ 在本示例程序中我们将修改Timer.2中的例子,使定时器每秒被激活一次。例子 将示范如何给你的函数指针传递附加参数。
       </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span> <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">asio</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
@@ -38,19 +35,15 @@
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">date_time</span><span class="special">/</span><span class="identifier">posix_time</span><span class="special">/</span><span class="identifier">posix_time</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
 </pre>
 <p>
- To implement a repeating timer using asio you need to change the timer's - expiry time in your callback function, and to then start a new asynchronous - wait. Obviously this means that the callback function will need to be able - to access the timer object. To this end we add two new parameters to the - <code class="computeroutput"><span class="identifier">print</span></code> function: + 使用Asio实现一个重复定时器,你必须在你的回调函数中去改变定时器的终 止时间,然后开始一个新的异步等待。显然这意味着回调函数必须拥有改变定时器对象 的权限。为此我们为 + <code class="computeroutput"><span class="identifier">print</span></code>函数增加两个新参数:
       </p>
 <div class="itemizedlist"><ul type="disc">
 <li>
-          A pointer to a timer object.
+          一个指向定时器对象的指针。
         </li>
 <li>
- A counter so that we can stop the program when the timer fires for the
-          sixth time.
+          一个用于当定时器第6次被激活时我们可以中止程序的计数器。
         </li>
 </ul></div>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">print</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&amp;</span> <span class="comment">/*e*/</span><span class="special">,</span>
@@ -58,13 +51,9 @@
 <span class="special">{</span>
 </pre>
 <p>
- As mentioned above, this tutorial program uses a counter to stop running - when the timer fires for the sixth time. However you will observe that there - is no explicit call to ask the io_service to stop. Recall that in tutorial - Timer.2 we learnt that the <a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a> - function completes when there is no more "work" to do. By not starting - a new asynchronous wait on the timer when <code class="computeroutput"><span class="identifier">count</span></code>
-        reaches 5, the io_service will run out of work and stop running.
+ 如上所示,示例程序使用了一个计数器,当定时器被第6次激活时,用来中止 程序。然而,你将看到这里并没有显式地要求io_service对象中止。回忆示例2中,当 没有更多“工作”去做时,<a class="link" href="../reference/io_service/run.html" title="io_service::run">io_service::run()</a> + 函数完成。在计数器<code class="computeroutput"><span class="identifier">达到</span></code> + 5时,定时器并没有启动一个新的异步等待。该io_service执行完工作后停止 运行。
       </p>
<pre class="programlisting"> <span class="keyword">if</span> <span class="special">(*</span><span class="identifier">count</span> <span class="special">&lt;</span> <span class="number">5</span><span class="special">)</span>
   <span class="special">{</span>
@@ -72,33 +61,22 @@
<span class="special">++(*</span><span class="identifier">count</span><span class="special">);</span>
 </pre>
 <p>
- Next we move the expiry time for the timer along by one second from the previous - expiry time. By calculating the new expiry time relative to the old, we can - ensure that the timer does not drift away from the whole-second mark due
-        to any delays in processing the handler.
+ 接着,我们推迟定时器的终止时间。通过在原先的终止时间上增加延时,我 们可以确保定时器不会在处理回调函数所需时间内到期。
       </p>
<pre class="programlisting"> <span class="identifier">t</span><span class="special">-&gt;</span><span class="identifier">expires_at</span><span class="special">(</span><span class="identifier">t</span><span class="special">-&gt;</span><span class="identifier">expires_at</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">posix_time</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">1</span><span class="special">));</span>
 </pre>
 <p>
- Then we start a new asynchronous wait on the timer. As you can see, the boost::bind() - function is used to associate the extra parameters with your callback handler. - The <a class="link" href="../reference/basic_deadline_timer/async_wait.html" title="basic_deadline_timer::async_wait">deadline_timer::async_wait()</a> - function expects a handler function (or function object) with the signature - <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&amp;)</span></code>. Binding the additional parameters - converts your <code class="computeroutput"><span class="identifier">print</span></code> function
-        into a function object that matches the signature correctly.
+ 接着我们在定时器中启动一个新的异步等待。我们必须使用boost::bind() 函数给你的回调函数绑定额外的参数,因为<a class="link" href="../reference/basic_deadline_timer/async_wait.html" title="basic_deadline_timer::async_wait">deadline_timer::async_wait()</a>
+        函数只期望得到一个拥用
+ <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&amp;)</span></code> + 签名的函数指针(或函数对象)。为你的<code class="computeroutput"><span class="identifier">print</span></code>函数绑定 附加的参数后,它就成为与签名精确匹配的函数对象。
       </p>
 <p>
- See the <a href="http://www.boost.org/libs/bind/bind.html"; target="_top">Boost.Bind
-        documentation</a> for more information on how to use boost::bind().
+ 查看<a href="http://www.boost.org/libs/bind/bind.html"; target="_top">Boost.Bind
+        文档</a>文档以获得更多如何使用boost::bind()的信息。
       </p>
 <p>
- In this example, the boost::asio::placeholders::error argument to boost::bind() - is a named placeholder for the error object passed to the handler. When initiating - the asynchronous operation, and if using boost::bind(), you must specify - only the arguments that match the handler's parameter list. In tutorial Timer.4 - you will see that this placeholder may be elided if the parameter is not
-        needed by the callback handler.
+ 在本例中,boost::bind()的boost::asio::placeholders::error参数是为 了给回调函数传入一个error对象。当开始异步操作时,如果使用boost::bind(),你必 须指定和回调函数的参数列表相匹配的一个参数。在示例4中,如果在回调函数中,这 个参数不是必需的,这个占位符会被省略。
       </p>
<pre class="programlisting"> <span class="identifier">t</span><span class="special">-&gt;</span><span class="identifier">async_wait</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">print</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">placeholders</span><span class="special">::</span><span class="identifier">error</span><span class="special">,</span> <span class="identifier">t</span><span class="special">,</span> <span class="identifier">count</span><span class="special">));</span>
@@ -110,17 +88,14 @@
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span> <span class="identifier">io</span><span class="special">;</span>
 </pre>
 <p>
- A new <code class="computeroutput"><span class="identifier">count</span></code> variable is added - so that we can stop the program when the timer fires for the sixth time. + 为了在定时器第6次被激活时终止程序,我们添加一个新的<code class="computeroutput"><span class="identifier">count</span></code>变量。
       </p>
<pre class="programlisting"> <span class="keyword">int</span> <span class="identifier">count</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">deadline_timer</span> <span class="identifier">t</span><span class="special">(</span><span class="identifier">io</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">posix_time</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">1</span><span class="special">));</span>
 </pre>
 <p>
- As in Step 4, when making the call to <a class="link" href="../reference/basic_deadline_timer/async_wait.html" title="basic_deadline_timer::async_wait">deadline_timer::async_wait()</a> - from <code class="computeroutput"><span class="identifier">main</span></code> we bind the additional - parameters needed for the <code class="computeroutput"><span class="identifier">print</span></code>
-        function.
+ 在第四步中,当在<code class="computeroutput"><span class="identifier">主函数</span></code>中的调用<a class="link" href="../reference/basic_deadline_timer/async_wait.html" title="basic_deadline_timer::async_wait">deadline_timer::async_wait()</a> + 函数时,我们绑定<code class="computeroutput"><span class="identifier">print</span></code>函数所需要的附加参数。
       </p>
<pre class="programlisting"> <span class="identifier">t</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">print</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">placeholders</span><span class="special">::</span><span class="identifier">error</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">t</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">count</span><span class="special">));</span>
@@ -128,9 +103,9 @@
<span class="identifier">io</span><span class="special">.</span><span class="identifier">run</span><span class="special">();</span>
 </pre>
 <p>
- Finally, just to prove that the <code class="computeroutput"><span class="identifier">count</span></code> - variable was being used in the <code class="computeroutput"><span class="identifier">print</span></code>
-        handler function, we will print out its new value.
+ 最后,为了证明<code class="computeroutput"><span class="identifier">count</span></code> + 变量在<code class="computeroutput"><span class="identifier">print</span></code>
+        函数句柄中被使用,我们打印出它的值。
       </p>
<pre class="programlisting"> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Final count is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">count</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="special">;</span>

@@ -138,18 +113,16 @@
 <span class="special">}</span>
 </pre>
 <p>
- See the <a class="link" href="tuttimer3/src.html" title="Source listing for Timer.3">full source listing</a> + 查看<a class="link" href="tuttimer3/src.html" title="Source listing for Timer.3">本例全部源代码</a>
       </p>
 <p>
- Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a> + 返回<a class="link" href="../tutorial.html" title="Tutorial">指南 </a>
       </p>
 <p>
- Previous: <a class="link" href="tuttimer2.html" title="Timer.2 - Using a timer asynchronously">Timer.2 - Using a
-        timer asynchronously</a>
+ 前一例: <a class="link" href="tuttimer2.html" title="Timer.2 - Using a timer asynchronously">Timer.2 - 使用异步定时器</a>
       </p>
 <p>
- Next: <a class="link" href="tuttimer4.html" title="Timer.4 - Using a member function as a handler">Timer.4 - Using a member
-        function as a handler</a>
+ 后一例: <a class="link" href="tuttimer4.html" title="Timer.4 - Using a member function as a handler">Timer.4 - 成员函数作为回调函数</a>
       </p>
 </div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"; width="100%"><tr>

Other related posts:

  • » [boost-doc-zh] r291 committed - [No log message] - codesite-noreply