Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libstdc++-v3 / include / backward / binders.h
blob5043154222bc977785e12058558d679b49b44320
1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
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.
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.
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.
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.
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
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.
45 * Copyright (c) 1996-1998
46 * Silicon Graphics Computer Systems, Inc.
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.
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.
62 #ifndef _GLIBCXX_BINDERS_H
63 #define _GLIBCXX_BINDERS_H 1
65 _GLIBCXX_BEGIN_NAMESPACE(std)
67 // 20.3.6 binders
68 /** @defgroup binders Binder Classes
69 * @ingroup functors
71 * Binders turn functions/functors with two arguments into functors with
72 * a single argument, storing an argument to be applied later. For
73 * example, a variable @c B of type @c binder1st is constructed from a
74 * functor @c f and an argument @c x. Later, B's @c operator() is called
75 * with a single argument @c y. The return value is the value of @c f(x,y).
76 * @c B can be "called" with various arguments (y1, y2, ...) and will in
77 * turn call @c f(x,y1), @c f(x,y2), ...
79 * The function @c bind1st is provided to save some typing. It takes the
80 * function and an argument as parameters, and returns an instance of
81 * @c binder1st.
83 * The type @c binder2nd and its creator function @c bind2nd do the same
84 * thing, but the stored argument is passed as the second parameter instead
85 * of the first, e.g., @c bind2nd(std::minus<float>,1.3) will create a
86 * functor whose @c operator() accepts a floating-point number, subtracts
87 * 1.3 from it, and returns the result. (If @c bind1st had been used,
88 * the functor would perform "1.3 - x" instead.
90 * Creator-wrapper functions like @c bind1st are intended to be used in
91 * calling algorithms. Their return values will be temporary objects.
92 * (The goal is to not require you to type names like
93 * @c std::binder1st<std::plus<int>> for declaring a variable to hold the
94 * return value from @c bind1st(std::plus<int>,5).
96 * These become more useful when combined with the composition functions.
98 * @{
100 /// One of the @link binders binder functors@endlink.
101 template<typename _Operation>
102 class binder1st
103 : public unary_function<typename _Operation::second_argument_type,
104 typename _Operation::result_type>
106 protected:
107 _Operation op;
108 typename _Operation::first_argument_type value;
110 public:
111 binder1st(const _Operation& __x,
112 const typename _Operation::first_argument_type& __y)
113 : op(__x), value(__y) { }
115 typename _Operation::result_type
116 operator()(const typename _Operation::second_argument_type& __x) const
117 { return op(value, __x); }
119 // _GLIBCXX_RESOLVE_LIB_DEFECTS
120 // 109. Missing binders for non-const sequence elements
121 typename _Operation::result_type
122 operator()(typename _Operation::second_argument_type& __x) const
123 { return op(value, __x); }
124 } _GLIBCXX_DEPRECATED_ATTR;
126 /// One of the @link binders binder functors@endlink.
127 template<typename _Operation, typename _Tp>
128 inline binder1st<_Operation>
129 bind1st(const _Operation& __fn, const _Tp& __x)
131 typedef typename _Operation::first_argument_type _Arg1_type;
132 return binder1st<_Operation>(__fn, _Arg1_type(__x));
135 /// One of the @link binders binder functors@endlink.
136 template<typename _Operation>
137 class binder2nd
138 : public unary_function<typename _Operation::first_argument_type,
139 typename _Operation::result_type>
141 protected:
142 _Operation op;
143 typename _Operation::second_argument_type value;
145 public:
146 binder2nd(const _Operation& __x,
147 const typename _Operation::second_argument_type& __y)
148 : op(__x), value(__y) { }
150 typename _Operation::result_type
151 operator()(const typename _Operation::first_argument_type& __x) const
152 { return op(__x, value); }
154 // _GLIBCXX_RESOLVE_LIB_DEFECTS
155 // 109. Missing binders for non-const sequence elements
156 typename _Operation::result_type
157 operator()(typename _Operation::first_argument_type& __x) const
158 { return op(__x, value); }
159 } _GLIBCXX_DEPRECATED_ATTR;
161 /// One of the @link binders binder functors@endlink.
162 template<typename _Operation, typename _Tp>
163 inline binder2nd<_Operation>
164 bind2nd(const _Operation& __fn, const _Tp& __x)
166 typedef typename _Operation::second_argument_type _Arg2_type;
167 return binder2nd<_Operation>(__fn, _Arg2_type(__x));
169 /** @} */
171 _GLIBCXX_END_NAMESPACE
173 #endif /* _GLIBCXX_BINDERS_H */