is_backend.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <boost/multiprecision/detail/number_base.hpp>
  8. namespace boost { namespace multiprecision { namespace detail {
  9. template <class T>
  10. struct has_signed_types
  11. {
  12. template <class U>
  13. static double check(U*, typename U::signed_types* = 0);
  14. static char check(...);
  15. static T* get();
  16. static constexpr bool value = sizeof(check(get())) == sizeof(double);
  17. };
  18. template <class T>
  19. struct has_unsigned_types
  20. {
  21. template <class U>
  22. static double check(U*, typename U::unsigned_types* = 0);
  23. static char check(...);
  24. static T* get();
  25. static constexpr bool value = sizeof(check(get())) == sizeof(double);
  26. };
  27. template <class T>
  28. struct has_float_types
  29. {
  30. template <class U>
  31. static double check(U*, typename U::float_types* = 0);
  32. static char check(...);
  33. static T* get();
  34. static constexpr bool value = sizeof(check(get())) == sizeof(double);
  35. };
  36. template <class T>
  37. struct is_backend
  38. {
  39. static constexpr const bool value = has_signed_types<T>::value && has_unsigned_types<T>::value && has_float_types<T>::value;
  40. };
  41. template <class Backend>
  42. struct other_backend
  43. {
  44. using type = typename std::conditional<
  45. std::is_same<number<Backend>, number<Backend, et_on> >::value,
  46. number<Backend, et_off>, number<Backend, et_on> >::type;
  47. };
  48. template <class B, class V>
  49. struct number_from_backend
  50. {
  51. using type = typename std::conditional<
  52. std::is_convertible<V, number<B> >::value,
  53. number<B>,
  54. typename other_backend<B>::type>::type;
  55. };
  56. template <bool b, class T, class U>
  57. struct is_first_backend_imp
  58. {
  59. static constexpr const bool value = false;
  60. };
  61. template <class T, class U>
  62. struct is_first_backend_imp<true, T, U>
  63. {
  64. static constexpr const bool value = std::is_convertible<U, number<T, et_on> >::value || std::is_convertible<U, number<T, et_off> >::value;
  65. };
  66. template <class T, class U>
  67. struct is_first_backend : is_first_backend_imp<is_backend<T>::value, T, U>
  68. {};
  69. template <bool b, class T, class U>
  70. struct is_second_backend_imp
  71. {
  72. static constexpr const bool value = false;
  73. };
  74. template <class T, class U>
  75. struct is_second_backend_imp<true, T, U>
  76. {
  77. static constexpr const 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;
  78. };
  79. template <class T, class U>
  80. struct is_second_backend : is_second_backend_imp<is_backend<U>::value, T, U>
  81. {};
  82. }
  83. }
  84. } // namespace boost::multiprecision::detail
  85. #endif // BOOST_MP_IS_BACKEND_HPP