is_backend.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2015 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MP_IS_BACKEND_HPP
  6. #define BOOST_MP_IS_BACKEND_HPP
  7. #include <type_traits>
  8. #include <boost/multiprecision/detail/number_base.hpp>
  9. namespace boost { namespace multiprecision { namespace detail {
  10. template <class T>
  11. struct has_signed_types
  12. {
  13. template <class U>
  14. static double check(U*, typename U::signed_types* = nullptr);
  15. static char check(...);
  16. static T* get();
  17. static constexpr bool value = sizeof(check(get())) == sizeof(double);
  18. };
  19. template <class T>
  20. struct has_unsigned_types
  21. {
  22. template <class U>
  23. static double check(U*, typename U::unsigned_types* = nullptr);
  24. static char check(...);
  25. static T* get();
  26. static constexpr bool value = sizeof(check(get())) == sizeof(double);
  27. };
  28. template <class T>
  29. struct has_float_types
  30. {
  31. template <class U>
  32. static double check(U*, typename U::float_types* = nullptr);
  33. static char check(...);
  34. static T* get();
  35. static constexpr bool value = sizeof(check(get())) == sizeof(double);
  36. };
  37. template <class T>
  38. struct is_backend : public std::integral_constant<bool, has_signed_types<T>::value && has_unsigned_types<T>::value && has_float_types<T>::value> {};
  39. template <class Backend>
  40. struct other_backend
  41. {
  42. using type = typename std::conditional<
  43. std::is_same<number<Backend>, number<Backend, et_on> >::value,
  44. number<Backend, et_off>, number<Backend, et_on> >::type;
  45. };
  46. template <class B, class V>
  47. struct number_from_backend
  48. {
  49. using type = typename std::conditional<
  50. std::is_convertible<V, number<B> >::value,
  51. number<B>,
  52. typename other_backend<B>::type>::type;
  53. };
  54. template <bool b, class T, class U>
  55. struct is_first_backend_imp : public std::false_type {};
  56. template <class T, class U>
  57. struct is_first_backend_imp<true, T, U> : public std::integral_constant < bool, std::is_convertible<U, number<T, et_on> >::value || std::is_convertible<U, number<T, et_off> >::value> {};
  58. template <class T, class U>
  59. struct is_first_backend : is_first_backend_imp<is_backend<T>::value, T, U>
  60. {};
  61. template <bool b, class T, class U>
  62. struct is_second_backend_imp
  63. {
  64. static constexpr bool value = false;
  65. };
  66. template <class T, class U>
  67. struct is_second_backend_imp<true, T, U>
  68. {
  69. static constexpr bool value = (std::is_convertible<T, number<U, et_on> >::value || std::is_convertible<T, number<U, et_off> >::value) && !is_first_backend<T, U>::value;
  70. };
  71. template <class T, class U>
  72. struct is_second_backend : is_second_backend_imp<is_backend<U>::value, T, U>
  73. {};
  74. }
  75. }
  76. } // namespace boost::multiprecision::detail
  77. #endif // BOOST_MP_IS_BACKEND_HPP