| 1 | /*! |
| 2 | @file |
| 3 | Defines `boost::hana::repeat`. |
| 4 | |
| 5 | Copyright Louis Dionne 2013-2022 |
| 6 | Distributed under the Boost Software License, Version 1.0. |
| 7 | (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 8 | */ |
| 9 | |
| 10 | #ifndef BOOST_HANA_REPEAT_HPP |
| 11 | #define BOOST_HANA_REPEAT_HPP |
| 12 | |
| 13 | #include <boost/hana/fwd/repeat.hpp> |
| 14 | |
| 15 | #include <boost/hana/concept/integral_constant.hpp> |
| 16 | #include <boost/hana/config.hpp> |
| 17 | #include <boost/hana/core/dispatch.hpp> |
| 18 | |
| 19 | #include <cstddef> |
| 20 | #include <utility> |
| 21 | |
| 22 | |
| 23 | namespace boost { namespace hana { |
| 24 | template <typename I, bool condition> |
| 25 | struct repeat_impl<I, when<condition>> : default_ { |
| 26 | template <typename F, std::size_t ...i> |
| 27 | static constexpr void repeat_helper(F&& f, std::index_sequence<i...>) { |
| 28 | using Swallow = std::size_t[]; |
| 29 | (void)Swallow{0, ((void)f(), i)...}; |
| 30 | } |
| 31 | |
| 32 | template <typename N, typename F> |
| 33 | static constexpr auto apply(N const&, F&& f) { |
| 34 | static_assert(N::value >= 0, "hana::repeat(n, f) requires 'n' to be non-negative" ); |
| 35 | constexpr std::size_t n = N::value; |
| 36 | repeat_helper(static_cast<F&&>(f), std::make_index_sequence<n>{}); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | //! @cond |
| 41 | template <typename N, typename F> |
| 42 | constexpr void repeat_t::operator()(N const& n, F&& f) const { |
| 43 | using I = typename hana::tag_of<N>::type; |
| 44 | using Repeat = BOOST_HANA_DISPATCH_IF(repeat_impl<I>, |
| 45 | hana::IntegralConstant<I>::value |
| 46 | ); |
| 47 | |
| 48 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 49 | static_assert(hana::IntegralConstant<I>::value, |
| 50 | "hana::repeat(n, f) requires 'n' to be an IntegralConstant" ); |
| 51 | #endif |
| 52 | |
| 53 | return Repeat::apply(n, static_cast<F&&>(f)); |
| 54 | } |
| 55 | //! @endcond |
| 56 | }} // end namespace boost::hana |
| 57 | |
| 58 | #endif // !BOOST_HANA_REPEAT_HPP |
| 59 | |