3 #include <folly/futures/Future.h>
4 #include <folly/Executor.h>
20 public std::enable_shared_from_this<Generator<T>>
30 virtual folly::Future<T>
next(folly::Executor* executor =
nullptr) = 0;
39 template <
typename C>
static char test(
typename C::ValueType*);
40 template <
typename C>
static long test(...);
41 static const bool value =
sizeof(test<T>(0)) ==
sizeof(char);
44 template <typename T, bool = HasValueType<T>::value>
47 static const bool value =
false;
51 struct IsGenerator<T, true>
53 typedef typename T::ValueType type;
54 static const bool value = std::is_base_of<Generator<type>, T>::value;
61 template <
typename GeneratorType>
65 static_assert(IsGenerator<GeneratorType>::value,
66 "Need to use Generator or its subclass as a type "
67 "parameter for SynchronousGenerator.");
68 typedef typename GeneratorType::ValueType ValueType;
70 virtual folly::Future<ValueType> next(folly::Executor* executor =
nullptr)
override;
84 template <
typename GeneratorType>
85 folly::Future<typename SynchronousGenerator<GeneratorType>::ValueType>
90 auto ptr = this->shared_from_this();
92 std::shared_ptr<Me> me = std::dynamic_pointer_cast<Me>(ptr);
94 return folly::makeFuture<ValueType>(ValueType());
96 std::weak_ptr<Me> weak = me;
98 auto func = [weak]() -> ValueType {
99 std::shared_ptr<Me> me = weak.lock();
101 std::lock_guard<std::mutex> g(me->m_mutex);
102 return me->generateNextItem();
109 return folly::via(executor, func);
111 return folly::makeFutureWith(func);