You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_FUNCTIONAL_ON_HPP
  14. #define RANGES_V3_FUNCTIONAL_ON_HPP
  15. #include <concepts/concepts.hpp>
  16. #include <range/v3/detail/config.hpp>
  17. #include <range/v3/functional/invoke.hpp>
  18. namespace ranges
  19. {
  20. /// \addtogroup group-functional
  21. /// @{
  22. template<typename Fn1, typename Fn2>
  23. struct transformed
  24. {
  25. private:
  26. RANGES_NO_UNIQUE_ADDRESS
  27. Fn1 first_;
  28. RANGES_NO_UNIQUE_ADDRESS
  29. Fn2 second_;
  30. public:
  31. transformed() = default;
  32. constexpr transformed(Fn1 fn1, Fn2 fn2)
  33. : first_(static_cast<Fn1 &&>(fn1))
  34. , second_(static_cast<Fn2 &&>(fn2))
  35. {}
  36. // clang-format off
  37. template<typename... Args>
  38. auto CPP_auto_fun(operator())(Args &&... args)
  39. (
  40. return invoke(first_, invoke(second_, static_cast<Args &&>(args)...))
  41. )
  42. template<typename... Args>
  43. auto CPP_auto_fun(operator())(Args &&... args)(const)
  44. (
  45. return invoke((Fn1 const &)first_,
  46. invoke((Fn2 const &)second_, static_cast<Args &&>(args)...))
  47. )
  48. // clang-format on
  49. };
  50. struct on_fn
  51. {
  52. template<typename Fn1, typename Fn2>
  53. constexpr transformed<Fn1, Fn2> operator()(Fn1 fn1, Fn2 fn2) const
  54. {
  55. return transformed<Fn1, Fn2>{detail::move(fn1), detail::move(fn2)};
  56. }
  57. };
  58. /// \ingroup group-functional
  59. /// \sa `on_fn`
  60. RANGES_INLINE_VARIABLE(on_fn, on)
  61. /// @}
  62. } // namespace ranges
  63. #endif