scope_guard.cpp 1.1 KB

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