function_type.hpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/functional/overloaded_function
  6. #ifndef BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_DETAIL_FUNCTION_TYPE_HPP_
  7. #define BOOST_FUNCTIONAL_OVERLOADED_FUNCTION_DETAIL_FUNCTION_TYPE_HPP_
  8. #include <boost/function_types/is_function.hpp>
  9. #include <boost/function_types/is_function_pointer.hpp>
  10. #include <boost/function_types/is_function_reference.hpp>
  11. #include <boost/function_types/function_type.hpp>
  12. #include <boost/function_types/parameter_types.hpp>
  13. #include <boost/function_types/result_type.hpp>
  14. #include <boost/type_traits/remove_pointer.hpp>
  15. #include <boost/type_traits/remove_reference.hpp>
  16. #include <boost/function.hpp>
  17. #include <boost/mpl/if.hpp>
  18. #include <boost/mpl/identity.hpp>
  19. #include <boost/mpl/pop_front.hpp>
  20. #include <boost/mpl/push_front.hpp>
  21. #include <boost/typeof/typeof.hpp>
  22. // Do not use namespace ::detail because overloaded_function is already a class.
  23. namespace boost { namespace overloaded_function_detail {
  24. // Requires: F is a monomorphic functor (i.e., has non-template `operator()`).
  25. // Returns: F's function type `result_type (arg1_type, arg2_type, ...)`.
  26. // It does not assume F typedef result_type, arg1_type, ... but needs typeof.
  27. template<typename F>
  28. class functor_type {
  29. // NOTE: clang does not accept extra parenthesis `&(...)`.
  30. typedef BOOST_TYPEOF_TPL(&F::operator()) call_ptr;
  31. public:
  32. typedef
  33. typename boost::function_types::function_type<
  34. typename boost::mpl::push_front<
  35. typename boost::mpl::pop_front< // Remove functor type (1st).
  36. typename boost::function_types::parameter_types<
  37. call_ptr>::type
  38. >::type
  39. , typename boost::function_types::result_type<call_ptr>::type
  40. >::type
  41. >::type
  42. type;
  43. };
  44. // NOTE: When using boost::function in Boost.Typeof emulation mode, the user
  45. // has to register boost::functionN instead of boost::function in oder to
  46. // do TYPEOF(F::operator()). That is confusing, so boost::function is handled
  47. // separately so it does not require any Boost.Typeof registration at all.
  48. template<typename F>
  49. struct functor_type< boost::function<F> > {
  50. typedef F type;
  51. };
  52. // Requires: F is a function type, pointer, reference, or monomorphic functor.
  53. // Returns: F's function type `result_type (arg1_type, arg2_type, ...)`.
  54. template<typename F>
  55. struct function_type {
  56. typedef
  57. typename boost::mpl::if_<boost::function_types::is_function<F>,
  58. boost::mpl::identity<F>
  59. ,
  60. typename boost::mpl::if_<boost::function_types::
  61. is_function_pointer<F>,
  62. boost::remove_pointer<F>
  63. ,
  64. typename boost::mpl::if_<boost::function_types::
  65. is_function_reference<F>,
  66. boost::remove_reference<F>
  67. , // Else, requires that F is a functor.
  68. functor_type<F>
  69. >::type
  70. >::type
  71. >::type
  72. ::type type;
  73. };
  74. } } // namespace
  75. #endif // #include guard