1 // ------------------------------------------------------------------------------
2 // Copyright (c) 2000 Cadenza New Zealand Ltd
3 // Distributed under the Boost Software License, Version 1.0. (See accompany-
4 // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 // ------------------------------------------------------------------------------
6 // Tests for the Boost functional.hpp header file
7 //
8 // Note that functional.hpp relies on partial specialisation to be
9 // effective. If your compiler lacks this feature, very few of the
10 // tests would compile, and so have been excluded from the test.
11 // ------------------------------------------------------------------------------
12 // $Id$
13 // ------------------------------------------------------------------------------
14 // $Log$
15 // Revision 1.3 2006/12/02 13:57:32 andreas_huber69
16 // Fixed license & copyright issues.
17 //
18 // From Mark Rodgers Fri Dec 1 12:59:14 2006
19 // X-Apparently-To: ahd6974-boostorg -at- yahoo.com via 68.142.206.160; Fri, 01 Dec 2006 12:59:41 -0800
20 // X-Originating-IP: [195.112.4.54]
21 // Return-Path: <mark.rodgers -at- cadenza.co.nz>
22 // Authentication-Results: mta550.mail.mud.yahoo.com from=cadenza.co.nz; domainkeys=neutral (no sig)
23 // Received: from 195.112.4.54 (EHLO smtp.nildram.co.uk) (195.112.4.54) by mta550.mail.mud.yahoo.com with SMTP; Fri, 01 Dec 2006 12:59:40 -0800
24 // Received: from snagglepuss.cadenza.co.nz (81-6-246-87.dyn.gotadsl.co.uk [81.6.246.87]) by smtp.nildram.co.uk (Postfix) with ESMTP id D32EA2B6D8C for <ahd6974-boostorg -at- yahoo.com>; Fri, 1 Dec 2006 20:59:35 +0000 (GMT)
25 // Received: from penfold.cadenza.co.nz ([192.168.55.56]) by snagglepuss.cadenza.co.nz with esmtp (Exim 4.63) (envelope-from <mark.rodgers -at- cadenza.co.nz>) id J9M4Y9-0009TO-9K for ahd6974-boostorg -at- yahoo.com; Fri, 01 Dec 2006 20:58:57 +0000
26 // Message-ID: <457097A2.1090305@cadenza.co.nz>
27 // Date: Fri, 01 Dec 2006 20:59:14 +0000
28 // From: "Mark Rodgers" <mark.rodgers -at- cadenza.co.nz>
29 // User-Agent: Thunderbird 1.5.0.8 (Macintosh/20061025)
30 // MIME-Version: 1.0
31 // To: ahd6974-boostorg -at- yahoo.com [Edit - Delete]
32 // Subject: Re: [boost] Reminder: Need your permission to correct license & copyright issues
33 // References: <379990.36007.qm@web33507.mail.mud.yahoo.com>
34 // In-Reply-To: <379990.36007.qm@web33507.mail.mud.yahoo.com>
35 // Content-Type: text/plain; charset=ISO-8859-1; format=flowed
36 // Content-Transfer-Encoding: 7bit
37 // Content-Length: 812
38 // Gidday Andreas
39 //
40 // Sure that's fine. I'm happy for you to do 1, 2 and 3.
41 //
42 // Regards
43 // Mark
44 //
45 // Andreas Huber wrote:
46 // > Hello Mark
47 // >
48 // > Quite a while ago it was decided that every file that goes into the
49 // > 1.34 release of the Boost distribution (www.boost.org) needs uniform
50 // > license and copyright information. For more information please see:
51 // >
52 // > <http://www.boost.org/more/license_info.html>
53 // >
54 // > You are receiving this email because several files you contributed
55 // > lack such information or have an old license:
56 // >
57 // > boost/functional/functional.hpp
58 // > boost/libs/functional/binders.html
59 // > boost/libs/functional/function_test.cpp
60 // > boost/libs/functional/function_traits.html
61 // > boost/libs/functional/index.html
62 // > boost/libs/functional/mem_fun.html
63 // > boost/libs/functional/negators.html
64 // > boost/libs/functional/ptr_fun.html
65 // > boost/people/mark_rodgers.htm
66 // >
67 // > I therefore kindly ask you to grant the permission to do the
68 // > following:
69 // >
70 // > 1. For the files above that already have a license text (all except
71 // > mark_rodgers.htm), replace the license text with:
72 // >
73 // > "Distributed under the Boost Software License, Version 1.0. (See
74 // > accompanying file LICENSE_1_0.txt or copy at
75 // > http://www.boost.org/LICENSE_1_0.txt)"
76 // >
77 // > 2. For the file that does not yet have a license and copyright
78 // > (mark_rodgers.htm) add the same license text as under 1. and add the
79 // > following copyright:
80 // >
81 // > "(c) Copyright Mark Rodgers 2000"
82 // >
83 // > 3. (Optional) I would also want to convert all HTML files to conform
84 // > the HTML 4.01 Standard by running them through HTML Tidy, see
85 // > <http://tidy.sf.net>
86 // >
87 // > It would be great if you could grant me permission to do 1 & 2 and
88 // > optionally also 3.
89 // >
90 // > Thank you!
91 // >
92 // > Regards,
93 // >
94 // > Andreas Huber
95 // >
96 //
97 // Revision 1.2 2001/09/22 11:52:24 johnmaddock
98 // Intel C++ fixes: no void return types supported.
99 //
100 // Revision 1.1.1.1 2000/07/07 16:04:18 beman
101 // 1.16.1 initial CVS checkin
102 //
103 // Revision 1.3 2000/06/26 09:44:01 mark
104 // Updated following feedback from Jens Maurer.
105 //
106 // Revision 1.2 2000/05/17 08:31:45 mark
107 // Added extra tests now that function traits work correctly.
108 // For compilers with no support for partial specialisation,
109 // excluded tests that won't work.
110 //
111 // Revision 1.1 2000/05/07 09:14:41 mark
112 // Initial revision
113 // ------------------------------------------------------------------------------
114
115 // To demonstrate what the boosted function object adapters do for
116 // you, try compiling with USE_STD defined. This will endeavour to
117 // use the standard function object adapters, but is likely to result
118 // in numerous errors due to the fact that you cannot have references
119 // to references.
120 #ifdef USE_STD
121 #include <functional>
122 #define boost std
123 #else
124 #include <boost/functional.hpp>
125 #endif
126
127 #include <algorithm>
128 #include <iostream>
129 #include <iterator>
130 #include <string>
131 #include <vector>
132
133 class Person
134 {
135 public:
Person()136 Person() {}
Person(const char * n)137 Person(const char *n) : name(n) {}
138
get_name() const139 const std::string &get_name() const { return name; }
print(std::ostream & os) const140 void print(std::ostream &os) const { os << name << " "; }
set_name(const std::string & n)141 void set_name(const std::string &n) { name = n; std::cout << name << " "; }
clear_name()142 std::string clear_name() { std::string ret = name; name = ""; return ret; }
do_something(int) const143 void do_something(int) const {}
144
is_fred() const145 bool is_fred() const { return name == "Fred"; }
146
147 private:
148 std::string name;
149 };
150
151 namespace
152 {
is_equal(const std::string & s1,const std::string & s2)153 bool is_equal(const std::string &s1, const std::string &s2)
154 {
155 return s1 == s2;
156 }
157
is_betty(const std::string & s)158 bool is_betty(const std::string &s)
159 {
160 return s == "Betty";
161 }
162
do_set_name(Person * p,const std::string & name)163 void do_set_name(Person *p, const std::string &name)
164 {
165 p->set_name(name);
166 }
167
do_set_name_ref(Person & p,const std::string & name)168 void do_set_name_ref(Person &p, const std::string &name)
169 {
170 p.set_name(name);
171 }
172 }
173
main()174 int main()
175 {
176 std::vector<Person> v1;
177 v1.push_back("Fred");
178 v1.push_back("Wilma");
179 v1.push_back("Barney");
180 v1.push_back("Betty");
181
182 const std::vector<Person> cv1(v1.begin(), v1.end());
183
184 std::vector<std::string> v2;
185 v2.push_back("Fred");
186 v2.push_back("Wilma");
187 v2.push_back("Barney");
188 v2.push_back("Betty");
189
190 Person person;
191 Person &r = person;
192
193 Person fred("Fred");
194 Person wilma("Wilma");
195 Person barney("Barney");
196 Person betty("Betty");
197 std::vector<Person*> v3;
198 v3.push_back(&fred);
199 v3.push_back(&wilma);
200 v3.push_back(&barney);
201 v3.push_back(&betty);
202
203 const std::vector<Person*> cv3(v3.begin(), v3.end());
204 std::vector<const Person*> v3c(v3.begin(), v3.end());
205
206 std::ostream &os = std::cout;
207
208 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL)
209 // unary_traits, unary_negate
210 std::transform(v2.begin(), v2.end(),
211 std::ostream_iterator<bool>(std::cout, " "),
212 boost::not1(is_betty));
213
214 std::cout << '\n';
215 std::transform(v1.begin(), v1.end(),
216 std::ostream_iterator<bool>(std::cout, " "),
217 boost::not1(boost::mem_fun_ref(&Person::is_fred)));
218
219 // binary_traits, binary_negate
220 std::cout << '\n';
221 std::transform(v2.begin(), v2.end(),
222 std::ostream_iterator<bool>(std::cout, " "),
223 boost::bind1st(boost::not2(is_equal), "Betty"));
224
225 std::cout << '\n';
226 std::transform(v2.begin(), v2.end(),
227 std::ostream_iterator<bool>(std::cout, " "),
228 boost::bind2nd(boost::not2(is_equal), "Betty"));
229
230 // pointer_to_unary_function
231 std::cout << '\n';
232 std::transform(v2.begin(), v2.end(),
233 std::ostream_iterator<bool>(std::cout, " "),
234 boost::not1(boost::ptr_fun(is_betty)));
235
236 // binary_traits, bind1st, bind2nd
237 std::cout << '\n';
238 std::transform(v2.begin(), v2.end(),
239 std::ostream_iterator<bool>(std::cout, " "),
240 boost::bind1st(is_equal, "Betty"));
241
242 std::cout << '\n';
243 std::transform(v2.begin(), v2.end(),
244 std::ostream_iterator<bool>(std::cout, " "),
245 boost::bind2nd(is_equal, "Betty"));
246
247 // pointer_to_binary_function, bind1st
248 std::cout << '\n';
249 std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name), &person));
250
251 std::cout << '\n';
252 std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), person));
253
254 std::cout << '\n';
255 std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), r));
256
257 // binary_traits
258 std::cout << '\n';
259 std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name, &person));
260
261 std::cout << '\n';
262 std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, person));
263
264 std::cout << '\n';
265 std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, r));
266 #endif
267
268 // const_mem_fun_t
269 std::cout << '\n';
270 std::transform(v3.begin(), v3.end(),
271 std::ostream_iterator<std::string>(std::cout, " "),
272 boost::mem_fun(&Person::get_name));
273
274 std::cout << '\n';
275 std::transform(cv3.begin(), cv3.end(),
276 std::ostream_iterator<std::string>(std::cout, " "),
277 boost::mem_fun(&Person::get_name));
278
279 std::cout << '\n';
280 std::transform(v3c.begin(), v3c.end(),
281 std::ostream_iterator<std::string>(std::cout, " "),
282 boost::mem_fun(&Person::get_name));
283
284 // const_mem_fun_ref_t
285 std::cout << '\n';
286 std::transform(v1.begin(), v1.end(),
287 std::ostream_iterator<std::string>(std::cout, " "),
288 boost::mem_fun_ref(&Person::get_name));
289
290 std::cout << '\n';
291 std::transform(cv1.begin(), cv1.end(),
292 std::ostream_iterator<std::string>(std::cout, " "),
293 boost::mem_fun_ref(&Person::get_name));
294
295 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
296 // const_mem_fun1_t, bind2nd
297 std::cout << '\n';
298 std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), std::cout));
299
300 std::cout << '\n';
301 std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), os));
302
303 // const_mem_fun1_ref_t, bind2nd
304 std::cout << '\n';
305 std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), std::cout));
306
307 std::cout << '\n';
308 std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), os));
309
310 // mem_fun1_t, bind1st
311 std::cout << '\n';
312 std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun(&Person::set_name), &person));
313
314 // mem_fun1_ref_t, bind1st
315 std::cout << '\n';
316 std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), person));
317
318 std::cout << '\n';
319 std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), r));
320 #endif
321
322 // mem_fun_t
323 std::cout << '\n';
324 std::transform(v3.begin(), v3.end(), std::ostream_iterator<std::string>(std::cout, " "),
325 boost::mem_fun(&Person::clear_name));
326
327 // mem_fun_ref_t
328 std::cout << '\n';
329 std::transform(v1.begin(), v1.end(), std::ostream_iterator<std::string>(std::cout, " "),
330 boost::mem_fun_ref(&Person::clear_name));
331
332 std::cout << '\n';
333 return 0;
334 }
335