function_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. // To demonstrate what the boosted function object adapters do for
  115. // you, try compiling with USE_STD defined. This will endeavour to
  116. // use the standard function object adapters, but is likely to result
  117. // in numerous errors due to the fact that you cannot have references
  118. // to references.
  119. #ifdef USE_STD
  120. #include <functional>
  121. #define boost std
  122. #else
  123. #include <boost/functional.hpp>
  124. #endif
  125. #include <algorithm>
  126. #include <iostream>
  127. #include <iterator>
  128. #include <string>
  129. #include <vector>
  130. class Person
  131. {
  132. public:
  133. Person() {}
  134. Person(const char *n) : name(n) {}
  135. const std::string &get_name() const { return name; }
  136. void print(std::ostream &os) const { os << name << " "; }
  137. void set_name(const std::string &n) { name = n; std::cout << name << " "; }
  138. std::string clear_name() { std::string ret = name; name = ""; return ret; }
  139. void do_something(int) const {}
  140. bool is_fred() const { return name == "Fred"; }
  141. private:
  142. std::string name;
  143. };
  144. namespace
  145. {
  146. bool is_equal(const std::string &s1, const std::string &s2)
  147. {
  148. return s1 == s2;
  149. }
  150. bool is_betty(const std::string &s)
  151. {
  152. return s == "Betty";
  153. }
  154. void do_set_name(Person *p, const std::string &name)
  155. {
  156. p->set_name(name);
  157. }
  158. void do_set_name_ref(Person &p, const std::string &name)
  159. {
  160. p.set_name(name);
  161. }
  162. }
  163. int main()
  164. {
  165. std::vector<Person> v1;
  166. v1.push_back("Fred");
  167. v1.push_back("Wilma");
  168. v1.push_back("Barney");
  169. v1.push_back("Betty");
  170. const std::vector<Person> cv1(v1.begin(), v1.end());
  171. std::vector<std::string> v2;
  172. v2.push_back("Fred");
  173. v2.push_back("Wilma");
  174. v2.push_back("Barney");
  175. v2.push_back("Betty");
  176. Person person;
  177. Person &r = person;
  178. Person fred("Fred");
  179. Person wilma("Wilma");
  180. Person barney("Barney");
  181. Person betty("Betty");
  182. std::vector<Person*> v3;
  183. v3.push_back(&fred);
  184. v3.push_back(&wilma);
  185. v3.push_back(&barney);
  186. v3.push_back(&betty);
  187. const std::vector<Person*> cv3(v3.begin(), v3.end());
  188. std::vector<const Person*> v3c(v3.begin(), v3.end());
  189. std::ostream &os = std::cout;
  190. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__ICL)
  191. // unary_traits, unary_negate
  192. std::transform(v2.begin(), v2.end(),
  193. std::ostream_iterator<bool>(std::cout, " "),
  194. boost::not1(is_betty));
  195. std::cout << '\n';
  196. std::transform(v1.begin(), v1.end(),
  197. std::ostream_iterator<bool>(std::cout, " "),
  198. boost::not1(boost::mem_fun_ref(&Person::is_fred)));
  199. // binary_traits, binary_negate
  200. std::cout << '\n';
  201. std::transform(v2.begin(), v2.end(),
  202. std::ostream_iterator<bool>(std::cout, " "),
  203. boost::bind1st(boost::not2(is_equal), "Betty"));
  204. std::cout << '\n';
  205. std::transform(v2.begin(), v2.end(),
  206. std::ostream_iterator<bool>(std::cout, " "),
  207. boost::bind2nd(boost::not2(is_equal), "Betty"));
  208. // pointer_to_unary_function
  209. std::cout << '\n';
  210. std::transform(v2.begin(), v2.end(),
  211. std::ostream_iterator<bool>(std::cout, " "),
  212. boost::not1(boost::ptr_fun(is_betty)));
  213. // binary_traits, bind1st, bind2nd
  214. std::cout << '\n';
  215. std::transform(v2.begin(), v2.end(),
  216. std::ostream_iterator<bool>(std::cout, " "),
  217. boost::bind1st(is_equal, "Betty"));
  218. std::cout << '\n';
  219. std::transform(v2.begin(), v2.end(),
  220. std::ostream_iterator<bool>(std::cout, " "),
  221. boost::bind2nd(is_equal, "Betty"));
  222. // pointer_to_binary_function, bind1st
  223. std::cout << '\n';
  224. std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name), &person));
  225. std::cout << '\n';
  226. std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), person));
  227. std::cout << '\n';
  228. std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::ptr_fun(do_set_name_ref), r));
  229. // binary_traits
  230. std::cout << '\n';
  231. std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name, &person));
  232. std::cout << '\n';
  233. std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, person));
  234. std::cout << '\n';
  235. std::for_each(v2.begin(), v2.end(), boost::bind1st(do_set_name_ref, r));
  236. #endif
  237. // const_mem_fun_t
  238. std::cout << '\n';
  239. std::transform(v3.begin(), v3.end(),
  240. std::ostream_iterator<std::string>(std::cout, " "),
  241. boost::mem_fun(&Person::get_name));
  242. std::cout << '\n';
  243. std::transform(cv3.begin(), cv3.end(),
  244. std::ostream_iterator<std::string>(std::cout, " "),
  245. boost::mem_fun(&Person::get_name));
  246. std::cout << '\n';
  247. std::transform(v3c.begin(), v3c.end(),
  248. std::ostream_iterator<std::string>(std::cout, " "),
  249. boost::mem_fun(&Person::get_name));
  250. // const_mem_fun_ref_t
  251. std::cout << '\n';
  252. std::transform(v1.begin(), v1.end(),
  253. std::ostream_iterator<std::string>(std::cout, " "),
  254. boost::mem_fun_ref(&Person::get_name));
  255. std::cout << '\n';
  256. std::transform(cv1.begin(), cv1.end(),
  257. std::ostream_iterator<std::string>(std::cout, " "),
  258. boost::mem_fun_ref(&Person::get_name));
  259. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  260. // const_mem_fun1_t, bind2nd
  261. std::cout << '\n';
  262. std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), std::cout));
  263. std::cout << '\n';
  264. std::for_each(v3.begin(), v3.end(), boost::bind2nd(boost::mem_fun(&Person::print), os));
  265. // const_mem_fun1_ref_t, bind2nd
  266. std::cout << '\n';
  267. std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), std::cout));
  268. std::cout << '\n';
  269. std::for_each(v1.begin(), v1.end(), boost::bind2nd(boost::mem_fun_ref(&Person::print), os));
  270. // mem_fun1_t, bind1st
  271. std::cout << '\n';
  272. std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun(&Person::set_name), &person));
  273. // mem_fun1_ref_t, bind1st
  274. std::cout << '\n';
  275. std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), person));
  276. std::cout << '\n';
  277. std::for_each(v2.begin(), v2.end(), boost::bind1st(boost::mem_fun_ref(&Person::set_name), r));
  278. #endif
  279. // mem_fun_t
  280. std::cout << '\n';
  281. std::transform(v3.begin(), v3.end(), std::ostream_iterator<std::string>(std::cout, " "),
  282. boost::mem_fun(&Person::clear_name));
  283. // mem_fun_ref_t
  284. std::cout << '\n';
  285. std::transform(v1.begin(), v1.end(), std::ostream_iterator<std::string>(std::cout, " "),
  286. boost::mem_fun_ref(&Person::clear_name));
  287. std::cout << '\n';
  288. return 0;
  289. }