1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* 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 <algorithm>
#include <boost/poly_collection/any_collection.hpp>
#include <boost/poly_collection/base_collection.hpp>
#include <boost/type_erasure/operators.hpp>
#include <memory>
#include <random>
#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<sprite> c;
//[segmented_structure_1
//= std::unique_ptr<sprite> make_sprite()
//= {
//<-
auto make_sprite=[]()->std::unique_ptr<sprite>{
//->
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<warrior>(id++);break;
case 1: return std::make_unique<juggernaut>(id++);break;
case 2: return std::make_unique<goblin>(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<<c.is_registered<warrior>()<<"\n"; // prints 0
std::cout<<c.is_registered(typeid(warrior))<<"\n"; // alternate syntax
//]
//[segmented_structure_3
c.register_types<warrior,juggernaut,goblin>();
// 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<renderable> 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<<c.size()<<"\n"; // 8 sprites
std::cout<<c.size<juggernaut>()<<"\n"; // 2 juggernauts
std::cout<<c.size(typeid(juggernaut))<<"\n"; // alternate syntax
c.clear<juggernaut>(); // remove juggenauts only
std::cout<<c.empty<juggernaut>()<<"\n"; // 1 (no juggernauts left)
std::cout<<c.size()<<"\n"; // 6 sprites remaining
//]
//[segmented_structure_6
const char* comma="";
for(auto first=c.begin(typeid(warrior)),last=c.end(typeid(warrior));
first!=last;++first){
std::cout<<comma;
first->render(std::cout);
comma=",";
}
std::cout<<"\n";
//]
//[segmented_structure_7
/*=const char**/ comma="";
for(const auto& x:c.segment(typeid(warrior))){
std::cout<<comma;
x.render(std::cout);
comma=",";
}
std::cout<<"\n";
//]
//[segmented_structure_8
/*=const char**/ comma="";
for(auto first=c.begin<warrior>(),last=c.end<warrior>();
first!=last;++first){
first->rank.insert(0,"super");
std::cout<<comma;
first->render(std::cout);
comma=",";
}
std::cout<<"\n";
// range-based for loop alternative
/*=const char**/ comma="";
for(auto& x:c.segment<warrior>()){
x.rank.insert(0,"super");
//<-
auto it=x.rank.begin();
x.rank.erase(it,it+5); // undo previos op, 5==len("super");
//->
std::cout<<comma;
x.render(std::cout);
comma=",";
}
std::cout<<"\n";
//]
auto render=[&](){
//[segmented_structure_9
const char* comma="";
for(auto seg:c.segment_traversal()){
for(sprite& s:seg){
std::cout<<comma;
s.render(std::cout);
comma=",";
}
}
std::cout<<"\n";
//]
};
render();
//[segmented_structure_10
c.reserve<goblin>(100); // no reallocation till we exceed 100 goblins
std::cout<<c.capacity<goblin>()<<"\n"; // prints 100
//]
//[segmented_structure_11
c.reserve(1000); // reserve(1000) for each segment
std::cout<<c.capacity<warrior>()<<", "
<<c.capacity<juggernaut>()<<", "
<<c.capacity<goblin>()<<"\n"; // prints 1000, 1000, 1000
//]
}
|