/* Copyright 2016-2017 Joaquin M Lopez Munoz. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * See http://www.boost.org/libs/poly_collection for library home page. */ /* segment-specific operations */ #include #include #include #include #include #include #include "rolegame.hpp" std::ostream& operator<<(std::ostream& os,const sprite& s) { s.render(os); return os; } std::ostream& operator<<(std::ostream& os,const window& w) { w.display(os); return os; } int main() { boost::base_collection c; //[segmented_structure_1 //= std::unique_ptr make_sprite() //= { //<- auto make_sprite=[]()->std::unique_ptr{ //-> static std::mt19937 gen{92748}; static std::discrete_distribution<> rnd{{1,1,1}}; static int id=0; switch(rnd(gen)){ //<- default: //-> case 0: return std::make_unique(id++);break; case 1: return std::make_unique(id++);break; case 2: return std::make_unique(id++);break; } //<- }; //-> //= } //= ... //= //<- try{ //-> for(int i=0;i<8;++i)c.insert(*make_sprite()); // throws boost::poly_collection::unregistered_type //<- }catch(boost::poly_collection::unregistered_type&){} //-> //] //[segmented_structure_2 std::cout<()<<"\n"; // prints 0 std::cout<(); // everything works fine now for(int i=0;i<8;++i)c.insert(*make_sprite()); //] using renderable=boost::type_erasure::ostreamable<>; //[segmented_structure_4 boost::any_collection c1,c2; //= ... // populate c2 //= //<- c2.insert(window{"pop-up"}); try{ //-> c1.insert(*c2.begin()); // throws: actual type of *c2.begin() not known by c1 //<- }catch(boost::poly_collection::unregistered_type&){} //-> //] //[segmented_structure_5 //= ... // populate c with 8 assorted entities //= std::cout<()<<"\n"; // 2 juggernauts std::cout<(); // remove juggenauts only std::cout<()<<"\n"; // 1 (no juggernauts left) std::cout<render(std::cout); comma=","; } std::cout<<"\n"; //] //[segmented_structure_7 /*=const char**/ comma=""; for(const auto& x:c.segment(typeid(warrior))){ std::cout<(),last=c.end(); first!=last;++first){ first->rank.insert(0,"super"); std::cout<render(std::cout); comma=","; } std::cout<<"\n"; // range-based for loop alternative /*=const char**/ comma=""; for(auto& x:c.segment()){ x.rank.insert(0,"super"); //<- auto it=x.rank.begin(); x.rank.erase(it,it+5); // undo previos op, 5==len("super"); //-> std::cout<(100); // no reallocation till we exceed 100 goblins std::cout<()<<"\n"; // prints 100 //] //[segmented_structure_11 c.reserve(1000); // reserve(1000) for each segment std::cout<()<<", " <()<<", " <()<<"\n"; // prints 1000, 1000, 1000 //] }