insert_range.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*!
  2. @file
  3. Defines `boost::hana::insert_range`.
  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_INSERT_RANGE_HPP
  9. #define BOOST_HANA_INSERT_RANGE_HPP
  10. #include <boost/hana/fwd/insert_range.hpp>
  11. #include <boost/hana/concat.hpp>
  12. #include <boost/hana/concept/foldable.hpp>
  13. #include <boost/hana/concept/sequence.hpp>
  14. #include <boost/hana/config.hpp>
  15. #include <boost/hana/core/to.hpp>
  16. #include <boost/hana/core/dispatch.hpp>
  17. #include <boost/hana/drop_front.hpp>
  18. #include <boost/hana/take_front.hpp>
  19. BOOST_HANA_NAMESPACE_BEGIN
  20. //! @cond
  21. template <typename Xs, typename N, typename Elements>
  22. constexpr auto insert_range_t::operator()(Xs&& xs, N&& n, Elements&& elements) const {
  23. using S = typename hana::tag_of<Xs>::type;
  24. using InsertRange = BOOST_HANA_DISPATCH_IF(insert_range_impl<S>,
  25. hana::Sequence<Xs>::value &&
  26. hana::Foldable<Elements>::value
  27. );
  28. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  29. static_assert(hana::Sequence<Xs>::value,
  30. "hana::insert_range(xs, n, elements) requires 'xs' to be a Sequence");
  31. static_assert(hana::Foldable<Elements>::value,
  32. "hana::insert_range(xs, n, elements) requires 'elements' to be a Foldable");
  33. #endif
  34. return InsertRange::apply(static_cast<Xs&&>(xs),
  35. static_cast<N&&>(n),
  36. static_cast<Elements&&>(elements));
  37. }
  38. //! @endcond
  39. template <typename S, bool condition>
  40. struct insert_range_impl<S, when<condition>> {
  41. template <typename Xs, typename N, typename Elements>
  42. static constexpr auto apply(Xs&& xs, N const& n, Elements&& e) {
  43. return hana::concat(
  44. hana::concat(
  45. hana::take_front(xs, n),
  46. hana::to<S>(static_cast<Elements&&>(e))
  47. ),
  48. hana::drop_front(xs, n)
  49. );
  50. }
  51. };
  52. BOOST_HANA_NAMESPACE_END
  53. #endif // !BOOST_HANA_INSERT_RANGE_HPP