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: functional.hpp 36246 2006-12-02 14:17:26Z andreas_huber69 $
10 // ------------------------------------------------------------------------------
12 #ifndef BOOST_FUNCTIONAL_HPP
13 #define BOOST_FUNCTIONAL_HPP
15 #include <boost/config.hpp>
16 #include <boost/call_traits.hpp>
21 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
22 // --------------------------------------------------------------------------
23 // The following traits classes allow us to avoid the need for ptr_fun
24 // because the types of arguments and the result of a function can be
27 // In addition to the standard types defined in unary_function and
28 // binary_function, we add
30 // - function_type, the type of the function or function object itself.
32 // - param_type, the type that should be used for passing the function or
33 // function object as an argument.
34 // --------------------------------------------------------------------------
37 template <class Operation
>
38 struct unary_traits_imp
;
40 template <class Operation
>
41 struct unary_traits_imp
<Operation
*>
43 typedef Operation function_type
;
44 typedef const function_type
& param_type
;
45 typedef typename
Operation::result_type result_type
;
46 typedef typename
Operation::argument_type argument_type
;
49 template <class R
, class A
>
50 struct unary_traits_imp
<R(*)(A
)>
52 typedef R (*function_type
)(A
);
53 typedef R (*param_type
)(A
);
54 typedef R result_type
;
55 typedef A argument_type
;
58 template <class Operation
>
59 struct binary_traits_imp
;
61 template <class Operation
>
62 struct binary_traits_imp
<Operation
*>
64 typedef Operation function_type
;
65 typedef const function_type
& param_type
;
66 typedef typename
Operation::result_type result_type
;
67 typedef typename
Operation::first_argument_type first_argument_type
;
68 typedef typename
Operation::second_argument_type second_argument_type
;
71 template <class R
, class A1
, class A2
>
72 struct binary_traits_imp
<R(*)(A1
,A2
)>
74 typedef R (*function_type
)(A1
,A2
);
75 typedef R (*param_type
)(A1
,A2
);
76 typedef R result_type
;
77 typedef A1 first_argument_type
;
78 typedef A2 second_argument_type
;
82 template <class Operation
>
85 typedef typename
detail::unary_traits_imp
<Operation
*>::function_type function_type
;
86 typedef typename
detail::unary_traits_imp
<Operation
*>::param_type param_type
;
87 typedef typename
detail::unary_traits_imp
<Operation
*>::result_type result_type
;
88 typedef typename
detail::unary_traits_imp
<Operation
*>::argument_type argument_type
;
91 template <class R
, class A
>
92 struct unary_traits
<R(*)(A
)>
94 typedef R (*function_type
)(A
);
95 typedef R (*param_type
)(A
);
96 typedef R result_type
;
97 typedef A argument_type
;
100 template <class Operation
>
103 typedef typename
detail::binary_traits_imp
<Operation
*>::function_type function_type
;
104 typedef typename
detail::binary_traits_imp
<Operation
*>::param_type param_type
;
105 typedef typename
detail::binary_traits_imp
<Operation
*>::result_type result_type
;
106 typedef typename
detail::binary_traits_imp
<Operation
*>::first_argument_type first_argument_type
;
107 typedef typename
detail::binary_traits_imp
<Operation
*>::second_argument_type second_argument_type
;
110 template <class R
, class A1
, class A2
>
111 struct binary_traits
<R(*)(A1
,A2
)>
113 typedef R (*function_type
)(A1
,A2
);
114 typedef R (*param_type
)(A1
,A2
);
115 typedef R result_type
;
116 typedef A1 first_argument_type
;
117 typedef A2 second_argument_type
;
119 #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
120 // --------------------------------------------------------------------------
121 // If we have no partial specialisation available, decay to a situation
122 // that is no worse than in the Standard, i.e., ptr_fun will be required.
123 // --------------------------------------------------------------------------
125 template <class Operation
>
128 typedef Operation function_type
;
129 typedef const Operation
& param_type
;
130 typedef typename
Operation::result_type result_type
;
131 typedef typename
Operation::argument_type argument_type
;
134 template <class Operation
>
137 typedef Operation function_type
;
138 typedef const Operation
& param_type
;
139 typedef typename
Operation::result_type result_type
;
140 typedef typename
Operation::first_argument_type first_argument_type
;
141 typedef typename
Operation::second_argument_type second_argument_type
;
143 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
145 // --------------------------------------------------------------------------
146 // unary_negate, not1
147 // --------------------------------------------------------------------------
148 template <class Predicate
>
150 : public std::unary_function
<typename unary_traits
<Predicate
>::argument_type
,bool>
153 explicit unary_negate(typename unary_traits
<Predicate
>::param_type x
)
157 bool operator()(typename call_traits
<typename unary_traits
<Predicate
>::argument_type
>::param_type x
) const
162 typename unary_traits
<Predicate
>::function_type pred
;
165 template <class Predicate
>
166 unary_negate
<Predicate
> not1(const Predicate
&pred
)
168 // The cast is to placate Borland C++Builder in certain circumstances.
169 // I don't think it should be necessary.
170 return unary_negate
<Predicate
>((typename unary_traits
<Predicate
>::param_type
)pred
);
173 template <class Predicate
>
174 unary_negate
<Predicate
> not1(Predicate
&pred
)
176 return unary_negate
<Predicate
>(pred
);
179 // --------------------------------------------------------------------------
180 // binary_negate, not2
181 // --------------------------------------------------------------------------
182 template <class Predicate
>
184 : public std::binary_function
<typename binary_traits
<Predicate
>::first_argument_type
,
185 typename binary_traits
<Predicate
>::second_argument_type
,
189 explicit binary_negate(typename binary_traits
<Predicate
>::param_type x
)
193 bool operator()(typename call_traits
<typename binary_traits
<Predicate
>::first_argument_type
>::param_type x
,
194 typename call_traits
<typename binary_traits
<Predicate
>::second_argument_type
>::param_type y
) const
199 typename binary_traits
<Predicate
>::function_type pred
;
202 template <class Predicate
>
203 binary_negate
<Predicate
> not2(const Predicate
&pred
)
205 // The cast is to placate Borland C++Builder in certain circumstances.
206 // I don't think it should be necessary.
207 return binary_negate
<Predicate
>((typename binary_traits
<Predicate
>::param_type
)pred
);
210 template <class Predicate
>
211 binary_negate
<Predicate
> not2(Predicate
&pred
)
213 return binary_negate
<Predicate
>(pred
);
216 // --------------------------------------------------------------------------
217 // binder1st, bind1st
218 // --------------------------------------------------------------------------
219 template <class Operation
>
221 : public std::unary_function
<typename binary_traits
<Operation
>::second_argument_type
,
222 typename binary_traits
<Operation
>::result_type
>
225 binder1st(typename binary_traits
<Operation
>::param_type x
,
226 typename call_traits
<typename binary_traits
<Operation
>::first_argument_type
>::param_type y
)
231 typename binary_traits
<Operation
>::result_type
232 operator()(typename call_traits
<typename binary_traits
<Operation
>::second_argument_type
>::param_type x
) const
238 typename binary_traits
<Operation
>::function_type op
;
239 typename binary_traits
<Operation
>::first_argument_type value
;
242 template <class Operation
>
243 inline binder1st
<Operation
> bind1st(const Operation
&op
,
244 typename call_traits
<
245 typename binary_traits
<Operation
>::first_argument_type
248 // The cast is to placate Borland C++Builder in certain circumstances.
249 // I don't think it should be necessary.
250 return binder1st
<Operation
>((typename binary_traits
<Operation
>::param_type
)op
, x
);
253 template <class Operation
>
254 inline binder1st
<Operation
> bind1st(Operation
&op
,
255 typename call_traits
<
256 typename binary_traits
<Operation
>::first_argument_type
259 return binder1st
<Operation
>(op
, x
);
262 // --------------------------------------------------------------------------
263 // binder2nd, bind2nd
264 // --------------------------------------------------------------------------
265 template <class Operation
>
267 : public std::unary_function
<typename binary_traits
<Operation
>::first_argument_type
,
268 typename binary_traits
<Operation
>::result_type
>
271 binder2nd(typename binary_traits
<Operation
>::param_type x
,
272 typename call_traits
<typename binary_traits
<Operation
>::second_argument_type
>::param_type y
)
277 typename binary_traits
<Operation
>::result_type
278 operator()(typename call_traits
<typename binary_traits
<Operation
>::first_argument_type
>::param_type x
) const
284 typename binary_traits
<Operation
>::function_type op
;
285 typename binary_traits
<Operation
>::second_argument_type value
;
288 template <class Operation
>
289 inline binder2nd
<Operation
> bind2nd(const Operation
&op
,
290 typename call_traits
<
291 typename binary_traits
<Operation
>::second_argument_type
294 // The cast is to placate Borland C++Builder in certain circumstances.
295 // I don't think it should be necessary.
296 return binder2nd
<Operation
>((typename binary_traits
<Operation
>::param_type
)op
, x
);
299 template <class Operation
>
300 inline binder2nd
<Operation
> bind2nd(Operation
&op
,
301 typename call_traits
<
302 typename binary_traits
<Operation
>::second_argument_type
305 return binder2nd
<Operation
>(op
, x
);
308 // --------------------------------------------------------------------------
310 // --------------------------------------------------------------------------
311 template <class S
, class T
>
312 class mem_fun_t
: public std::unary_function
<T
*, S
>
315 explicit mem_fun_t(S (T::*p
)())
319 S
operator()(T
* p
) const
327 template <class S
, class T
, class A
>
328 class mem_fun1_t
: public std::binary_function
<T
*, A
, S
>
331 explicit mem_fun1_t(S (T::*p
)(A
))
335 S
operator()(T
* p
, typename call_traits
<A
>::param_type x
) const
343 template <class S
, class T
>
344 class const_mem_fun_t
: public std::unary_function
<const T
*, S
>
347 explicit const_mem_fun_t(S (T::*p
)() const)
351 S
operator()(const T
* p
) const
359 template <class S
, class T
, class A
>
360 class const_mem_fun1_t
: public std::binary_function
<const T
*, A
, S
>
363 explicit const_mem_fun1_t(S (T::*p
)(A
) const)
367 S
operator()(const T
* p
, typename call_traits
<A
>::param_type x
) const
372 S (T::*ptr
)(A
) const;
375 template<class S
, class T
>
376 inline mem_fun_t
<S
,T
> mem_fun(S (T::*f
)())
378 return mem_fun_t
<S
,T
>(f
);
381 template<class S
, class T
, class A
>
382 inline mem_fun1_t
<S
,T
,A
> mem_fun(S (T::*f
)(A
))
384 return mem_fun1_t
<S
,T
,A
>(f
);
387 #ifndef BOOST_NO_POINTER_TO_MEMBER_CONST
388 template<class S
, class T
>
389 inline const_mem_fun_t
<S
,T
> mem_fun(S (T::*f
)() const)
391 return const_mem_fun_t
<S
,T
>(f
);
394 template<class S
, class T
, class A
>
395 inline const_mem_fun1_t
<S
,T
,A
> mem_fun(S (T::*f
)(A
) const)
397 return const_mem_fun1_t
<S
,T
,A
>(f
);
399 #endif // BOOST_NO_POINTER_TO_MEMBER_CONST
401 // --------------------------------------------------------------------------
403 // --------------------------------------------------------------------------
404 template <class S
, class T
>
405 class mem_fun_ref_t
: public std::unary_function
<T
&, S
>
408 explicit mem_fun_ref_t(S (T::*p
)())
412 S
operator()(T
& p
) const
420 template <class S
, class T
, class A
>
421 class mem_fun1_ref_t
: public std::binary_function
<T
&, A
, S
>
424 explicit mem_fun1_ref_t(S (T::*p
)(A
))
428 S
operator()(T
& p
, typename call_traits
<A
>::param_type x
) const
436 template <class S
, class T
>
437 class const_mem_fun_ref_t
: public std::unary_function
<const T
&, S
>
440 explicit const_mem_fun_ref_t(S (T::*p
)() const)
445 S
operator()(const T
&p
) const
453 template <class S
, class T
, class A
>
454 class const_mem_fun1_ref_t
: public std::binary_function
<const T
&, A
, S
>
457 explicit const_mem_fun1_ref_t(S (T::*p
)(A
) const)
462 S
operator()(const T
& p
, typename call_traits
<A
>::param_type x
) const
467 S (T::*ptr
)(A
) const;
470 template<class S
, class T
>
471 inline mem_fun_ref_t
<S
,T
> mem_fun_ref(S (T::*f
)())
473 return mem_fun_ref_t
<S
,T
>(f
);
476 template<class S
, class T
, class A
>
477 inline mem_fun1_ref_t
<S
,T
,A
> mem_fun_ref(S (T::*f
)(A
))
479 return mem_fun1_ref_t
<S
,T
,A
>(f
);
482 #ifndef BOOST_NO_POINTER_TO_MEMBER_CONST
483 template<class S
, class T
>
484 inline const_mem_fun_ref_t
<S
,T
> mem_fun_ref(S (T::*f
)() const)
486 return const_mem_fun_ref_t
<S
,T
>(f
);
489 template<class S
, class T
, class A
>
490 inline const_mem_fun1_ref_t
<S
,T
,A
> mem_fun_ref(S (T::*f
)(A
) const)
492 return const_mem_fun1_ref_t
<S
,T
,A
>(f
);
494 #endif // BOOST_NO_POINTER_TO_MEMBER_CONST
496 // --------------------------------------------------------------------------
498 // --------------------------------------------------------------------------
499 template <class Arg
, class Result
>
500 class pointer_to_unary_function
: public std::unary_function
<Arg
,Result
>
503 explicit pointer_to_unary_function(Result (*f
)(Arg
))
508 Result
operator()(typename call_traits
<Arg
>::param_type x
) const
517 template <class Arg
, class Result
>
518 inline pointer_to_unary_function
<Arg
,Result
> ptr_fun(Result (*f
)(Arg
))
520 return pointer_to_unary_function
<Arg
,Result
>(f
);
523 template <class Arg1
, class Arg2
, class Result
>
524 class pointer_to_binary_function
: public std::binary_function
<Arg1
,Arg2
,Result
>
527 explicit pointer_to_binary_function(Result (*f
)(Arg1
, Arg2
))
532 Result
operator()(typename call_traits
<Arg1
>::param_type x
, typename call_traits
<Arg2
>::param_type y
) const
538 Result (*func
)(Arg1
, Arg2
);
541 template <class Arg1
, class Arg2
, class Result
>
542 inline pointer_to_binary_function
<Arg1
,Arg2
,Result
> ptr_fun(Result (*f
)(Arg1
, Arg2
))
544 return pointer_to_binary_function
<Arg1
,Arg2
,Result
>(f
);