world_checkpoint_all_seq.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_LAMBDAS
  9. # error "lambda functions required"
  10. #else
  11. #include <boost/scope_exit.hpp>
  12. #include <boost/foreach.hpp>
  13. #include <boost/typeof/typeof.hpp>
  14. #include <boost/typeof/std/vector.hpp>
  15. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  16. #include <boost/detail/lightweight_test.hpp>
  17. #include <vector>
  18. #include <iostream>
  19. #include <sstream>
  20. struct person {
  21. typedef unsigned int id_t;
  22. typedef unsigned int evolution_t;
  23. id_t id;
  24. evolution_t evolution;
  25. person(void) : id(0), evolution(0) {}
  26. friend std::ostream& operator<<(std::ostream& o, person const& p) {
  27. return o << "person(" << p.id << ", " << p.evolution << ")";
  28. }
  29. };
  30. BOOST_TYPEOF_REGISTER_TYPE(person)
  31. struct world {
  32. world(void) : next_id_(1) {}
  33. void add_person(person const& a_person);
  34. friend std::ostream& operator<<(std::ostream& o, world const& w) {
  35. o << "world(" << w.next_id_ << ", {";
  36. BOOST_FOREACH(person const& p, w.persons_) {
  37. o << " " << p << ", ";
  38. }
  39. return o << "})";
  40. }
  41. private:
  42. person::id_t next_id_;
  43. std::vector<person> persons_;
  44. };
  45. BOOST_TYPEOF_REGISTER_TYPE(world)
  46. void world::add_person(person const& a_person) {
  47. persons_.push_back(a_person);
  48. // This block must be no-throw.
  49. person& p = persons_.back();
  50. person::evolution_t checkpoint = p.evolution;
  51. BOOST_SCOPE_EXIT_ALL( (&) (checkpoint) (this) ) {
  52. if(checkpoint == p.evolution) this->persons_.pop_back();
  53. };
  54. // ...
  55. checkpoint = ++p.evolution;
  56. // Assign new identifier to the person.
  57. person::id_t const prev_id = p.id;
  58. p.id = next_id_++;
  59. BOOST_SCOPE_EXIT_ALL( (=) (&p) ) {
  60. if(checkpoint == p.evolution) {
  61. this->next_id_ = p.id;
  62. p.id = prev_id;
  63. }
  64. };
  65. // ...
  66. checkpoint = ++p.evolution;
  67. }
  68. int main(void) {
  69. person adam, eva;
  70. std::ostringstream oss;
  71. oss << adam;
  72. std::cout << oss.str() << std::endl;
  73. BOOST_TEST(oss.str() == "person(0, 0)");
  74. oss.str("");
  75. oss << eva;
  76. std::cout << oss.str() << std::endl;
  77. BOOST_TEST(oss.str() == "person(0, 0)");
  78. world w;
  79. w.add_person(adam);
  80. w.add_person(eva);
  81. oss.str("");
  82. oss << w;
  83. std::cout << oss.str() << std::endl;
  84. BOOST_TEST(oss.str() == "world(3, { person(1, 2), person(2, 2), })");
  85. return boost::report_errors();
  86. }
  87. #endif // variadic macros