| 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 | |
| 9 | /* Boost.PolyCollection exceptions */ |
| 10 | |
| 11 | #include <algorithm> |
| 12 | #include <array> |
| 13 | #include <boost/poly_collection/base_collection.hpp> |
| 14 | #include <random> |
| 15 | #include "rolegame.hpp" |
| 16 | |
| 17 | int main() |
| 18 | { |
| 19 | boost::base_collection<sprite> c,c2; |
| 20 | |
| 21 | // populate c |
| 22 | |
| 23 | std::mt19937 gen{92748}; // some arbitrary random seed |
| 24 | std::discrete_distribution<> rnd{{1,1,1}}; |
| 25 | for(int i=0;i<8;++i){ // assign each type with 1/3 probability |
| 26 | switch(rnd(gen)){ |
| 27 | case 0: c.insert(x: warrior{i});break; |
| 28 | case 1: c.insert(x: juggernaut{i});break; |
| 29 | case 2: c.insert(x: goblin{i});break; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | auto render=[](const boost::base_collection<sprite>& c){ |
| 34 | const char* comma=""; |
| 35 | for(const sprite& s:c){ |
| 36 | std::cout<<comma; |
| 37 | s.render(os&: std::cout); |
| 38 | comma=","; |
| 39 | } |
| 40 | std::cout<<"\n"; |
| 41 | }; |
| 42 | render(c); |
| 43 | |
| 44 | try{ |
| 45 | //[exceptions_1 |
| 46 | c.insert(x: elf{0}); // no problem |
| 47 | //= ... |
| 48 | //= |
| 49 | c2=c; // throws boost::poly_collection::not_copy_constructible |
| 50 | //] |
| 51 | }catch(boost::poly_collection::not_copy_constructible&){} |
| 52 | |
| 53 | try{ |
| 54 | //[exceptions_2 |
| 55 | c.clear<elf>(); // get rid of non-copyable elfs |
| 56 | c2=c; // now it works |
| 57 | // check that the two are indeed equal |
| 58 | std::cout<<(c==c2)<<"\n"; |
| 59 | // throws boost::poly_collection::not_equality_comparable |
| 60 | //] |
| 61 | }catch(boost::poly_collection::not_equality_comparable&){} |
| 62 | } |
| 63 |
