is_fun.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) 2009-2016 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/utility/enable_if.hpp>
  9. #include <boost/type_traits.hpp>
  10. #include <boost/function_types/is_function_pointer.hpp>
  11. #include <boost/function_types/function_arity.hpp>
  12. #include <boost/function_types/result_type.hpp>
  13. namespace boost { namespace cnv
  14. {
  15. typedef ::boost::type_traits::yes_type yes_type;
  16. typedef ::boost::type_traits:: no_type no_type;
  17. template <bool has_operator, typename Functor, typename TypeOut>
  18. struct check_functor { BOOST_STATIC_CONSTANT(bool, value = false); };
  19. template<typename Func, typename TypeOut, class Enable =void>
  20. struct is_fun { BOOST_STATIC_CONSTANT(bool, value = false); };
  21. template <typename Functor, typename TypeOut>
  22. struct check_functor<true, Functor, TypeOut>
  23. {
  24. static yes_type test (TypeOut const&);
  25. static no_type test (...);
  26. static const bool value = sizeof(yes_type) == sizeof(test(((Functor*) 0)->operator()()));
  27. };
  28. template<typename Functor, typename TypeOut>
  29. struct is_fun<Functor, TypeOut,
  30. typename enable_if_c<is_class<Functor>::value && !is_convertible<Functor, TypeOut>::value, void>::type>
  31. {
  32. BOOST_DECLARE_HAS_MEMBER(has_funop, operator());
  33. BOOST_STATIC_CONSTANT(bool, value = (check_functor<has_funop<Functor>::value, Functor, TypeOut>::value));
  34. };
  35. template<typename Function, typename TypeOut>
  36. struct is_fun<Function, TypeOut,
  37. typename enable_if_c<
  38. function_types::is_function_pointer<Function>::value &&
  39. function_types::function_arity<Function>::value == 0 &&
  40. !is_same<Function, TypeOut>::value,
  41. void>::type>
  42. {
  43. typedef TypeOut out_type;
  44. typedef typename function_types::result_type<Function>::type func_out_type;
  45. BOOST_STATIC_CONSTANT(bool, value = (is_convertible<func_out_type, out_type>::value));
  46. };
  47. }}
  48. #endif // BOOST_CONVERT_IS_FUNCTION_HPP