world_tpl_seq.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 <vector>
  13. struct person {};
  14. BOOST_TYPEOF_REGISTER_TYPE(person)
  15. template<typename 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_TEMPLATE(world, 1)
  23. template<typename Person>
  24. void world<Person>::add_person(Person const& a_person) {
  25. bool commit = false;
  26. persons_.push_back(a_person);
  27. BOOST_SCOPE_EXIT_TPL( (&commit) (this_) ) {
  28. if(!commit) this_->persons_.pop_back();
  29. } BOOST_SCOPE_EXIT_END
  30. // ...
  31. commit = true;
  32. }
  33. int main(void) {
  34. world<person> w;
  35. person p;
  36. w.add_person(p);
  37. BOOST_TEST(w.population() == 1);
  38. return boost::report_errors();
  39. }