indirect.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. indirect.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_INDIRECT_H
  8. #define BOOST_HOF_GUARD_FUNCTION_INDIRECT_H
  9. /// indirect
  10. /// ========
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `indirect` function adaptor dereferences the object before calling it.
  16. ///
  17. /// Synopsis
  18. /// --------
  19. ///
  20. /// template<class F>
  21. /// constexpr indirect_adaptor<F> indirect(F f);
  22. ///
  23. /// Semantics
  24. /// ---------
  25. ///
  26. /// assert(indirect(f)(xs...) == (*f)(xs...));
  27. ///
  28. /// Requirements
  29. /// ------------
  30. ///
  31. /// F must be:
  32. ///
  33. /// * MoveConstructible
  34. /// * Dereferenceable
  35. ///
  36. /// Example
  37. /// -------
  38. ///
  39. /// #include <boost/hof.hpp>
  40. /// #include <cassert>
  41. /// #include <memory>
  42. /// using namespace boost::hof;
  43. ///
  44. /// struct sum
  45. /// {
  46. /// template<class T, class U>
  47. /// T operator()(T x, U y) const
  48. /// {
  49. /// return x+y;
  50. /// }
  51. /// };
  52. ///
  53. /// int main() {
  54. /// int r = indirect(std::make_unique<sum>())(3,2);
  55. /// assert(r == 5);
  56. /// }
  57. ///
  58. #include <boost/hof/detail/delegate.hpp>
  59. #include <boost/hof/detail/result_of.hpp>
  60. #include <boost/hof/reveal.hpp>
  61. #include <boost/hof/always.hpp>
  62. #include <boost/hof/detail/move.hpp>
  63. #include <boost/hof/detail/make.hpp>
  64. #include <boost/hof/detail/static_const_var.hpp>
  65. namespace boost { namespace hof {
  66. // TODO: Support non-classes as well
  67. template<class F>
  68. struct indirect_adaptor : F
  69. {
  70. typedef indirect_adaptor fit_rewritable1_tag;
  71. BOOST_HOF_INHERIT_CONSTRUCTOR(indirect_adaptor, F);
  72. template<class... Ts>
  73. constexpr const F& base_function(Ts&&... xs) const noexcept
  74. {
  75. return boost::hof::always_ref(*this)(xs...);
  76. }
  77. struct failure
  78. : failure_for<decltype(*std::declval<F>())>
  79. {};
  80. BOOST_HOF_RETURNS_CLASS(indirect_adaptor);
  81. template<class... Ts>
  82. constexpr BOOST_HOF_SFINAE_RESULT(decltype(*std::declval<F>()), id_<Ts>...)
  83. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  84. (
  85. (*BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...)))(BOOST_HOF_FORWARD(Ts)(xs)...)
  86. );
  87. };
  88. template<class F>
  89. struct indirect_adaptor<F*>
  90. {
  91. typedef indirect_adaptor fit_rewritable1_tag;
  92. F* f;
  93. constexpr indirect_adaptor() noexcept
  94. {}
  95. constexpr indirect_adaptor(F* x) noexcept
  96. : f(x)
  97. {}
  98. template<class... Ts>
  99. constexpr F& base_function(Ts&&...) const noexcept
  100. {
  101. return *f;
  102. }
  103. struct failure
  104. : failure_for<F>
  105. {};
  106. BOOST_HOF_RETURNS_CLASS(indirect_adaptor);
  107. template<class... Ts>
  108. constexpr BOOST_HOF_SFINAE_RESULT(F, id_<Ts>...)
  109. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  110. (
  111. (BOOST_HOF_MANGLE_CAST(F&)(BOOST_HOF_CONST_THIS->base_function(xs...)))(BOOST_HOF_FORWARD(Ts)(xs)...)
  112. );
  113. };
  114. BOOST_HOF_DECLARE_STATIC_VAR(indirect, detail::make<indirect_adaptor>);
  115. }} // namespace boost::hof
  116. #endif