repeat.hpp 1.8 KB

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