2012-01-18 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_function.h
blob88655fc55c3cfc166bbd3033dd1db47717f2c55a
1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011
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.
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.
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.
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/>.
28 * Copyright (c) 1994
29 * Hewlett-Packard Company
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.
40 * Copyright (c) 1996-1998
41 * Silicon Graphics Computer Systems, Inc.
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.
52 /** @file bits/stl_function.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{functional}
57 #ifndef _STL_FUNCTION_H
58 #define _STL_FUNCTION_H 1
60 namespace std _GLIBCXX_VISIBILITY(default)
62 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 // 20.3.1 base classes
65 /** @defgroup functors Function Objects
66 * @ingroup utilities
68 * Function objects, or @e functors, are objects with an @c operator()
69 * defined and accessible. They can be passed as arguments to algorithm
70 * templates and used in place of a function pointer. Not only is the
71 * resulting expressiveness of the library increased, but the generated
72 * code can be more efficient than what you might write by hand. When we
73 * refer to @a functors, then, generally we include function pointers in
74 * the description as well.
76 * Often, functors are only created as temporaries passed to algorithm
77 * calls, rather than being created as named variables.
79 * Two examples taken from the standard itself follow. To perform a
80 * by-element addition of two vectors @c a and @c b containing @c double,
81 * and put the result in @c a, use
82 * \code
83 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
84 * \endcode
85 * To negate every element in @c a, use
86 * \code
87 * transform(a.begin(), a.end(), a.begin(), negate<double>());
88 * \endcode
89 * The addition and negation functions will be inlined directly.
91 * The standard functors are derived from structs named @c unary_function
92 * and @c binary_function. These two classes contain nothing but typedefs,
93 * to aid in generic (template) programming. If you write your own
94 * functors, you might consider doing the same.
96 * @{
98 /**
99 * This is one of the @link functors functor base classes@endlink.
101 template<typename _Arg, typename _Result>
102 struct unary_function
104 /// @c argument_type is the type of the argument
105 typedef _Arg argument_type;
107 /// @c result_type is the return type
108 typedef _Result result_type;
112 * This is one of the @link functors functor base classes@endlink.
114 template<typename _Arg1, typename _Arg2, typename _Result>
115 struct binary_function
117 /// @c first_argument_type is the type of the first argument
118 typedef _Arg1 first_argument_type;
120 /// @c second_argument_type is the type of the second argument
121 typedef _Arg2 second_argument_type;
123 /// @c result_type is the return type
124 typedef _Result result_type;
126 /** @} */
128 // 20.3.2 arithmetic
129 /** @defgroup arithmetic_functors Arithmetic Classes
130 * @ingroup functors
132 * Because basic math often needs to be done during an algorithm,
133 * the library provides functors for those operations. See the
134 * documentation for @link functors the base classes@endlink
135 * for examples of their use.
137 * @{
139 /// One of the @link arithmetic_functors math functors@endlink.
140 template<typename _Tp>
141 struct plus : public binary_function<_Tp, _Tp, _Tp>
144 operator()(const _Tp& __x, const _Tp& __y) const
145 { return __x + __y; }
148 /// One of the @link arithmetic_functors math functors@endlink.
149 template<typename _Tp>
150 struct minus : public binary_function<_Tp, _Tp, _Tp>
153 operator()(const _Tp& __x, const _Tp& __y) const
154 { return __x - __y; }
157 /// One of the @link arithmetic_functors math functors@endlink.
158 template<typename _Tp>
159 struct multiplies : public binary_function<_Tp, _Tp, _Tp>
162 operator()(const _Tp& __x, const _Tp& __y) const
163 { return __x * __y; }
166 /// One of the @link arithmetic_functors math functors@endlink.
167 template<typename _Tp>
168 struct divides : public binary_function<_Tp, _Tp, _Tp>
171 operator()(const _Tp& __x, const _Tp& __y) const
172 { return __x / __y; }
175 /// One of the @link arithmetic_functors math functors@endlink.
176 template<typename _Tp>
177 struct modulus : public binary_function<_Tp, _Tp, _Tp>
180 operator()(const _Tp& __x, const _Tp& __y) const
181 { return __x % __y; }
184 /// One of the @link arithmetic_functors math functors@endlink.
185 template<typename _Tp>
186 struct negate : public unary_function<_Tp, _Tp>
189 operator()(const _Tp& __x) const
190 { return -__x; }
192 /** @} */
194 // 20.3.3 comparisons
195 /** @defgroup comparison_functors Comparison Classes
196 * @ingroup functors
198 * The library provides six wrapper functors for all the basic comparisons
199 * in C++, like @c <.
201 * @{
203 /// One of the @link comparison_functors comparison functors@endlink.
204 template<typename _Tp>
205 struct equal_to : public binary_function<_Tp, _Tp, bool>
207 bool
208 operator()(const _Tp& __x, const _Tp& __y) const
209 { return __x == __y; }
212 /// One of the @link comparison_functors comparison functors@endlink.
213 template<typename _Tp>
214 struct not_equal_to : public binary_function<_Tp, _Tp, bool>
216 bool
217 operator()(const _Tp& __x, const _Tp& __y) const
218 { return __x != __y; }
221 /// One of the @link comparison_functors comparison functors@endlink.
222 template<typename _Tp>
223 struct greater : public binary_function<_Tp, _Tp, bool>
225 bool
226 operator()(const _Tp& __x, const _Tp& __y) const
227 { return __x > __y; }
230 /// One of the @link comparison_functors comparison functors@endlink.
231 template<typename _Tp>
232 struct less : public binary_function<_Tp, _Tp, bool>
234 bool
235 operator()(const _Tp& __x, const _Tp& __y) const
236 { return __x < __y; }
239 /// One of the @link comparison_functors comparison functors@endlink.
240 template<typename _Tp>
241 struct greater_equal : public binary_function<_Tp, _Tp, bool>
243 bool
244 operator()(const _Tp& __x, const _Tp& __y) const
245 { return __x >= __y; }
248 /// One of the @link comparison_functors comparison functors@endlink.
249 template<typename _Tp>
250 struct less_equal : public binary_function<_Tp, _Tp, bool>
252 bool
253 operator()(const _Tp& __x, const _Tp& __y) const
254 { return __x <= __y; }
256 /** @} */
258 // 20.3.4 logical operations
259 /** @defgroup logical_functors Boolean Operations Classes
260 * @ingroup functors
262 * Here are wrapper functors for Boolean operations: @c &&, @c ||,
263 * and @c !.
265 * @{
267 /// One of the @link logical_functors Boolean operations functors@endlink.
268 template<typename _Tp>
269 struct logical_and : public binary_function<_Tp, _Tp, bool>
271 bool
272 operator()(const _Tp& __x, const _Tp& __y) const
273 { return __x && __y; }
276 /// One of the @link logical_functors Boolean operations functors@endlink.
277 template<typename _Tp>
278 struct logical_or : public binary_function<_Tp, _Tp, bool>
280 bool
281 operator()(const _Tp& __x, const _Tp& __y) const
282 { return __x || __y; }
285 /// One of the @link logical_functors Boolean operations functors@endlink.
286 template<typename _Tp>
287 struct logical_not : public unary_function<_Tp, bool>
289 bool
290 operator()(const _Tp& __x) const
291 { return !__x; }
293 /** @} */
295 // _GLIBCXX_RESOLVE_LIB_DEFECTS
296 // DR 660. Missing Bitwise Operations.
297 template<typename _Tp>
298 struct bit_and : public binary_function<_Tp, _Tp, _Tp>
301 operator()(const _Tp& __x, const _Tp& __y) const
302 { return __x & __y; }
305 template<typename _Tp>
306 struct bit_or : public binary_function<_Tp, _Tp, _Tp>
309 operator()(const _Tp& __x, const _Tp& __y) const
310 { return __x | __y; }
313 template<typename _Tp>
314 struct bit_xor : public binary_function<_Tp, _Tp, _Tp>
317 operator()(const _Tp& __x, const _Tp& __y) const
318 { return __x ^ __y; }
321 // 20.3.5 negators
322 /** @defgroup negators Negators
323 * @ingroup functors
325 * The functions @c not1 and @c not2 each take a predicate functor
326 * and return an instance of @c unary_negate or
327 * @c binary_negate, respectively. These classes are functors whose
328 * @c operator() performs the stored predicate function and then returns
329 * the negation of the result.
331 * For example, given a vector of integers and a trivial predicate,
332 * \code
333 * struct IntGreaterThanThree
334 * : public std::unary_function<int, bool>
336 * bool operator() (int x) { return x > 3; }
337 * };
339 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
340 * \endcode
341 * The call to @c find_if will locate the first index (i) of @c v for which
342 * <code>!(v[i] > 3)</code> is true.
344 * The not1/unary_negate combination works on predicates taking a single
345 * argument. The not2/binary_negate combination works on predicates which
346 * take two arguments.
348 * @{
350 /// One of the @link negators negation functors@endlink.
351 template<typename _Predicate>
352 class unary_negate
353 : public unary_function<typename _Predicate::argument_type, bool>
355 protected:
356 _Predicate _M_pred;
358 public:
359 explicit
360 unary_negate(const _Predicate& __x) : _M_pred(__x) { }
362 bool
363 operator()(const typename _Predicate::argument_type& __x) const
364 { return !_M_pred(__x); }
367 /// One of the @link negators negation functors@endlink.
368 template<typename _Predicate>
369 inline unary_negate<_Predicate>
370 not1(const _Predicate& __pred)
371 { return unary_negate<_Predicate>(__pred); }
373 /// One of the @link negators negation functors@endlink.
374 template<typename _Predicate>
375 class binary_negate
376 : public binary_function<typename _Predicate::first_argument_type,
377 typename _Predicate::second_argument_type, bool>
379 protected:
380 _Predicate _M_pred;
382 public:
383 explicit
384 binary_negate(const _Predicate& __x) : _M_pred(__x) { }
386 bool
387 operator()(const typename _Predicate::first_argument_type& __x,
388 const typename _Predicate::second_argument_type& __y) const
389 { return !_M_pred(__x, __y); }
392 /// One of the @link negators negation functors@endlink.
393 template<typename _Predicate>
394 inline binary_negate<_Predicate>
395 not2(const _Predicate& __pred)
396 { return binary_negate<_Predicate>(__pred); }
397 /** @} */
399 // 20.3.7 adaptors pointers functions
400 /** @defgroup pointer_adaptors Adaptors for pointers to functions
401 * @ingroup functors
403 * The advantage of function objects over pointers to functions is that
404 * the objects in the standard library declare nested typedefs describing
405 * their argument and result types with uniform names (e.g., @c result_type
406 * from the base classes @c unary_function and @c binary_function).
407 * Sometimes those typedefs are required, not just optional.
409 * Adaptors are provided to turn pointers to unary (single-argument) and
410 * binary (double-argument) functions into function objects. The
411 * long-winded functor @c pointer_to_unary_function is constructed with a
412 * function pointer @c f, and its @c operator() called with argument @c x
413 * returns @c f(x). The functor @c pointer_to_binary_function does the same
414 * thing, but with a double-argument @c f and @c operator().
416 * The function @c ptr_fun takes a pointer-to-function @c f and constructs
417 * an instance of the appropriate functor.
419 * @{
421 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
422 template<typename _Arg, typename _Result>
423 class pointer_to_unary_function : public unary_function<_Arg, _Result>
425 protected:
426 _Result (*_M_ptr)(_Arg);
428 public:
429 pointer_to_unary_function() { }
431 explicit
432 pointer_to_unary_function(_Result (*__x)(_Arg))
433 : _M_ptr(__x) { }
435 _Result
436 operator()(_Arg __x) const
437 { return _M_ptr(__x); }
440 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
441 template<typename _Arg, typename _Result>
442 inline pointer_to_unary_function<_Arg, _Result>
443 ptr_fun(_Result (*__x)(_Arg))
444 { return pointer_to_unary_function<_Arg, _Result>(__x); }
446 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
447 template<typename _Arg1, typename _Arg2, typename _Result>
448 class pointer_to_binary_function
449 : public binary_function<_Arg1, _Arg2, _Result>
451 protected:
452 _Result (*_M_ptr)(_Arg1, _Arg2);
454 public:
455 pointer_to_binary_function() { }
457 explicit
458 pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
459 : _M_ptr(__x) { }
461 _Result
462 operator()(_Arg1 __x, _Arg2 __y) const
463 { return _M_ptr(__x, __y); }
466 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
467 template<typename _Arg1, typename _Arg2, typename _Result>
468 inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
469 ptr_fun(_Result (*__x)(_Arg1, _Arg2))
470 { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
471 /** @} */
473 template<typename _Tp>
474 struct _Identity : public unary_function<_Tp,_Tp>
476 _Tp&
477 operator()(_Tp& __x) const
478 { return __x; }
480 const _Tp&
481 operator()(const _Tp& __x) const
482 { return __x; }
485 template<typename _Pair>
486 struct _Select1st : public unary_function<_Pair,
487 typename _Pair::first_type>
489 typename _Pair::first_type&
490 operator()(_Pair& __x) const
491 { return __x.first; }
493 const typename _Pair::first_type&
494 operator()(const _Pair& __x) const
495 { return __x.first; }
497 #ifdef __GXX_EXPERIMENTAL_CXX0X__
498 template<typename _Pair2>
499 typename _Pair2::first_type&
500 operator()(_Pair2& __x) const
501 { return __x.first; }
503 template<typename _Pair2>
504 const typename _Pair2::first_type&
505 operator()(const _Pair2& __x) const
506 { return __x.first; }
507 #endif
510 template<typename _Pair>
511 struct _Select2nd : public unary_function<_Pair,
512 typename _Pair::second_type>
514 typename _Pair::second_type&
515 operator()(_Pair& __x) const
516 { return __x.second; }
518 const typename _Pair::second_type&
519 operator()(const _Pair& __x) const
520 { return __x.second; }
523 // 20.3.8 adaptors pointers members
524 /** @defgroup memory_adaptors Adaptors for pointers to members
525 * @ingroup functors
527 * There are a total of 8 = 2^3 function objects in this family.
528 * (1) Member functions taking no arguments vs member functions taking
529 * one argument.
530 * (2) Call through pointer vs call through reference.
531 * (3) Const vs non-const member function.
533 * All of this complexity is in the function objects themselves. You can
534 * ignore it by using the helper function mem_fun and mem_fun_ref,
535 * which create whichever type of adaptor is appropriate.
537 * @{
539 /// One of the @link memory_adaptors adaptors for member
540 /// pointers@endlink.
541 template<typename _Ret, typename _Tp>
542 class mem_fun_t : public unary_function<_Tp*, _Ret>
544 public:
545 explicit
546 mem_fun_t(_Ret (_Tp::*__pf)())
547 : _M_f(__pf) { }
549 _Ret
550 operator()(_Tp* __p) const
551 { return (__p->*_M_f)(); }
553 private:
554 _Ret (_Tp::*_M_f)();
557 /// One of the @link memory_adaptors adaptors for member
558 /// pointers@endlink.
559 template<typename _Ret, typename _Tp>
560 class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
562 public:
563 explicit
564 const_mem_fun_t(_Ret (_Tp::*__pf)() const)
565 : _M_f(__pf) { }
567 _Ret
568 operator()(const _Tp* __p) const
569 { return (__p->*_M_f)(); }
571 private:
572 _Ret (_Tp::*_M_f)() const;
575 /// One of the @link memory_adaptors adaptors for member
576 /// pointers@endlink.
577 template<typename _Ret, typename _Tp>
578 class mem_fun_ref_t : public unary_function<_Tp, _Ret>
580 public:
581 explicit
582 mem_fun_ref_t(_Ret (_Tp::*__pf)())
583 : _M_f(__pf) { }
585 _Ret
586 operator()(_Tp& __r) const
587 { return (__r.*_M_f)(); }
589 private:
590 _Ret (_Tp::*_M_f)();
593 /// One of the @link memory_adaptors adaptors for member
594 /// pointers@endlink.
595 template<typename _Ret, typename _Tp>
596 class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
598 public:
599 explicit
600 const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
601 : _M_f(__pf) { }
603 _Ret
604 operator()(const _Tp& __r) const
605 { return (__r.*_M_f)(); }
607 private:
608 _Ret (_Tp::*_M_f)() const;
611 /// One of the @link memory_adaptors adaptors for member
612 /// pointers@endlink.
613 template<typename _Ret, typename _Tp, typename _Arg>
614 class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
616 public:
617 explicit
618 mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
619 : _M_f(__pf) { }
621 _Ret
622 operator()(_Tp* __p, _Arg __x) const
623 { return (__p->*_M_f)(__x); }
625 private:
626 _Ret (_Tp::*_M_f)(_Arg);
629 /// One of the @link memory_adaptors adaptors for member
630 /// pointers@endlink.
631 template<typename _Ret, typename _Tp, typename _Arg>
632 class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
634 public:
635 explicit
636 const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
637 : _M_f(__pf) { }
639 _Ret
640 operator()(const _Tp* __p, _Arg __x) const
641 { return (__p->*_M_f)(__x); }
643 private:
644 _Ret (_Tp::*_M_f)(_Arg) const;
647 /// One of the @link memory_adaptors adaptors for member
648 /// pointers@endlink.
649 template<typename _Ret, typename _Tp, typename _Arg>
650 class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
652 public:
653 explicit
654 mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
655 : _M_f(__pf) { }
657 _Ret
658 operator()(_Tp& __r, _Arg __x) const
659 { return (__r.*_M_f)(__x); }
661 private:
662 _Ret (_Tp::*_M_f)(_Arg);
665 /// One of the @link memory_adaptors adaptors for member
666 /// pointers@endlink.
667 template<typename _Ret, typename _Tp, typename _Arg>
668 class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
670 public:
671 explicit
672 const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
673 : _M_f(__pf) { }
675 _Ret
676 operator()(const _Tp& __r, _Arg __x) const
677 { return (__r.*_M_f)(__x); }
679 private:
680 _Ret (_Tp::*_M_f)(_Arg) const;
683 // Mem_fun adaptor helper functions. There are only two:
684 // mem_fun and mem_fun_ref.
685 template<typename _Ret, typename _Tp>
686 inline mem_fun_t<_Ret, _Tp>
687 mem_fun(_Ret (_Tp::*__f)())
688 { return mem_fun_t<_Ret, _Tp>(__f); }
690 template<typename _Ret, typename _Tp>
691 inline const_mem_fun_t<_Ret, _Tp>
692 mem_fun(_Ret (_Tp::*__f)() const)
693 { return const_mem_fun_t<_Ret, _Tp>(__f); }
695 template<typename _Ret, typename _Tp>
696 inline mem_fun_ref_t<_Ret, _Tp>
697 mem_fun_ref(_Ret (_Tp::*__f)())
698 { return mem_fun_ref_t<_Ret, _Tp>(__f); }
700 template<typename _Ret, typename _Tp>
701 inline const_mem_fun_ref_t<_Ret, _Tp>
702 mem_fun_ref(_Ret (_Tp::*__f)() const)
703 { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
705 template<typename _Ret, typename _Tp, typename _Arg>
706 inline mem_fun1_t<_Ret, _Tp, _Arg>
707 mem_fun(_Ret (_Tp::*__f)(_Arg))
708 { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
710 template<typename _Ret, typename _Tp, typename _Arg>
711 inline const_mem_fun1_t<_Ret, _Tp, _Arg>
712 mem_fun(_Ret (_Tp::*__f)(_Arg) const)
713 { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
715 template<typename _Ret, typename _Tp, typename _Arg>
716 inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
717 mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
718 { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
720 template<typename _Ret, typename _Tp, typename _Arg>
721 inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
722 mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
723 { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
725 /** @} */
727 _GLIBCXX_END_NAMESPACE_VERSION
728 } // namespace
730 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_USE_DEPRECATED
731 # include <backward/binders.h>
732 #endif
734 #endif /* _STL_FUNCTION_H */