proxygen
flow_single_sender.h
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 #pragma once
17 
21 
22 namespace folly {
23 namespace pushmi {
24 
25 template <class PE, class E, class... VN>
27  union data {
28  void* pobj_ = nullptr;
29  char buffer_[sizeof(std::tuple<VN...>)]; // can hold a V in-situ
30  } data_{};
31  template <class Wrapped>
32  static constexpr bool insitu() {
33  return sizeof(Wrapped) <= sizeof(data::buffer_) &&
35  }
36  struct vtable {
37  static void s_op(data&, data*) {}
38  static any_executor<E> s_executor(data&) { return {}; }
40  void (*op_)(data&, data*) = vtable::s_op;
42  void (*submit_)(data&, any_flow_receiver<PE, std::ptrdiff_t, E, VN...>) =
44  };
45  static constexpr vtable const noop_ {};
46  vtable const* vptr_ = &noop_;
47  template <class Wrapped>
50  struct s {
51  static void op(data& src, data* dst) {
52  if (dst)
53  dst->pobj_ = std::exchange(src.pobj_, nullptr);
54  delete static_cast<Wrapped const*>(src.pobj_);
55  }
56  static any_executor<E> executor(data& src) {
57  return any_executor<E>{
58  ::folly::pushmi::executor(*static_cast<Wrapped*>(src.pobj_))};
59  }
60  static void submit(
61  data& src,
64  *static_cast<Wrapped*>(src.pobj_), std::move(out));
65  }
66  };
67  static const vtable vtbl{s::op, s::executor, s::submit};
68  data_.pobj_ = new Wrapped(std::move(obj));
69  vptr_ = &vtbl;
70  }
71  template <class Wrapped>
74  struct s {
75  static void op(data& src, data* dst) {
76  if (dst)
77  new (dst->buffer_)
78  Wrapped(std::move(*static_cast<Wrapped*>((void*)src.buffer_)));
79  static_cast<Wrapped const*>((void*)src.buffer_)->~Wrapped();
80  }
81  static any_executor<E> executor(data& src) {
83  *static_cast<Wrapped*>((void*)src.buffer_))};
84  }
85  static void submit(
86  data& src,
89  *static_cast<Wrapped*>((void*)src.buffer_), std::move(out));
90  }
91  };
92  static const vtable vtbl{s::op, s::executor, s::submit};
93  new (data_.buffer_) Wrapped(std::move(obj));
94  vptr_ = &vtbl;
95  }
96  template <class T, class U = std::decay_t<T>>
97  using wrapped_t =
99  public:
101 
102  any_flow_single_sender() = default;
105  that.vptr_->op_(that.data_, &data_);
106  std::swap(that.vptr_, vptr_);
107  }
108  PUSHMI_TEMPLATE (class Wrapped)
109  (requires FlowSender<wrapped_t<Wrapped>, is_single<>>)
110  explicit any_flow_single_sender(Wrapped obj) noexcept(insitu<Wrapped>())
113  vptr_->op_(data_, nullptr);
114  }
116  this->~any_flow_single_sender();
117  new ((void*)this) any_flow_single_sender(std::move(that));
118  return *this;
119  }
121  return vptr_->executor_(data_);
122  }
124  vptr_->submit_(data_, std::move(out));
125  }
126 };
127 
128 // Class static definitions:
129 template <class PE, class E, class... VN>
130 constexpr typename any_flow_single_sender<PE, E, VN...>::vtable const
132 
133 template <class SF, class EXF>
134 class flow_single_sender<SF, EXF> {
135  SF sf_;
136  EXF exf_;
137 
138  public:
139  using properties = property_set<is_sender<>, is_flow<>, is_single<>>;
140 
141  constexpr flow_single_sender() = default;
142  constexpr explicit flow_single_sender(SF sf)
143  : sf_(std::move(sf)) {}
144  constexpr flow_single_sender(SF sf, EXF exf)
145  : sf_(std::move(sf)), exf_(std::move(exf)) {}
146 
147  auto executor() { return exf_(); }
148  PUSHMI_TEMPLATE(class Out)
149  (requires Receiver<Out> && Invocable<SF&, Out>)
150  void submit(Out out) {
151  sf_(std::move(out));
152  }
153 };
154 
155 template <
156  PUSHMI_TYPE_CONSTRAINT(Sender<is_single<>, is_flow<>>) Data,
157  class DSF,
158  class DEXF>
159 class flow_single_sender<Data, DSF, DEXF> {
161  DSF sf_;
162  DEXF exf_;
163 
164  public:
167  property_set<is_sender<>, is_flow<>, is_single<>>>;
168 
169  constexpr flow_single_sender() = default;
170  constexpr explicit flow_single_sender(Data data)
171  : data_(std::move(data)) {}
172  constexpr flow_single_sender(Data data, DSF sf)
173  : data_(std::move(data)), sf_(std::move(sf)) {}
174  constexpr flow_single_sender(Data data, DSF sf, DEXF exf)
175  : data_(std::move(data)), sf_(std::move(sf)), exf_(std::move(exf)) {}
176 
177  auto executor() { return exf_(data_); }
178  PUSHMI_TEMPLATE(class Out)
179  (requires PUSHMI_EXP(lazy::Receiver<Out> PUSHMI_AND
180  lazy::Invocable<DSF&, Data&, Out>))
181  void submit(Out out) {
182  sf_(data_, std::move(out));
183  }
184 };
185 
186 template <>
188  : public flow_single_sender<ignoreSF, trampolineEXF> {
189 public:
190  flow_single_sender() = default;
191 };
192 
194 // make_flow_single_sender
196  inline auto operator()() const {
198  }
199  PUSHMI_TEMPLATE(class SF)
200  (requires True<> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
201  auto operator()(SF sf) const {
203  }
204  PUSHMI_TEMPLATE(class SF, class EXF)
205  (requires True<> && Invocable<EXF&>
206  PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
207  auto operator()(SF sf, EXF exf) const {
209  }
210  PUSHMI_TEMPLATE(class Data)
211  (requires True<> && Sender<Data, is_single<>, is_flow<>>)
212  auto operator()(Data d) const {
214  }
215  PUSHMI_TEMPLATE(class Data, class DSF)
216  (requires Sender<Data, is_single<>, is_flow<>>)
217  auto operator()(Data d, DSF sf) const {
219  }
220  PUSHMI_TEMPLATE(class Data, class DSF, class DEXF)
221  (requires Sender<Data, is_single<>, is_flow<>> && Invocable<DEXF&, Data&>)
222  auto operator()(Data d, DSF sf, DEXF exf) const {
224  std::move(sf), std::move(exf)};
225  }
226 } const make_flow_single_sender {};
227 
229 // deduction guides
230 #if __cpp_deduction_guides >= 201703
232 
233 PUSHMI_TEMPLATE(class SF)
234  (requires True<> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
236 
237 PUSHMI_TEMPLATE(class SF, class EXF)
238  (requires True<> && Invocable<EXF&>
239  PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
241 
242 PUSHMI_TEMPLATE(class Data)
243  (requires True<> && Sender<Data, is_single<>, is_flow<>>)
245 
246 PUSHMI_TEMPLATE(class Data, class DSF)
247  (requires Sender<Data, is_single<>, is_flow<>>)
249 
250 PUSHMI_TEMPLATE(class Data, class DSF, class DEXF)
251  (requires Sender<Data, is_single<>, is_flow<>> && Invocable<DEXF&, Data&>)
253 #endif
254 
255 template<>
258 
259 
260 } // namespace pushmi
261 } // namespace folly
requires PUSHMI_EXP(lazy::Receiver< Out > PUSHMI_AND lazy::Invocable< DSF &, Data &, Out >)) void submit(Out out)
std::true_type True
Definition: TypeList.h:82
static any_executor< E > s_executor(data &)
constexpr flow_single_sender(Data data, DSF sf, DEXF exf)
any_flow_single_sender(Wrapped obj, std::false_type)
requires FlowSender< wrapped_t< Wrapped >, is_single<> > any_flow_single_sender(Wrapped obj) noexcept(insitu< Wrapped >())
any_flow_single_sender(Wrapped obj, std::true_type) noexcept
std::enable_if_t< PropertySet< __properties_t< property_set_traits< T >>>, __properties_t< property_set_traits< T >>> properties_t
Definition: properties.h:105
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
std::enable_if_t<!std::is_same< U, any_flow_single_sender >::value, U > wrapped_t
any_flow_single_sender(any_flow_single_sender &&that) noexcept
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
property_set_insert_t< properties_t< Data >, property_set< is_sender<>, is_flow<>, is_single<>>> properties
static constexpr vtable const noop_
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
typename std::enable_if_t< PropertySet< PS0 > &&PropertySet< PS1 >, detail::property_set_insert< PS0, PS1 >>::type property_set_insert_t
Definition: properties.h:153
void submit(any_flow_receiver< PE, std::ptrdiff_t, E, VN... > out)
bool_constant< true > true_type
Definition: gtest-port.h:2210
#define PUSHMI_AND
Definition: concept_def.h:424
void(* submit_)(data &, any_flow_receiver< PE, std::ptrdiff_t, E, VN... >)
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
static const char *const value
Definition: Conv.cpp:50
#define PUSHMI_INLINE_VAR
Definition: concept_def.h:60
std::integral_constant< bool, B > bool_
Definition: concept_def.h:443
char buffer_[sizeof(std::tuple< VN... >)]
requires requires(detail::apply_impl(std::declval< F >(), std::declval< Tuple >(), detail::tupidxs< Tuple >{}))) const expr decltype(auto) apply(F &&f
union folly::pushmi::any_flow_single_sender::data data_
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
PUSHMI_TEMPLATE(class E=std::exception_ptr, class Wrapped)(requires Sender< detail
Definition: executor.h:102
decltype(pe) PE
static set< string > s
const
Definition: upload.py:398
bool_constant< false > false_type
Definition: gtest-port.h:2209
#define PUSHMI_TYPE_CONSTRAINT(...)
Definition: concept_def.h:420
static void s_submit(data &, any_flow_receiver< PE, std::ptrdiff_t, E, VN... >)
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
#define PUSHMI_BROKEN_SUBSUMPTION(...)
Definition: concept_def.h:419
any_flow_single_sender & operator=(any_flow_single_sender &&that) noexcept
requires Receiver< Out > &&Invocable< SF &, Out > void submit(Out out)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::make_flow_single_sender_fn make_flow_single_sender