transform.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*!
  2. @file
  3. Defines `boost::hana::transform`.
  4. Copyright Louis Dionne 2013-2022
  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_TRANSFORM_HPP
  9. #define BOOST_HANA_TRANSFORM_HPP
  10. #include <boost/hana/fwd/transform.hpp>
  11. #include <boost/hana/bool.hpp>
  12. #include <boost/hana/concept/functor.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/core/make.hpp>
  17. #include <boost/hana/functional/always.hpp>
  18. #include <boost/hana/fwd/adjust_if.hpp>
  19. #include <boost/hana/unpack.hpp>
  20. namespace boost { namespace hana {
  21. //! @cond
  22. template <typename Xs, typename F>
  23. constexpr auto transform_t::operator()(Xs&& xs, F&& f) const {
  24. using S = typename hana::tag_of<Xs>::type;
  25. using Transform = BOOST_HANA_DISPATCH_IF(transform_impl<S>,
  26. hana::Functor<S>::value
  27. );
  28. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  29. static_assert(hana::Functor<S>::value,
  30. "hana::transform(xs, f) requires 'xs' to be a Functor");
  31. #endif
  32. return Transform::apply(static_cast<Xs&&>(xs), static_cast<F&&>(f));
  33. }
  34. //! @endcond
  35. template <typename Fun, bool condition>
  36. struct transform_impl<Fun, when<condition>> : default_ {
  37. template <typename Xs, typename F>
  38. static constexpr auto apply(Xs&& xs, F&& f) {
  39. return hana::adjust_if(static_cast<Xs&&>(xs),
  40. hana::always(hana::true_c),
  41. static_cast<F&&>(f));
  42. }
  43. };
  44. template <typename S>
  45. struct transform_impl<S, when<Sequence<S>::value>> {
  46. //! @cond
  47. template <typename F>
  48. struct transformer {
  49. F f;
  50. template <typename ...Xs>
  51. constexpr auto operator()(Xs&& ...xs) const {
  52. return hana::make<S>((*f)(static_cast<Xs&&>(xs))...);
  53. }
  54. };
  55. //! @endcond
  56. template <typename Xs, typename F>
  57. static constexpr auto apply(Xs&& xs, F&& f) {
  58. // We use a pointer to workaround a Clang 3.5 ICE
  59. return hana::unpack(static_cast<Xs&&>(xs),
  60. transformer<decltype(&f)>{&f});
  61. }
  62. };
  63. }} // end namespace boost::hana
  64. #endif // !BOOST_HANA_TRANSFORM_HPP