1 /*=============================================================================
2 For Boost Bind:
3 Copyright (c) 2001-2004 Peter Dimov and Multi Media Ltd.
4 Copyright (c) 2001 David Abrahams
5 Copyright (c) 2005 Peter Dimov
6 For Boost Phoenix:
7 Copyright (c) 2001-2010 Joel de Guzman
8 Copyright (c) 2010 Thomas Heller
9 For the example:
10 Copyright (c) 2011 Paul Heil
11 Copyright (c) 2015 John Fletcher
12
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 ==============================================================================*/
16 // bind_goose.cpp
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
19 //
20 // Show different ways of using boost bind and phoenix to handle deletion.
21 //
22
23
24 #include <iostream>
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>
31 #include <functional>
32 #include <string>
33 #include <vector>
34
35 ////////////////////////////////////////////
36 // Set up the list here
37 ////////////////////////////////////////////
make_list()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" );
43 return list;
44 }
45 //////////////////////////////////////////////
46 // First example using standard library only
47 //////////////////////////////////////////////
IsGoose(const std::string & s)48 bool IsGoose( const std::string& s )
49 {
50 return s == "goose";
51 }
52
delete_value1(std::vector<std::string> & list)53 void delete_value1(std::vector< std::string > &list )
54 {
55 list.erase( std::remove_if( list.begin(), list.end(), IsGoose ), list.end() );
56 }
57
out_string(const std::string & s)58 void out_string(const std::string &s)
59 {
60 std::cout << s << std::endl;
61 }
62
show_list1(const std::vector<std::string> & list)63 void show_list1( const std::vector< std::string > &list )
64 {
65 std::for_each(list.begin(), list.end(), out_string);
66 }
67
68 //////////////////////////////////////////////
69 // Second example using boost bind
70 //////////////////////////////////////////////
71
isValue(const std::string & s1,const std::string & s2)72 bool isValue(const std::string &s1, const std::string &s2)
73 {
74 return s1==s2;
75 }
76
delete_value2(std::vector<std::string> & list,const std::string & value)77 void delete_value2(std::vector< std::string > &list, const std::string & value)
78 {
79 list.erase(
80 std::remove_if(
81 list.begin(),
82 list.end(),
83 boost::bind(
84 isValue, // &isValue works as well.
85 _1, // Boost.Bind placeholder
86 boost::cref( value ) ) ),
87 list.end() );
88 }
89
90 ///////////////////////////////////////////////////////
91 // Third example using boost phoenix for the comparison
92 ///////////////////////////////////////////////////////
93
94 namespace phx = boost::phoenix;
95 using phx::placeholders::arg1;
96 using phx::placeholders::arg2;
97
delete_value3(std::vector<std::string> & list,const std::string & value)98 void delete_value3(std::vector< std::string > &list, const std::string & value)
99 {
100 list.erase( std::remove_if(
101 list.begin(),
102 list.end(),
103 // This needs header boost/phoenix/operator/comparison.
104 // arg1 is a Boost.Phoenix placeholder.
105 arg1 == phx::cref( value ) ),
106 list.end() );
107 }
108
109 //////////////////////////////////////////////////////////////
110 // Third example using boost phoenix for the algorithm as well
111 //////////////////////////////////////////////////////////////
112
delete_value4(std::vector<std::string> & list,const std::string & value)113 void delete_value4(std::vector< std::string > &list, const std::string & value)
114 {
115 // This need header boost/phoenix/stl/algorithm/transformation
116 list.erase( phx::remove_if( arg1, arg2 )
117 ( list, arg1 == phx::cref( value ) ),
118 list.end() );
119 }
120
main()121 int main() {
122 std::cout << "--------------------------------" << std::endl;
123 std::cout << "Delete the goose examples." << std::endl;
124 std::cout << "--------------------------------" << std::endl;
125 std::string value = "goose";
126
127 std::vector< std::string > list1 = make_list();
128 delete_value1(list1);
129 show_list1(list1);
130 std::cout << "--------------------------------" << std::endl;
131 std::vector< std::string > list2 = make_list();
132 delete_value2(list2,value);
133 show_list1(list2);
134 std::cout << "--------------------------------" << std::endl;
135 std::vector< std::string > list3 = make_list();
136 delete_value3(list3,value);
137 show_list1(list3);
138 std::cout << "--------------------------------" << std::endl;
139 std::vector< std::string > list4 = make_list();
140 delete_value4(list4,value);
141 show_list1(list4);
142 std::cout << "--------------------------------" << std::endl;
143 return 0;
144 }
145