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