# binders.h

Go to the documentation of this file.

    00001 // Functor implementations -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
    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 2, 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 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 /*
    00032  *
    00033  * Copyright (c) 1994
    00034  * Hewlett-Packard Company
    00035  *
    00036  * Permission to use, copy, modify, distribute and sell this software
    00037  * and its documentation for any purpose is hereby granted without fee,
    00038  * provided that the above copyright notice appear in all copies and
    00039  * that both that copyright notice and this permission notice appear
    00040  * in supporting documentation.  Hewlett-Packard Company makes no
    00041  * representations about the suitability of this software for any
    00042  * purpose.  It is provided "as is" without express or implied warranty.
    00043  *
    00044  *
    00045  * Copyright (c) 1996-1998
    00046  * Silicon Graphics Computer Systems, Inc.
    00047  *
    00048  * Permission to use, copy, modify, distribute and sell this software
    00049  * and its documentation for any purpose is hereby granted without fee,
    00050  * provided that the above copyright notice appear in all copies and
    00051  * that both that copyright notice and this permission notice appear
    00052  * in supporting documentation.  Silicon Graphics makes no
    00053  * representations about the suitability of this software for any
    00054  * purpose.  It is provided "as is" without express or implied warranty.
    00055  */
    00056
    00057 /** @file backward/binders.h
    00058  *  This is an internal header file, included by other library headers.
    00059  *  You should not attempt to use it directly.
    00060  */
    00061
    00062 #ifndef _GLIBCXX_BINDERS_H
    00063 #define _GLIBCXX_BINDERS_H 1
    00064
    00065 _GLIBCXX_BEGIN_NAMESPACE(std)
    00066
    00067   // 20.3.6 binders
    00068   /** @defgroup s20_3_6_binder Binder Classes
    00069    *  Binders turn functions/functors with two arguments into functors with
    00070    *  a single argument, storing an argument to be applied later.  For
    00071    *  example, a variable @c B of type @c binder1st is constructed from a
    00072    *  functor @c f and an argument @c x.  Later, B's @c operator() is called
    00073    *  with a single argument @c y.  The return value is the value of @c f(x,y).
    00074    *  @c B can be "called" with various arguments (y1, y2, ...) and will in
    00075    *  turn call @c 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 "1.3 - x" 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    *  @{
    00097    */
    00098   /// One of the @link s20_3_6_binder binder functors@endlink.
    00099   template<typename _Operation>
    00100     class binder1st
    00101     : public unary_function<typename _Operation::second_argument_type,
    00102                 typename _Operation::result_type>
    00103     {
    00104     protected:
    00105       _Operation op;
    00106       typename _Operation::first_argument_type value;
    00107
    00108     public:
    00109       binder1st(const _Operation& __x,
    00110         const typename _Operation::first_argument_type& __y)
    00111       : op(__x), value(__y) { }
    00112
    00113       typename _Operation::result_type
    00114       operator()(const typename _Operation::second_argument_type& __x) const
    00115       { return op(value, __x); }
    00116
    00117       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00118       // 109.  Missing binders for non-const sequence elements
    00119       typename _Operation::result_type
    00120       operator()(typename _Operation::second_argument_type& __x) const
    00121       { return op(value, __x); }
    00122     } _GLIBCXX_DEPRECATED_ATTR;
    00123
    00124   /// One of the @link s20_3_6_binder binder functors@endlink.
    00125   template<typename _Operation, typename _Tp>
    00126     inline binder1st<_Operation>
    00127     bind1st(const _Operation& __fn, const _Tp& __x)
    00128     {
    00129       typedef typename _Operation::first_argument_type _Arg1_type;
    00130       return binder1st<_Operation>(__fn, _Arg1_type(__x));
    00131     }
    00132
    00133   /// One of the @link s20_3_6_binder binder functors@endlink.
    00134   template<typename _Operation>
    00135     class binder2nd
    00136     : public unary_function<typename _Operation::first_argument_type,
    00137                 typename _Operation::result_type>
    00138     {
    00139     protected:
    00140       _Operation op;
    00141       typename _Operation::second_argument_type value;
    00142
    00143     public:
    00144       binder2nd(const _Operation& __x,
    00145         const typename _Operation::second_argument_type& __y)
    00146       : op(__x), value(__y) { }
    00147
    00148       typename _Operation::result_type
    00149       operator()(const typename _Operation::first_argument_type& __x) const
    00150       { return op(__x, value); }
    00151
    00152       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00153       // 109.  Missing binders for non-const sequence elements
    00154       typename _Operation::result_type
    00155       operator()(typename _Operation::first_argument_type& __x) const
    00156       { return op(__x, value); }
    00157     } _GLIBCXX_DEPRECATED_ATTR;
    00158
    00159   /// One of the @link s20_3_6_binder binder functors@endlink.
    00160   template<typename _Operation, typename _Tp>
    00161     inline binder2nd<_Operation>
    00162     bind2nd(const _Operation& __fn, const _Tp& __x)
    00163     {
    00164       typedef typename _Operation::second_argument_type _Arg2_type;
    00165       return binder2nd<_Operation>(__fn, _Arg2_type(__x));
    00166     }
    00167   /** @}  */
    00168
    00169 _GLIBCXX_END_NAMESPACE
    00170
    00171 #endif /* _GLIBCXX_BINDERS_H */