VladIstLove

Untitled

May 17th, 2021
579
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #include <await/futures/core/future.hpp>
  4.  
  5. #include <vector>
  6. #include <memory>
  7. #include <type_traits>
  8. #include <twist/stdlike/mutex.hpp>
  9.  
  10. namespace await::futures::detail {
  11.  
  12. // Generic algorithm
  13.  
  14. template <typename T>
  15. class BaseCombinator {
  16.  public:
  17.   virtual ~BaseCombinator() = default;
  18.   virtual void SetValue(T&& value) = 0;
  19.   virtual void SetError(wheels::Error error) = 0;
  20.   virtual void Finish() = 0;
  21. };
  22.  
  23. template <template <typename> class Combinator, typename T>
  24. void Set(wheels::Result<T>& res,
  25.          std::shared_ptr<Combinator<T>> shared_combinator) {
  26.   if (res.HasError()) {
  27.     shared_combinator->SetError(std::move(res.GetError()));
  28.   } else {
  29.     shared_combinator->SetValue(std::move(*res));
  30.   }
  31. }
  32.  
  33. template <template <typename> class Combinator, typename T>
  34. auto Combine(std::vector<Future<T>> inputs) {
  35.   auto shared_combinator = std::make_shared<Combinator<T>>();
  36.  
  37.   for (auto&& input : inputs) {
  38.     std::move(input).Subscribe(
  39.         [shared_combinator](wheels::Result<T> res) mutable {
  40.           Set<Combinator, T>(res, shared_combinator);
  41.         });
  42.   }
  43.  
  44.   return std::move(shared_combinator->MakeFuture());
  45. }
  46.  
  47. }  // namespace await::futures::detail
  48.  
RAW Paste Data