Branch data Line data Source code
1 : : // Functor implementations -*- C++ -*-
2 : :
3 : : // Copyright (C) 2001-2013 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 : :
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 : :
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.
19 : :
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/>.
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.
53 : : * Do not attempt to use it directly. @headername{functional}
54 : : */
55 : :
56 : : #ifndef _BACKWARD_BINDERS_H
57 : : #define _BACKWARD_BINDERS_H 1
58 : :
59 : : namespace std _GLIBCXX_VISIBILITY(default)
60 : : {
61 : : _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 : :
63 : : // 20.3.6 binders
64 : : /** @defgroup binders Binder Classes
65 : : * @ingroup functors
66 : : *
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), ...
75 : : *
76 : : * The function @c bind1st is provided to save some typing. It takes the
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
82 : : * of the first, e.g., @c bind2nd(std::minus<float>(),1.3) will create a
83 : : * functor whose @c operator() accepts a floating-point number, subtracts
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.
86 : : *
87 : : * Creator-wrapper functions like @c bind1st are intended to be used in
88 : : * calling algorithms. Their return values will be temporary objects.
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
91 : : * return value from @c bind1st(std::plus<int>(),5).
92 : : *
93 : : * These become more useful when combined with the composition functions.
94 : : *
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).
100 : : * @{
101 : : */
102 : : /// One of the @link binders binder functors@endlink.
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); }
126 : : } _GLIBCXX_DEPRECATED;
127 : :
128 : : /// One of the @link binders binder functors@endlink.
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 : :
137 : : /// One of the @link binders binder functors@endlink.
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 : 333 : binder2nd(const _Operation& __x,
149 : : const typename _Operation::second_argument_type& __y)
150 : 333 : : op(__x), value(__y) { }
151 : :
152 : : typename _Operation::result_type
153 : 909 : operator()(const typename _Operation::first_argument_type& __x) const
154 : 909 : { 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); }
161 : : } _GLIBCXX_DEPRECATED;
162 : :
163 : : /// One of the @link binders binder functors@endlink.
164 : : template<typename _Operation, typename _Tp>
165 : : inline binder2nd<_Operation>
166 : 333 : bind2nd(const _Operation& __fn, const _Tp& __x)
167 : : {
168 : : typedef typename _Operation::second_argument_type _Arg2_type;
169 : 333 : return binder2nd<_Operation>(__fn, _Arg2_type(__x));
170 : : }
171 : : /** @} */
172 : :
173 : : _GLIBCXX_END_NAMESPACE_VERSION
174 : : } // namespace
175 : :
176 : : #endif /* _BACKWARD_BINDERS_H */
|