libstdc++
|
00001 // Functor implementations -*- C++ -*- 00002 00003 // Copyright (C) 2001-2015 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1996-1998 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file backward/binders.h 00052 * This is an internal header file, included by other library headers. 00053 * Do not attempt to use it directly. @headername{functional} 00054 */ 00055 00056 #ifndef _BACKWARD_BINDERS_H 00057 #define _BACKWARD_BINDERS_H 1 00058 00059 // Suppress deprecated warning for this file. 00060 #pragma GCC diagnostic push 00061 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 00062 00063 namespace std _GLIBCXX_VISIBILITY(default) 00064 { 00065 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00066 00067 // 20.3.6 binders 00068 /** @defgroup binders Binder Classes 00069 * @ingroup functors 00070 * 00071 * Binders turn functions/functors with two arguments into functors 00072 * with a single argument, storing an argument to be applied later. 00073 * For example, a variable @c B of type @c binder1st is constructed 00074 * from a functor @c f and an argument @c x. Later, B's @c 00075 * operator() is called with a single argument @c y. The return 00076 * value is the value of @c f(x,y). @c B can be @a called with 00077 * various arguments (y1, y2, ...) and will in turn call @c 00078 * f(x,y1), @c f(x,y2), ... 00079 * 00080 * The function @c bind1st is provided to save some typing. It takes the 00081 * function and an argument as parameters, and returns an instance of 00082 * @c binder1st. 00083 * 00084 * The type @c binder2nd and its creator function @c bind2nd do the same 00085 * thing, but the stored argument is passed as the second parameter instead 00086 * of the first, e.g., @c bind2nd(std::minus<float>(),1.3) will create a 00087 * functor whose @c operator() accepts a floating-point number, subtracts 00088 * 1.3 from it, and returns the result. (If @c bind1st had been used, 00089 * the functor would perform <em>1.3 - x</em> instead. 00090 * 00091 * Creator-wrapper functions like @c bind1st are intended to be used in 00092 * calling algorithms. Their return values will be temporary objects. 00093 * (The goal is to not require you to type names like 00094 * @c std::binder1st<std::plus<int>> for declaring a variable to hold the 00095 * return value from @c bind1st(std::plus<int>(),5). 00096 * 00097 * These become more useful when combined with the composition functions. 00098 * 00099 * These functions are deprecated in C++11 and can be replaced by 00100 * @c std::bind (or @c std::tr1::bind) which is more powerful and flexible, 00101 * supporting functions with any number of arguments. Uses of @c bind1st 00102 * can be replaced by @c std::bind(f, x, std::placeholders::_1) and 00103 * @c bind2nd by @c std::bind(f, std::placeholders::_1, x). 00104 * @{ 00105 */ 00106 /// One of the @link binders binder functors@endlink. 00107 template<typename _Operation> 00108 class binder1st 00109 : public unary_function<typename _Operation::second_argument_type, 00110 typename _Operation::result_type> 00111 { 00112 protected: 00113 _Operation op; 00114 typename _Operation::first_argument_type value; 00115 00116 public: 00117 binder1st(const _Operation& __x, 00118 const typename _Operation::first_argument_type& __y) 00119 : op(__x), value(__y) { } 00120 00121 typename _Operation::result_type 00122 operator()(const typename _Operation::second_argument_type& __x) const 00123 { return op(value, __x); } 00124 00125 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00126 // 109. Missing binders for non-const sequence elements 00127 typename _Operation::result_type 00128 operator()(typename _Operation::second_argument_type& __x) const 00129 { return op(value, __x); } 00130 } _GLIBCXX_DEPRECATED; 00131 00132 /// One of the @link binders binder functors@endlink. 00133 template<typename _Operation, typename _Tp> 00134 inline binder1st<_Operation> 00135 bind1st(const _Operation& __fn, const _Tp& __x) 00136 { 00137 typedef typename _Operation::first_argument_type _Arg1_type; 00138 return binder1st<_Operation>(__fn, _Arg1_type(__x)); 00139 } 00140 00141 /// One of the @link binders binder functors@endlink. 00142 template<typename _Operation> 00143 class binder2nd 00144 : public unary_function<typename _Operation::first_argument_type, 00145 typename _Operation::result_type> 00146 { 00147 protected: 00148 _Operation op; 00149 typename _Operation::second_argument_type value; 00150 00151 public: 00152 binder2nd(const _Operation& __x, 00153 const typename _Operation::second_argument_type& __y) 00154 : op(__x), value(__y) { } 00155 00156 typename _Operation::result_type 00157 operator()(const typename _Operation::first_argument_type& __x) const 00158 { return op(__x, value); } 00159 00160 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00161 // 109. Missing binders for non-const sequence elements 00162 typename _Operation::result_type 00163 operator()(typename _Operation::first_argument_type& __x) const 00164 { return op(__x, value); } 00165 } _GLIBCXX_DEPRECATED; 00166 00167 /// One of the @link binders binder functors@endlink. 00168 template<typename _Operation, typename _Tp> 00169 inline binder2nd<_Operation> 00170 bind2nd(const _Operation& __fn, const _Tp& __x) 00171 { 00172 typedef typename _Operation::second_argument_type _Arg2_type; 00173 return binder2nd<_Operation>(__fn, _Arg2_type(__x)); 00174 } 00175 /** @} */ 00176 00177 _GLIBCXX_END_NAMESPACE_VERSION 00178 } // namespace 00179 00180 #pragma GCC diagnostic pop 00181 00182 #endif /* _BACKWARD_BINDERS_H */