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