/*! @file Defines `boost::hana::while_`. @copyright Louis Dionne 2013-2017 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_WHILE_HPP #define BOOST_HANA_WHILE_HPP #include #include #include #include #include #include #include #include #include #include BOOST_HANA_NAMESPACE_BEGIN //! @cond template constexpr decltype(auto) while_t::operator()(Pred&& pred, State&& state, F&& f) const { using Cond = decltype(pred(state)); using Bool = typename hana::tag_of::type; using While = BOOST_HANA_DISPATCH_IF(while_impl, hana::Logical::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Logical::value, "hana::while_(pred, state, f) requires 'pred(state)' to be a Logical"); #endif return While::apply(static_cast(pred), static_cast(state), static_cast(f)); } //! @endcond template struct while_impl> : hana::default_ { template static constexpr auto apply(Args&& ...) = delete; }; template struct while_impl::value>> { template static auto apply(Pred&& pred, State&& state, F&& f) -> decltype( true ? f(static_cast(state)) : static_cast(state) ) { if (pred(state)) { decltype(auto) r = f(static_cast(state)); return hana::while_(static_cast(pred), static_cast(r), static_cast(f)); } else { return static_cast(state); } } }; template struct while_impl::value && hana::Logical::value >> { template static constexpr State while_helper(hana::false_, Pred&&, State&& state, F&&) { return static_cast(state); } template static constexpr decltype(auto) while_helper(hana::true_, Pred&& pred, State&& state, F&& f) { decltype(auto) r = f(static_cast(state)); return hana::while_(static_cast(pred), static_cast(r), static_cast(f)); } template static constexpr decltype(auto) apply(Pred&& pred, State&& state, F&& f) { // Since `pred(state)` returns a `Constant`, we do not actually // need to call it; we only need its decltype. However, we still // call it to run potential side effects. I'm not sure whether // that is desirable, since we pretty much take for granted that // functions are pure, but we'll do it like this for now. Also, I // think there is something rather deep hidden behind this, and // understanding what must be done here should give us a better // understanding of something non-trivial. auto cond_ = pred(state); constexpr auto cond = hana::value(cond_); constexpr bool truth_value = hana::if_(cond, true, false); return while_helper(hana::bool_c, static_cast(pred), static_cast(state), static_cast(f)); } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_WHILE_HPP