You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
7.0KB

  1. // Functor implementations -*- C++ -*-
  2. // Copyright (C) 2001-2020 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. } _GLIBCXX_DEPRECATED;
  118. /// One of the @link binders binder functors@endlink.
  119. template<typename _Operation, typename _Tp>
  120. inline binder1st<_Operation>
  121. bind1st(const _Operation& __fn, const _Tp& __x)
  122. {
  123. typedef typename _Operation::first_argument_type _Arg1_type;
  124. return binder1st<_Operation>(__fn, _Arg1_type(__x));
  125. }
  126. /// One of the @link binders binder functors@endlink.
  127. template<typename _Operation>
  128. class binder2nd
  129. : public unary_function<typename _Operation::first_argument_type,
  130. typename _Operation::result_type>
  131. {
  132. protected:
  133. _Operation op;
  134. typename _Operation::second_argument_type value;
  135. public:
  136. binder2nd(const _Operation& __x,
  137. const typename _Operation::second_argument_type& __y)
  138. : op(__x), value(__y) { }
  139. typename _Operation::result_type
  140. operator()(const typename _Operation::first_argument_type& __x) const
  141. { return op(__x, value); }
  142. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  143. // 109. Missing binders for non-const sequence elements
  144. typename _Operation::result_type
  145. operator()(typename _Operation::first_argument_type& __x) const
  146. { return op(__x, value); }
  147. } _GLIBCXX_DEPRECATED;
  148. /// One of the @link binders binder functors@endlink.
  149. template<typename _Operation, typename _Tp>
  150. inline binder2nd<_Operation>
  151. bind2nd(const _Operation& __fn, const _Tp& __x)
  152. {
  153. typedef typename _Operation::second_argument_type _Arg2_type;
  154. return binder2nd<_Operation>(__fn, _Arg2_type(__x));
  155. }
  156. /** @} */
  157. _GLIBCXX_END_NAMESPACE_VERSION
  158. } // namespace
  159. #pragma GCC diagnostic pop
  160. #endif /* _BACKWARD_BINDERS_H */