• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright (C) 2006-2009, 2012 Alexander Nasonov
3 // Copyright (C) 2012 Lorenzo Caminiti
4 // Distributed under the Boost Software License, Version 1.0
5 // (see accompanying file LICENSE_1_0.txt or a copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 // Home at http://www.boost.org/libs/scope_exit
8 
9 #include <boost/config.hpp>
10 #ifdef BOOST_NO_CXX11_VARIADIC_MACROS
11 #   error "variadic macros required"
12 #else
13 
14 #include <boost/scope_exit.hpp>
15 #include <boost/typeof/std/string.hpp>
16 #include <boost/typeof/std/map.hpp>
17 #include <map>
18 #include <string>
19 #include <utility>
20 
main(void)21 int main(void) {
22     //[scope_guard_decl
23     bool commit = false;
24     std::string currency("EUR");
25     double rate = 1.3326;
26     std::map<std::string, double> rates;
27     bool currency_rate_inserted =
28             rates.insert(std::make_pair(currency, rate)).second;
29     // Transaction...
30     //]
31 
32     //[scope_guard_exit
33     BOOST_SCOPE_EXIT(currency_rate_inserted, &commit, &rates, &currency) {
34         if(currency_rate_inserted && !commit) rates.erase(currency);
35     } BOOST_SCOPE_EXIT_END
36 
37     // ...
38 
39     commit = true;
40     //]
41 
42     return 0;
43 }
44 
45 #endif // variadic macros
46 
47