Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:53

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // Copyright (c) Lewis Baker
0003 // Licenced under MIT license. See LICENSE.txt for details.
0004 ///////////////////////////////////////////////////////////////////////////////
0005 #ifndef CPPCORO_WHEN_ALL_READY_HPP_INCLUDED
0006 #define CPPCORO_WHEN_ALL_READY_HPP_INCLUDED
0007 
0008 #include <cppcoro/config.hpp>
0009 #include <cppcoro/awaitable_traits.hpp>
0010 #include <cppcoro/is_awaitable.hpp>
0011 
0012 #include <cppcoro/detail/when_all_ready_awaitable.hpp>
0013 #include <cppcoro/detail/when_all_task.hpp>
0014 #include <cppcoro/detail/unwrap_reference.hpp>
0015 
0016 #include <tuple>
0017 #include <utility>
0018 #include <vector>
0019 #include <type_traits>
0020 
0021 namespace cppcoro
0022 {
0023     template<
0024         typename... AWAITABLES,
0025         std::enable_if_t<std::conjunction_v<
0026             is_awaitable<detail::unwrap_reference_t<std::remove_reference_t<AWAITABLES>>>...>, int> = 0>
0027     [[nodiscard]]
0028     CPPCORO_FORCE_INLINE auto when_all_ready(AWAITABLES&&... awaitables)
0029     {
0030         return detail::when_all_ready_awaitable<std::tuple<detail::when_all_task<
0031             typename awaitable_traits<detail::unwrap_reference_t<std::remove_reference_t<AWAITABLES>>>::await_result_t>...>>(
0032                 std::make_tuple(detail::make_when_all_task(std::forward<AWAITABLES>(awaitables))...));
0033     }
0034 
0035     // TODO: Generalise this from vector<AWAITABLE> to arbitrary sequence of awaitable.
0036 
0037     template<
0038         typename AWAITABLE,
0039         typename RESULT = typename awaitable_traits<detail::unwrap_reference_t<AWAITABLE>>::await_result_t>
0040     [[nodiscard]] auto when_all_ready(std::vector<AWAITABLE> awaitables)
0041     {
0042         std::vector<detail::when_all_task<RESULT>> tasks;
0043 
0044         tasks.reserve(awaitables.size());
0045 
0046         for (auto& awaitable : awaitables)
0047         {
0048             tasks.emplace_back(detail::make_when_all_task(std::move(awaitable)));
0049         }
0050 
0051         return detail::when_all_ready_awaitable<std::vector<detail::when_all_task<RESULT>>>(
0052             std::move(tasks));
0053     }
0054 }
0055 
0056 #endif