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