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