binders.h 6.4 KB

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