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