Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:43:35

0001 /*!
0002 @file
0003 Defines `boost::hana::div`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_DIV_HPP
0011 #define BOOST_HANA_DIV_HPP
0012 
0013 #include <boost/hana/fwd/div.hpp>
0014 
0015 #include <boost/hana/concept/constant.hpp>
0016 #include <boost/hana/concept/euclidean_ring.hpp>
0017 #include <boost/hana/config.hpp>
0018 #include <boost/hana/core/to.hpp>
0019 #include <boost/hana/core/dispatch.hpp>
0020 #include <boost/hana/detail/canonical_constant.hpp>
0021 #include <boost/hana/detail/has_common_embedding.hpp>
0022 #include <boost/hana/value.hpp>
0023 
0024 #include <type_traits>
0025 
0026 
0027 namespace boost { namespace hana {
0028     //! @cond
0029     template <typename X, typename Y>
0030     constexpr decltype(auto) div_t::operator()(X&& x, Y&& y) const {
0031         using T = typename hana::tag_of<X>::type;
0032         using U = typename hana::tag_of<Y>::type;
0033         using Div = BOOST_HANA_DISPATCH_IF(decltype(div_impl<T, U>{}),
0034             hana::EuclideanRing<T>::value &&
0035             hana::EuclideanRing<U>::value &&
0036             !is_default<div_impl<T, U>>::value
0037         );
0038 
0039     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0040         static_assert(hana::EuclideanRing<T>::value,
0041         "hana::div(x, y) requires 'x' to be an EuclideanRing");
0042 
0043         static_assert(hana::EuclideanRing<U>::value,
0044         "hana::div(x, y) requires 'y' to be an EuclideanRing");
0045 
0046         static_assert(!is_default<div_impl<T, U>>::value,
0047         "hana::div(x, y) requires 'x' and 'y' to be embeddable "
0048         "in a common EuclideanRing");
0049     #endif
0050 
0051         return Div::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
0052     }
0053     //! @endcond
0054 
0055     template <typename T, typename U, bool condition>
0056     struct div_impl<T, U, when<condition>> : default_ {
0057         template <typename ...Args>
0058         static constexpr auto apply(Args&& ...) = delete;
0059     };
0060 
0061     // Cross-type overload
0062     template <typename T, typename U>
0063     struct div_impl<T, U, when<
0064         detail::has_nontrivial_common_embedding<EuclideanRing, T, U>::value
0065     >> {
0066         using C = typename common<T, U>::type;
0067         template <typename X, typename Y>
0068         static constexpr decltype(auto) apply(X&& x, Y&& y) {
0069             return hana::div(hana::to<C>(static_cast<X&&>(x)),
0070                              hana::to<C>(static_cast<Y&&>(y)));
0071         }
0072     };
0073 
0074     //////////////////////////////////////////////////////////////////////////
0075     // Model for integral data types
0076     //////////////////////////////////////////////////////////////////////////
0077     template <typename T>
0078     struct div_impl<T, T, when<std::is_integral<T>::value &&
0079                                !std::is_same<T, bool>::value>> {
0080         template <typename X, typename Y>
0081         static constexpr decltype(auto) apply(X&& x, Y&& y)
0082         { return static_cast<X&&>(x) / static_cast<Y&&>(y); }
0083     };
0084 
0085     //////////////////////////////////////////////////////////////////////////
0086     // Model for Constants over an EuclideanRing
0087     //////////////////////////////////////////////////////////////////////////
0088     namespace detail {
0089         template <typename C, typename X, typename Y>
0090         struct constant_from_div {
0091             static constexpr auto value = hana::div(hana::value<X>(), hana::value<Y>());
0092             using hana_tag = detail::CanonicalConstant<typename C::value_type>;
0093         };
0094     }
0095 
0096     template <typename C>
0097     struct div_impl<C, C, when<
0098         hana::Constant<C>::value &&
0099         EuclideanRing<typename C::value_type>::value
0100     >> {
0101         template <typename X, typename Y>
0102         static constexpr decltype(auto) apply(X const&, Y const&)
0103         { return hana::to<C>(detail::constant_from_div<C, X, Y>{}); }
0104     };
0105 }} // end namespace boost::hana
0106 
0107 #endif // !BOOST_HANA_DIV_HPP