1// ------------------------------------------------------------------------------
2// Copyright (c) 2000 Cadenza New Zealand Ltd
3// Distributed under the Boost Software License, Version 1.0. (See accompany-
4// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5// ------------------------------------------------------------------------------
6// Boost functional.hpp header file
7// See http://www.boost.org/libs/functional for documentation.
8// ------------------------------------------------------------------------------
9// $Id$
10// ------------------------------------------------------------------------------
11
12#ifndef BOOST_FUNCTIONAL_HPP
13#define BOOST_FUNCTIONAL_HPP
14
15#include <boost/config.hpp>
16#include <boost/call_traits.hpp>
17#include <functional>
18
19namespace boost
20{
21 namespace functional
22 {
23 namespace detail {
24 // std::unary_function and std::binary_function were both removed
25 // in C++17.
26
27 template <typename Arg1, typename Result>
28 struct unary_function
29 {
30 typedef Arg1 argument_type;
31 typedef Result result_type;
32 };
33
34 template <typename Arg1, typename Arg2, typename Result>
35 struct binary_function
36 {
37 typedef Arg1 first_argument_type;
38 typedef Arg2 second_argument_type;
39 typedef Result result_type;
40 };
41 }
42 }
43
44#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
45 // --------------------------------------------------------------------------
46 // The following traits classes allow us to avoid the need for ptr_fun
47 // because the types of arguments and the result of a function can be
48 // deduced.
49 //
50 // In addition to the standard types defined in unary_function and
51 // binary_function, we add
52 //
53 // - function_type, the type of the function or function object itself.
54 //
55 // - param_type, the type that should be used for passing the function or
56 // function object as an argument.
57 // --------------------------------------------------------------------------
58 namespace detail
59 {
60 template <class Operation>
61 struct unary_traits_imp;
62
63 template <class Operation>
64 struct unary_traits_imp<Operation*>
65 {
66 typedef Operation function_type;
67 typedef const function_type & param_type;
68 typedef typename Operation::result_type result_type;
69 typedef typename Operation::argument_type argument_type;
70 };
71
72 template <class R, class A>
73 struct unary_traits_imp<R(*)(A)>
74 {
75 typedef R (*function_type)(A);
76 typedef R (*param_type)(A);
77 typedef R result_type;
78 typedef A argument_type;
79 };
80
81 template <class Operation>
82 struct binary_traits_imp;
83
84 template <class Operation>
85 struct binary_traits_imp<Operation*>
86 {
87 typedef Operation function_type;
88 typedef const function_type & param_type;
89 typedef typename Operation::result_type result_type;
90 typedef typename Operation::first_argument_type first_argument_type;
91 typedef typename Operation::second_argument_type second_argument_type;
92 };
93
94 template <class R, class A1, class A2>
95 struct binary_traits_imp<R(*)(A1,A2)>
96 {
97 typedef R (*function_type)(A1,A2);
98 typedef R (*param_type)(A1,A2);
99 typedef R result_type;
100 typedef A1 first_argument_type;
101 typedef A2 second_argument_type;
102 };
103 } // namespace detail
104
105 template <class Operation>
106 struct unary_traits
107 {
108 typedef typename detail::unary_traits_imp<Operation*>::function_type function_type;
109 typedef typename detail::unary_traits_imp<Operation*>::param_type param_type;
110 typedef typename detail::unary_traits_imp<Operation*>::result_type result_type;
111 typedef typename detail::unary_traits_imp<Operation*>::argument_type argument_type;
112 };
113
114 template <class R, class A>
115 struct unary_traits<R(*)(A)>
116 {
117 typedef R (*function_type)(A);
118 typedef R (*param_type)(A);
119 typedef R result_type;
120 typedef A argument_type;
121 };
122
123 template <class Operation>
124 struct binary_traits
125 {
126 typedef typename detail::binary_traits_imp<Operation*>::function_type function_type;
127 typedef typename detail::binary_traits_imp<Operation*>::param_type param_type;
128 typedef typename detail::binary_traits_imp<Operation*>::result_type result_type;
129 typedef typename detail::binary_traits_imp<Operation*>::first_argument_type first_argument_type;
130 typedef typename detail::binary_traits_imp<Operation*>::second_argument_type second_argument_type;
131 };
132
133 template <class R, class A1, class A2>
134 struct binary_traits<R(*)(A1,A2)>
135 {
136 typedef R (*function_type)(A1,A2);
137 typedef R (*param_type)(A1,A2);
138 typedef R result_type;
139 typedef A1 first_argument_type;
140 typedef A2 second_argument_type;
141 };
142#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
143 // --------------------------------------------------------------------------
144 // If we have no partial specialisation available, decay to a situation
145 // that is no worse than in the Standard, i.e., ptr_fun will be required.
146 // --------------------------------------------------------------------------
147
148 template <class Operation>
149 struct unary_traits
150 {
151 typedef Operation function_type;
152 typedef const Operation& param_type;
153 typedef typename Operation::result_type result_type;
154 typedef typename Operation::argument_type argument_type;
155 };
156
157 template <class Operation>
158 struct binary_traits
159 {
160 typedef Operation function_type;
161 typedef const Operation & param_type;
162 typedef typename Operation::result_type result_type;
163 typedef typename Operation::first_argument_type first_argument_type;
164 typedef typename Operation::second_argument_type second_argument_type;
165 };
166#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
167
168 // --------------------------------------------------------------------------
169 // unary_negate, not1
170 // --------------------------------------------------------------------------
171 template <class Predicate>
172 class unary_negate
173 : public boost::functional::detail::unary_function<typename unary_traits<Predicate>::argument_type,bool>
174 {
175 public:
176 explicit unary_negate(typename unary_traits<Predicate>::param_type x)
177 :
178 pred(x)
179 {}
180 bool operator()(typename call_traits<typename unary_traits<Predicate>::argument_type>::param_type x) const
181 {
182 return !pred(x);
183 }
184 private:
185 typename unary_traits<Predicate>::function_type pred;
186 };
187
188 template <class Predicate>
189 unary_negate<Predicate> not1(const Predicate &pred)
190 {
191 // The cast is to placate Borland C++Builder in certain circumstances.
192 // I don't think it should be necessary.
193 return unary_negate<Predicate>((typename unary_traits<Predicate>::param_type)pred);
194 }
195
196 template <class Predicate>
197 unary_negate<Predicate> not1(Predicate &pred)
198 {
199 return unary_negate<Predicate>(pred);
200 }
201
202 // --------------------------------------------------------------------------
203 // binary_negate, not2
204 // --------------------------------------------------------------------------
205 template <class Predicate>
206 class binary_negate
207 : public boost::functional::detail::binary_function<
208 typename binary_traits<Predicate>::first_argument_type,
209 typename binary_traits<Predicate>::second_argument_type,
210 bool>
211 {
212 public:
213 explicit binary_negate(typename binary_traits<Predicate>::param_type x)
214 :
215 pred(x)
216 {}
217 bool operator()(typename call_traits<typename binary_traits<Predicate>::first_argument_type>::param_type x,
218 typename call_traits<typename binary_traits<Predicate>::second_argument_type>::param_type y) const
219 {
220 return !pred(x,y);
221 }
222 private:
223 typename binary_traits<Predicate>::function_type pred;
224 };
225
226 template <class Predicate>
227 binary_negate<Predicate> not2(const Predicate &pred)
228 {
229 // The cast is to placate Borland C++Builder in certain circumstances.
230 // I don't think it should be necessary.
231 return binary_negate<Predicate>((typename binary_traits<Predicate>::param_type)pred);
232 }
233
234 template <class Predicate>
235 binary_negate<Predicate> not2(Predicate &pred)
236 {
237 return binary_negate<Predicate>(pred);
238 }
239
240 // --------------------------------------------------------------------------
241 // binder1st, bind1st
242 // --------------------------------------------------------------------------
243 template <class Operation>
244 class binder1st
245 : public boost::functional::detail::unary_function<
246 typename binary_traits<Operation>::second_argument_type,
247 typename binary_traits<Operation>::result_type>
248 {
249 public:
250 binder1st(typename binary_traits<Operation>::param_type x,
251 typename call_traits<typename binary_traits<Operation>::first_argument_type>::param_type y)
252 :
253 op(x), value(y)
254 {}
255
256 typename binary_traits<Operation>::result_type
257 operator()(typename call_traits<typename binary_traits<Operation>::second_argument_type>::param_type x) const
258 {
259 return op(value, x);
260 }
261
262 protected:
263 typename binary_traits<Operation>::function_type op;
264 typename binary_traits<Operation>::first_argument_type value;
265 };
266
267 template <class Operation>
268 inline binder1st<Operation> bind1st(const Operation &op,
269 typename call_traits<
270 typename binary_traits<Operation>::first_argument_type
271 >::param_type x)
272 {
273 // The cast is to placate Borland C++Builder in certain circumstances.
274 // I don't think it should be necessary.
275 return binder1st<Operation>((typename binary_traits<Operation>::param_type)op, x);
276 }
277
278 template <class Operation>
279 inline binder1st<Operation> bind1st(Operation &op,
280 typename call_traits<
281 typename binary_traits<Operation>::first_argument_type
282 >::param_type x)
283 {
284 return binder1st<Operation>(op, x);
285 }
286
287 // --------------------------------------------------------------------------
288 // binder2nd, bind2nd
289 // --------------------------------------------------------------------------
290 template <class Operation>
291 class binder2nd
292 : public boost::functional::detail::unary_function<
293 typename binary_traits<Operation>::first_argument_type,
294 typename binary_traits<Operation>::result_type>
295 {
296 public:
297 binder2nd(typename binary_traits<Operation>::param_type x,
298 typename call_traits<typename binary_traits<Operation>::second_argument_type>::param_type y)
299 :
300 op(x), value(y)
301 {}
302
303 typename binary_traits<Operation>::result_type
304 operator()(typename call_traits<typename binary_traits<Operation>::first_argument_type>::param_type x) const
305 {
306 return op(x, value);
307 }
308
309 protected:
310 typename binary_traits<Operation>::function_type op;
311 typename binary_traits<Operation>::second_argument_type value;
312 };
313
314 template <class Operation>
315 inline binder2nd<Operation> bind2nd(const Operation &op,
316 typename call_traits<
317 typename binary_traits<Operation>::second_argument_type
318 >::param_type x)
319 {
320 // The cast is to placate Borland C++Builder in certain circumstances.
321 // I don't think it should be necessary.
322 return binder2nd<Operation>((typename binary_traits<Operation>::param_type)op, x);
323 }
324
325 template <class Operation>
326 inline binder2nd<Operation> bind2nd(Operation &op,
327 typename call_traits<
328 typename binary_traits<Operation>::second_argument_type
329 >::param_type x)
330 {
331 return binder2nd<Operation>(op, x);
332 }
333
334 // --------------------------------------------------------------------------
335 // mem_fun, etc
336 // --------------------------------------------------------------------------
337 template <class S, class T>
338 class mem_fun_t : public boost::functional::detail::unary_function<T*, S>
339 {
340 public:
341 explicit mem_fun_t(S (T::*p)())
342 :
343 ptr(p)
344 {}
345 S operator()(T* p) const
346 {
347 return (p->*ptr)();
348 }
349 private:
350 S (T::*ptr)();
351 };
352
353 template <class S, class T, class A>
354 class mem_fun1_t : public boost::functional::detail::binary_function<T*, A, S>
355 {
356 public:
357 explicit mem_fun1_t(S (T::*p)(A))
358 :
359 ptr(p)
360 {}
361 S operator()(T* p, typename call_traits<A>::param_type x) const
362 {
363 return (p->*ptr)(x);
364 }
365 private:
366 S (T::*ptr)(A);
367 };
368
369 template <class S, class T>
370 class const_mem_fun_t : public boost::functional::detail::unary_function<const T*, S>
371 {
372 public:
373 explicit const_mem_fun_t(S (T::*p)() const)
374 :
375 ptr(p)
376 {}
377 S operator()(const T* p) const
378 {
379 return (p->*ptr)();
380 }
381 private:
382 S (T::*ptr)() const;
383 };
384
385 template <class S, class T, class A>
386 class const_mem_fun1_t : public boost::functional::detail::binary_function<const T*, A, S>
387 {
388 public:
389 explicit const_mem_fun1_t(S (T::*p)(A) const)
390 :
391 ptr(p)
392 {}
393 S operator()(const T* p, typename call_traits<A>::param_type x) const
394 {
395 return (p->*ptr)(x);
396 }
397 private:
398 S (T::*ptr)(A) const;
399 };
400
401 template<class S, class T>
402 inline mem_fun_t<S,T> mem_fun(S (T::*f)())
403 {
404 return mem_fun_t<S,T>(f);
405 }
406
407 template<class S, class T, class A>
408 inline mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A))
409 {
410 return mem_fun1_t<S,T,A>(f);
411 }
412
413#ifndef BOOST_NO_POINTER_TO_MEMBER_CONST
414 template<class S, class T>
415 inline const_mem_fun_t<S,T> mem_fun(S (T::*f)() const)
416 {
417 return const_mem_fun_t<S,T>(f);
418 }
419
420 template<class S, class T, class A>
421 inline const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const)
422 {
423 return const_mem_fun1_t<S,T,A>(f);
424 }
425#endif // BOOST_NO_POINTER_TO_MEMBER_CONST
426
427 // --------------------------------------------------------------------------
428 // mem_fun_ref, etc
429 // --------------------------------------------------------------------------
430 template <class S, class T>
431 class mem_fun_ref_t : public boost::functional::detail::unary_function<T&, S>
432 {
433 public:
434 explicit mem_fun_ref_t(S (T::*p)())
435 :
436 ptr(p)
437 {}
438 S operator()(T& p) const
439 {
440 return (p.*ptr)();
441 }
442 private:
443 S (T::*ptr)();
444 };
445
446 template <class S, class T, class A>
447 class mem_fun1_ref_t : public boost::functional::detail::binary_function<T&, A, S>
448 {
449 public:
450 explicit mem_fun1_ref_t(S (T::*p)(A))
451 :
452 ptr(p)
453 {}
454 S operator()(T& p, typename call_traits<A>::param_type x) const
455 {
456 return (p.*ptr)(x);
457 }
458 private:
459 S (T::*ptr)(A);
460 };
461
462 template <class S, class T>
463 class const_mem_fun_ref_t : public boost::functional::detail::unary_function<const T&, S>
464 {
465 public:
466 explicit const_mem_fun_ref_t(S (T::*p)() const)
467 :
468 ptr(p)
469 {}
470
471 S operator()(const T &p) const
472 {
473 return (p.*ptr)();
474 }
475 private:
476 S (T::*ptr)() const;
477 };
478
479 template <class S, class T, class A>
480 class const_mem_fun1_ref_t : public boost::functional::detail::binary_function<const T&, A, S>
481 {
482 public:
483 explicit const_mem_fun1_ref_t(S (T::*p)(A) const)
484 :
485 ptr(p)
486 {}
487
488 S operator()(const T& p, typename call_traits<A>::param_type x) const
489 {
490 return (p.*ptr)(x);
491 }
492 private:
493 S (T::*ptr)(A) const;
494 };
495
496 template<class S, class T>
497 inline mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)())
498 {
499 return mem_fun_ref_t<S,T>(f);
500 }
501
502 template<class S, class T, class A>
503 inline mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A))
504 {
505 return mem_fun1_ref_t<S,T,A>(f);
506 }
507
508#ifndef BOOST_NO_POINTER_TO_MEMBER_CONST
509 template<class S, class T>
510 inline const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const)
511 {
512 return const_mem_fun_ref_t<S,T>(f);
513 }
514
515 template<class S, class T, class A>
516 inline const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const)
517 {
518 return const_mem_fun1_ref_t<S,T,A>(f);
519 }
520#endif // BOOST_NO_POINTER_TO_MEMBER_CONST
521
522 // --------------------------------------------------------------------------
523 // ptr_fun
524 // --------------------------------------------------------------------------
525 template <class Arg, class Result>
526 class pointer_to_unary_function : public boost::functional::detail::unary_function<Arg,Result>
527 {
528 public:
529 explicit pointer_to_unary_function(Result (*f)(Arg))
530 :
531 func(f)
532 {}
533
534 Result operator()(typename call_traits<Arg>::param_type x) const
535 {
536 return func(x);
537 }
538
539 private:
540 Result (*func)(Arg);
541 };
542
543 template <class Arg, class Result>
544 inline pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg))
545 {
546 return pointer_to_unary_function<Arg,Result>(f);
547 }
548
549 template <class Arg1, class Arg2, class Result>
550 class pointer_to_binary_function : public boost::functional::detail::binary_function<Arg1,Arg2,Result>
551 {
552 public:
553 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2))
554 :
555 func(f)
556 {}
557
558 Result operator()(typename call_traits<Arg1>::param_type x, typename call_traits<Arg2>::param_type y) const
559 {
560 return func(x,y);
561 }
562
563 private:
564 Result (*func)(Arg1, Arg2);
565 };
566
567 template <class Arg1, class Arg2, class Result>
568 inline pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1, Arg2))
569 {
570 return pointer_to_binary_function<Arg1,Arg2,Result>(f);
571 }
572} // namespace boost
573
574#endif
575

source code of include/boost/functional.hpp