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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// ------------------------------------------------------------------------------
// Tests for the Boost functional.hpp header file
//
// Note that functional.hpp relies on partial specialisation to be
// effective. If your compiler lacks this feature, very few of the
// tests would compile, and so have been excluded from the test.
// ------------------------------------------------------------------------------
// Copyright (c) 2000
// Cadenza New Zealand Ltd
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without
// fee, provided that the above copyright notice appears in all copies
// and that both the copyright notice and this permission notice
// appear in supporting documentation. Cadenza New Zealand Ltd makes
// no representations about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.
// ------------------------------------------------------------------------------
// $Id: function_test.cpp,v 1.2 2001/09/22 11:52:24 johnmaddock Exp $
// ------------------------------------------------------------------------------
// $Log: function_test.cpp,v $
// Revision 1.2 2001/09/22 11:52:24 johnmaddock
// Intel C++ fixes: no void return types supported.
//
// Revision 1.1.1.1 2000/07/07 16:04:18 beman
// 1.16.1 initial CVS checkin
//
// Revision 1.3 2000/06/26 09:44:01 mark
// Updated following feedback from Jens Maurer.
//
// Revision 1.2 2000/05/17 08:31:45 mark
// Added extra tests now that function traits work correctly.
// For compilers with no support for partial specialisation,
// excluded tests that won't work.
//
// Revision 1.1 2000/05/07 09:14:41 mark
// Initial revision
// ------------------------------------------------------------------------------
// To demonstrate what the boosted function object adapters do for
// you, try compiling with USE_STD defined. This will endeavour to
// use the standard function object adapters, but is likely to result
// in numerous errors due to the fact that you cannot have references
// to references.
#ifdef USE_STD
#include <functional>
#define boost std
#else
#include <boost/functional.hpp>
#endif
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
class Person
{
public:
Person() {}
Person(const char *n) : name(n) {}
const std::string &get_name() const { return name; }
void print(std::ostream &os) const { os << name << " "; }
void set_name(const std::string &n) { name = n; std::cout << name << " "; }
std::string clear_name() { std::string ret = name; name = ""; return ret; }
void do_something(int) const {}
bool is_fred() const { return name == "Fred"; }
private:
std::string name;
};
namespace
{
bool is_equal(const std::string &s1, const std::string &s2)
{
return s1 == s2;
}
bool is_betty(const std::string &s)
{
return s == "Betty";
}
void do_set_name(Person *p, const std::string &name)
{
p->set_name(name);
}
void do_set_name_ref(Person &p, const std::string &name)
{
p.set_name(name);
}
}
int main()
{
std::vector<Person> v1;
v1.push_back("Fred");
v1.push_back("Wilma");
v1.push_back("Barney");
v1.push_back("Betty");
const std::vector<Person> cv1(v1.begin(), v1.end());
std::vector<std::string> v2;
v2.push_back("Fred");
v2.push_back("Wilma");
v2.push_back("Barney");
v2.push_back("Betty");
Person person;
Person &r = person;
Person fred("Fred");
Person wilma("Wilma");
Person barney("Barney");
Person betty("Betty");
std::vector<Person*> v3;
v3.push_back(&fred);
v3.push_back(&wilma);
v3.push_back(&barney);
v3.push_back(&betty);
const std::vector<Person*> cv3(v3.begin(), v3.end());
std::vector<const Person*> v3c(v3.begin(), v3.end());
std::ostream &os = std::cout;
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL)
// unary_traits, unary_negate
std::transform(v2.begin(), v2.end(),
std::ostream_iterator<bool>(std::cout, " "),
boost::not1(is_betty));
std::cout << '\n';
std::transform(v1.begin(), v1.end(),
std::ostream_iterator<bool>(std::cout, " "),
boost::not1(boost::mem_fun_ref(&Person::is_fred)));
// binary_traits, binary_negate
std::cout << '\n';
std::transform(v2.begin(), v2.end(),
std::ostream_iterator<bool>(std::cout, " "),
boost::bind1st(boost::not2(is_equal), "Betty"));
std::cout << '\n';
std::transform(v2.begin(), v2.end(),
std::ostream_iterator<bool>(std::cout, " "),
boost::bind2nd(boost::not2(is_equal), "Betty"));
// pointer_to_unary_function
std::cout << '\n';
std::transform(v2.begin(), v2.end(),
std::ostream_iterator<bool>(std::cout, " "),
boost::not1(boost::ptr_fun(is_betty)));
// binary_traits, bind1st, bind2nd
std::cout << '\n';
std::transform(v2.begin(), v2.end(),
std::ostream_iterator<bool>(std::cout, " "),
boost::bind1st(is_equal, "Betty"));
std::cout << '\n';
std::transform(v2.begin(), v2.end(),
std::ostream_iterator<bool>(std::cout, " "),
boost::bind2nd(is_equal, "Betty"));
// pointer_to_binary_function, bind1st
std::cout << '\n';
std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name), &person));
std::cout << '\n';
std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), person));
std::cout << '\n';
std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), r));
// binary_traits
std::cout << '\n';
std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name, &person));
std::cout << '\n';
std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, person));
std::cout << '\n';
std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, r));
#endif
// const_mem_fun_t
std::cout << '\n';
std::transform(v3.begin(), v3.end(),
std::ostream_iterator<std::string>(std::cout, " "),
boost::mem_fun(&Person::get_name));
std::cout << '\n';
std::transform(cv3.begin(), cv3.end(),
std::ostream_iterator<std::string>(std::cout, " "),
boost::mem_fun(&Person::get_name));
std::cout << '\n';
std::transform(v3c.begin(), v3c.end(),
std::ostream_iterator<std::string>(std::cout, " "),
boost::mem_fun(&Person::get_name));
// const_mem_fun_ref_t
std::cout << '\n';
std::transform(v1.begin(), v1.end(),
std::ostream_iterator<std::string>(std::cout, " "),
boost::mem_fun_ref(&Person::get_name));
std::cout << '\n';
std::transform(cv1.begin(), cv1.end(),
std::ostream_iterator<std::string>(std::cout, " "),
boost::mem_fun_ref(&Person::get_name));
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// const_mem_fun1_t, bind2nd
std::cout << '\n';
std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), std::cout));
std::cout << '\n';
std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), os));
// const_mem_fun1_ref_t, bind2nd
std::cout << '\n';
std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), std::cout));
std::cout << '\n';
std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), os));
// mem_fun1_t, bind1st
std::cout << '\n';
std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun(&Person::set_name), &person));
// mem_fun1_ref_t, bind1st
std::cout << '\n';
std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), person));
std::cout << '\n';
std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), r));
#endif
// mem_fun_t
std::cout << '\n';
std::transform(v3.begin(), v3.end(), std::ostream_iterator<std::string>(std::cout, " "),
boost::mem_fun(&Person::clear_name));
// mem_fun_ref_t
std::cout << '\n';
std::transform(v1.begin(), v1.end(), std::ostream_iterator<std::string>(std::cout, " "),
boost::mem_fun_ref(&Person::clear_name));
std::cout << '\n';
return 0;
}
|