binders.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Functor implementations -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996-1998
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file backward/binders.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{functional}
  48. */
  49. #ifndef _BACKWARD_BINDERS_H
  50. #define _BACKWARD_BINDERS_H 1
  51. // Suppress deprecated warning for this file.
  52. #pragma GCC diagnostic push
  53. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  54. namespace std _GLIBCXX_VISIBILITY(default)
  55. {
  56. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  57. // 20.3.6 binders
  58. /** @defgroup binders Binder Classes
  59. * @ingroup functors
  60. *
  61. * Binders turn functions/functors with two arguments into functors
  62. * with a single argument, storing an argument to be applied later.
  63. * For example, a variable @c B of type @c binder1st is constructed
  64. * from a functor @c f and an argument @c x. Later, B's @c
  65. * operator() is called with a single argument @c y. The return
  66. * value is the value of @c f(x,y). @c B can be @a called with
  67. * various arguments (y1, y2, ...) and will in turn call @c
  68. * f(x,y1), @c f(x,y2), ...
  69. *
  70. * The function @c bind1st is provided to save some typing. It takes the
  71. * function and an argument as parameters, and returns an instance of
  72. * @c binder1st.
  73. *
  74. * The type @c binder2nd and its creator function @c bind2nd do the same
  75. * thing, but the stored argument is passed as the second parameter instead
  76. * of the first, e.g., @c bind2nd(std::minus<float>(),1.3) will create a
  77. * functor whose @c operator() accepts a floating-point number, subtracts
  78. * 1.3 from it, and returns the result. (If @c bind1st had been used,
  79. * the functor would perform <em>1.3 - x</em> instead.
  80. *
  81. * Creator-wrapper functions like @c bind1st are intended to be used in
  82. * calling algorithms. Their return values will be temporary objects.
  83. * (The goal is to not require you to type names like
  84. * @c std::binder1st<std::plus<int>> for declaring a variable to hold the
  85. * return value from @c bind1st(std::plus<int>(),5).
  86. *
  87. * These become more useful when combined with the composition functions.
  88. *
  89. * These functions are deprecated in C++11 and can be replaced by
  90. * @c std::bind (or @c std::tr1::bind) which is more powerful and flexible,
  91. * supporting functions with any number of arguments. Uses of @c bind1st
  92. * can be replaced by @c std::bind(f, x, std::placeholders::_1) and
  93. * @c bind2nd by @c std::bind(f, std::placeholders::_1, x).
  94. * @{
  95. */
  96. /// One of the @link binders binder functors@endlink.
  97. template<typename _Operation>
  98. class binder1st
  99. : public unary_function<typename _Operation::second_argument_type,
  100. typename _Operation::result_type>
  101. {
  102. protected:
  103. _Operation op;
  104. typename _Operation::first_argument_type value;
  105. public:
  106. binder1st(const _Operation& __x,
  107. const typename _Operation::first_argument_type& __y)
  108. : op(__x), value(__y) { }
  109. typename _Operation::result_type
  110. operator()(const typename _Operation::second_argument_type& __x) const
  111. { return op(value, __x); }
  112. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  113. // 109. Missing binders for non-const sequence elements
  114. typename _Operation::result_type
  115. operator()(typename _Operation::second_argument_type& __x) const
  116. { return op(value, __x); }
  117. } _GLIBCXX11_DEPRECATED_SUGGEST("std::bind");
  118. /// One of the @link binders binder functors@endlink.
  119. template<typename _Operation, typename _Tp>
  120. _GLIBCXX11_DEPRECATED_SUGGEST("std::bind")
  121. inline binder1st<_Operation>
  122. bind1st(const _Operation& __fn, const _Tp& __x)
  123. {
  124. typedef typename _Operation::first_argument_type _Arg1_type;
  125. return binder1st<_Operation>(__fn, _Arg1_type(__x));
  126. }
  127. /// One of the @link binders binder functors@endlink.
  128. template<typename _Operation>
  129. class binder2nd
  130. : public unary_function<typename _Operation::first_argument_type,
  131. typename _Operation::result_type>
  132. {
  133. protected:
  134. _Operation op;
  135. typename _Operation::second_argument_type value;
  136. public:
  137. binder2nd(const _Operation& __x,
  138. const typename _Operation::second_argument_type& __y)
  139. : op(__x), value(__y) { }
  140. typename _Operation::result_type
  141. operator()(const typename _Operation::first_argument_type& __x) const
  142. { return op(__x, value); }
  143. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  144. // 109. Missing binders for non-const sequence elements
  145. typename _Operation::result_type
  146. operator()(typename _Operation::first_argument_type& __x) const
  147. { return op(__x, value); }
  148. } _GLIBCXX11_DEPRECATED_SUGGEST("std::bind");
  149. /// One of the @link binders binder functors@endlink.
  150. template<typename _Operation, typename _Tp>
  151. _GLIBCXX11_DEPRECATED_SUGGEST("std::bind")
  152. inline binder2nd<_Operation>
  153. bind2nd(const _Operation& __fn, const _Tp& __x)
  154. {
  155. typedef typename _Operation::second_argument_type _Arg2_type;
  156. return binder2nd<_Operation>(__fn, _Arg2_type(__x));
  157. }
  158. /** @} */
  159. _GLIBCXX_END_NAMESPACE_VERSION
  160. } // namespace
  161. #pragma GCC diagnostic pop
  162. #endif /* _BACKWARD_BINDERS_H */