stl_function.h

Go to the documentation of this file.
00001 // Functor implementations -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002 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 __GLIBCPP_INTERNAL_FUNCTION_H
00062 #define __GLIBCPP_INTERNAL_FUNCTION_H
00063 
00064 namespace std
00065 {
00066 // 20.3.1 base classes
00101 template <class _Arg, class _Result>
00102 struct unary_function {
00103   typedef _Arg argument_type;   
00104   typedef _Result result_type;  
00105 };
00106 
00110 template <class _Arg1, class _Arg2, class _Result>
00111 struct binary_function {
00112   typedef _Arg1 first_argument_type;   
00113   typedef _Arg2 second_argument_type;  
00114   typedef _Result result_type;         
00115 };      
00118 // 20.3.2 arithmetic
00126 
00127 template <class _Tp>
00128 struct plus : public binary_function<_Tp,_Tp,_Tp> {
00129   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
00130 };
00131 
00133 template <class _Tp>
00134 struct minus : public binary_function<_Tp,_Tp,_Tp> {
00135   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
00136 };
00137 
00139 template <class _Tp>
00140 struct multiplies : public binary_function<_Tp,_Tp,_Tp> {
00141   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
00142 };
00143 
00145 template <class _Tp>
00146 struct divides : public binary_function<_Tp,_Tp,_Tp> {
00147   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
00148 };
00149 
00151 template <class _Tp>
00152 struct modulus : public binary_function<_Tp,_Tp,_Tp> 
00153 {
00154   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
00155 };
00156 
00158 template <class _Tp>
00159 struct negate : public unary_function<_Tp,_Tp> 
00160 {
00161   _Tp operator()(const _Tp& __x) const { return -__x; }
00162 };
00165 // 20.3.3 comparisons
00172 
00173 template <class _Tp>
00174 struct equal_to : public binary_function<_Tp,_Tp,bool> 
00175 {
00176   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
00177 };
00178 
00180 template <class _Tp>
00181 struct not_equal_to : public binary_function<_Tp,_Tp,bool> 
00182 {
00183   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
00184 };
00185 
00187 template <class _Tp>
00188 struct greater : public binary_function<_Tp,_Tp,bool> 
00189 {
00190   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
00191 };
00192 
00194 template <class _Tp>
00195 struct less : public binary_function<_Tp,_Tp,bool> 
00196 {
00197   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
00198 };
00199 
00201 template <class _Tp>
00202 struct greater_equal : public binary_function<_Tp,_Tp,bool>
00203 {
00204   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
00205 };
00206 
00208 template <class _Tp>
00209 struct less_equal : public binary_function<_Tp,_Tp,bool> 
00210 {
00211   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
00212 };
00215 // 20.3.4 logical operations
00221 
00222 template <class _Tp>
00223 struct logical_and : public binary_function<_Tp,_Tp,bool>
00224 {
00225   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
00226 };
00227 
00229 template <class _Tp>
00230 struct logical_or : public binary_function<_Tp,_Tp,bool>
00231 {
00232   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
00233 };
00234 
00236 template <class _Tp>
00237 struct logical_not : public unary_function<_Tp,bool>
00238 {
00239   bool operator()(const _Tp& __x) const { return !__x; }
00240 };
00243 // 20.3.5 negators
00270 
00271 template <class _Predicate>
00272 class unary_negate
00273   : public unary_function<typename _Predicate::argument_type, bool> {
00274 protected:
00275   _Predicate _M_pred;
00276 public:
00277   explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {}
00278   bool operator()(const typename _Predicate::argument_type& __x) const {
00279     return !_M_pred(__x);
00280   }
00281 };
00282 
00284 template <class _Predicate>
00285 inline unary_negate<_Predicate> 
00286 not1(const _Predicate& __pred)
00287 {
00288   return unary_negate<_Predicate>(__pred);
00289 }
00290 
00292 template <class _Predicate> 
00293 class binary_negate 
00294   : public binary_function<typename _Predicate::first_argument_type,
00295                            typename _Predicate::second_argument_type,
00296                            bool> {
00297 protected:
00298   _Predicate _M_pred;
00299 public:
00300   explicit binary_negate(const _Predicate& __x) : _M_pred(__x) {}
00301   bool operator()(const typename _Predicate::first_argument_type& __x, 
00302                   const typename _Predicate::second_argument_type& __y) const
00303   {
00304     return !_M_pred(__x, __y); 
00305   }
00306 };
00307 
00309 template <class _Predicate>
00310 inline binary_negate<_Predicate> 
00311 not2(const _Predicate& __pred)
00312 {
00313   return binary_negate<_Predicate>(__pred);
00314 }
00317 // 20.3.6 binders
00348 
00349 template <class _Operation> 
00350 class binder1st
00351   : public unary_function<typename _Operation::second_argument_type,
00352                           typename _Operation::result_type> {
00353 protected:
00354   _Operation op;
00355   typename _Operation::first_argument_type value;
00356 public:
00357   binder1st(const _Operation& __x,
00358             const typename _Operation::first_argument_type& __y)
00359       : op(__x), value(__y) {}
00360   typename _Operation::result_type
00361   operator()(const typename _Operation::second_argument_type& __x) const {
00362     return op(value, __x); 
00363   }
00364 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00365   //109.  Missing binders for non-const sequence elements
00366   typename _Operation::result_type
00367   operator()(typename _Operation::second_argument_type& __x) const {
00368     return op(value, __x); 
00369   }
00370 #endif
00371 };
00372 
00374 template <class _Operation, class _Tp>
00375 inline binder1st<_Operation> 
00376 bind1st(const _Operation& __fn, const _Tp& __x) 
00377 {
00378   typedef typename _Operation::first_argument_type _Arg1_type;
00379   return binder1st<_Operation>(__fn, _Arg1_type(__x));
00380 }
00381 
00383 template <class _Operation> 
00384 class binder2nd
00385   : public unary_function<typename _Operation::first_argument_type,
00386                           typename _Operation::result_type> {
00387 protected:
00388   _Operation op;
00389   typename _Operation::second_argument_type value;
00390 public:
00391   binder2nd(const _Operation& __x,
00392             const typename _Operation::second_argument_type& __y) 
00393       : op(__x), value(__y) {}
00394   typename _Operation::result_type
00395   operator()(const typename _Operation::first_argument_type& __x) const {
00396     return op(__x, value); 
00397   }
00398 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00399   //109.  Missing binders for non-const sequence elements
00400   typename _Operation::result_type
00401   operator()(typename _Operation::first_argument_type& __x) const {
00402     return op(__x, value); 
00403   }
00404 #endif
00405 };
00406 
00408 template <class _Operation, class _Tp>
00409 inline binder2nd<_Operation> 
00410 bind2nd(const _Operation& __fn, const _Tp& __x) 
00411 {
00412   typedef typename _Operation::second_argument_type _Arg2_type;
00413   return binder2nd<_Operation>(__fn, _Arg2_type(__x));
00414 }
00417 // 20.3.7 adaptors pointers functions
00437 
00438 template <class _Arg, class _Result>
00439 class pointer_to_unary_function : public unary_function<_Arg, _Result> {
00440 protected:
00441   _Result (*_M_ptr)(_Arg);
00442 public:
00443   pointer_to_unary_function() {}
00444   explicit pointer_to_unary_function(_Result (*__x)(_Arg)) : _M_ptr(__x) {}
00445   _Result operator()(_Arg __x) const { return _M_ptr(__x); }
00446 };
00447 
00449 template <class _Arg, class _Result>
00450 inline pointer_to_unary_function<_Arg, _Result> ptr_fun(_Result (*__x)(_Arg))
00451 {
00452   return pointer_to_unary_function<_Arg, _Result>(__x);
00453 }
00454 
00456 template <class _Arg1, class _Arg2, class _Result>
00457 class pointer_to_binary_function : 
00458   public binary_function<_Arg1,_Arg2,_Result> {
00459 protected:
00460     _Result (*_M_ptr)(_Arg1, _Arg2);
00461 public:
00462     pointer_to_binary_function() {}
00463     explicit pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2)) 
00464       : _M_ptr(__x) {}
00465     _Result operator()(_Arg1 __x, _Arg2 __y) const {
00466       return _M_ptr(__x, __y);
00467     }
00468 };
00469 
00471 template <class _Arg1, class _Arg2, class _Result>
00472 inline pointer_to_binary_function<_Arg1,_Arg2,_Result> 
00473 ptr_fun(_Result (*__x)(_Arg1, _Arg2)) {
00474   return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__x);
00475 }
00478 template <class _Tp>
00479 struct _Identity : public unary_function<_Tp,_Tp> {
00480   _Tp& operator()(_Tp& __x) const { return __x; }
00481   const _Tp& operator()(const _Tp& __x) const { return __x; }
00482 };
00483 
00484 template <class _Pair>
00485 struct _Select1st : public unary_function<_Pair, typename _Pair::first_type> {
00486   typename _Pair::first_type& operator()(_Pair& __x) const {
00487     return __x.first;
00488   }
00489   const typename _Pair::first_type& operator()(const _Pair& __x) const {
00490     return __x.first;
00491   }
00492 };
00493 
00494 template <class _Pair>
00495 struct _Select2nd : public unary_function<_Pair, typename _Pair::second_type>
00496 {
00497   typename _Pair::second_type& operator()(_Pair& __x) const {
00498     return __x.second;
00499   }
00500   const typename _Pair::second_type& operator()(const _Pair& __x) const {
00501     return __x.second;
00502   }
00503 };
00504 
00505 // 20.3.8 adaptors pointers members
00527 
00528 template <class _Ret, class _Tp>
00529 class mem_fun_t : public unary_function<_Tp*,_Ret> {
00530 public:
00531   explicit mem_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
00532   _Ret operator()(_Tp* __p) const { return (__p->*_M_f)(); }
00533 private:
00534   _Ret (_Tp::*_M_f)();
00535 };
00536 
00538 template <class _Ret, class _Tp>
00539 class const_mem_fun_t : public unary_function<const _Tp*,_Ret> {
00540 public:
00541   explicit const_mem_fun_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}
00542   _Ret operator()(const _Tp* __p) const { return (__p->*_M_f)(); }
00543 private:
00544   _Ret (_Tp::*_M_f)() const;
00545 };
00546 
00548 template <class _Ret, class _Tp>
00549 class mem_fun_ref_t : public unary_function<_Tp,_Ret> {
00550 public:
00551   explicit mem_fun_ref_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
00552   _Ret operator()(_Tp& __r) const { return (__r.*_M_f)(); }
00553 private:
00554   _Ret (_Tp::*_M_f)();
00555 };
00556 
00558 template <class _Ret, class _Tp>
00559 class const_mem_fun_ref_t : public unary_function<_Tp,_Ret> {
00560 public:
00561   explicit const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}
00562   _Ret operator()(const _Tp& __r) const { return (__r.*_M_f)(); }
00563 private:
00564   _Ret (_Tp::*_M_f)() const;
00565 };
00566 
00568 template <class _Ret, class _Tp, class _Arg>
00569 class mem_fun1_t : public binary_function<_Tp*,_Arg,_Ret> {
00570 public:
00571   explicit mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
00572   _Ret operator()(_Tp* __p, _Arg __x) const { return (__p->*_M_f)(__x); }
00573 private:
00574   _Ret (_Tp::*_M_f)(_Arg);
00575 };
00576 
00578 template <class _Ret, class _Tp, class _Arg>
00579 class const_mem_fun1_t : public binary_function<const _Tp*,_Arg,_Ret> {
00580 public:
00581   explicit const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
00582   _Ret operator()(const _Tp* __p, _Arg __x) const
00583     { return (__p->*_M_f)(__x); }
00584 private:
00585   _Ret (_Tp::*_M_f)(_Arg) const;
00586 };
00587 
00589 template <class _Ret, class _Tp, class _Arg>
00590 class mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> {
00591 public:
00592   explicit mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
00593   _Ret operator()(_Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); }
00594 private:
00595   _Ret (_Tp::*_M_f)(_Arg);
00596 };
00597 
00599 template <class _Ret, class _Tp, class _Arg>
00600 class const_mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> {
00601 public:
00602   explicit const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
00603   _Ret operator()(const _Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); }
00604 private:
00605   _Ret (_Tp::*_M_f)(_Arg) const;
00606 };
00607 
00609 template <class _Tp>
00610 class mem_fun_t<void, _Tp> : public unary_function<_Tp*,void> {
00611 public:
00612   explicit mem_fun_t(void (_Tp::*__pf)()) : _M_f(__pf) {}
00613   void operator()(_Tp* __p) const { (__p->*_M_f)(); }
00614 private:
00615   void (_Tp::*_M_f)();
00616 };
00617 
00619 template <class _Tp>
00620 class const_mem_fun_t<void, _Tp> : public unary_function<const _Tp*,void> {
00621 public:
00622   explicit const_mem_fun_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}
00623   void operator()(const _Tp* __p) const { (__p->*_M_f)(); }
00624 private:
00625   void (_Tp::*_M_f)() const;
00626 };
00627 
00629 template <class _Tp>
00630 class mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {
00631 public:
00632   explicit mem_fun_ref_t(void (_Tp::*__pf)()) : _M_f(__pf) {}
00633   void operator()(_Tp& __r) const { (__r.*_M_f)(); }
00634 private:
00635   void (_Tp::*_M_f)();
00636 };
00637 
00639 template <class _Tp>
00640 class const_mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {
00641 public:
00642   explicit const_mem_fun_ref_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}
00643   void operator()(const _Tp& __r) const { (__r.*_M_f)(); }
00644 private:
00645   void (_Tp::*_M_f)() const;
00646 };
00647 
00649 template <class _Tp, class _Arg>
00650 class mem_fun1_t<void, _Tp, _Arg> : public binary_function<_Tp*,_Arg,void> {
00651 public:
00652   explicit mem_fun1_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
00653   void operator()(_Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }
00654 private:
00655   void (_Tp::*_M_f)(_Arg);
00656 };
00657 
00659 template <class _Tp, class _Arg>
00660 class const_mem_fun1_t<void, _Tp, _Arg> 
00661   : public binary_function<const _Tp*,_Arg,void> {
00662 public:
00663   explicit const_mem_fun1_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
00664   void operator()(const _Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }
00665 private:
00666   void (_Tp::*_M_f)(_Arg) const;
00667 };
00668 
00670 template <class _Tp, class _Arg>
00671 class mem_fun1_ref_t<void, _Tp, _Arg>
00672   : public binary_function<_Tp,_Arg,void> {
00673 public:
00674   explicit mem_fun1_ref_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
00675   void operator()(_Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); }
00676 private:
00677   void (_Tp::*_M_f)(_Arg);
00678 };
00679 
00681 template <class _Tp, class _Arg>
00682 class const_mem_fun1_ref_t<void, _Tp, _Arg>
00683   : public binary_function<_Tp,_Arg,void> {
00684 public:
00685   explicit const_mem_fun1_ref_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
00686   void operator()(const _Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); }
00687 private:
00688   void (_Tp::*_M_f)(_Arg) const;
00689 };
00690 
00691 
00692 // Mem_fun adaptor helper functions.  There are only two:
00693 // mem_fun and mem_fun_ref.
00694 
00695 template <class _Ret, class _Tp>
00696 inline mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)())
00697   { return mem_fun_t<_Ret,_Tp>(__f); }
00698 
00699 template <class _Ret, class _Tp>
00700 inline const_mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)() const)
00701   { return const_mem_fun_t<_Ret,_Tp>(__f); }
00702 
00703 template <class _Ret, class _Tp>
00704 inline mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)()) 
00705   { return mem_fun_ref_t<_Ret,_Tp>(__f); }
00706 
00707 template <class _Ret, class _Tp>
00708 inline const_mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)() const)
00709   { return const_mem_fun_ref_t<_Ret,_Tp>(__f); }
00710 
00711 template <class _Ret, class _Tp, class _Arg>
00712 inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg))
00713   { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
00714 
00715 template <class _Ret, class _Tp, class _Arg>
00716 inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg) const)
00717   { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
00718 
00719 template <class _Ret, class _Tp, class _Arg>
00720 inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
00721   { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
00722 
00723 template <class _Ret, class _Tp, class _Arg>
00724 inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg>
00725 mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
00726   { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
00727 
00730 } // namespace std
00731 
00732 #endif /* __GLIBCPP_INTERNAL_FUNCTION_H */
00733 
00734 // Local Variables:
00735 // mode:C++
00736 // End:

Generated on Thu Nov 21 03:12:50 2002 for libstdc++-v3 Source by doxygen1.2.18-20021030