Home | History | Annotate | Download | only in backward
      1 // Functor implementations -*- C++ -*-
      2 
      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 3, or (at your option)
     10 // any later version.
     11 
     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.
     16 
     17 // Under Section 7 of GPL version 3, you are granted additional
     18 // permissions described in the GCC Runtime Library Exception, version
     19 // 3.1, as published by the Free Software Foundation.
     20 
     21 // You should have received a copy of the GNU General Public License and
     22 // a copy of the GCC Runtime Library Exception along with this program;
     23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24 // <http://www.gnu.org/licenses/>.
     25 
     26 /*
     27  *
     28  * Copyright (c) 1994
     29  * Hewlett-Packard Company
     30  *
     31  * Permission to use, copy, modify, distribute and sell this software
     32  * and its documentation for any purpose is hereby granted without fee,
     33  * provided that the above copyright notice appear in all copies and
     34  * that both that copyright notice and this permission notice appear
     35  * in supporting documentation.  Hewlett-Packard Company makes no
     36  * representations about the suitability of this software for any
     37  * purpose.  It is provided "as is" without express or implied warranty.
     38  *
     39  *
     40  * Copyright (c) 1996-1998
     41  * Silicon Graphics Computer Systems, Inc.
     42  *
     43  * Permission to use, copy, modify, distribute and sell this software
     44  * and its documentation for any purpose is hereby granted without fee,
     45  * provided that the above copyright notice appear in all copies and
     46  * that both that copyright notice and this permission notice appear
     47  * in supporting documentation.  Silicon Graphics makes no
     48  * representations about the suitability of this software for any
     49  * purpose.  It is provided "as is" without express or implied warranty.
     50  */
     51 
     52 /** @file backward/binders.h
     53  *  This is an internal header file, included by other library headers.
     54  *  You should not attempt to use it directly.
     55  */
     56 
     57 #ifndef _GLIBCXX_BINDERS_H
     58 #define _GLIBCXX_BINDERS_H 1
     59 
     60 _GLIBCXX_BEGIN_NAMESPACE(std)
     61 
     62   // 20.3.6 binders
     63   /** @defgroup binders Binder Classes
     64    * @ingroup functors
     65    *
     66    *  Binders turn functions/functors with two arguments into functors with
     67    *  a single argument, storing an argument to be applied later.  For
     68    *  example, a variable @c B of type @c binder1st is constructed from a
     69    *  functor @c f and an argument @c x.  Later, B's @c operator() is called
     70    *  with a single argument @c y.  The return value is the value of @c f(x,y).
     71    *  @c B can be "called" with various arguments (y1, y2, ...) and will in
     72    *  turn call @c f(x,y1), @c f(x,y2), ...
     73    *
     74    *  The function @c bind1st is provided to save some typing.  It takes the
     75    *  function and an argument as parameters, and returns an instance of
     76    *  @c binder1st.
     77    *
     78    *  The type @c binder2nd and its creator function @c bind2nd do the same
     79    *  thing, but the stored argument is passed as the second parameter instead
     80    *  of the first, e.g., @c bind2nd(std::minus<float>,1.3) will create a
     81    *  functor whose @c operator() accepts a floating-point number, subtracts
     82    *  1.3 from it, and returns the result.  (If @c bind1st had been used,
     83    *  the functor would perform "1.3 - x" instead.
     84    *
     85    *  Creator-wrapper functions like @c bind1st are intended to be used in
     86    *  calling algorithms.  Their return values will be temporary objects.
     87    *  (The goal is to not require you to type names like
     88    *  @c std::binder1st<std::plus<int>> for declaring a variable to hold the
     89    *  return value from @c bind1st(std::plus<int>,5).
     90    *
     91    *  These become more useful when combined with the composition functions.
     92    *
     93    *  @{
     94    */
     95   /// One of the @link binders binder functors@endlink.
     96   template<typename _Operation>
     97     class binder1st
     98     : public unary_function<typename _Operation::second_argument_type,
     99 			    typename _Operation::result_type>
    100     {
    101     protected:
    102       _Operation op;
    103       typename _Operation::first_argument_type value;
    104 
    105     public:
    106       binder1st(const _Operation& __x,
    107 		const typename _Operation::first_argument_type& __y)
    108       : op(__x), value(__y) { }
    109 
    110       typename _Operation::result_type
    111       operator()(const typename _Operation::second_argument_type& __x) const
    112       { return op(value, __x); }
    113 
    114       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    115       // 109.  Missing binders for non-const sequence elements
    116       typename _Operation::result_type
    117       operator()(typename _Operation::second_argument_type& __x) const
    118       { return op(value, __x); }
    119     } _GLIBCXX_DEPRECATED_ATTR;
    120 
    121   /// One of the @link binders binder functors@endlink.
    122   template<typename _Operation, typename _Tp>
    123     inline binder1st<_Operation>
    124     bind1st(const _Operation& __fn, const _Tp& __x)
    125     {
    126       typedef typename _Operation::first_argument_type _Arg1_type;
    127       return binder1st<_Operation>(__fn, _Arg1_type(__x));
    128     }
    129 
    130   /// One of the @link binders binder functors@endlink.
    131   template<typename _Operation>
    132     class binder2nd
    133     : public unary_function<typename _Operation::first_argument_type,
    134 			    typename _Operation::result_type>
    135     {
    136     protected:
    137       _Operation op;
    138       typename _Operation::second_argument_type value;
    139 
    140     public:
    141       binder2nd(const _Operation& __x,
    142 		const typename _Operation::second_argument_type& __y)
    143       : op(__x), value(__y) { }
    144 
    145       typename _Operation::result_type
    146       operator()(const typename _Operation::first_argument_type& __x) const
    147       { return op(__x, value); }
    148 
    149       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    150       // 109.  Missing binders for non-const sequence elements
    151       typename _Operation::result_type
    152       operator()(typename _Operation::first_argument_type& __x) const
    153       { return op(__x, value); }
    154     } _GLIBCXX_DEPRECATED_ATTR;
    155 
    156   /// One of the @link binders binder functors@endlink.
    157   template<typename _Operation, typename _Tp>
    158     inline binder2nd<_Operation>
    159     bind2nd(const _Operation& __fn, const _Tp& __x)
    160     {
    161       typedef typename _Operation::second_argument_type _Arg2_type;
    162       return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    163     }
    164   /** @}  */
    165 
    166 _GLIBCXX_END_NAMESPACE
    167 
    168 #endif /* _GLIBCXX_BINDERS_H */
    169