]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Functor implementations -*- C++ -*- |
2 | ||
9efc7479 | 3 | // Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
42526146 PE |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 2, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License along | |
17 | // with this library; see the file COPYING. If not, write to the Free | |
18 | // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
19 | // USA. | |
20 | ||
21 | // As a special exception, you may use this file as part of a free software | |
22 | // library without restriction. Specifically, if other files instantiate | |
23 | // templates or use macros or inline functions from this file, or you compile | |
24 | // this file and link it with other files to produce an executable, this | |
25 | // file does not by itself cause the resulting executable to be covered by | |
26 | // the GNU General Public License. This exception does not however | |
27 | // invalidate any other reasons why the executable file might be covered by | |
28 | // the GNU General Public License. | |
29 | ||
725dc051 BK |
30 | /* |
31 | * | |
32 | * Copyright (c) 1994 | |
33 | * Hewlett-Packard Company | |
34 | * | |
35 | * Permission to use, copy, modify, distribute and sell this software | |
36 | * and its documentation for any purpose is hereby granted without fee, | |
37 | * provided that the above copyright notice appear in all copies and | |
38 | * that both that copyright notice and this permission notice appear | |
39 | * in supporting documentation. Hewlett-Packard Company makes no | |
40 | * representations about the suitability of this software for any | |
41 | * purpose. It is provided "as is" without express or implied warranty. | |
42 | * | |
43 | * | |
44 | * Copyright (c) 1996-1998 | |
45 | * Silicon Graphics Computer Systems, Inc. | |
46 | * | |
47 | * Permission to use, copy, modify, distribute and sell this software | |
48 | * and its documentation for any purpose is hereby granted without fee, | |
49 | * provided that the above copyright notice appear in all copies and | |
50 | * that both that copyright notice and this permission notice appear | |
51 | * in supporting documentation. Silicon Graphics makes no | |
52 | * representations about the suitability of this software for any | |
53 | * purpose. It is provided "as is" without express or implied warranty. | |
54 | */ | |
55 | ||
77cd227e | 56 | /** @file stl_function.h |
729e3d3f PE |
57 | * This is an internal header file, included by other library headers. |
58 | * You should not attempt to use it directly. | |
725dc051 BK |
59 | */ |
60 | ||
3d7c150e BK |
61 | #ifndef _FUNCTION_H |
62 | #define _FUNCTION_H 1 | |
725dc051 | 63 | |
d53d7f6e PE |
64 | namespace std |
65 | { | |
77cd227e PE |
66 | // 20.3.1 base classes |
67 | /** @defgroup s20_3_1_base Functor Base Classes | |
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 "functors," then, generally we include function pointers in | |
74 | * the description as well. | |
75 | * | |
76 | * Often, functors are only created as temporaries passed to algorithm | |
77 | * calls, rather than being created as named variables. | |
78 | * | |
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. | |
90 | * | |
91 | * The standard functiors 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. | |
95 | * | |
96 | * @{ | |
97 | */ | |
98 | /** | |
729e3d3f | 99 | * This is one of the @link s20_3_1_base functor base classes@endlink. |
77cd227e | 100 | */ |
725dc051 BK |
101 | template <class _Arg, class _Result> |
102 | struct unary_function { | |
77cd227e PE |
103 | typedef _Arg argument_type; ///< @c argument_type is the type of the argument (no surprises here) |
104 | typedef _Result result_type; ///< @c result_type is the return type | |
725dc051 BK |
105 | }; |
106 | ||
77cd227e | 107 | /** |
729e3d3f | 108 | * This is one of the @link s20_3_1_base functor base classes@endlink. |
77cd227e | 109 | */ |
725dc051 BK |
110 | template <class _Arg1, class _Arg2, class _Result> |
111 | struct binary_function { | |
77cd227e PE |
112 | typedef _Arg1 first_argument_type; ///< the type of the first argument (no surprises here) |
113 | typedef _Arg2 second_argument_type; ///< the type of the second argument | |
114 | typedef _Result result_type; ///< type of the return type | |
725dc051 | 115 | }; |
77cd227e | 116 | /** @} */ |
725dc051 | 117 | |
77cd227e PE |
118 | // 20.3.2 arithmetic |
119 | /** @defgroup s20_3_2_arithmetic Arithmetic Classes | |
120 | * Because basic math often needs to be done during an algorithm, the library | |
121 | * provides functors for those operations. See the documentation for | |
729e3d3f | 122 | * @link s20_3_1_base the base classes@endlink for examples of their use. |
77cd227e PE |
123 | * |
124 | * @{ | |
125 | */ | |
729e3d3f | 126 | /// One of the @link s20_3_2_arithmetic math functors@endlink. |
725dc051 BK |
127 | template <class _Tp> |
128 | struct plus : public binary_function<_Tp,_Tp,_Tp> { | |
129 | _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; } | |
130 | }; | |
131 | ||
729e3d3f | 132 | /// One of the @link s20_3_2_arithmetic math functors@endlink. |
725dc051 BK |
133 | template <class _Tp> |
134 | struct minus : public binary_function<_Tp,_Tp,_Tp> { | |
135 | _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; } | |
136 | }; | |
137 | ||
729e3d3f | 138 | /// One of the @link s20_3_2_arithmetic math functors@endlink. |
725dc051 BK |
139 | template <class _Tp> |
140 | struct multiplies : public binary_function<_Tp,_Tp,_Tp> { | |
141 | _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; } | |
142 | }; | |
143 | ||
729e3d3f | 144 | /// One of the @link s20_3_2_arithmetic math functors@endlink. |
725dc051 BK |
145 | template <class _Tp> |
146 | struct divides : public binary_function<_Tp,_Tp,_Tp> { | |
147 | _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; } | |
148 | }; | |
149 | ||
729e3d3f | 150 | /// One of the @link s20_3_2_arithmetic math functors@endlink. |
725dc051 BK |
151 | template <class _Tp> |
152 | struct modulus : public binary_function<_Tp,_Tp,_Tp> | |
153 | { | |
154 | _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; } | |
155 | }; | |
156 | ||
729e3d3f | 157 | /// One of the @link s20_3_2_arithmetic math functors@endlink. |
725dc051 BK |
158 | template <class _Tp> |
159 | struct negate : public unary_function<_Tp,_Tp> | |
160 | { | |
161 | _Tp operator()(const _Tp& __x) const { return -__x; } | |
162 | }; | |
77cd227e | 163 | /** @} */ |
725dc051 | 164 | |
77cd227e PE |
165 | // 20.3.3 comparisons |
166 | /** @defgroup s20_3_3_comparisons Comparison Classes | |
167 | * The library provides six wrapper functors for all the basic comparisons | |
168 | * in C++, like @c <. | |
169 | * | |
170 | * @{ | |
171 | */ | |
729e3d3f | 172 | /// One of the @link s20_3_3_comparisons comparison functors@endlink. |
725dc051 BK |
173 | template <class _Tp> |
174 | struct equal_to : public binary_function<_Tp,_Tp,bool> | |
175 | { | |
176 | bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; } | |
177 | }; | |
178 | ||
729e3d3f | 179 | /// One of the @link s20_3_3_comparisons comparison functors@endlink. |
725dc051 BK |
180 | template <class _Tp> |
181 | struct not_equal_to : public binary_function<_Tp,_Tp,bool> | |
182 | { | |
183 | bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; } | |
184 | }; | |
185 | ||
729e3d3f | 186 | /// One of the @link s20_3_3_comparisons comparison functors@endlink. |
725dc051 BK |
187 | template <class _Tp> |
188 | struct greater : public binary_function<_Tp,_Tp,bool> | |
189 | { | |
190 | bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; } | |
191 | }; | |
192 | ||
729e3d3f | 193 | /// One of the @link s20_3_3_comparisons comparison functors@endlink. |
725dc051 BK |
194 | template <class _Tp> |
195 | struct less : public binary_function<_Tp,_Tp,bool> | |
196 | { | |
197 | bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; } | |
198 | }; | |
199 | ||
729e3d3f | 200 | /// One of the @link s20_3_3_comparisons comparison functors@endlink. |
725dc051 BK |
201 | template <class _Tp> |
202 | struct greater_equal : public binary_function<_Tp,_Tp,bool> | |
203 | { | |
204 | bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; } | |
205 | }; | |
206 | ||
729e3d3f | 207 | /// One of the @link s20_3_3_comparisons comparison functors@endlink. |
725dc051 BK |
208 | template <class _Tp> |
209 | struct less_equal : public binary_function<_Tp,_Tp,bool> | |
210 | { | |
211 | bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; } | |
212 | }; | |
77cd227e | 213 | /** @} */ |
725dc051 | 214 | |
77cd227e PE |
215 | // 20.3.4 logical operations |
216 | /** @defgroup s20_3_4_logical Boolean Operations Classes | |
217 | * Here are wrapper functors for Boolean operations: @c &&, @c ||, and @c !. | |
218 | * | |
219 | * @{ | |
220 | */ | |
729e3d3f | 221 | /// One of the @link s20_3_4_logical Boolean operations functors@endlink. |
725dc051 BK |
222 | template <class _Tp> |
223 | struct logical_and : public binary_function<_Tp,_Tp,bool> | |
224 | { | |
225 | bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; } | |
226 | }; | |
227 | ||
729e3d3f | 228 | /// One of the @link s20_3_4_logical Boolean operations functors@endlink. |
725dc051 BK |
229 | template <class _Tp> |
230 | struct logical_or : public binary_function<_Tp,_Tp,bool> | |
231 | { | |
232 | bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; } | |
233 | }; | |
234 | ||
729e3d3f | 235 | /// One of the @link s20_3_4_logical Boolean operations functors@endlink. |
725dc051 BK |
236 | template <class _Tp> |
237 | struct logical_not : public unary_function<_Tp,bool> | |
238 | { | |
239 | bool operator()(const _Tp& __x) const { return !__x; } | |
240 | }; | |
77cd227e PE |
241 | /** @} */ |
242 | ||
243 | // 20.3.5 negators | |
244 | /** @defgroup s20_3_5_negators Negators | |
245 | * The functions @c not1 and @c not2 each take a predicate functor | |
246 | * and return an instance of @c unary_negate or | |
247 | * @c binary_negate, respectively. These classes are functors whose | |
248 | * @c operator() performs the stored predicate function and then returns | |
249 | * the negation of the result. | |
250 | * | |
251 | * For example, given a vector of integers and a trivial predicate, | |
252 | * \code | |
253 | * struct IntGreaterThanThree | |
254 | * : public std::unary_function<int, bool> | |
255 | * { | |
256 | * bool operator() (int x) { return x > 3; } | |
257 | * }; | |
258 | * | |
259 | * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree())); | |
260 | * \endcode | |
261 | * The call to @c find_if will locate the first index (i) of @c v for which | |
262 | * "!(v[i] > 3)" is true. | |
263 | * | |
264 | * The not1/unary_negate combination works on predicates taking a single | |
265 | * argument. The not2/binary_negate combination works on predicates which | |
266 | * take two arguments. | |
267 | * | |
268 | * @{ | |
269 | */ | |
729e3d3f | 270 | /// One of the @link s20_3_5_negators negation functors@endlink. |
725dc051 BK |
271 | template <class _Predicate> |
272 | class unary_negate | |
273 | : public unary_function<typename _Predicate::argument_type, bool> { | |
274 | protected: | |
275 | _Predicate _M_pred; | |
276 | public: | |
277 | explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {} | |
278 | bool operator()(const typename _Predicate::argument_type& __x) const { | |
279 | return !_M_pred(__x); | |
280 | } | |
281 | }; | |
282 | ||
729e3d3f | 283 | /// One of the @link s20_3_5_negators negation functors@endlink. |
725dc051 BK |
284 | template <class _Predicate> |
285 | inline unary_negate<_Predicate> | |
286 | not1(const _Predicate& __pred) | |
287 | { | |
288 | return unary_negate<_Predicate>(__pred); | |
289 | } | |
290 | ||
729e3d3f | 291 | /// One of the @link s20_3_5_negators negation functors@endlink. |
725dc051 BK |
292 | template <class _Predicate> |
293 | class binary_negate | |
294 | : public binary_function<typename _Predicate::first_argument_type, | |
295 | typename _Predicate::second_argument_type, | |
296 | bool> { | |
297 | protected: | |
298 | _Predicate _M_pred; | |
299 | public: | |
300 | explicit binary_negate(const _Predicate& __x) : _M_pred(__x) {} | |
301 | bool operator()(const typename _Predicate::first_argument_type& __x, | |
302 | const typename _Predicate::second_argument_type& __y) const | |
303 | { | |
304 | return !_M_pred(__x, __y); | |
305 | } | |
306 | }; | |
307 | ||
729e3d3f | 308 | /// One of the @link s20_3_5_negators negation functors@endlink. |
725dc051 BK |
309 | template <class _Predicate> |
310 | inline binary_negate<_Predicate> | |
311 | not2(const _Predicate& __pred) | |
312 | { | |
313 | return binary_negate<_Predicate>(__pred); | |
314 | } | |
77cd227e PE |
315 | /** @} */ |
316 | ||
317 | // 20.3.6 binders | |
318 | /** @defgroup s20_3_6_binder Binder Classes | |
319 | * Binders turn functions/functors with two arguments into functors with | |
320 | * a single argument, storing an argument to be applied later. For | |
321 | * example, an variable @c B of type @c binder1st is constructed from a functor | |
322 | * @c f and an argument @c x. Later, B's @c operator() is called with a | |
323 | * single argument @c y. The return value is the value of @c f(x,y). | |
324 | * @c B can be "called" with various arguments (y1, y2, ...) and will in | |
325 | * turn call @c f(x,y1), @c f(x,y2), ... | |
326 | * | |
327 | * The function @c bind1st is provided to save some typing. It takes the | |
328 | * function and an argument as parameters, and returns an instance of | |
329 | * @c binder1st. | |
330 | * | |
331 | * The type @c binder2nd and its creator function @c bind2nd do the same | |
332 | * thing, but the stored argument is passed as the second parameter instead | |
333 | * of the first, e.g., @c bind2nd(std::minus<float>,1.3) will create a | |
334 | * functor whose @c operator() accepts a floating-point number, subtracts | |
335 | * 1.3 from it, and returns the result. (If @c bind1st had been used, | |
336 | * the functor would perform "1.3 - x" instead. | |
337 | * | |
338 | * Creator-wrapper functions like @c bind1st are intended to be used in | |
339 | * calling algorithms. Their return values will be temporary objects. | |
340 | * (The goal is to not require you to type names like | |
341 | * @c std::binder1st<std::plus<int>> for declaring a variable to hold the | |
342 | * return value from @c bind1st(std::plus<int>,5). | |
343 | * | |
344 | * These become more useful when combined with the composition functions. | |
345 | * | |
346 | * @{ | |
347 | */ | |
729e3d3f | 348 | /// One of the @link s20_3_6_binder binder functors@endlink. |
725dc051 BK |
349 | template <class _Operation> |
350 | class binder1st | |
351 | : public unary_function<typename _Operation::second_argument_type, | |
352 | typename _Operation::result_type> { | |
353 | protected: | |
354 | _Operation op; | |
355 | typename _Operation::first_argument_type value; | |
356 | public: | |
357 | binder1st(const _Operation& __x, | |
358 | const typename _Operation::first_argument_type& __y) | |
359 | : op(__x), value(__y) {} | |
360 | typename _Operation::result_type | |
361 | operator()(const typename _Operation::second_argument_type& __x) const { | |
362 | return op(value, __x); | |
363 | } | |
3d7c150e | 364 | #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS |
77cd227e | 365 | //109. Missing binders for non-const sequence elements |
d52a9847 BK |
366 | typename _Operation::result_type |
367 | operator()(typename _Operation::second_argument_type& __x) const { | |
368 | return op(value, __x); | |
369 | } | |
370 | #endif | |
725dc051 BK |
371 | }; |
372 | ||
729e3d3f | 373 | /// One of the @link s20_3_6_binder binder functors@endlink. |
725dc051 BK |
374 | template <class _Operation, class _Tp> |
375 | inline binder1st<_Operation> | |
376 | bind1st(const _Operation& __fn, const _Tp& __x) | |
377 | { | |
378 | typedef typename _Operation::first_argument_type _Arg1_type; | |
379 | return binder1st<_Operation>(__fn, _Arg1_type(__x)); | |
380 | } | |
381 | ||
729e3d3f | 382 | /// One of the @link s20_3_6_binder binder functors@endlink. |
725dc051 BK |
383 | template <class _Operation> |
384 | class binder2nd | |
385 | : public unary_function<typename _Operation::first_argument_type, | |
386 | typename _Operation::result_type> { | |
387 | protected: | |
388 | _Operation op; | |
389 | typename _Operation::second_argument_type value; | |
390 | public: | |
391 | binder2nd(const _Operation& __x, | |
392 | const typename _Operation::second_argument_type& __y) | |
393 | : op(__x), value(__y) {} | |
394 | typename _Operation::result_type | |
395 | operator()(const typename _Operation::first_argument_type& __x) const { | |
396 | return op(__x, value); | |
397 | } | |
3d7c150e | 398 | #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS |
77cd227e | 399 | //109. Missing binders for non-const sequence elements |
d52a9847 BK |
400 | typename _Operation::result_type |
401 | operator()(typename _Operation::first_argument_type& __x) const { | |
402 | return op(__x, value); | |
403 | } | |
404 | #endif | |
725dc051 BK |
405 | }; |
406 | ||
729e3d3f | 407 | /// One of the @link s20_3_6_binder binder functors@endlink. |
725dc051 BK |
408 | template <class _Operation, class _Tp> |
409 | inline binder2nd<_Operation> | |
410 | bind2nd(const _Operation& __fn, const _Tp& __x) | |
411 | { | |
412 | typedef typename _Operation::second_argument_type _Arg2_type; | |
413 | return binder2nd<_Operation>(__fn, _Arg2_type(__x)); | |
414 | } | |
77cd227e PE |
415 | /** @} */ |
416 | ||
77cd227e PE |
417 | // 20.3.7 adaptors pointers functions |
418 | /** @defgroup s20_3_7_adaptors Adaptors for pointers to functions | |
419 | * The advantage of function objects over pointers to functions is that | |
420 | * the objects in the standard library declare nested typedefs describing | |
421 | * their argument and result types with uniform names (e.g., @c result_type | |
422 | * from the base classes @c unary_function and @c binary_function). | |
423 | * Sometimes those typedefs are required, not just optional. | |
424 | * | |
425 | * Adaptors are provided to turn pointers to unary (single-argument) and | |
426 | * binary (double-argument) functions into function objects. The long-winded | |
427 | * functor @c pointer_to_unary_function is constructed with a function | |
428 | * pointer @c f, and its @c operator() called with argument @c x returns | |
429 | * @c f(x). The functor @c pointer_to_binary_function does the same thing, | |
430 | * but with a double-argument @c f and @c operator(). | |
431 | * | |
432 | * The function @c ptr_fun takes a pointer-to-function @c f and constructs | |
433 | * an instance of the appropriate functor. | |
434 | * | |
435 | * @{ | |
436 | */ | |
729e3d3f | 437 | /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. |
725dc051 BK |
438 | template <class _Arg, class _Result> |
439 | class pointer_to_unary_function : public unary_function<_Arg, _Result> { | |
440 | protected: | |
441 | _Result (*_M_ptr)(_Arg); | |
442 | public: | |
443 | pointer_to_unary_function() {} | |
444 | explicit pointer_to_unary_function(_Result (*__x)(_Arg)) : _M_ptr(__x) {} | |
445 | _Result operator()(_Arg __x) const { return _M_ptr(__x); } | |
446 | }; | |
447 | ||
729e3d3f | 448 | /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. |
725dc051 BK |
449 | template <class _Arg, class _Result> |
450 | inline pointer_to_unary_function<_Arg, _Result> ptr_fun(_Result (*__x)(_Arg)) | |
451 | { | |
452 | return pointer_to_unary_function<_Arg, _Result>(__x); | |
453 | } | |
454 | ||
729e3d3f | 455 | /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. |
725dc051 BK |
456 | template <class _Arg1, class _Arg2, class _Result> |
457 | class pointer_to_binary_function : | |
458 | public binary_function<_Arg1,_Arg2,_Result> { | |
459 | protected: | |
460 | _Result (*_M_ptr)(_Arg1, _Arg2); | |
461 | public: | |
462 | pointer_to_binary_function() {} | |
463 | explicit pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2)) | |
464 | : _M_ptr(__x) {} | |
465 | _Result operator()(_Arg1 __x, _Arg2 __y) const { | |
466 | return _M_ptr(__x, __y); | |
467 | } | |
468 | }; | |
469 | ||
729e3d3f | 470 | /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. |
725dc051 BK |
471 | template <class _Arg1, class _Arg2, class _Result> |
472 | inline pointer_to_binary_function<_Arg1,_Arg2,_Result> | |
473 | ptr_fun(_Result (*__x)(_Arg1, _Arg2)) { | |
474 | return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__x); | |
475 | } | |
77cd227e | 476 | /** @} */ |
725dc051 | 477 | |
725dc051 BK |
478 | template <class _Tp> |
479 | struct _Identity : public unary_function<_Tp,_Tp> { | |
480 | _Tp& operator()(_Tp& __x) const { return __x; } | |
481 | const _Tp& operator()(const _Tp& __x) const { return __x; } | |
482 | }; | |
483 | ||
725dc051 BK |
484 | template <class _Pair> |
485 | struct _Select1st : public unary_function<_Pair, typename _Pair::first_type> { | |
486 | typename _Pair::first_type& operator()(_Pair& __x) const { | |
487 | return __x.first; | |
488 | } | |
489 | const typename _Pair::first_type& operator()(const _Pair& __x) const { | |
490 | return __x.first; | |
491 | } | |
492 | }; | |
493 | ||
494 | template <class _Pair> | |
495 | struct _Select2nd : public unary_function<_Pair, typename _Pair::second_type> | |
496 | { | |
497 | typename _Pair::second_type& operator()(_Pair& __x) const { | |
498 | return __x.second; | |
499 | } | |
500 | const typename _Pair::second_type& operator()(const _Pair& __x) const { | |
501 | return __x.second; | |
502 | } | |
503 | }; | |
504 | ||
77cd227e PE |
505 | // 20.3.8 adaptors pointers members |
506 | /** @defgroup s20_3_8_memadaptors Adaptors for pointers to members | |
507 | * There are a total of 16 = 2^4 function objects in this family. | |
508 | * (1) Member functions taking no arguments vs member functions taking | |
509 | * one argument. | |
510 | * (2) Call through pointer vs call through reference. | |
511 | * (3) Member function with void return type vs member function with | |
512 | * non-void return type. | |
513 | * (4) Const vs non-const member function. | |
514 | * | |
515 | * Note that choice (3) is nothing more than a workaround: according | |
516 | * to the draft, compilers should handle void and non-void the same way. | |
517 | * This feature is not yet widely implemented, though. You can only use | |
518 | * member functions returning void if your compiler supports partial | |
519 | * specialization. | |
520 | * | |
521 | * All of this complexity is in the function objects themselves. You can | |
522 | * ignore it by using the helper function mem_fun and mem_fun_ref, | |
523 | * which create whichever type of adaptor is appropriate. | |
77cd227e PE |
524 | * |
525 | * @{ | |
526 | */ | |
729e3d3f | 527 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
528 | template <class _Ret, class _Tp> |
529 | class mem_fun_t : public unary_function<_Tp*,_Ret> { | |
530 | public: | |
531 | explicit mem_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {} | |
532 | _Ret operator()(_Tp* __p) const { return (__p->*_M_f)(); } | |
533 | private: | |
534 | _Ret (_Tp::*_M_f)(); | |
535 | }; | |
536 | ||
729e3d3f | 537 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
538 | template <class _Ret, class _Tp> |
539 | class const_mem_fun_t : public unary_function<const _Tp*,_Ret> { | |
540 | public: | |
541 | explicit const_mem_fun_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {} | |
542 | _Ret operator()(const _Tp* __p) const { return (__p->*_M_f)(); } | |
543 | private: | |
544 | _Ret (_Tp::*_M_f)() const; | |
545 | }; | |
546 | ||
729e3d3f | 547 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
548 | template <class _Ret, class _Tp> |
549 | class mem_fun_ref_t : public unary_function<_Tp,_Ret> { | |
550 | public: | |
551 | explicit mem_fun_ref_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {} | |
552 | _Ret operator()(_Tp& __r) const { return (__r.*_M_f)(); } | |
553 | private: | |
554 | _Ret (_Tp::*_M_f)(); | |
555 | }; | |
556 | ||
729e3d3f | 557 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
558 | template <class _Ret, class _Tp> |
559 | class const_mem_fun_ref_t : public unary_function<_Tp,_Ret> { | |
560 | public: | |
561 | explicit const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {} | |
562 | _Ret operator()(const _Tp& __r) const { return (__r.*_M_f)(); } | |
563 | private: | |
564 | _Ret (_Tp::*_M_f)() const; | |
565 | }; | |
566 | ||
729e3d3f | 567 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
568 | template <class _Ret, class _Tp, class _Arg> |
569 | class mem_fun1_t : public binary_function<_Tp*,_Arg,_Ret> { | |
570 | public: | |
571 | explicit mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {} | |
572 | _Ret operator()(_Tp* __p, _Arg __x) const { return (__p->*_M_f)(__x); } | |
573 | private: | |
574 | _Ret (_Tp::*_M_f)(_Arg); | |
575 | }; | |
576 | ||
729e3d3f | 577 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
578 | template <class _Ret, class _Tp, class _Arg> |
579 | class const_mem_fun1_t : public binary_function<const _Tp*,_Arg,_Ret> { | |
580 | public: | |
581 | explicit const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {} | |
582 | _Ret operator()(const _Tp* __p, _Arg __x) const | |
583 | { return (__p->*_M_f)(__x); } | |
584 | private: | |
585 | _Ret (_Tp::*_M_f)(_Arg) const; | |
586 | }; | |
587 | ||
729e3d3f | 588 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
589 | template <class _Ret, class _Tp, class _Arg> |
590 | class mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> { | |
591 | public: | |
592 | explicit mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {} | |
593 | _Ret operator()(_Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); } | |
594 | private: | |
595 | _Ret (_Tp::*_M_f)(_Arg); | |
596 | }; | |
597 | ||
729e3d3f | 598 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
599 | template <class _Ret, class _Tp, class _Arg> |
600 | class const_mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> { | |
601 | public: | |
602 | explicit const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {} | |
603 | _Ret operator()(const _Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); } | |
604 | private: | |
605 | _Ret (_Tp::*_M_f)(_Arg) const; | |
606 | }; | |
607 | ||
729e3d3f | 608 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
609 | template <class _Tp> |
610 | class mem_fun_t<void, _Tp> : public unary_function<_Tp*,void> { | |
611 | public: | |
612 | explicit mem_fun_t(void (_Tp::*__pf)()) : _M_f(__pf) {} | |
613 | void operator()(_Tp* __p) const { (__p->*_M_f)(); } | |
614 | private: | |
615 | void (_Tp::*_M_f)(); | |
616 | }; | |
617 | ||
729e3d3f | 618 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
619 | template <class _Tp> |
620 | class const_mem_fun_t<void, _Tp> : public unary_function<const _Tp*,void> { | |
621 | public: | |
622 | explicit const_mem_fun_t(void (_Tp::*__pf)() const) : _M_f(__pf) {} | |
623 | void operator()(const _Tp* __p) const { (__p->*_M_f)(); } | |
624 | private: | |
625 | void (_Tp::*_M_f)() const; | |
626 | }; | |
627 | ||
729e3d3f | 628 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
629 | template <class _Tp> |
630 | class mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> { | |
631 | public: | |
632 | explicit mem_fun_ref_t(void (_Tp::*__pf)()) : _M_f(__pf) {} | |
633 | void operator()(_Tp& __r) const { (__r.*_M_f)(); } | |
634 | private: | |
635 | void (_Tp::*_M_f)(); | |
636 | }; | |
637 | ||
729e3d3f | 638 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
639 | template <class _Tp> |
640 | class const_mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> { | |
641 | public: | |
642 | explicit const_mem_fun_ref_t(void (_Tp::*__pf)() const) : _M_f(__pf) {} | |
643 | void operator()(const _Tp& __r) const { (__r.*_M_f)(); } | |
644 | private: | |
645 | void (_Tp::*_M_f)() const; | |
646 | }; | |
647 | ||
729e3d3f | 648 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
649 | template <class _Tp, class _Arg> |
650 | class mem_fun1_t<void, _Tp, _Arg> : public binary_function<_Tp*,_Arg,void> { | |
651 | public: | |
652 | explicit mem_fun1_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {} | |
653 | void operator()(_Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); } | |
654 | private: | |
655 | void (_Tp::*_M_f)(_Arg); | |
656 | }; | |
657 | ||
729e3d3f | 658 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
659 | template <class _Tp, class _Arg> |
660 | class const_mem_fun1_t<void, _Tp, _Arg> | |
661 | : public binary_function<const _Tp*,_Arg,void> { | |
662 | public: | |
663 | explicit const_mem_fun1_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {} | |
664 | void operator()(const _Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); } | |
665 | private: | |
666 | void (_Tp::*_M_f)(_Arg) const; | |
667 | }; | |
668 | ||
729e3d3f | 669 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
670 | template <class _Tp, class _Arg> |
671 | class mem_fun1_ref_t<void, _Tp, _Arg> | |
672 | : public binary_function<_Tp,_Arg,void> { | |
673 | public: | |
674 | explicit mem_fun1_ref_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {} | |
675 | void operator()(_Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); } | |
676 | private: | |
677 | void (_Tp::*_M_f)(_Arg); | |
678 | }; | |
679 | ||
729e3d3f | 680 | /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. |
725dc051 BK |
681 | template <class _Tp, class _Arg> |
682 | class const_mem_fun1_ref_t<void, _Tp, _Arg> | |
683 | : public binary_function<_Tp,_Arg,void> { | |
684 | public: | |
685 | explicit const_mem_fun1_ref_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {} | |
686 | void operator()(const _Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); } | |
687 | private: | |
688 | void (_Tp::*_M_f)(_Arg) const; | |
689 | }; | |
690 | ||
725dc051 BK |
691 | |
692 | // Mem_fun adaptor helper functions. There are only two: | |
01b0d4b8 | 693 | // mem_fun and mem_fun_ref. |
725dc051 BK |
694 | |
695 | template <class _Ret, class _Tp> | |
696 | inline mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)()) | |
697 | { return mem_fun_t<_Ret,_Tp>(__f); } | |
698 | ||
699 | template <class _Ret, class _Tp> | |
700 | inline const_mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)() const) | |
701 | { return const_mem_fun_t<_Ret,_Tp>(__f); } | |
702 | ||
703 | template <class _Ret, class _Tp> | |
704 | inline mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)()) | |
705 | { return mem_fun_ref_t<_Ret,_Tp>(__f); } | |
706 | ||
707 | template <class _Ret, class _Tp> | |
708 | inline const_mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)() const) | |
709 | { return const_mem_fun_ref_t<_Ret,_Tp>(__f); } | |
710 | ||
711 | template <class _Ret, class _Tp, class _Arg> | |
712 | inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg)) | |
713 | { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); } | |
714 | ||
715 | template <class _Ret, class _Tp, class _Arg> | |
716 | inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg) const) | |
717 | { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); } | |
718 | ||
719 | template <class _Ret, class _Tp, class _Arg> | |
720 | inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun_ref(_Ret (_Tp::*__f)(_Arg)) | |
721 | { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); } | |
722 | ||
723 | template <class _Ret, class _Tp, class _Arg> | |
724 | inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg> | |
725 | mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const) | |
726 | { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); } | |
727 | ||
77cd227e | 728 | /** @} */ |
725dc051 | 729 | |
d53d7f6e | 730 | } // namespace std |
725dc051 | 731 | |
3d7c150e | 732 | #endif /* _FUNCTION_H */ |
725dc051 BK |
733 | |
734 | // Local Variables: | |
735 | // mode:C++ | |
736 | // End: |