on.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <range/v3/detail/prologue.hpp>
  19. namespace ranges
  20. {
  21. /// \addtogroup group-functional
  22. /// @{
  23. template<typename Fn1, typename Fn2>
  24. struct transformed
  25. {
  26. private:
  27. RANGES_NO_UNIQUE_ADDRESS
  28. Fn1 first_;
  29. RANGES_NO_UNIQUE_ADDRESS
  30. Fn2 second_;
  31. public:
  32. transformed() = default;
  33. constexpr transformed(Fn1 fn1, Fn2 fn2)
  34. : first_(static_cast<Fn1 &&>(fn1))
  35. , second_(static_cast<Fn2 &&>(fn2))
  36. {}
  37. // clang-format off
  38. template<typename... Args>
  39. auto CPP_auto_fun(operator())(Args &&... args)
  40. (
  41. return invoke(first_, invoke(second_, static_cast<Args &&>(args)...))
  42. )
  43. template<typename... Args>
  44. auto CPP_auto_fun(operator())(Args &&... args)(const)
  45. (
  46. return invoke((Fn1 const &)first_,
  47. invoke((Fn2 const &)second_, static_cast<Args &&>(args))...)
  48. )
  49. // clang-format on
  50. };
  51. struct on_fn
  52. {
  53. template<typename Fn1, typename Fn2>
  54. constexpr transformed<Fn1, Fn2> operator()(Fn1 fn1, Fn2 fn2) const
  55. {
  56. return transformed<Fn1, Fn2>{detail::move(fn1), detail::move(fn2)};
  57. }
  58. };
  59. /// \ingroup group-functional
  60. /// \sa `on_fn`
  61. RANGES_INLINE_VARIABLE(on_fn, on)
  62. /// @}
  63. } // namespace ranges
  64. #include <range/v3/detail/epilogue.hpp>
  65. #endif