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