]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/phoenix/example/bind_goose.cpp
1 /*=============================================================================
3 Copyright (c) 2001-2004 Peter Dimov and Multi Media Ltd.
4 Copyright (c) 2001 David Abrahams
5 Copyright (c) 2005 Peter Dimov
7 Copyright (c) 2001-2010 Joel de Guzman
8 Copyright (c) 2010 Thomas Heller
10 Copyright (c) 2011 Paul Heil
11 Copyright (c) 2015 John Fletcher
13 Distributed under the Boost Software License, Version 1.0. (See accompanying
14 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
15 ==============================================================================*/
17 // This example is based on code by Paul Heil to be found here:
18 // http://www.codeproject.com/Tips/248492/How-does-boost-phoenix-improve-boost-bind
20 // Show different ways of using boost bind and phoenix to handle deletion.
25 #include <boost/function.hpp>
26 #include <boost/bind.hpp>
27 #include <boost/phoenix/core.hpp>
28 #include <boost/phoenix/bind.hpp>
29 #include <boost/phoenix/operator/comparison.hpp>
30 #include <boost/phoenix/stl/algorithm/transformation.hpp>
35 ////////////////////////////////////////////
36 // Set up the list here
37 ////////////////////////////////////////////
38 std::vector
< std::string
> make_list() {
39 std::vector
< std::string
> list
;
40 list
.push_back( "duck" );
41 list
.push_back( "duck" );
42 list
.push_back( "goose" );
45 //////////////////////////////////////////////
46 // First example using standard library only
47 //////////////////////////////////////////////
48 bool IsGoose( const std::string
& s
)
53 void delete_value1(std::vector
< std::string
> &list
)
55 list
.erase( std::remove_if( list
.begin(), list
.end(), IsGoose
), list
.end() );
58 void out_string(const std::string
&s
)
60 std::cout
<< s
<< std::endl
;
63 void show_list1( const std::vector
< std::string
> &list
)
65 std::for_each(list
.begin(), list
.end(), out_string
);
68 //////////////////////////////////////////////
69 // Second example using boost bind
70 //////////////////////////////////////////////
72 bool isValue(const std::string
&s1
, const std::string
&s2
)
77 void delete_value2(std::vector
< std::string
> &list
, const std::string
& value
)
84 isValue
, // &isValue works as well.
85 _1
, // Boost.Bind placeholder
86 boost::cref( value
) ) ),
90 ///////////////////////////////////////////////////////
91 // Third example using boost phoenix for the comparison
92 ///////////////////////////////////////////////////////
94 namespace phx
= boost::phoenix
;
95 using phx::placeholders::arg1
;
96 using phx::placeholders::arg2
;
98 void delete_value3(std::vector
< std::string
> &list
, const std::string
& value
)
100 list
.erase( std::remove_if(
103 // This needs header boost/phoenix/operator/comparison.
104 // arg1 is a Boost.Phoenix placeholder.
105 arg1
== phx::cref( value
) ),
109 //////////////////////////////////////////////////////////////
110 // Third example using boost phoenix for the algorithm as well
111 //////////////////////////////////////////////////////////////
113 void delete_value4(std::vector
< std::string
> &list
, const std::string
& value
)
115 // This need header boost/phoenix/stl/algorithm/transformation
116 list
.erase( phx::remove_if( arg1
, arg2
)
117 ( list
, arg1
== phx::cref( value
) ),
122 std::cout
<< "--------------------------------" << std::endl
;
123 std::cout
<< "Delete the goose examples." << std::endl
;
124 std::cout
<< "--------------------------------" << std::endl
;
125 std::string value
= "goose";
127 std::vector
< std::string
> list1
= make_list();
128 delete_value1(list1
);
130 std::cout
<< "--------------------------------" << std::endl
;
131 std::vector
< std::string
> list2
= make_list();
132 delete_value2(list2
,value
);
134 std::cout
<< "--------------------------------" << std::endl
;
135 std::vector
< std::string
> list3
= make_list();
136 delete_value3(list3
,value
);
138 std::cout
<< "--------------------------------" << std::endl
;
139 std::vector
< std::string
> list4
= make_list();
140 delete_value4(list4
,value
);
142 std::cout
<< "--------------------------------" << std::endl
;