result.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. result.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_RESULT_H
  8. #define BOOST_HOF_GUARD_RESULT_H
  9. /// result
  10. /// ======
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `result` function adaptor sets the return type for the function, which
  16. /// can be useful when dealing with multiple overloads. Since the return type
  17. /// is no longer dependent on the parameters passed to the function, the
  18. /// `result_adaptor` provides a nested `result_type` that is the return type
  19. /// of the function.
  20. ///
  21. /// Synopsis
  22. /// --------
  23. ///
  24. /// template<class Result, class F>
  25. /// constexpr result_adaptor<Result, F> result(F f);
  26. ///
  27. /// Requirements
  28. /// ------------
  29. ///
  30. /// F must be:
  31. ///
  32. /// * [ConstInvocable](ConstInvocable)
  33. /// * MoveConstructible
  34. ///
  35. /// Example
  36. /// -------
  37. ///
  38. /// #include <boost/hof.hpp>
  39. /// #include <cassert>
  40. ///
  41. /// struct id
  42. /// {
  43. /// template<class T>
  44. /// T operator()(T x) const
  45. /// {
  46. /// return x;
  47. /// }
  48. /// };
  49. ///
  50. /// int main() {
  51. /// auto int_result = boost::hof::result<int>(id());
  52. /// static_assert(std::is_same<decltype(int_result(true)), int>::value, "Not the same type");
  53. /// }
  54. ///
  55. #include <boost/hof/detail/callable_base.hpp>
  56. #include <boost/hof/is_invocable.hpp>
  57. #include <boost/hof/always.hpp>
  58. #include <boost/hof/reveal.hpp>
  59. namespace boost { namespace hof {
  60. template<class Result, class F>
  61. struct result_adaptor : detail::callable_base<F>
  62. {
  63. BOOST_HOF_INHERIT_CONSTRUCTOR(result_adaptor, detail::callable_base<F>)
  64. typedef Result result_type;
  65. struct failure
  66. : failure_for<detail::callable_base<F>>
  67. {};
  68. template<class... Ts>
  69. constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const
  70. {
  71. return boost::hof::always_ref(*this)(xs...);
  72. }
  73. template<class... Ts, class=typename std::enable_if<(boost::hof::is_invocable<F, Ts...>::value)>::type>
  74. constexpr result_type operator()(Ts&&... xs) const
  75. {
  76. return this->base_function(xs...)(BOOST_HOF_FORWARD(Ts)(xs)...);
  77. };
  78. };
  79. template<class F>
  80. struct result_adaptor<void, F> : detail::callable_base<F>
  81. {
  82. BOOST_HOF_INHERIT_CONSTRUCTOR(result_adaptor, detail::callable_base<F>)
  83. typedef void result_type;
  84. template<class... Ts>
  85. constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const
  86. {
  87. return boost::hof::always_ref(*this)(xs...);
  88. }
  89. template<class... Ts, class=typename std::enable_if<(boost::hof::is_invocable<F, Ts...>::value)>::type>
  90. constexpr typename detail::holder<Ts...>::type operator()(Ts&&... xs) const
  91. {
  92. return (typename detail::holder<Ts...>::type)this->base_function(xs...)(BOOST_HOF_FORWARD(Ts)(xs)...);
  93. };
  94. };
  95. #if BOOST_HOF_HAS_VARIABLE_TEMPLATES
  96. namespace result_detail {
  97. template<class Result>
  98. struct result_f
  99. {
  100. template<class F>
  101. constexpr result_adaptor<Result, F> operator()(F f) const
  102. {
  103. return result_adaptor<Result, F>(boost::hof::move(f));
  104. }
  105. };
  106. }
  107. template<class Result>
  108. static constexpr auto result = result_detail::result_f<Result>{};
  109. #else
  110. template<class Result, class F>
  111. constexpr result_adaptor<Result, F> result(F f)
  112. {
  113. return result_adaptor<Result, F>(boost::hof::move(f));
  114. }
  115. #endif
  116. }} // namespace boost::hof
  117. #endif