world_checkpoint.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/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. //[world_checkpoint
  47. void world::add_person(person const& a_person) {
  48. persons_.push_back(a_person);
  49. // This block must be no-throw.
  50. person& p = persons_.back();
  51. person::evolution_t checkpoint = p.evolution;
  52. BOOST_SCOPE_EXIT(checkpoint, &p, &persons_) {
  53. if(checkpoint == p.evolution) persons_.pop_back();
  54. } BOOST_SCOPE_EXIT_END
  55. // ...
  56. checkpoint = ++p.evolution;
  57. // Assign new identifier to the person.
  58. person::id_t const prev_id = p.id;
  59. p.id = next_id_++;
  60. BOOST_SCOPE_EXIT(checkpoint, &p, &next_id_, prev_id) {
  61. if(checkpoint == p.evolution) {
  62. next_id_ = p.id;
  63. p.id = prev_id;
  64. }
  65. } BOOST_SCOPE_EXIT_END
  66. // ...
  67. checkpoint = ++p.evolution;
  68. }
  69. //]
  70. int main(void) {
  71. person adam, eva;
  72. std::ostringstream oss;
  73. oss << adam;
  74. std::cout << oss.str() << std::endl;
  75. BOOST_TEST(oss.str() == "person(0, 0)");
  76. oss.str("");
  77. oss << eva;
  78. std::cout << oss.str() << std::endl;
  79. BOOST_TEST(oss.str() == "person(0, 0)");
  80. world w;
  81. w.add_person(adam);
  82. w.add_person(eva);
  83. oss.str("");
  84. oss << w;
  85. std::cout << oss.str() << std::endl;
  86. BOOST_TEST(oss.str() == "world(3, { person(1, 2), person(2, 2), })");
  87. return boost::report_errors();
  88. }
  89. #endif // variadic macros