# 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
    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  *  You should not attempt to use it directly.
    00055  */
    00056
    00057 #ifndef _GLIBCXX_BINDERS_H
    00058 #define _GLIBCXX_BINDERS_H 1
    00059
    00060 _GLIBCXX_BEGIN_NAMESPACE(std)
    00061
    00062   // 20.3.6 binders
    00063   /** @defgroup binders Binder Classes
    00064    * @ingroup functors
    00065    *
    00066    *  Binders turn functions/functors with two arguments into functors with
    00067    *  a single argument, storing an argument to be applied later.  For
    00068    *  example, a variable @c B of type @c binder1st is constructed from a
    00069    *  functor @c f and an argument @c x.  Later, B's @c operator() is called
    00070    *  with a single argument @c y.  The return value is the value of @c f(x,y).
    00071    *  @c B can be "called" with various arguments (y1, y2, ...) and will in
    00072    *  turn call @c f(x,y1), @c f(x,y2), ...
    00073    *
    00074    *  The function @c bind1st is provided to save some typing.  It takes the
    00075    *  function and an argument as parameters, and returns an instance of
    00076    *  @c binder1st.
    00077    *
    00078    *  The type @c binder2nd and its creator function @c bind2nd do the same
    00079    *  thing, but the stored argument is passed as the second parameter instead
    00080    *  of the first, e.g., @c bind2nd(std::minus<float>,1.3) will create a
    00081    *  functor whose @c operator() accepts a floating-point number, subtracts
    00082    *  1.3 from it, and returns the result.  (If @c bind1st had been used,
    00083    *  the functor would perform "1.3 - x" instead.
    00084    *
    00085    *  Creator-wrapper functions like @c bind1st are intended to be used in
    00086    *  calling algorithms.  Their return values will be temporary objects.
    00087    *  (The goal is to not require you to type names like
    00088    *  @c std::binder1st<std::plus<int>> for declaring a variable to hold the
    00089    *  return value from @c bind1st(std::plus<int>,5).
    00090    *
    00091    *  These become more useful when combined with the composition functions.
    00092    *
    00093    *  @{
    00094    */
    00095   /// One of the @link binders binder functors@endlink.
    00096   template<typename _Operation>
    00097     class binder1st
    00098     : public unary_function<typename _Operation::second_argument_type,
    00099                 typename _Operation::result_type>
    00100     {
    00101     protected:
    00102       _Operation op;
    00103       typename _Operation::first_argument_type value;
    00104
    00105     public:
    00106       binder1st(const _Operation& __x,
    00107         const typename _Operation::first_argument_type& __y)
    00108       : op(__x), value(__y) { }
    00109
    00110       typename _Operation::result_type
    00111       operator()(const typename _Operation::second_argument_type& __x) const
    00112       { return op(value, __x); }
    00113
    00114       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00115       // 109.  Missing binders for non-const sequence elements
    00116       typename _Operation::result_type
    00117       operator()(typename _Operation::second_argument_type& __x) const
    00118       { return op(value, __x); }
    00119     } _GLIBCXX_DEPRECATED_ATTR;
    00120
    00121   /// One of the @link binders binder functors@endlink.
    00122   template<typename _Operation, typename _Tp>
    00123     inline binder1st<_Operation>
    00124     bind1st(const _Operation& __fn, const _Tp& __x)
    00125     {
    00126       typedef typename _Operation::first_argument_type _Arg1_type;
    00127       return binder1st<_Operation>(__fn, _Arg1_type(__x));
    00128     }
    00129
    00130   /// One of the @link binders binder functors@endlink.
    00131   template<typename _Operation>
    00132     class binder2nd
    00133     : public unary_function<typename _Operation::first_argument_type,
    00134                 typename _Operation::result_type>
    00135     {
    00136     protected:
    00137       _Operation op;
    00138       typename _Operation::second_argument_type value;
    00139
    00140     public:
    00141       binder2nd(const _Operation& __x,
    00142         const typename _Operation::second_argument_type& __y)
    00143       : op(__x), value(__y) { }
    00144
    00145       typename _Operation::result_type
    00146       operator()(const typename _Operation::first_argument_type& __x) const
    00147       { return op(__x, value); }
    00148
    00149       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00150       // 109.  Missing binders for non-const sequence elements
    00151       typename _Operation::result_type
    00152       operator()(typename _Operation::first_argument_type& __x) const
    00153       { return op(__x, value); }
    00154     } _GLIBCXX_DEPRECATED_ATTR;
    00155
    00156   /// One of the @link binders binder functors@endlink.
    00157   template<typename _Operation, typename _Tp>
    00158     inline binder2nd<_Operation>
    00159     bind2nd(const _Operation& __fn, const _Tp& __x)
    00160     {
    00161       typedef typename _Operation::second_argument_type _Arg2_type;
    00162       return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    00163     }
    00164   /** @}  */
    00165
    00166 _GLIBCXX_END_NAMESPACE
    00167
    00168 #endif /* _GLIBCXX_BINDERS_H */