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 00056 /** @file stl_function.h 00057 * This is an internal header file, included by other library headers. 00058 * You should not attempt to use it directly. 00059 */ 00060 00061 #ifndef __GLIBCPP_INTERNAL_FUNCTION_H 00062 #define __GLIBCPP_INTERNAL_FUNCTION_H 00063 00064 namespace std 00065 { 00066 // 20.3.1 base classes 00067 /** @defgroup s20_3_1_base Functor Base Classes 00068 * Function objects, or @e functors, are objects with an @c operator() 00069 * defined and accessible. They can be passed as arguments to algorithm 00070 * templates and used in place of a function pointer. Not only is the 00071 * resulting expressiveness of the library increased, but the generated 00072 * code can be more efficient than what you might write by hand. When we 00073 * refer to "functors," then, generally we include function pointers in 00074 * the description as well. 00075 * 00076 * Often, functors are only created as temporaries passed to algorithm 00077 * calls, rather than being created as named variables. 00078 * 00079 * Two examples taken from the standard itself follow. To perform a 00080 * by-element addition of two vectors @c a and @c b containing @c double, 00081 * and put the result in @c a, use 00082 * \code 00083 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>()); 00084 * \endcode 00085 * To negate every element in @c a, use 00086 * \code 00087 * transform(a.begin(), a.end(), a.begin(), negate<double>()); 00088 * \endcode 00089 * The addition and negation functions will be inlined directly. 00090 * 00091 * The standard functiors are derived from structs named @c unary_function 00092 * and @c binary_function. These two classes contain nothing but typedefs, 00093 * to aid in generic (template) programming. If you write your own 00094 * functors, you might consider doing the same. 00095 * 00096 * @{ 00097 */ 00098 /** 00099 * This is one of the @link s20_3_1_base functor base classes@endlink. 00100 */ 00101 template <class _Arg, class _Result> 00102 struct unary_function { 00103 typedef _Arg argument_type; ///< @c argument_type is the type of the argument (no surprises here) 00104 typedef _Result result_type; ///< @c result_type is the return type 00105 }; 00106 00107 /** 00108 * This is one of the @link s20_3_1_base functor base classes@endlink. 00109 */ 00110 template <class _Arg1, class _Arg2, class _Result> 00111 struct binary_function { 00112 typedef _Arg1 first_argument_type; ///< the type of the first argument (no surprises here) 00113 typedef _Arg2 second_argument_type; ///< the type of the second argument 00114 typedef _Result result_type; ///< type of the return type 00115 }; 00116 /** @} */ 00117 00118 // 20.3.2 arithmetic 00119 /** @defgroup s20_3_2_arithmetic Arithmetic Classes 00120 * Because basic math often needs to be done during an algorithm, the library 00121 * provides functors for those operations. See the documentation for 00122 * @link s20_3_1_base the base classes@endlink for examples of their use. 00123 * 00124 * @{ 00125 */ 00126 /// One of the @link s20_3_2_arithmetic math functors@endlink. 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 00132 /// One of the @link s20_3_2_arithmetic math functors@endlink. 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 00138 /// One of the @link s20_3_2_arithmetic math functors@endlink. 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 00144 /// One of the @link s20_3_2_arithmetic math functors@endlink. 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 00150 /// One of the @link s20_3_2_arithmetic math functors@endlink. 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 00157 /// One of the @link s20_3_2_arithmetic math functors@endlink. 00158 template <class _Tp> 00159 struct negate : public unary_function<_Tp,_Tp> 00160 { 00161 _Tp operator()(const _Tp& __x) const { return -__x; } 00162 }; 00163 /** @} */ 00164 00165 // 20.3.3 comparisons 00166 /** @defgroup s20_3_3_comparisons Comparison Classes 00167 * The library provides six wrapper functors for all the basic comparisons 00168 * in C++, like @c <. 00169 * 00170 * @{ 00171 */ 00172 /// One of the @link s20_3_3_comparisons comparison functors@endlink. 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 00179 /// One of the @link s20_3_3_comparisons comparison functors@endlink. 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 00186 /// One of the @link s20_3_3_comparisons comparison functors@endlink. 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 00193 /// One of the @link s20_3_3_comparisons comparison functors@endlink. 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 00200 /// One of the @link s20_3_3_comparisons comparison functors@endlink. 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 00207 /// One of the @link s20_3_3_comparisons comparison functors@endlink. 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 }; 00213 /** @} */ 00214 00215 // 20.3.4 logical operations 00216 /** @defgroup s20_3_4_logical Boolean Operations Classes 00217 * Here are wrapper functors for Boolean operations: @c &&, @c ||, and @c !. 00218 * 00219 * @{ 00220 */ 00221 /// One of the @link s20_3_4_logical Boolean operations functors@endlink. 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 00228 /// One of the @link s20_3_4_logical Boolean operations functors@endlink. 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 00235 /// One of the @link s20_3_4_logical Boolean operations functors@endlink. 00236 template <class _Tp> 00237 struct logical_not : public unary_function<_Tp,bool> 00238 { 00239 bool operator()(const _Tp& __x) const { return !__x; } 00240 }; 00241 /** @} */ 00242 00243 // 20.3.5 negators 00244 /** @defgroup s20_3_5_negators Negators 00245 * The functions @c not1 and @c not2 each take a predicate functor 00246 * and return an instance of @c unary_negate or 00247 * @c binary_negate, respectively. These classes are functors whose 00248 * @c operator() performs the stored predicate function and then returns 00249 * the negation of the result. 00250 * 00251 * For example, given a vector of integers and a trivial predicate, 00252 * \code 00253 * struct IntGreaterThanThree 00254 * : public std::unary_function<int, bool> 00255 * { 00256 * bool operator() (int x) { return x > 3; } 00257 * }; 00258 * 00259 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree())); 00260 * \endcode 00261 * The call to @c find_if will locate the first index (i) of @c v for which 00262 * "!(v[i] > 3)" is true. 00263 * 00264 * The not1/unary_negate combination works on predicates taking a single 00265 * argument. The not2/binary_negate combination works on predicates which 00266 * take two arguments. 00267 * 00268 * @{ 00269 */ 00270 /// One of the @link s20_3_5_negators negation functors@endlink. 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 00283 /// One of the @link s20_3_5_negators negation functors@endlink. 00284 template <class _Predicate> 00285 inline unary_negate<_Predicate> 00286 not1(const _Predicate& __pred) 00287 { 00288 return unary_negate<_Predicate>(__pred); 00289 } 00290 00291 /// One of the @link s20_3_5_negators negation functors@endlink. 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 00308 /// One of the @link s20_3_5_negators negation functors@endlink. 00309 template <class _Predicate> 00310 inline binary_negate<_Predicate> 00311 not2(const _Predicate& __pred) 00312 { 00313 return binary_negate<_Predicate>(__pred); 00314 } 00315 /** @} */ 00316 00317 // 20.3.6 binders 00318 /** @defgroup s20_3_6_binder Binder Classes 00319 * Binders turn functions/functors with two arguments into functors with 00320 * a single argument, storing an argument to be applied later. For 00321 * example, an variable @c B of type @c binder1st is constructed from a functor 00322 * @c f and an argument @c x. Later, B's @c operator() is called with a 00323 * single argument @c y. The return value is the value of @c f(x,y). 00324 * @c B can be "called" with various arguments (y1, y2, ...) and will in 00325 * turn call @c f(x,y1), @c f(x,y2), ... 00326 * 00327 * The function @c bind1st is provided to save some typing. It takes the 00328 * function and an argument as parameters, and returns an instance of 00329 * @c binder1st. 00330 * 00331 * The type @c binder2nd and its creator function @c bind2nd do the same 00332 * thing, but the stored argument is passed as the second parameter instead 00333 * of the first, e.g., @c bind2nd(std::minus<float>,1.3) will create a 00334 * functor whose @c operator() accepts a floating-point number, subtracts 00335 * 1.3 from it, and returns the result. (If @c bind1st had been used, 00336 * the functor would perform "1.3 - x" instead. 00337 * 00338 * Creator-wrapper functions like @c bind1st are intended to be used in 00339 * calling algorithms. Their return values will be temporary objects. 00340 * (The goal is to not require you to type names like 00341 * @c std::binder1st<std::plus<int>> for declaring a variable to hold the 00342 * return value from @c bind1st(std::plus<int>,5). 00343 * 00344 * These become more useful when combined with the composition functions. 00345 * 00346 * @{ 00347 */ 00348 /// One of the @link s20_3_6_binder binder functors@endlink. 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 00373 /// One of the @link s20_3_6_binder binder functors@endlink. 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 00382 /// One of the @link s20_3_6_binder binder functors@endlink. 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 00407 /// One of the @link s20_3_6_binder binder functors@endlink. 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 } 00415 /** @} */ 00416 00417 // 20.3.7 adaptors pointers functions 00418 /** @defgroup s20_3_7_adaptors Adaptors for pointers to functions 00419 * The advantage of function objects over pointers to functions is that 00420 * the objects in the standard library declare nested typedefs describing 00421 * their argument and result types with uniform names (e.g., @c result_type 00422 * from the base classes @c unary_function and @c binary_function). 00423 * Sometimes those typedefs are required, not just optional. 00424 * 00425 * Adaptors are provided to turn pointers to unary (single-argument) and 00426 * binary (double-argument) functions into function objects. The long-winded 00427 * functor @c pointer_to_unary_function is constructed with a function 00428 * pointer @c f, and its @c operator() called with argument @c x returns 00429 * @c f(x). The functor @c pointer_to_binary_function does the same thing, 00430 * but with a double-argument @c f and @c operator(). 00431 * 00432 * The function @c ptr_fun takes a pointer-to-function @c f and constructs 00433 * an instance of the appropriate functor. 00434 * 00435 * @{ 00436 */ 00437 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. 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 00448 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. 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 00455 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. 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 00470 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. 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 } 00476 /** @} */ 00477 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 00506 /** @defgroup s20_3_8_memadaptors Adaptors for pointers to members 00507 * There are a total of 16 = 2^4 function objects in this family. 00508 * (1) Member functions taking no arguments vs member functions taking 00509 * one argument. 00510 * (2) Call through pointer vs call through reference. 00511 * (3) Member function with void return type vs member function with 00512 * non-void return type. 00513 * (4) Const vs non-const member function. 00514 * 00515 * Note that choice (3) is nothing more than a workaround: according 00516 * to the draft, compilers should handle void and non-void the same way. 00517 * This feature is not yet widely implemented, though. You can only use 00518 * member functions returning void if your compiler supports partial 00519 * specialization. 00520 * 00521 * All of this complexity is in the function objects themselves. You can 00522 * ignore it by using the helper function mem_fun and mem_fun_ref, 00523 * which create whichever type of adaptor is appropriate. 00524 * 00525 * @{ 00526 */ 00527 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00537 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00547 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00557 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00567 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00577 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00588 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00598 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00608 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00618 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00628 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00638 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00648 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00658 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00669 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00680 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 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 00728 /** @} */ 00729 00730 } // namespace std 00731 00732 #endif /* __GLIBCPP_INTERNAL_FUNCTION_H */ 00733 00734 // Local Variables: 00735 // mode:C++ 00736 // End:

Generated on Wed Aug 4 21:43:18 2004 for libstdc++-v3 Source by doxygen 1.3.8