1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Timer.4 - Using a member function as a handler</title> 5<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../tutorial.html" title="Tutorial"> 9<link rel="prev" href="tuttimer3/src.html" title="Source listing for Timer.3"> 10<link rel="next" href="tuttimer4/src.html" title="Source listing for Timer.4"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td> 15<td align="center"><a href="../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="tuttimer3/src.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tuttimer4/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_asio.tutorial.tuttimer4"></a><a class="link" href="tuttimer4.html" title="Timer.4 - Using a member function as a handler">Timer.4 - Using a member 28 function as a handler</a> 29</h3></div></div></div> 30<p> 31 In this tutorial we will see how to use a class member function as a callback 32 handler. The program should execute identically to the tutorial program from 33 tutorial Timer.3. 34 </p> 35<pre class="programlisting">#include <iostream> 36#include <boost/asio.hpp> 37#include <boost/bind/bind.hpp> 38</pre> 39<p> 40 Instead of defining a free function <code class="computeroutput">print</code> as the callback handler, 41 as we did in the earlier tutorial programs, we now define a class called 42 <code class="computeroutput">printer</code>. 43 </p> 44<pre class="programlisting">class printer 45{ 46public: 47</pre> 48<p> 49 The constructor of this class will take a reference to the io_context object 50 and use it when initialising the <code class="computeroutput">timer_</code> member. The counter 51 used to shut down the program is now also a member of the class. 52 </p> 53<pre class="programlisting"> printer(boost::asio::io_context& io) 54 : timer_(io, boost::asio::chrono::seconds(1)), 55 count_(0) 56 { 57</pre> 58<p> 59 The boost::bind() function works just as well with class member functions 60 as with free functions. Since all non-static class member functions have 61 an implicit <code class="computeroutput">this</code> parameter, we need to bind <code class="computeroutput">this</code> 62 to the function. As in tutorial Timer.3, boost::bind() converts our callback 63 handler (now a member function) into a function object that can be invoked 64 as though it has the signature <code class="computeroutput">void(const boost::system::error_code&)</code>. 65 </p> 66<p> 67 You will note that the boost::asio::placeholders::error placeholder is not 68 specified here, as the <code class="computeroutput">print</code> member function does not accept 69 an error object as a parameter. 70 </p> 71<pre class="programlisting"> timer_.async_wait(boost::bind(&printer::print, this)); 72 } 73</pre> 74<p> 75 In the class destructor we will print out the final value of the counter. 76 </p> 77<pre class="programlisting"> ~printer() 78 { 79 std::cout << "Final count is " << count_ << std::endl; 80 } 81</pre> 82<p> 83 The <code class="computeroutput">print</code> member function is very similar to the <code class="computeroutput">print</code> 84 function from tutorial Timer.3, except that it now operates on the class 85 data members instead of having the timer and counter passed in as parameters. 86 </p> 87<pre class="programlisting"> void print() 88 { 89 if (count_ < 5) 90 { 91 std::cout << count_ << std::endl; 92 ++count_; 93 94 timer_.expires_at(timer_.expiry() + boost::asio::chrono::seconds(1)); 95 timer_.async_wait(boost::bind(&printer::print, this)); 96 } 97 } 98 99private: 100 boost::asio::steady_timer timer_; 101 int count_; 102}; 103</pre> 104<p> 105 The <code class="computeroutput">main</code> function is much simpler than before, as it now declares 106 a local <code class="computeroutput">printer</code> object before running the io_context as normal. 107 </p> 108<pre class="programlisting">int main() 109{ 110 boost::asio::io_context io; 111 printer p(io); 112 io.run(); 113 114 return 0; 115} 116</pre> 117<p> 118 See the <a class="link" href="tuttimer4/src.html" title="Source listing for Timer.4">full source listing</a> 119 </p> 120<p> 121 Return to the <a class="link" href="../tutorial.html" title="Tutorial">tutorial index</a> 122 </p> 123<p> 124 Previous: <a class="link" href="tuttimer3.html" title="Timer.3 - Binding arguments to a handler">Timer.3 - Binding 125 arguments to a handler</a> 126 </p> 127<p> 128 Next: <a class="link" href="tuttimer5.html" title="Timer.5 - Synchronising handlers in multithreaded programs">Timer.5 - Synchronising 129 handlers in multithreaded programs</a> 130 </p> 131</div> 132<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 133<td align="left"></td> 134<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 135 Kohlhoff<p> 136 Distributed under the Boost Software License, Version 1.0. (See accompanying 137 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 138 </p> 139</div></td> 140</tr></table> 141<hr> 142<div class="spirit-nav"> 143<a accesskey="p" href="tuttimer3/src.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tuttimer4/src.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 144</div> 145</body> 146</html> 147