1219019Sgabor// Functor implementations -*- C++ -*- 2219019Sgabor 3219019Sgabor// Copyright (C) 2001-2022 Free Software Foundation, Inc. 4219019Sgabor// 5219019Sgabor// This file is part of the GNU ISO C++ Library. This library is free 6219019Sgabor// software; you can redistribute it and/or modify it under the 7219019Sgabor// terms of the GNU General Public License as published by the 8219019Sgabor// Free Software Foundation; either version 3, or (at your option) 9219019Sgabor// any later version. 10219019Sgabor 11219019Sgabor// This library is distributed in the hope that it will be useful, 12219019Sgabor// but WITHOUT ANY WARRANTY; without even the implied warranty of 13219019Sgabor// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14219019Sgabor// GNU General Public License for more details. 15219019Sgabor 16219019Sgabor// Under Section 7 of GPL version 3, you are granted additional 17219019Sgabor// permissions described in the GCC Runtime Library Exception, version 18219019Sgabor// 3.1, as published by the Free Software Foundation. 19219019Sgabor 20219019Sgabor// You should have received a copy of the GNU General Public License and 21219019Sgabor// a copy of the GCC Runtime Library Exception along with this program; 22219019Sgabor// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23219019Sgabor// <http://www.gnu.org/licenses/>. 24219019Sgabor 25219019Sgabor/* 26219019Sgabor * 27219019Sgabor * Copyright (c) 1994 28219019Sgabor * Hewlett-Packard Company 29219019Sgabor * 30219019Sgabor * Permission to use, copy, modify, distribute and sell this software 31219019Sgabor * and its documentation for any purpose is hereby granted without fee, 32219019Sgabor * provided that the above copyright notice appear in all copies and 33219019Sgabor * that both that copyright notice and this permission notice appear 34219019Sgabor * in supporting documentation. Hewlett-Packard Company makes no 35219019Sgabor * representations about the suitability of this software for any 36219019Sgabor * purpose. It is provided "as is" without express or implied warranty. 37219019Sgabor * 38219019Sgabor * 39219019Sgabor * Copyright (c) 1996-1998 40219019Sgabor * Silicon Graphics Computer Systems, Inc. 41219019Sgabor * 42219019Sgabor * Permission to use, copy, modify, distribute and sell this software 43219019Sgabor * and its documentation for any purpose is hereby granted without fee, 44219019Sgabor * provided that the above copyright notice appear in all copies and 45219019Sgabor * that both that copyright notice and this permission notice appear 46219019Sgabor * in supporting documentation. Silicon Graphics makes no 47219019Sgabor * representations about the suitability of this software for any 48219019Sgabor * purpose. It is provided "as is" without express or implied warranty. 49219019Sgabor */ 50219019Sgabor 51219019Sgabor/** @file backward/binders.h 52219019Sgabor * This is an internal header file, included by other library headers. 53219019Sgabor * Do not attempt to use it directly. @headername{functional} 54219019Sgabor */ 55219019Sgabor 56219019Sgabor#ifndef _BACKWARD_BINDERS_H 57219019Sgabor#define _BACKWARD_BINDERS_H 1 58219019Sgabor 59219019Sgabor// Suppress deprecated warning for this file. 60219019Sgabor#pragma GCC diagnostic push 61219019Sgabor#pragma GCC diagnostic ignored "-Wdeprecated-declarations" 62219019Sgabor 63219019Sgabornamespace std _GLIBCXX_VISIBILITY(default) 64219019Sgabor{ 65219019Sgabor_GLIBCXX_BEGIN_NAMESPACE_VERSION 66219019Sgabor 67219019Sgabor // 20.3.6 binders 68219019Sgabor /** @defgroup binders Binder Classes 69219019Sgabor * @ingroup functors 70219019Sgabor * 71219019Sgabor * Binders turn functions/functors with two arguments into functors 72219019Sgabor * with a single argument, storing an argument to be applied later. 73219019Sgabor * For example, a variable @c B of type @c binder1st is constructed 74219019Sgabor * from a functor @c f and an argument @c x. Later, B's @c 75219019Sgabor * operator() is called with a single argument @c y. The return 76219019Sgabor * value is the value of @c f(x,y). @c B can be @a called with 77219019Sgabor * various arguments (y1, y2, ...) and will in turn call @c 78219019Sgabor * f(x,y1), @c f(x,y2), ... 79219019Sgabor * 80219019Sgabor * The function @c bind1st is provided to save some typing. It takes the 81219019Sgabor * function and an argument as parameters, and returns an instance of 82219019Sgabor * @c binder1st. 83219019Sgabor * 84219019Sgabor * The type @c binder2nd and its creator function @c bind2nd do the same 85219019Sgabor * thing, but the stored argument is passed as the second parameter instead 86219019Sgabor * of the first, e.g., @c bind2nd(std::minus<float>(),1.3) will create a 87219019Sgabor * functor whose @c operator() accepts a floating-point number, subtracts 88219019Sgabor * 1.3 from it, and returns the result. (If @c bind1st had been used, 89219019Sgabor * the functor would perform <em>1.3 - x</em> instead. 90219019Sgabor * 91219019Sgabor * Creator-wrapper functions like @c bind1st are intended to be used in 92219019Sgabor * calling algorithms. Their return values will be temporary objects. 93219019Sgabor * (The goal is to not require you to type names like 94219019Sgabor * @c std::binder1st<std::plus<int>> for declaring a variable to hold the 95219019Sgabor * return value from @c bind1st(std::plus<int>(),5). 96219019Sgabor * 97219019Sgabor * These become more useful when combined with the composition functions. 98219019Sgabor * 99219019Sgabor * These functions are deprecated in C++11 and can be replaced by 100219019Sgabor * @c std::bind (or @c std::tr1::bind) which is more powerful and flexible, 101219019Sgabor * supporting functions with any number of arguments. Uses of @c bind1st 102219019Sgabor * can be replaced by @c std::bind(f, x, std::placeholders::_1) and 103219019Sgabor * @c bind2nd by @c std::bind(f, std::placeholders::_1, x). 104219019Sgabor * @{ 105219019Sgabor */ 106219019Sgabor /// One of the @link binders binder functors@endlink. 107219019Sgabor template<typename _Operation> 108219019Sgabor class binder1st 109219019Sgabor : public unary_function<typename _Operation::second_argument_type, 110219019Sgabor typename _Operation::result_type> 111219019Sgabor { 112219019Sgabor protected: 113219019Sgabor _Operation op; 114219019Sgabor typename _Operation::first_argument_type value; 115219019Sgabor 116219019Sgabor public: 117219019Sgabor binder1st(const _Operation& __x, 118219019Sgabor const typename _Operation::first_argument_type& __y) 119219019Sgabor : op(__x), value(__y) { } 120219019Sgabor 121219019Sgabor typename _Operation::result_type 122219019Sgabor operator()(const typename _Operation::second_argument_type& __x) const 123219019Sgabor { return op(value, __x); } 124219019Sgabor 125219019Sgabor // _GLIBCXX_RESOLVE_LIB_DEFECTS 126219019Sgabor // 109. Missing binders for non-const sequence elements 127219019Sgabor typename _Operation::result_type 128219019Sgabor operator()(typename _Operation::second_argument_type& __x) const 129219019Sgabor { return op(value, __x); } 130219019Sgabor } _GLIBCXX11_DEPRECATED_SUGGEST("std::bind"); 131219019Sgabor 132219019Sgabor /// One of the @link binders binder functors@endlink. 133219019Sgabor template<typename _Operation, typename _Tp> 134219019Sgabor _GLIBCXX11_DEPRECATED_SUGGEST("std::bind") 135219019Sgabor inline binder1st<_Operation> 136219019Sgabor bind1st(const _Operation& __fn, const _Tp& __x) 137219019Sgabor { 138219019Sgabor typedef typename _Operation::first_argument_type _Arg1_type; 139219019Sgabor return binder1st<_Operation>(__fn, _Arg1_type(__x)); 140219019Sgabor } 141219019Sgabor 142219019Sgabor /// One of the @link binders binder functors@endlink. 143219019Sgabor template<typename _Operation> 144219019Sgabor class binder2nd 145219019Sgabor : public unary_function<typename _Operation::first_argument_type, 146219019Sgabor typename _Operation::result_type> 147219019Sgabor { 148219019Sgabor protected: 149219019Sgabor _Operation op; 150219019Sgabor typename _Operation::second_argument_type value; 151219019Sgabor 152219019Sgabor public: 153219019Sgabor binder2nd(const _Operation& __x, 154219019Sgabor const typename _Operation::second_argument_type& __y) 155219019Sgabor : op(__x), value(__y) { } 156219019Sgabor 157219019Sgabor typename _Operation::result_type 158219019Sgabor operator()(const typename _Operation::first_argument_type& __x) const 159219019Sgabor { return op(__x, value); } 160219019Sgabor 161219019Sgabor // _GLIBCXX_RESOLVE_LIB_DEFECTS 162219019Sgabor // 109. Missing binders for non-const sequence elements 163219019Sgabor typename _Operation::result_type 164219019Sgabor operator()(typename _Operation::first_argument_type& __x) const 165219019Sgabor { return op(__x, value); } 166219019Sgabor } _GLIBCXX11_DEPRECATED_SUGGEST("std::bind"); 167219019Sgabor 168219019Sgabor /// One of the @link binders binder functors@endlink. 169219019Sgabor template<typename _Operation, typename _Tp> 170219019Sgabor _GLIBCXX11_DEPRECATED_SUGGEST("std::bind") 171219019Sgabor inline binder2nd<_Operation> 172219019Sgabor bind2nd(const _Operation& __fn, const _Tp& __x) 173219019Sgabor { 174219019Sgabor typedef typename _Operation::second_argument_type _Arg2_type; 175219019Sgabor return binder2nd<_Operation>(__fn, _Arg2_type(__x)); 176219019Sgabor } 177219019Sgabor /** @} */ 178219019Sgabor 179219019Sgabor_GLIBCXX_END_NAMESPACE_VERSION 180219019Sgabor} // namespace 181219019Sgabor 182219019Sgabor#pragma GCC diagnostic pop 183219019Sgabor 184219019Sgabor#endif /* _BACKWARD_BINDERS_H */ 185219019Sgabor