protect.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. protect.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_PROTECT_H
  8. #define BOOST_HOF_GUARD_FUNCTION_PROTECT_H
  9. /// protect
  10. /// =======
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `protect` function adaptor can be used to make a bind expression be
  16. /// treated as a normal function instead. Both `bind` and
  17. /// [`lazy`](/include/boost/hof/lazy) eargerly evaluates nested bind expressions.
  18. /// The `protect` adaptor masks the type so `bind` or
  19. /// [`lazy`](/include/boost/hof/lazy) no longer recognizes the function as bind
  20. /// expression and evaluates it.
  21. ///
  22. /// Synopsis
  23. /// --------
  24. ///
  25. /// template<class F>
  26. /// constexpr protect_adaptor<F> protect(F f);
  27. ///
  28. /// Semantics
  29. /// ---------
  30. ///
  31. /// assert(lazy(f)(protect(lazy(g)(_1)))() == f(lazy(g)(_1)))
  32. ///
  33. /// Requirements
  34. /// ------------
  35. ///
  36. /// F must be:
  37. ///
  38. /// * [ConstInvocable](ConstInvocable)
  39. /// * MoveConstructible
  40. ///
  41. /// Example
  42. /// -------
  43. ///
  44. /// #include <boost/hof.hpp>
  45. /// #include <cassert>
  46. /// using namespace boost::hof;
  47. ///
  48. /// int main() {
  49. /// auto lazy_id = lazy(identity)(_1);
  50. /// auto lazy_apply = lazy(apply)(protect(lazy_id), _1);
  51. /// assert(lazy_apply(3) == 3);
  52. /// }
  53. ///
  54. /// See Also
  55. /// --------
  56. ///
  57. /// * [lazy](lazy)
  58. ///
  59. #include <utility>
  60. #include <boost/hof/reveal.hpp>
  61. #include <boost/hof/detail/forward.hpp>
  62. #include <boost/hof/detail/make.hpp>
  63. #include <boost/hof/detail/static_const_var.hpp>
  64. namespace boost { namespace hof {
  65. template<class F>
  66. struct protect_adaptor : detail::callable_base<F>
  67. {
  68. typedef protect_adaptor fit_rewritable1_tag;
  69. BOOST_HOF_INHERIT_CONSTRUCTOR(protect_adaptor, detail::callable_base<F>)
  70. };
  71. BOOST_HOF_DECLARE_STATIC_VAR(protect, detail::make<protect_adaptor>);
  72. }} // namespace boost::hof
  73. #endif