• 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/scope_exit.hpp>
10 #include <boost/typeof/typeof.hpp>
11 #include <boost/typeof/std/vector.hpp>
12 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
13 #include <boost/detail/lightweight_test.hpp>
14 #include <boost/config.hpp>
15 #include <vector>
16 
17 struct person {};
18 BOOST_TYPEOF_REGISTER_TYPE(person)
19 
20 struct world {
21     void add_person(person const& a_person);
populationworld22     size_t population(void) const { return persons_.size(); }
23 
24 private:
25     std::vector<person> persons_;
26 };
BOOST_TYPEOF_REGISTER_TYPE(world)27 BOOST_TYPEOF_REGISTER_TYPE(world)
28 
29 void world::add_person(person const& a_person) {
30     bool commit = false;
31 
32     persons_.push_back(a_person);
33     BOOST_SCOPE_EXIT( (&commit) (this_) ) {
34         if(!commit) this_->persons_.pop_back();
35     } BOOST_SCOPE_EXIT_END
36 
37     // ...
38 
39     commit = true;
40 }
41 
main(void)42 int main(void) {
43     world w;
44     person p;
45     w.add_person(p);
46     BOOST_TEST(w.population() == 1);
47     return boost::report_errors();
48 }
49 
50