• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* basic usage of boost::any_collection */
10 
11 #include <boost/poly_collection/any_collection.hpp>
12 #include <boost/type_erasure/operators.hpp>
13 #include <random>
14 #include "rolegame.hpp"
15 
16 //[basic_any_1
operator <<(std::ostream & os,const sprite & s)17 std::ostream& operator<<(std::ostream& os,const sprite& s)
18 {
19   s.render(os);
20   return os;
21 }
22 
23 // std::string already has a suitable operator<<
24 
operator <<(std::ostream & os,const window & w)25 std::ostream& operator<<(std::ostream& os,const window& w)
26 {
27   w.display(os);
28   return os;
29 }
30 //]
31 
main()32 int main()
33 {
34 //[basic_any_2
35 //=  #include <boost/poly_collection/any_collection.hpp>
36 //=  #include <boost/type_erasure/operators.hpp>
37 //=  ...
38 //=
39   using renderable=boost::type_erasure::ostreamable<>;
40   boost::any_collection<renderable> c;
41 //]
42 
43 //[basic_any_3
44   // populate with sprites
45   std::mt19937                 gen{92748}; // some arbitrary random seed
46   std::discrete_distribution<> rnd{{1,1,1}};
47   for(int i=0;i<4;++i){        // assign each type with 1/3 probability
48     switch(rnd(gen)){
49       case 0: c.insert(warrior{i});break;
50       case 1: c.insert(juggernaut{i});break;
51       case 2: c.insert(goblin{i});break;
52     }
53   }
54 
55   // populate with messages
56   c.insert(std::string{"\"stamina: 10,000\""});
57   c.insert(std::string{"\"game over\""});
58 
59   // populate with windows
60   c.insert(window{"pop-up 1"});
61   c.insert(window{"pop-up 2"});
62 //]
63 
64 
65 //[basic_any_4
66   const char* comma="";
67   for(const auto& r:c){
68     std::cout<<comma<<r;
69     comma=",";
70   }
71   std::cout<<"\n";
72 //]
73 }
74