]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Annex: No Variadic Macros</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="up" href="../index.html" title="Chapter 1. Boost.ScopeExit 1.1.0"> | |
9 | <link rel="prev" href="alternatives.html" title="Annex: Alternatives"> | |
10 | <link rel="next" href="../reference.html" title="Reference"> | |
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="alternatives.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../reference.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> | |
24 | </div> | |
25 | <div class="section"> | |
26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> | |
27 | <a name="scope_exit.no_variadic_macros"></a><a class="link" href="no_variadic_macros.html" title="Annex: No Variadic Macros">Annex: No Variadic Macros</a> | |
28 | </h2></div></div></div> | |
29 | <p> | |
30 | This section presents an alternative syntax for compilers without variadic | |
31 | macro support. | |
32 | </p> | |
33 | <h4> | |
34 | <a name="scope_exit.no_variadic_macros.h0"></a> | |
35 | <span class="phrase"><a name="scope_exit.no_variadic_macros.sequence_syntax"></a></span><a class="link" href="no_variadic_macros.html#scope_exit.no_variadic_macros.sequence_syntax">Sequence | |
36 | Syntax</a> | |
37 | </h4> | |
38 | <p> | |
39 | Most modern compilers support variadic macros (notably, these include GCC, | |
40 | MSVC, and all C++11 compilers). <a href="#ftn.scope_exit.no_variadic_macros.f0" class="footnote" name="scope_exit.no_variadic_macros.f0"><sup class="footnote">[10]</sup></a> However, in the rare case that programmers need to use this library | |
41 | on a complier without variaidc macros, this library also allows to specify | |
42 | the capture list using a <a href="http://www.boost.org/libs/preprocessor" target="_top">Boost.Preprocessor</a> | |
43 | sequence where tokens are separated by round parenthesis <code class="computeroutput"><span class="special">()</span></code>: | |
44 | </p> | |
45 | <pre class="programlisting"><span class="special">(</span><span class="identifier">capture1</span><span class="special">)</span> <span class="special">(</span><span class="identifier">capture2</span><span class="special">)</span> <span class="special">...</span> <span class="comment">// All compilers.</span> | |
46 | </pre> | |
47 | <p> | |
48 | Instead of the comma-separated list that we have seen so far which requires | |
49 | variadic macros: | |
50 | </p> | |
51 | <pre class="programlisting"><span class="identifier">capture1</span><span class="special">,</span> <span class="identifier">capture2</span><span class="special">,</span> <span class="special">...</span> <span class="comment">// Only compilers with variadic macros.</span> | |
52 | </pre> | |
53 | <p> | |
54 | For example, the following syntax is accepted on all compilers with and without | |
55 | variadic macros (see also <a href="../../../test/world_seq.cpp" target="_top"><code class="literal">world_seq.cpp</code></a> | |
56 | and <a href="../../../test/world.cpp" target="_top"><code class="literal">world.cpp</code></a>): | |
57 | </p> | |
58 | <div class="informaltable"><table class="table"> | |
59 | <colgroup> | |
60 | <col> | |
61 | <col> | |
62 | </colgroup> | |
63 | <thead><tr> | |
64 | <th> | |
65 | <p> | |
66 | Boost.Preprocessor Sequence (All Compilers) | |
67 | </p> | |
68 | </th> | |
69 | <th> | |
70 | <p> | |
71 | Comma-Separated List (Variadic Macros Only) | |
72 | </p> | |
73 | </th> | |
74 | </tr></thead> | |
75 | <tbody><tr> | |
76 | <td> | |
77 | <p> | |
78 | </p> | |
79 | <pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-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> | |
80 | <span class="keyword">bool</span> <span class="identifier">commit</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span> | |
81 | ||
82 | <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> | |
83 | <span class="comment">// Following block is executed when the enclosing scope exits.</span> | |
84 | <span class="identifier">BOOST_SCOPE_EXIT</span><span class="special">(</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> <span class="special">{</span> | |
85 | <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> | |
86 | <span class="special">}</span> <span class="identifier">BOOST_SCOPE_EXIT_END</span> | |
87 | ||
88 | <span class="comment">// ... // (3) other operations</span> | |
89 | ||
90 | <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> | |
91 | <span class="special">}</span> | |
92 | </pre> | |
93 | <p> | |
94 | </p> | |
95 | </td> | |
96 | <td> | |
97 | <p> | |
98 | </p> | |
99 | <pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-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> | |
100 | <span class="keyword">bool</span> <span class="identifier">commit</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span> | |
101 | ||
102 | <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> | |
103 | <span class="comment">// Following block is executed when the enclosing scope exits.</span> | |
104 | <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> | |
105 | <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> | |
106 | <span class="special">}</span> <span class="identifier">BOOST_SCOPE_EXIT_END</span> | |
107 | ||
108 | <span class="comment">// ... // (3) other operations</span> | |
109 | ||
110 | <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> | |
111 | <span class="special">}</span> | |
112 | </pre> | |
113 | <p> | |
114 | </p> | |
115 | </td> | |
116 | </tr></tbody> | |
117 | </table></div> | |
118 | <p> | |
119 | Note how the same macros accept both syntaxes on compilers with variadic macros | |
120 | and only the <a href="http://www.boost.org/libs/preprocessor" target="_top">Boost.Preprocessor</a> | |
121 | sequence syntax on compilers without variadic macros. Older versions of this | |
122 | library used to only support the <a href="http://www.boost.org/libs/preprocessor" target="_top">Boost.Preprocessor</a> | |
123 | sequence syntax so this syntax is supported also for backward compatibility. | |
124 | However, in the current version of this library and on compilers with variadic | |
125 | macros, the comma-separated syntax is preferred because it is more readable. | |
126 | </p> | |
127 | <p> | |
128 | Finally, an empty capture list is always specified using <code class="computeroutput"><span class="keyword">void</span></code> | |
129 | on compilers with and without variaidc macros (see also <a href="../../../test/world_void.cpp" target="_top"><code class="literal">world_void.cpp</code></a>): | |
130 | </p> | |
131 | <p> | |
132 | </p> | |
133 | <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">world_t</span> <span class="special">{</span> | |
134 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">person</span><span class="special">></span> <span class="identifier">persons</span><span class="special">;</span> | |
135 | <span class="keyword">bool</span> <span class="identifier">commit</span><span class="special">;</span> | |
136 | <span class="special">}</span> <span class="identifier">world</span><span class="special">;</span> <span class="comment">// Global variable.</span> | |
137 | ||
138 | <span class="keyword">void</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> | |
139 | <span class="identifier">world</span><span class="special">.</span><span class="identifier">commit</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span> | |
140 | <span class="identifier">world</span><span class="special">.</span><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> | |
141 | ||
142 | <span class="identifier">BOOST_SCOPE_EXIT</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// No captures.</span> | |
143 | <span class="keyword">if</span><span class="special">(!</span><span class="identifier">world</span><span class="special">.</span><span class="identifier">commit</span><span class="special">)</span> <span class="identifier">world</span><span class="special">.</span><span class="identifier">persons</span><span class="special">.</span><span class="identifier">pop_back</span><span class="special">();</span> | |
144 | <span class="special">}</span> <span class="identifier">BOOST_SCOPE_EXIT_END</span> | |
145 | ||
146 | <span class="comment">// ...</span> | |
147 | ||
148 | <span class="identifier">world</span><span class="special">.</span><span class="identifier">commit</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span> | |
149 | <span class="special">}</span> | |
150 | </pre> | |
151 | <p> | |
152 | </p> | |
153 | <h4> | |
154 | <a name="scope_exit.no_variadic_macros.h1"></a> | |
155 | <span class="phrase"><a name="scope_exit.no_variadic_macros.examples"></a></span><a class="link" href="no_variadic_macros.html#scope_exit.no_variadic_macros.examples">Examples</a> | |
156 | </h4> | |
157 | <p> | |
158 | For reference, the following is a list of most of the examples presented in | |
159 | this documentation reprogrammed using the <a href="http://www.boost.org/libs/preprocessor" target="_top">Boost.Preprocessor</a> | |
160 | sequence syntax instead of comma-separated lists (in alphabetic order): | |
161 | </p> | |
162 | <div class="informaltable"><table class="table"> | |
163 | <colgroup><col></colgroup> | |
164 | <thead><tr><th> | |
165 | <p> | |
166 | Files | |
167 | </p> | |
168 | </th></tr></thead> | |
169 | <tbody> | |
170 | <tr><td> | |
171 | <p> | |
172 | <a href="../../../test/same_line_seq.cpp" target="_top"><code class="literal">same_line_seq.cpp</code></a> | |
173 | </p> | |
174 | </td></tr> | |
175 | <tr><td> | |
176 | <p> | |
177 | <a href="../../../example/scope_guard_seq.cpp" target="_top"><code class="literal">scope_guard_seq.cpp</code></a> | |
178 | </p> | |
179 | </td></tr> | |
180 | <tr><td> | |
181 | <p> | |
182 | <a href="../../../example/try_catch_seq.cpp" target="_top"><code class="literal">try_catch_seq.cpp</code></a> | |
183 | </p> | |
184 | </td></tr> | |
185 | <tr><td> | |
186 | <p> | |
187 | <a href="../../../test/world_checkpoint_all_seq.cpp" target="_top"><code class="literal">world_checkpoint_all_seq.cpp</code></a> | |
188 | </p> | |
189 | </td></tr> | |
190 | <tr><td> | |
191 | <p> | |
192 | <a href="../../../test/world_checkpoint_seq.cpp" target="_top"><code class="literal">world_checkpoint_seq.cpp</code></a> | |
193 | </p> | |
194 | </td></tr> | |
195 | <tr><td> | |
196 | <p> | |
197 | <a href="../../../test/world_this_seq.cpp" target="_top"><code class="literal">world_this_seq.cpp</code></a> | |
198 | </p> | |
199 | </td></tr> | |
200 | <tr><td> | |
201 | <p> | |
202 | <a href="../../../test/world_tpl_seq.cpp" target="_top"><code class="literal">world_tpl_seq.cpp</code></a> | |
203 | </p> | |
204 | </td></tr> | |
205 | </tbody> | |
206 | </table></div> | |
207 | <div class="footnotes"> | |
208 | <br><hr style="width:100; text-align:left;margin-left: 0"> | |
209 | <div id="ftn.scope_exit.no_variadic_macros.f0" class="footnote"><p><a href="#scope_exit.no_variadic_macros.f0" class="para"><sup class="para">[10] </sup></a> | |
210 | A C++ compiler does not support variadic macros if the <a href="http://www.boost.org/libs/config" target="_top">Boost.Config</a> | |
211 | macro <code class="computeroutput"><span class="identifier">BOOST_NO_CXX11_VARIADIC_MACROS</span></code> | |
212 | is defined for that compiler. | |
213 | </p></div> | |
214 | </div> | |
215 | </div> | |
216 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
217 | <td align="left"></td> | |
218 | <td align="right"><div class="copyright-footer">Copyright © 2006-2012 Alexander Nasonov, Lorenzo Caminiti<p> | |
219 | Distributed under the Boost Software License, Version 1.0 (see accompanying | |
220 | 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>) | |
221 | </p> | |
222 | </div></td> | |
223 | </tr></table> | |
224 | <hr> | |
225 | <div class="spirit-nav"> | |
226 | <a accesskey="p" href="alternatives.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../reference.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> | |
227 | </div> | |
228 | </body> | |
229 | </html> |