]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/poly_collection/example/segmented_structure.cpp
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)
6 * See http://www.boost.org/libs/poly_collection for library home page.
9 /* segment-specific operations */
12 #include <boost/poly_collection/any_collection.hpp>
13 #include <boost/poly_collection/base_collection.hpp>
14 #include <boost/type_erasure/operators.hpp>
17 #include "rolegame.hpp"
19 std::ostream
& operator<<(std::ostream
& os
,const sprite
& s
)
25 std::ostream
& operator<<(std::ostream
& os
,const window
& w
)
33 boost::base_collection
<sprite
> c
;
35 //[segmented_structure_1
36 //= std::unique_ptr<sprite> make_sprite()
39 auto make_sprite
=[]()->std::unique_ptr
<sprite
>{
41 static std::mt19937 gen
{92748};
42 static std::discrete_distribution
<> rnd
{{1,1,1}};
49 case 0: return std::make_unique
<warrior
>(id
++);break;
50 case 1: return std::make_unique
<juggernaut
>(id
++);break;
51 case 2: return std::make_unique
<goblin
>(id
++);break;
62 for(int i
=0;i
<8;++i
)c
.insert(*make_sprite());
63 // throws boost::poly_collection::unregistered_type
65 }catch(boost::poly_collection::unregistered_type
&){}
69 //[segmented_structure_2
70 std::cout
<<c
.is_registered
<warrior
>()<<"\n"; // prints 0
71 std::cout
<<c
.is_registered(typeid(warrior
))<<"\n"; // alternate syntax
74 //[segmented_structure_3
75 c
.register_types
<warrior
,juggernaut
,goblin
>();
76 // everything works fine now
77 for(int i
=0;i
<8;++i
)c
.insert(*make_sprite());
80 using renderable
=boost::type_erasure::ostreamable
<>;
81 //[segmented_structure_4
82 boost::any_collection
<renderable
> c1
,c2
;
83 //= ... // populate c2
86 c2
.insert(window
{"pop-up"});
89 c1
.insert(*c2
.begin()); // throws: actual type of *c2.begin() not known by c1
91 }catch(boost::poly_collection::unregistered_type
&){}
95 //[segmented_structure_5
96 //= ... // populate c with 8 assorted entities
98 std::cout
<<c
.size()<<"\n"; // 8 sprites
99 std::cout
<<c
.size
<juggernaut
>()<<"\n"; // 2 juggernauts
100 std::cout
<<c
.size(typeid(juggernaut
))<<"\n"; // alternate syntax
101 c
.clear
<juggernaut
>(); // remove juggenauts only
102 std::cout
<<c
.empty
<juggernaut
>()<<"\n"; // 1 (no juggernauts left)
103 std::cout
<<c
.size()<<"\n"; // 6 sprites remaining
106 //[segmented_structure_6
107 const char* comma
="";
108 for(auto first
=c
.begin(typeid(warrior
)),last
=c
.end(typeid(warrior
));
109 first
!=last
;++first
){
111 first
->render(std::cout
);
117 //[segmented_structure_7
118 /*=const char**/ comma
="";
119 for(const auto& x
:c
.segment(typeid(warrior
))){
127 //[segmented_structure_8
128 /*=const char**/ comma
="";
129 for(auto first
=c
.begin
<warrior
>(),last
=c
.end
<warrior
>();
130 first
!=last
;++first
){
131 first
->rank
.insert(0,"super");
133 first
->render(std::cout
);
138 // range-based for loop alternative
140 /*=const char**/ comma
="";
141 for(auto& x
:c
.segment
<warrior
>()){
142 x
.rank
.insert(0,"super");
144 auto it
=x
.rank
.begin();
145 x
.rank
.erase(it
,it
+5); // undo previos op, 5==len("super");
156 //[segmented_structure_9
157 const char* comma
="";
158 for(auto seg
:c
.segment_traversal()){
170 //[segmented_structure_10
171 c
.reserve
<goblin
>(100); // no reallocation till we exceed 100 goblins
172 std::cout
<<c
.capacity
<goblin
>()<<"\n"; // prints 100
175 //[segmented_structure_11
176 c
.reserve(1000); // reserve(1000) for each segment
177 std::cout
<<c
.capacity
<warrior
>()<<", "
178 <<c
.capacity
<juggernaut
>()<<", "
179 <<c
.capacity
<goblin
>()<<"\n"; // prints 1000, 1000, 1000