proxygen
composition_3.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <algorithm>
17 #include <cassert>
18 #include <iostream>
19 #include <vector>
20 
23 
26 
27 // https://godbolt.org/g/rVLMTu
28 
29 using namespace folly::pushmi::aliases;
30 
31 // three models of submission deferral
32 // (none of these use an executor, they are all running
33 // synchronously on the main thread)
34 
35 // this constructs eagerly and submits just() lazily
37  printf("construct just\n");
38  return op::just(42) | op::tap([](int v) { printf("just - %d\n", v); });
39 }
40 
41 // this constructs defer() eagerly, constructs just() and submits just() lazily
43  return op::defer([] { return defer_execution(); });
44 }
45 
46 // this constructs defer(), constructs just() and submits just() eagerly
48  return defer_execution() | op::share<int>();
49 }
50 
51 int main() {
52  printf("\ncall defer_execution\n");
53  auto de = defer_execution();
54  printf("submit defer_execution\n");
55  de | op::submit();
56  // call defer_execution
57  // construct just
58  // submit defer_execution
59  // just - 42
60 
61  printf("\ncall defer_construction\n");
62  auto dc = defer_construction();
63  printf("submit defer_construction\n");
64  dc | op::submit();
65  // call defer_construction
66  // submit defer_construction
67  // construct just
68  // just - 42
69 
70  printf("\ncall eager_execution\n");
71  auto ee = eager_execution();
72  printf("submit eager_execution\n");
73  ee | op::submit();
74  // call eager_execution
75  // construct just
76  // just - 42
77  // submit eager_execution
78 
79  std::cout << "OK" << std::endl;
80  // OK
81 }
auto defer_construction()
PUSHMI_INLINE_VAR constexpr detail::tap_fn tap
Definition: tap.h:126
int main()
auto eager_execution()
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::defer_fn defer
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::just_fn just
auto defer_execution()