exceptions.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright 2016-2017 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. /* Boost.PolyCollection exceptions */
  9. #include <algorithm>
  10. #include <array>
  11. #include <boost/poly_collection/base_collection.hpp>
  12. #include <random>
  13. #include "rolegame.hpp"
  14. int main()
  15. {
  16. boost::base_collection<sprite> c,c2;
  17. // populate c
  18. std::mt19937 gen{92748}; // some arbitrary random seed
  19. std::discrete_distribution<> rnd{{1,1,1}};
  20. for(int i=0;i<8;++i){ // assign each type with 1/3 probability
  21. switch(rnd(gen)){
  22. case 0: c.insert(warrior{i});break;
  23. case 1: c.insert(juggernaut{i});break;
  24. case 2: c.insert(goblin{i});break;
  25. }
  26. }
  27. auto render=[](const boost::base_collection<sprite>& c){
  28. const char* comma="";
  29. for(const sprite& s:c){
  30. std::cout<<comma;
  31. s.render(std::cout);
  32. comma=",";
  33. }
  34. std::cout<<"\n";
  35. };
  36. render(c);
  37. try{
  38. //[exceptions_1
  39. c.insert(elf{0}); // no problem
  40. //= ...
  41. //=
  42. c2=c; // throws boost::poly_collection::not_copy_constructible
  43. //]
  44. }catch(boost::poly_collection::not_copy_constructible&){}
  45. try{
  46. //[exceptions_2
  47. c.clear<elf>(); // get rid of non-copyable elfs
  48. c2=c; // now it works
  49. // check that the two are indeed equal
  50. std::cout<<(c==c2)<<"\n";
  51. // throws boost::poly_collection::not_equality_comparable
  52. //]
  53. }catch(boost::poly_collection::not_equality_comparable&){}
  54. }