world_this_seq.cpp 1.2 KB

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