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