index.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Chapter&#160;1.&#160;Boost.ScopeExit 1.1.0</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="index.html" title="Chapter&#160;1.&#160;Boost.ScopeExit 1.1.0">
  8. <link rel="next" href="scope_exit/getting_started.html" title="Getting Started">
  9. </head>
  10. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  11. <table cellpadding="2" width="100%"><tr>
  12. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td>
  13. <td align="center"><a href="../../../../index.html">Home</a></td>
  14. <td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
  15. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  16. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  17. <td align="center"><a href="../../../../more/index.htm">More</a></td>
  18. </tr></table>
  19. <hr>
  20. <div class="spirit-nav"><a accesskey="n" href="scope_exit/getting_started.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a></div>
  21. <div class="chapter">
  22. <div class="titlepage"><div>
  23. <div><h2 class="title">
  24. <a name="scope_exit"></a>Chapter&#160;1.&#160;Boost.ScopeExit 1.1.0</h2></div>
  25. <div><div class="author"><h3 class="author">
  26. <span class="firstname">Alexander</span> <span class="surname">Nasonov</span>
  27. </h3></div></div>
  28. <div><div class="author"><h3 class="author">
  29. <span class="firstname">Lorenzo</span> <span class="surname">Caminiti <code class="email">&lt;<a class="email" href="mailto:lorcaminiti@gmail.com">lorcaminiti@gmail.com</a>&gt;</code></span>
  30. </h3></div></div>
  31. <div><p class="copyright">Copyright &#169; 2006-2012 Alexander Nasonov, Lorenzo Caminiti</p></div>
  32. <div><div class="legalnotice">
  33. <a name="scope_exit.legal"></a><p>
  34. Distributed under the Boost Software License, Version 1.0 (see accompanying
  35. file LICENSE_1_0.txt or a copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  36. </p>
  37. </div></div>
  38. </div></div>
  39. <div class="toc">
  40. <p><b>Table of Contents</b></p>
  41. <dl class="toc">
  42. <dt><span class="section"><a href="index.html#scope_exit.introduction">Introduction</a></span></dt>
  43. <dt><span class="section"><a href="scope_exit/getting_started.html">Getting Started</a></span></dt>
  44. <dd><dl>
  45. <dt><span class="section"><a href="scope_exit/getting_started.html#scope_exit.getting_started.this_documentation">This Documentation</a></span></dt>
  46. <dt><span class="section"><a href="scope_exit/getting_started.html#scope_exit.getting_started.compilers_and_platforms">Compilers
  47. and Platforms</a></span></dt>
  48. <dt><span class="section"><a href="scope_exit/getting_started.html#scope_exit.getting_started.installation">Installation</a></span></dt>
  49. </dl></dd>
  50. <dt><span class="section"><a href="scope_exit/tutorial.html">Tutorial</a></span></dt>
  51. <dd><dl>
  52. <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.capturing_variables">Capturing Variables</a></span></dt>
  53. <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.capturing_the_object__this_">Capturing
  54. The Object <code class="computeroutput"><span class="keyword">this</span></code></a></span></dt>
  55. <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.capturing_no_variable">Capturing
  56. No Variable</a></span></dt>
  57. <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.capturing_all_variables__c__11_only_">Capturing
  58. All Variables (C++11 Only)</a></span></dt>
  59. <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.template_workaround__gcc_">Template
  60. Workaround (GCC)</a></span></dt>
  61. <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.same_line_expansions">Same Line Expansions</a></span></dt>
  62. </dl></dd>
  63. <dt><span class="section"><a href="scope_exit/alternatives.html">Annex: Alternatives</a></span></dt>
  64. <dt><span class="section"><a href="scope_exit/no_variadic_macros.html">Annex: No Variadic Macros</a></span></dt>
  65. <dt><span class="section"><a href="reference.html">Reference</a></span></dt>
  66. <dt><span class="section"><a href="scope_exit/acknowledgements.html">Acknowledgements</a></span></dt>
  67. </dl>
  68. </div>
  69. <p>
  70. This library allows to execute arbitrary code when the enclosing scope exits.
  71. </p>
  72. <div class="section">
  73. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  74. <a name="scope_exit.introduction"></a><a class="link" href="index.html#scope_exit.introduction" title="Introduction">Introduction</a>
  75. </h2></div></div></div>
  76. <p>
  77. Nowadays, every C++ developer is familiar with the Resource Acquisition Is
  78. Initialization (<a href="http://www.research.att.com/~bs/glossary.html#Gresource-acquisition-is-initialization" target="_top">RAII</a>)
  79. technique. It binds resource acquisition and release to initialization and
  80. destruction of a variable that holds the resource. There are times when writing
  81. a special class for such a variable is not worth the effort. This is when
  82. <a class="link" href="index.html" title="Chapter&#160;1.&#160;Boost.ScopeExit 1.1.0">Boost.ScopeExit</a> comes into play.
  83. </p>
  84. <p>
  85. Programmers can put resource acquisition directly in their code and next to
  86. it, they can write code that releases the resource using this library. For
  87. example (see also <a href="../../test/world.cpp" target="_top"><code class="literal">world.cpp</code></a>):
  88. <a href="#ftn.scope_exit.introduction.f0" class="footnote" name="scope_exit.introduction.f0"><sup class="footnote">[1]</sup></a>
  89. </p>
  90. <p>
  91. </p>
  92. <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">world</span><span class="special">::</span><span class="identifier">add_person</span><span class="special">(</span><span class="identifier">person</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">a_person</span><span class="special">)</span> <span class="special">{</span>
  93. <span class="keyword">bool</span> <span class="identifier">commit</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span>
  94. <span class="identifier">persons_</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">a_person</span><span class="special">);</span> <span class="comment">// (1) direct action</span>
  95. <span class="comment">// Following block is executed when the enclosing scope exits.</span>
  96. <span class="identifier">BOOST_SCOPE_EXIT</span><span class="special">(&amp;</span><span class="identifier">commit</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">persons_</span><span class="special">)</span> <span class="special">{</span>
  97. <span class="keyword">if</span><span class="special">(!</span><span class="identifier">commit</span><span class="special">)</span> <span class="identifier">persons_</span><span class="special">.</span><span class="identifier">pop_back</span><span class="special">();</span> <span class="comment">// (2) rollback action</span>
  98. <span class="special">}</span> <span class="identifier">BOOST_SCOPE_EXIT_END</span>
  99. <span class="comment">// ... // (3) other operations</span>
  100. <span class="identifier">commit</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span> <span class="comment">// (4) disable rollback actions</span>
  101. <span class="special">}</span>
  102. </pre>
  103. <p>
  104. </p>
  105. </div>
  106. <div class="footnotes">
  107. <br><hr style="width:100; text-align:left;margin-left: 0">
  108. <div id="ftn.scope_exit.introduction.f0" class="footnote"><p><a href="#scope_exit.introduction.f0" class="para"><sup class="para">[1] </sup></a>
  109. Older versions of this library used a <a href="http://www.boost.org/libs/preprocessor" target="_top">Boost.Preprocessor</a>
  110. sequence to specify the list of captured variables. While maintaining full
  111. backward compatibility, it is now possible to specify the captured variables
  112. also using a comma-separated list (which is the preferred syntax). See the
  113. <a class="link" href="scope_exit/no_variadic_macros.html" title="Annex: No Variadic Macros">No Variadic Macros</a> section
  114. for more information.
  115. </p></div>
  116. </div>
  117. </div>
  118. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  119. <td align="left"><p><small>Last revised: December 10, 2019 at 00:22:04 GMT</small></p></td>
  120. <td align="right"><div class="copyright-footer"></div></td>
  121. </tr></table>
  122. <hr>
  123. <div class="spirit-nav"><a accesskey="n" href="scope_exit/getting_started.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a></div>
  124. </body>
  125. </html>