stl_function.h

Go to the documentation of this file.
00001 // Functor implementations -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  *
00032  * Copyright (c) 1994
00033  * Hewlett-Packard Company
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Hewlett-Packard Company makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  *
00043  *
00044  * Copyright (c) 1996-1998
00045  * Silicon Graphics Computer Systems, Inc.
00046  *
00047  * Permission to use, copy, modify, distribute and sell this software
00048  * and its documentation for any purpose is hereby granted without fee,
00049  * provided that the above copyright notice appear in all copies and
00050  * that both that copyright notice and this permission notice appear
00051  * in supporting documentation.  Silicon Graphics makes no
00052  * representations about the suitability of this software for any
00053  * purpose.  It is provided "as is" without express or implied warranty.
00054  */
00055 
00061 #ifndef _FUNCTION_H
00062 #define _FUNCTION_H 1
00063 
00064 namespace std
00065 {
00066   // 20.3.1 base classes
00101   template <class _Arg, class _Result>
00102     struct unary_function
00103     {
00104       typedef _Arg argument_type;   
00105 
00106 
00107       typedef _Result result_type;  
00108     };
00109 
00113   template <class _Arg1, class _Arg2, class _Result>
00114     struct binary_function
00115     {
00116       typedef _Arg1 first_argument_type;   
00117 
00118 
00119       typedef _Arg2 second_argument_type;  
00120       typedef _Result result_type;         
00121     };
00124   // 20.3.2 arithmetic
00132 
00133   template <class _Tp>
00134     struct plus : public binary_function<_Tp, _Tp, _Tp>
00135     {
00136       _Tp
00137       operator()(const _Tp& __x, const _Tp& __y) const
00138       { return __x + __y; }
00139     };
00140 
00142   template <class _Tp>
00143     struct minus : public binary_function<_Tp, _Tp, _Tp>
00144     {
00145       _Tp
00146       operator()(const _Tp& __x, const _Tp& __y) const
00147       { return __x - __y; }
00148     };
00149 
00151   template <class _Tp>
00152     struct multiplies : public binary_function<_Tp, _Tp, _Tp>
00153     {
00154       _Tp
00155       operator()(const _Tp& __x, const _Tp& __y) const
00156       { return __x * __y; }
00157     };
00158 
00160   template <class _Tp>
00161     struct divides : public binary_function<_Tp, _Tp, _Tp>
00162     {
00163       _Tp
00164       operator()(const _Tp& __x, const _Tp& __y) const
00165       { return __x / __y; }
00166     };
00167 
00169   template <class _Tp>
00170     struct modulus : public binary_function<_Tp, _Tp, _Tp>
00171     {
00172       _Tp
00173       operator()(const _Tp& __x, const _Tp& __y) const
00174       { return __x % __y; }
00175     };
00176 
00178   template <class _Tp>
00179     struct negate : public unary_function<_Tp, _Tp>
00180     {
00181       _Tp
00182       operator()(const _Tp& __x) const
00183       { return -__x; }
00184     };
00187   // 20.3.3 comparisons
00194 
00195   template <class _Tp>
00196     struct equal_to : public binary_function<_Tp, _Tp, bool>
00197     {
00198       bool
00199       operator()(const _Tp& __x, const _Tp& __y) const
00200       { return __x == __y; }
00201     };
00202 
00204   template <class _Tp>
00205     struct not_equal_to : public binary_function<_Tp, _Tp, bool>
00206     {
00207       bool
00208       operator()(const _Tp& __x, const _Tp& __y) const
00209       { return __x != __y; }
00210     };
00211 
00213   template <class _Tp>
00214     struct greater : public binary_function<_Tp, _Tp, bool>
00215     {
00216       bool
00217       operator()(const _Tp& __x, const _Tp& __y) const
00218       { return __x > __y; }
00219     };
00220 
00222   template <class _Tp>
00223     struct less : public binary_function<_Tp, _Tp, bool>
00224     {
00225       bool
00226       operator()(const _Tp& __x, const _Tp& __y) const
00227       { return __x < __y; }
00228     };
00229 
00231   template <class _Tp>
00232     struct greater_equal : public binary_function<_Tp, _Tp, bool>
00233     {
00234       bool
00235       operator()(const _Tp& __x, const _Tp& __y) const
00236       { return __x >= __y; }
00237     };
00238 
00240   template <class _Tp>
00241     struct less_equal : public binary_function<_Tp, _Tp, bool>
00242     {
00243       bool
00244       operator()(const _Tp& __x, const _Tp& __y) const
00245       { return __x <= __y; }
00246     };
00249   // 20.3.4 logical operations
00255 
00256   template <class _Tp>
00257     struct logical_and : public binary_function<_Tp, _Tp, bool>
00258     {
00259       bool
00260       operator()(const _Tp& __x, const _Tp& __y) const
00261       { return __x && __y; }
00262     };
00263 
00265   template <class _Tp>
00266     struct logical_or : public binary_function<_Tp, _Tp, bool>
00267     {
00268       bool
00269       operator()(const _Tp& __x, const _Tp& __y) const
00270       { return __x || __y; }
00271     };
00272 
00274   template <class _Tp>
00275     struct logical_not : public unary_function<_Tp, bool>
00276     {
00277       bool
00278       operator()(const _Tp& __x) const
00279       { return !__x; }
00280     };
00283   // 20.3.5 negators
00310 
00311   template <class _Predicate>
00312     class unary_negate
00313     : public unary_function<typename _Predicate::argument_type, bool>
00314     {
00315     protected:
00316       _Predicate _M_pred;
00317     public:
00318       explicit
00319       unary_negate(const _Predicate& __x) : _M_pred(__x) {}
00320 
00321       bool
00322       operator()(const typename _Predicate::argument_type& __x) const
00323       { return !_M_pred(__x); }
00324     };
00325 
00327   template <class _Predicate>
00328     inline unary_negate<_Predicate>
00329     not1(const _Predicate& __pred)
00330     { return unary_negate<_Predicate>(__pred); }
00331 
00333   template <class _Predicate>
00334     class binary_negate
00335     : public binary_function<typename _Predicate::first_argument_type,
00336                  typename _Predicate::second_argument_type,
00337                  bool>
00338     {
00339     protected:
00340       _Predicate _M_pred;
00341     public:
00342       explicit
00343       binary_negate(const _Predicate& __x)
00344       : _M_pred(__x) { }
00345 
00346       bool
00347       operator()(const typename _Predicate::first_argument_type& __x,
00348          const typename _Predicate::second_argument_type& __y) const
00349       { return !_M_pred(__x, __y); }
00350     };
00351 
00353   template <class _Predicate>
00354     inline binary_negate<_Predicate>
00355     not2(const _Predicate& __pred)
00356     { return binary_negate<_Predicate>(__pred); }
00359   // 20.3.6 binders
00390 
00391   template <class _Operation>
00392     class binder1st
00393     : public unary_function<typename _Operation::second_argument_type,
00394                 typename _Operation::result_type>
00395     {
00396     protected:
00397       _Operation op;
00398       typename _Operation::first_argument_type value;
00399     public:
00400       binder1st(const _Operation& __x,
00401         const typename _Operation::first_argument_type& __y)
00402       : op(__x), value(__y) {}
00403 
00404       typename _Operation::result_type
00405       operator()(const typename _Operation::second_argument_type& __x) const
00406       { return op(value, __x); }
00407 
00408       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00409       // 109.  Missing binders for non-const sequence elements
00410       typename _Operation::result_type
00411       operator()(typename _Operation::second_argument_type& __x) const
00412       { return op(value, __x); }
00413     };
00414 
00416   template <class _Operation, class _Tp>
00417     inline binder1st<_Operation>
00418     bind1st(const _Operation& __fn, const _Tp& __x)
00419     {
00420       typedef typename _Operation::first_argument_type _Arg1_type;
00421       return binder1st<_Operation>(__fn, _Arg1_type(__x));
00422     }
00423 
00425   template <class _Operation>
00426     class binder2nd
00427     : public unary_function<typename _Operation::first_argument_type,
00428                 typename _Operation::result_type>
00429     {
00430     protected:
00431       _Operation op;
00432       typename _Operation::second_argument_type value;
00433     public:
00434       binder2nd(const _Operation& __x,
00435         const typename _Operation::second_argument_type& __y)
00436       : op(__x), value(__y) {}
00437 
00438       typename _Operation::result_type
00439       operator()(const typename _Operation::first_argument_type& __x) const
00440       { return op(__x, value); }
00441 
00442       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00443       // 109.  Missing binders for non-const sequence elements
00444       typename _Operation::result_type
00445       operator()(typename _Operation::first_argument_type& __x) const
00446       { return op(__x, value); }
00447     };
00448 
00450   template <class _Operation, class _Tp>
00451     inline binder2nd<_Operation>
00452     bind2nd(const _Operation& __fn, const _Tp& __x)
00453     {
00454       typedef typename _Operation::second_argument_type _Arg2_type;
00455       return binder2nd<_Operation>(__fn, _Arg2_type(__x));
00456     }
00459   // 20.3.7 adaptors pointers functions
00479 
00480   template <class _Arg, class _Result>
00481     class pointer_to_unary_function : public unary_function<_Arg, _Result>
00482     {
00483     protected:
00484       _Result (*_M_ptr)(_Arg);
00485     public:
00486       pointer_to_unary_function() {}
00487 
00488       explicit
00489       pointer_to_unary_function(_Result (*__x)(_Arg))
00490       : _M_ptr(__x) {}
00491 
00492       _Result
00493       operator()(_Arg __x) const
00494       { return _M_ptr(__x); }
00495     };
00496 
00498   template <class _Arg, class _Result>
00499     inline pointer_to_unary_function<_Arg, _Result>
00500     ptr_fun(_Result (*__x)(_Arg))
00501     { return pointer_to_unary_function<_Arg, _Result>(__x); }
00502 
00504   template <class _Arg1, class _Arg2, class _Result>
00505     class pointer_to_binary_function
00506     : public binary_function<_Arg1, _Arg2, _Result>
00507     {
00508     protected:
00509       _Result (*_M_ptr)(_Arg1, _Arg2);
00510     public:
00511       pointer_to_binary_function() {}
00512 
00513       explicit
00514       pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
00515       : _M_ptr(__x) {}
00516 
00517       _Result
00518       operator()(_Arg1 __x, _Arg2 __y) const
00519       { return _M_ptr(__x, __y); }
00520     };
00521 
00523   template <class _Arg1, class _Arg2, class _Result>
00524     inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
00525     ptr_fun(_Result (*__x)(_Arg1, _Arg2))
00526     { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
00529   template <class _Tp>
00530     struct _Identity : public unary_function<_Tp,_Tp>
00531     {
00532       _Tp&
00533       operator()(_Tp& __x) const
00534       { return __x; }
00535 
00536       const _Tp&
00537       operator()(const _Tp& __x) const
00538       { return __x; }
00539     };
00540 
00541   template <class _Pair>
00542     struct _Select1st : public unary_function<_Pair,
00543                           typename _Pair::first_type>
00544     {
00545       typename _Pair::first_type&
00546       operator()(_Pair& __x) const
00547       { return __x.first; }
00548 
00549       const typename _Pair::first_type&
00550       operator()(const _Pair& __x) const
00551       { return __x.first; }
00552     };
00553 
00554   template <class _Pair>
00555     struct _Select2nd : public unary_function<_Pair,
00556                           typename _Pair::second_type>
00557     {
00558       typename _Pair::second_type&
00559       operator()(_Pair& __x) const
00560       { return __x.second; }
00561 
00562       const typename _Pair::second_type&
00563       operator()(const _Pair& __x) const
00564       { return __x.second; }
00565     };
00566 
00567   // 20.3.8 adaptors pointers members
00589 
00590   template <class _Ret, class _Tp>
00591     class mem_fun_t : public unary_function<_Tp*, _Ret>
00592     {
00593     public:
00594       explicit
00595       mem_fun_t(_Ret (_Tp::*__pf)())
00596       : _M_f(__pf) {}
00597 
00598       _Ret
00599       operator()(_Tp* __p) const
00600       { return (__p->*_M_f)(); }
00601     private:
00602       _Ret (_Tp::*_M_f)();
00603     };
00604 
00606   template <class _Ret, class _Tp>
00607     class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
00608     {
00609     public:
00610       explicit
00611       const_mem_fun_t(_Ret (_Tp::*__pf)() const)
00612       : _M_f(__pf) {}
00613 
00614       _Ret
00615       operator()(const _Tp* __p) const
00616       { return (__p->*_M_f)(); }
00617     private:
00618       _Ret (_Tp::*_M_f)() const;
00619     };
00620 
00622   template <class _Ret, class _Tp>
00623     class mem_fun_ref_t : public unary_function<_Tp, _Ret>
00624     {
00625     public:
00626       explicit
00627       mem_fun_ref_t(_Ret (_Tp::*__pf)())
00628       : _M_f(__pf) {}
00629 
00630       _Ret
00631       operator()(_Tp& __r) const
00632       { return (__r.*_M_f)(); }
00633     private:
00634       _Ret (_Tp::*_M_f)();
00635   };
00636 
00638   template <class _Ret, class _Tp>
00639     class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
00640     {
00641     public:
00642       explicit
00643       const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
00644       : _M_f(__pf) {}
00645 
00646       _Ret
00647       operator()(const _Tp& __r) const
00648       { return (__r.*_M_f)(); }
00649     private:
00650       _Ret (_Tp::*_M_f)() const;
00651     };
00652 
00654   template <class _Ret, class _Tp, class _Arg>
00655     class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
00656     {
00657     public:
00658       explicit
00659       mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
00660       : _M_f(__pf) {}
00661 
00662       _Ret
00663       operator()(_Tp* __p, _Arg __x) const
00664       { return (__p->*_M_f)(__x); }
00665     private:
00666       _Ret (_Tp::*_M_f)(_Arg);
00667     };
00668 
00670   template <class _Ret, class _Tp, class _Arg>
00671     class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
00672     {
00673     public:
00674       explicit
00675       const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
00676       : _M_f(__pf) {}
00677 
00678       _Ret
00679       operator()(const _Tp* __p, _Arg __x) const
00680       { return (__p->*_M_f)(__x); }
00681     private:
00682       _Ret (_Tp::*_M_f)(_Arg) const;
00683     };
00684 
00686   template <class _Ret, class _Tp, class _Arg>
00687     class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
00688     {
00689     public:
00690       explicit
00691       mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
00692       : _M_f(__pf) {}
00693 
00694       _Ret
00695       operator()(_Tp& __r, _Arg __x) const
00696       { return (__r.*_M_f)(__x); }
00697     private:
00698       _Ret (_Tp::*_M_f)(_Arg);
00699     };
00700 
00702   template <class _Ret, class _Tp, class _Arg>
00703     class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
00704     {
00705     public:
00706       explicit
00707       const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
00708       : _M_f(__pf) {}
00709 
00710       _Ret
00711       operator()(const _Tp& __r, _Arg __x) const
00712       { return (__r.*_M_f)(__x); }
00713     private:
00714       _Ret (_Tp::*_M_f)(_Arg) const;
00715     };
00716 
00718   template <class _Tp>
00719     class mem_fun_t<void, _Tp> : public unary_function<_Tp*, void>
00720     {
00721     public:
00722       explicit
00723       mem_fun_t(void (_Tp::*__pf)())
00724       : _M_f(__pf) {}
00725 
00726       void
00727       operator()(_Tp* __p) const
00728       { (__p->*_M_f)(); }
00729     private:
00730       void (_Tp::*_M_f)();
00731     };
00732 
00734   template <class _Tp>
00735     class const_mem_fun_t<void, _Tp> : public unary_function<const _Tp*, void>
00736     {
00737     public:
00738       explicit
00739       const_mem_fun_t(void (_Tp::*__pf)() const)
00740       : _M_f(__pf) {}
00741 
00742       void
00743       operator()(const _Tp* __p) const
00744       { (__p->*_M_f)(); }
00745     private:
00746       void (_Tp::*_M_f)() const;
00747     };
00748 
00750   template <class _Tp>
00751     class mem_fun_ref_t<void, _Tp> : public unary_function<_Tp, void>
00752     {
00753     public:
00754       explicit
00755       mem_fun_ref_t(void (_Tp::*__pf)())
00756       : _M_f(__pf) {}
00757 
00758       void
00759       operator()(_Tp& __r) const
00760       { (__r.*_M_f)(); }
00761     private:
00762       void (_Tp::*_M_f)();
00763     };
00764 
00766   template <class _Tp>
00767     class const_mem_fun_ref_t<void, _Tp> : public unary_function<_Tp, void>
00768     {
00769     public:
00770       explicit
00771       const_mem_fun_ref_t(void (_Tp::*__pf)() const)
00772       : _M_f(__pf) {}
00773 
00774       void
00775       operator()(const _Tp& __r) const
00776       { (__r.*_M_f)(); }
00777     private:
00778       void (_Tp::*_M_f)() const;
00779     };
00780 
00782   template <class _Tp, class _Arg>
00783     class mem_fun1_t<void, _Tp, _Arg> : public binary_function<_Tp*, _Arg, void>
00784     {
00785     public:
00786       explicit
00787       mem_fun1_t(void (_Tp::*__pf)(_Arg))
00788       : _M_f(__pf) {}
00789 
00790       void
00791       operator()(_Tp* __p, _Arg __x) const
00792       { (__p->*_M_f)(__x); }
00793     private:
00794       void (_Tp::*_M_f)(_Arg);
00795     };
00796 
00798   template <class _Tp, class _Arg>
00799     class const_mem_fun1_t<void, _Tp, _Arg>
00800     : public binary_function<const _Tp*, _Arg, void>
00801     {
00802     public:
00803       explicit
00804       const_mem_fun1_t(void (_Tp::*__pf)(_Arg) const)
00805       : _M_f(__pf) {}
00806 
00807       void
00808       operator()(const _Tp* __p, _Arg __x) const
00809       { (__p->*_M_f)(__x); }
00810     private:
00811       void (_Tp::*_M_f)(_Arg) const;
00812     };
00813 
00815   template <class _Tp, class _Arg>
00816     class mem_fun1_ref_t<void, _Tp, _Arg>
00817     : public binary_function<_Tp, _Arg, void>
00818     {
00819     public:
00820       explicit
00821       mem_fun1_ref_t(void (_Tp::*__pf)(_Arg))
00822       : _M_f(__pf) {}
00823 
00824       void
00825       operator()(_Tp& __r, _Arg __x) const
00826       { (__r.*_M_f)(__x); }
00827     private:
00828       void (_Tp::*_M_f)(_Arg);
00829     };
00830 
00832   template <class _Tp, class _Arg>
00833     class const_mem_fun1_ref_t<void, _Tp, _Arg>
00834     : public binary_function<_Tp, _Arg, void>
00835     {
00836     public:
00837       explicit
00838       const_mem_fun1_ref_t(void (_Tp::*__pf)(_Arg) const)
00839       : _M_f(__pf) {}
00840 
00841       void
00842       operator()(const _Tp& __r, _Arg __x) const
00843       { (__r.*_M_f)(__x); }
00844     private:
00845       void (_Tp::*_M_f)(_Arg) const;
00846     };
00847 
00848   // Mem_fun adaptor helper functions.  There are only two:
00849   // mem_fun and mem_fun_ref.
00850   template <class _Ret, class _Tp>
00851     inline mem_fun_t<_Ret, _Tp>
00852     mem_fun(_Ret (_Tp::*__f)())
00853     { return mem_fun_t<_Ret, _Tp>(__f); }
00854 
00855   template <class _Ret, class _Tp>
00856     inline const_mem_fun_t<_Ret, _Tp>
00857     mem_fun(_Ret (_Tp::*__f)() const)
00858     { return const_mem_fun_t<_Ret, _Tp>(__f); }
00859 
00860   template <class _Ret, class _Tp>
00861     inline mem_fun_ref_t<_Ret, _Tp>
00862     mem_fun_ref(_Ret (_Tp::*__f)())
00863     { return mem_fun_ref_t<_Ret, _Tp>(__f); }
00864 
00865   template <class _Ret, class _Tp>
00866     inline const_mem_fun_ref_t<_Ret, _Tp>
00867     mem_fun_ref(_Ret (_Tp::*__f)() const)
00868     { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
00869 
00870   template <class _Ret, class _Tp, class _Arg>
00871     inline mem_fun1_t<_Ret, _Tp, _Arg>
00872     mem_fun(_Ret (_Tp::*__f)(_Arg))
00873     { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
00874 
00875   template <class _Ret, class _Tp, class _Arg>
00876     inline const_mem_fun1_t<_Ret, _Tp, _Arg>
00877     mem_fun(_Ret (_Tp::*__f)(_Arg) const)
00878     { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
00879 
00880   template <class _Ret, class _Tp, class _Arg>
00881     inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
00882     mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
00883     { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
00884 
00885   template <class _Ret, class _Tp, class _Arg>
00886     inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
00887     mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
00888     { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
00889 
00892 } // namespace std
00893 
00894 #endif /* _FUNCTION_H */
00895 
00896 // Local Variables:
00897 // mode:C++
00898 // End:

Generated on Tue Jan 30 17:31:54 2007 for GNU C++ STL by doxygen 1.3.6