function1.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright David Abrahams 2006. Distributed under the Boost
  2. // Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_DETAIL_FUNCTION1_DWA200655_HPP
  5. # define BOOST_DETAIL_FUNCTION1_DWA200655_HPP
  6. # include <boost/concept_check.hpp>
  7. # include <boost/type_traits/remove_reference.hpp>
  8. # include <boost/type_traits/add_const.hpp>
  9. # include <boost/mpl/apply.hpp>
  10. namespace boost { namespace detail {
  11. // A utility for creating unary function objects that play nicely with
  12. // boost::result_of and that handle the forwarding problem.
  13. //
  14. // mpl::apply<F, A0>::type is expected to be a stateless function
  15. // object that accepts an argument of type A0&. It is also expected
  16. // to have a nested ::result_type identical to its return type.
  17. template<typename F>
  18. struct function1
  19. {
  20. template<typename Signature>
  21. struct result
  22. {};
  23. template<typename This, typename A0>
  24. struct result<This(A0)>
  25. {
  26. // How adding const to arguments handles rvalues.
  27. //
  28. // if A0 is arg0 is represents actual argument
  29. // -------- ------- --------------------------
  30. // T const & T const const T lvalue
  31. // T & T non-const T lvalue
  32. // T const T const const T rvalue
  33. // T T const non-const T rvalue
  34. typedef typename remove_reference<
  35. typename add_const< A0 >::type
  36. >::type arg0;
  37. typedef typename mpl::apply1<F, arg0>::type impl;
  38. typedef typename impl::result_type type;
  39. };
  40. // Handles mutable lvalues
  41. template<typename A0>
  42. typename result<function1(A0 &)>::type
  43. operator ()(A0 &a0) const
  44. {
  45. typedef typename result<function1(A0 &)>::impl impl;
  46. typedef typename result<function1(A0 &)>::type type;
  47. typedef A0 &arg0;
  48. BOOST_CONCEPT_ASSERT((UnaryFunction<impl, type, arg0>));
  49. //boost::function_requires<UnaryFunctionConcept<impl, type, arg0> >();
  50. return impl()(a0);
  51. }
  52. // Handles const lvalues and all rvalues
  53. template<typename A0>
  54. typename result<function1(A0 const &)>::type
  55. operator ()(A0 const &a0) const
  56. {
  57. typedef typename result<function1(A0 const &)>::impl impl;
  58. typedef typename result<function1(A0 const &)>::type type;
  59. typedef A0 const &arg0;
  60. BOOST_CONCEPT_ASSERT((UnaryFunction<impl, type, arg0>));
  61. //boost::function_requires<UnaryFunctionConcept<impl, type, arg0> >();
  62. return impl()(a0);
  63. }
  64. };
  65. }} // namespace boost::detail
  66. #endif // BOOST_DETAIL_FUNCTION1_DWA200655_HPP