MT Showcase SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
ValueStream.hpp
1 #pragma once
2 
3 #include <folly/futures/Future.h>
4 
5 #include <folly/Executor.h>
6 
7 
8 namespace Showcase
9 {
10 
11  class Showcase;
12 
14  template <typename T>
15  class ValueStream : public std::enable_shared_from_this<ValueStream<T>>
16  {
17  public:
18  typedef T ValueType;
19  ValueStream() {}
20  virtual ~ValueStream() {}
21 
27  int forEachSync(const std::function<void(const T&)>& f, int N,
28  const std::function<bool(const T&)>& endCond=nullptr);
29 
30  ValueStream(const ValueStream&) = delete;
31  ValueStream& operator=(const ValueStream&) = delete;
32 
33  virtual folly::Future<T> next() = 0;
34  };
35 
36  template <typename T>
37  using ValueStreamPtr = std::shared_ptr<ValueStream<T>>;
38 
39 
40  // ---------------------------------------------------------------------
41  // ---------------------------------------------------------------------
42  // ---------------------------------------------------------------------
43 
44 
45  template <typename T>
46  int ValueStream<T>::forEachSync(const std::function<void(const T&)>& f, int max,
47  const std::function<bool(const T&)>& endCond)
48  {
49  int consumed = 0;
50 
51  while(consumed < max) {
52  auto future = next().wait();
53 
54  ValueType ev = future.value();
55  if(endCond && endCond(ev))
56  break;
57 
58  f(ev);
59  ++consumed;
60  }
61  return consumed;
62  }
63 
64 }