match.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*=============================================================================
  2. Copyright (c) 2012 Paul Fultz II
  3. match.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_FUNCTION_OVERLOAD_H
  8. #define BOOST_HOF_GUARD_FUNCTION_OVERLOAD_H
  9. /// match
  10. /// =====
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `match` function adaptor combines several functions together and
  16. /// resolves which one should be called by using C++ overload resolution. This
  17. /// is different than the [`first_of`](/include/boost/hof/conditional) adaptor which resolves
  18. /// them based on order.
  19. ///
  20. /// Synopsis
  21. /// --------
  22. ///
  23. /// template<class... Fs>
  24. /// constexpr match_adaptor<Fs...> match(Fs...fs);
  25. ///
  26. /// Requirements
  27. /// ------------
  28. ///
  29. /// Fs must be:
  30. ///
  31. /// * [ConstInvocable](ConstInvocable)
  32. /// * MoveConstructible
  33. ///
  34. /// Example
  35. /// -------
  36. ///
  37. /// #include <boost/hof.hpp>
  38. /// using namespace boost::hof;
  39. ///
  40. /// struct int_class
  41. /// {
  42. /// int operator()(int) const
  43. /// {
  44. /// return 1;
  45. /// }
  46. /// };
  47. ///
  48. /// struct foo
  49. /// {};
  50. ///
  51. /// struct foo_class
  52. /// {
  53. /// foo operator()(foo) const
  54. /// {
  55. /// return foo();
  56. /// }
  57. /// };
  58. ///
  59. /// typedef match_adaptor<int_class, foo_class> fun;
  60. ///
  61. /// static_assert(std::is_same<int, decltype(fun()(1))>::value, "Failed match");
  62. /// static_assert(std::is_same<foo, decltype(fun()(foo()))>::value, "Failed match");
  63. ///
  64. /// int main() {}
  65. ///
  66. /// References
  67. /// ----------
  68. ///
  69. /// * [POO51](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0051r2.pdf) - Proposal for C++
  70. /// Proposal for C++ generic overload function
  71. ///
  72. #include <boost/hof/reveal.hpp>
  73. #include <boost/hof/detail/callable_base.hpp>
  74. #include <boost/hof/detail/delegate.hpp>
  75. #include <boost/hof/detail/move.hpp>
  76. #include <boost/hof/detail/make.hpp>
  77. #include <boost/hof/detail/static_const_var.hpp>
  78. namespace boost { namespace hof {
  79. template<class...Fs> struct match_adaptor;
  80. template<class F, class...Fs>
  81. struct match_adaptor<F, Fs...> : detail::callable_base<F>, match_adaptor<Fs...>
  82. {
  83. typedef match_adaptor<Fs...> base;
  84. typedef match_adaptor fit_rewritable_tag;
  85. struct failure
  86. : failure_for<detail::callable_base<F>, Fs...>
  87. {};
  88. BOOST_HOF_INHERIT_DEFAULT(match_adaptor, detail::callable_base<F>, base);
  89. template<class X, class... Xs, BOOST_HOF_ENABLE_IF_CONVERTIBLE(X, detail::callable_base<F>), BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(base, Xs...)>
  90. constexpr match_adaptor(X&& f1, Xs&& ... fs)
  91. : detail::callable_base<F>(BOOST_HOF_FORWARD(X)(f1)), base(BOOST_HOF_FORWARD(Xs)(fs)...)
  92. {}
  93. using F::operator();
  94. using base::operator();
  95. };
  96. template<class F>
  97. struct match_adaptor<F> : detail::callable_base<F>
  98. {
  99. typedef detail::callable_base<F> base;
  100. typedef match_adaptor fit_rewritable_tag;
  101. using F::operator();
  102. BOOST_HOF_INHERIT_CONSTRUCTOR(match_adaptor, detail::callable_base<F>);
  103. };
  104. BOOST_HOF_DECLARE_STATIC_VAR(match, detail::make<match_adaptor>);
  105. }} // namespace boost::hof
  106. #endif