fill.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*!
  2. @file
  3. Defines `boost::hana::fill`.
  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_FILL_HPP
  9. #define BOOST_HANA_FILL_HPP
  10. #include <boost/hana/fwd/fill.hpp>
  11. #include <boost/hana/concept/functor.hpp>
  12. #include <boost/hana/concept/sequence.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/core/make.hpp>
  16. #include <boost/hana/functional/always.hpp>
  17. #include <boost/hana/transform.hpp>
  18. #include <boost/hana/unpack.hpp>
  19. BOOST_HANA_NAMESPACE_BEGIN
  20. //! @cond
  21. template <typename Xs, typename Value>
  22. constexpr auto fill_t::operator()(Xs&& xs, Value&& value) const {
  23. using S = typename hana::tag_of<Xs>::type;
  24. using Fill = BOOST_HANA_DISPATCH_IF(fill_impl<S>,
  25. hana::Functor<S>::value
  26. );
  27. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  28. static_assert(hana::Functor<S>::value,
  29. "hana::fill(xs, value) requires 'xs' to be a Functor");
  30. #endif
  31. return Fill::apply(static_cast<Xs&&>(xs),
  32. static_cast<Value&&>(value));
  33. }
  34. //! @endcond
  35. template <typename Fun, bool condition>
  36. struct fill_impl<Fun, when<condition>> : default_ {
  37. template <typename Xs, typename Value>
  38. static constexpr auto apply(Xs&& xs, Value&& v) {
  39. return hana::transform(static_cast<Xs&&>(xs),
  40. hana::always(static_cast<Value&&>(v))
  41. );
  42. }
  43. };
  44. template <typename S>
  45. struct fill_impl<S, when<Sequence<S>::value>> {
  46. //! @cond
  47. template <typename V>
  48. struct filler {
  49. V const& v;
  50. template <typename ...Xs>
  51. constexpr auto operator()(Xs const& ...xs) const {
  52. return hana::make<S>(((void)xs, v)...);
  53. }
  54. };
  55. //! @endcond
  56. template <typename Xs, typename V>
  57. static constexpr auto apply(Xs const& xs, V const& v) {
  58. return hana::unpack(xs, filler<V>{v});
  59. }
  60. };
  61. BOOST_HANA_NAMESPACE_END
  62. #endif // !BOOST_HANA_FILL_HPP