is_fun.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (c) 2009-2020 Vladimir Batov.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. #ifndef BOOST_CONVERT_IS_FUNCTION_HPP
  5. #define BOOST_CONVERT_IS_FUNCTION_HPP
  6. #include <boost/convert/detail/config.hpp>
  7. #include <boost/convert/detail/has_member.hpp>
  8. #include <boost/function_types/is_function_pointer.hpp>
  9. #include <boost/function_types/function_arity.hpp>
  10. #include <boost/function_types/result_type.hpp>
  11. namespace boost { namespace cnv
  12. {
  13. using yes_type = ::boost::type_traits::yes_type;
  14. using no_type = ::boost::type_traits:: no_type;
  15. template <bool has_operator, typename Functor, typename TypeOut>
  16. struct check_functor { BOOST_STATIC_CONSTANT(bool, value = false); };
  17. template<typename Func, typename TypeOut, typename Enable = void>
  18. struct is_fun { BOOST_STATIC_CONSTANT(bool, value = false); };
  19. template <typename Functor, typename TypeOut>
  20. struct check_functor<true, Functor, TypeOut>
  21. {
  22. static yes_type test (TypeOut const&);
  23. static no_type test (...);
  24. static bool BOOST_CONSTEXPR_OR_CONST value = sizeof(yes_type) == sizeof(test(((Functor*) 0)->operator()()));
  25. };
  26. template<typename Functor, typename TypeOut>
  27. struct is_fun<Functor, TypeOut,
  28. typename enable_if_c<std::is_class<Functor>::value && !is_convertible<Functor, TypeOut>::value, void>::type>
  29. {
  30. BOOST_DECLARE_HAS_MEMBER(has_funop, operator());
  31. BOOST_STATIC_CONSTANT(bool, value = (check_functor<has_funop<Functor>::value, Functor, TypeOut>::value));
  32. };
  33. template<typename Function, typename TypeOut>
  34. struct is_fun<Function, TypeOut,
  35. typename enable_if_c<
  36. function_types::is_function_pointer<Function>::value &&
  37. function_types::function_arity<Function>::value == 0 &&
  38. !is_same<Function, TypeOut>::value,
  39. void>::type>
  40. {
  41. using out_type = TypeOut;
  42. using func_type = typename function_types::result_type<Function>::type;
  43. BOOST_STATIC_CONSTANT(bool, value = (is_convertible<func_type, out_type>::value));
  44. };
  45. }}
  46. #endif // BOOST_CONVERT_IS_FUNCTION_HPP