]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Chapter 1. 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.78.1"> | |
7 | <link rel="home" href="index.html" title="Chapter 1. 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 1. 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"><<a class="email" href="mailto:lorcaminiti@gmail.com">lorcaminiti@gmail.com</a>></code></span> | |
30 | </h3></div></div> | |
31 | <div><p class="copyright">Copyright © 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 | <dd><dl><dt><span class="section"><a href="reference.html#header.boost.scope_exit_hpp">Header <boost/scope_exit.hpp></a></span></dt></dl></dd> | |
67 | <dt><span class="section"><a href="scope_exit/acknowledgements.html">Acknowledgements</a></span></dt> | |
68 | </dl> | |
69 | </div> | |
70 | <p> | |
71 | This library allows to execute arbitrary code when the enclosing scope exits. | |
72 | </p> | |
73 | <div class="section"> | |
74 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> | |
75 | <a name="scope_exit.introduction"></a><a class="link" href="index.html#scope_exit.introduction" title="Introduction">Introduction</a> | |
76 | </h2></div></div></div> | |
77 | <p> | |
78 | Nowadays, every C++ developer is familiar with the Resource Acquisition Is | |
79 | Initialization (<a href="http://www.research.att.com/~bs/glossary.html#Gresource-acquisition-is-initialization" target="_top">RAII</a>) | |
80 | technique. It binds resource acquisition and release to initialization and | |
81 | destruction of a variable that holds the resource. There are times when writing | |
82 | a special class for such a variable is not worth the effort. This is when | |
83 | <a class="link" href="index.html" title="Chapter 1. Boost.ScopeExit 1.1.0">Boost.ScopeExit</a> comes into play. | |
84 | </p> | |
85 | <p> | |
86 | Programmers can put resource acquisition directly in their code and next to | |
87 | it, they can write code that releases the resource using this library. For | |
88 | example (see also <a href="../../test/world.cpp" target="_top"><code class="literal">world.cpp</code></a>): | |
89 | <a href="#ftn.scope_exit.introduction.f0" class="footnote" name="scope_exit.introduction.f0"><sup class="footnote">[1]</sup></a> | |
90 | </p> | |
91 | <p> | |
92 | </p> | |
93 | <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">&</span> <span class="identifier">a_person</span><span class="special">)</span> <span class="special">{</span> | |
94 | <span class="keyword">bool</span> <span class="identifier">commit</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span> | |
95 | ||
96 | <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> | |
97 | <span class="comment">// Following block is executed when the enclosing scope exits.</span> | |
98 | <span class="identifier">BOOST_SCOPE_EXIT</span><span class="special">(&</span><span class="identifier">commit</span><span class="special">,</span> <span class="special">&</span><span class="identifier">persons_</span><span class="special">)</span> <span class="special">{</span> | |
99 | <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> | |
100 | <span class="special">}</span> <span class="identifier">BOOST_SCOPE_EXIT_END</span> | |
101 | ||
102 | <span class="comment">// ... // (3) other operations</span> | |
103 | ||
104 | <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> | |
105 | <span class="special">}</span> | |
106 | </pre> | |
107 | <p> | |
108 | </p> | |
109 | </div> | |
110 | <div class="footnotes"> | |
111 | <br><hr style="width:100; text-align:left;margin-left: 0"> | |
112 | <div id="ftn.scope_exit.introduction.f0" class="footnote"><p><a href="#scope_exit.introduction.f0" class="para"><sup class="para">[1] </sup></a> | |
113 | Older versions of this library used a <a href="http://www.boost.org/libs/preprocessor" target="_top">Boost.Preprocessor</a> | |
114 | sequence to specify the list of captured variables. While maintaining full | |
115 | backward compatibility, it is now possible to specify the captured variables | |
116 | also using a comma-separated list (which is the preferred syntax). See the | |
117 | <a class="link" href="scope_exit/no_variadic_macros.html" title="Annex: No Variadic Macros">No Variadic Macros</a> section | |
118 | for more information. | |
119 | </p></div> | |
120 | </div> | |
121 | </div> | |
122 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
123 | <td align="left"><p><small>Last revised: October 15, 2015 at 00:12:00 GMT</small></p></td> | |
124 | <td align="right"><div class="copyright-footer"></div></td> | |
125 | </tr></table> | |
126 | <hr> | |
127 | <div class="spirit-nav"><a accesskey="n" href="scope_exit/getting_started.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a></div> | |
128 | </body> | |
129 | </html> |