]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/poly_collection/example/basic_function.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 /* basic usage of boost::function_collection */
11 #include <boost/poly_collection/function_collection.hpp>
16 #include "rolegame.hpp"
21 std::vector
<std::unique_ptr
<sprite
>> sprs
;
22 std::vector
<std::string
> msgs
;
23 std::vector
<window
> wnds
;
27 std::mt19937 gen
{92748}; // some arbitrary random seed
28 std::discrete_distribution
<> rnd
{{1,1,1}};
29 for(int i
=0;i
<4;++i
){ // assign each type with 1/3 probability
31 case 0: sprs
.push_back(std::make_unique
<warrior
>(i
));;break;
32 case 1: sprs
.push_back(std::make_unique
<juggernaut
>(i
));break;
33 case 2: sprs
.push_back(std::make_unique
<goblin
>(i
));break;
38 msgs
.push_back("\"stamina: 10,000\"");
39 msgs
.push_back("\"game over\"");
42 wnds
.emplace_back("pop-up 1");
43 wnds
.emplace_back("pop-up 2");
46 //= #include <boost/poly_collection/function_collection.hpp>
49 // function signature accepting std::ostream& and returning nothing
50 using render_callback
=void(std::ostream
&);
52 boost::function_collection
<render_callback
> c
;
57 auto render_sprite
=[](const sprite
& s
){
59 //= auto render_sprite(const sprite& s){
60 return [&](std::ostream
& os
){s
.render(os
);};
64 auto render_message
=[](const std::string
& m
){
66 //= auto render_message(const std::string& m){
67 return [&](std::ostream
& os
){os
<<m
;};
71 auto render_window
=[](const window
& w
){
73 //= auto render_window(const window& w){
74 return [&](std::ostream
& os
){w
.display(os
);};
78 for(const auto& ps
:sprs
)c
.insert(render_sprite(*ps
));
79 for(const auto& m
:msgs
)c
.insert(render_message(m
));
80 for(const auto& w
:wnds
)c
.insert(render_window(w
));
85 for(const auto& cbk
:c
){
95 cbk(std::cout
); // renders first element to std::cout
96 std::function
<render_callback
> f
=cbk
;
97 f(std::cout
); // exactly the same
101 //= *c.begin()=render_message("last minute message"); // compile-time error