insert.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*!
  2. @file
  3. Defines `boost::hana::insert`.
  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_HPP
  9. #define BOOST_HANA_INSERT_HPP
  10. #include <boost/hana/fwd/insert.hpp>
  11. #include <boost/hana/append.hpp>
  12. #include <boost/hana/concat.hpp>
  13. #include <boost/hana/concept/sequence.hpp>
  14. #include <boost/hana/config.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/drop_front.hpp>
  17. #include <boost/hana/take_front.hpp>
  18. BOOST_HANA_NAMESPACE_BEGIN
  19. //! @cond
  20. template <typename Set, typename ...Args>
  21. constexpr decltype(auto) insert_t::operator()(Set&& set, Args&& ...args) const {
  22. return insert_impl<typename hana::tag_of<Set>::type>::apply(
  23. static_cast<Set&&>(set),
  24. static_cast<Args&&>(args)...
  25. );
  26. }
  27. //! @endcond
  28. template <typename T, bool condition>
  29. struct insert_impl<T, when<condition>> : default_ {
  30. template <typename ...Args>
  31. static constexpr auto apply(Args&& ...) = delete;
  32. };
  33. template <typename S>
  34. struct insert_impl<S, when<Sequence<S>::value>> {
  35. template <typename Xs, typename N, typename Element>
  36. static constexpr auto apply(Xs&& xs, N const& n, Element&& e) {
  37. return hana::concat(hana::append(hana::take_front(xs, n),
  38. static_cast<Element&&>(e)),
  39. hana::drop_front(xs, n));
  40. }
  41. };
  42. BOOST_HANA_NAMESPACE_END
  43. #endif // !BOOST_HANA_INSERT_HPP