3131
3232namespace paimon {
3333
34- // / Submits a function to be executed asynchronously on a given executor and returns a future.
34+ // / Submits a function to be executed asynchronously on a given executor and returns a
35+ // / future.
3536// /
36- // / This function wraps the provided callable and submits it to the provided executor. The function
37- // / captures the result using a std::promise, which is used to fulfill the returned std::future. If
38- // / the callable throws an exception, the exception is captured and set in the promise.
37+ // / This function wraps the provided callable (`func`) and submits it to the provided `executor`.
38+ // / The function captures the result of `func` using a `std::promise`, which is used to fulfill
39+ // / the returned `std::future`. If the callable throws an exception, the exception is captured
40+ // / and set in the `promise`.
41+ // /
42+ // / @tparam Func The type of the callable function.
43+ // / @param executor The executor to run the function on. Must provide an `Add` method for task
44+ // / submission.
45+ // / @param func The function to execute asynchronously. Can be any callable object.
46+ // / @return std::future<decltype(func())> A future that holds the result of the function
47+ // / execution.
48+ // /
49+ // / @note If `func` returns `void`, the returned future is of type `std::future<void>`.
3950template <typename Func>
40- auto Via (Executor* executor, Func&& func) -> std::future<std::invoke_result_t<Func> > {
41- using ResultType = std:: invoke_result_t <Func> ;
51+ auto Via (Executor* executor, Func&& func) -> std::future<decltype(func()) > {
52+ using ResultType = decltype ( func ()) ;
4253
54+ // Check if func is callable (invocable)
4355 static_assert (std::is_invocable_v<Func>, " func must be callable" );
4456
57+ // Check if func is an empty std::function
4558 if constexpr (std::is_constructible_v<std::function<void ()>, Func>) {
4659 std::function<void ()> test_func = func;
4760 if (!test_func) {
4861 assert (false && " func cannot be an empty std::function" );
4962 }
5063 }
5164
65+ // Create a promise to store the result or exception.
5266 auto promise = std::make_shared<std::promise<ResultType>>();
53- auto future = promise->get_future ();
67+ auto future = promise->get_future (); // Retrieve the future associated with the promise.
5468
69+ // Wrap the task and submit it to the executor.
5570 executor->Add ([promise, func = std::forward<Func>(func)]() mutable {
5671 try {
5772 if constexpr (std::is_void_v<ResultType>) {
@@ -69,18 +84,34 @@ auto Via(Executor* executor, Func&& func) -> std::future<std::invoke_result_t<Fu
6984}
7085
7186// / Collects the results of multiple futures.
87+ // /
88+ // / This function waits for all provided futures to complete and collects their results.
89+ // / The results are returned in a `std::vector`, preserving the order of the input futures.
90+ // /
91+ // / @tparam T The type of the result stored in the futures.
92+ // / @param futures A container of futures (e.g., std::vector<std::future<T>>).
93+ // / @return std::vector<T> A vector of results corresponding to the input futures.
94+ // /
95+ // / @note If `T` is `void`, use Wait function instead.
7296template <typename T>
7397std::vector<T> CollectAll (std::vector<std::future<T>>& futures) {
7498 std::vector<T> results;
75- results.reserve (futures.size ());
99+ results.reserve (futures.size ()); // Reserve space to avoid reallocation.
76100 for (auto & future : futures) {
77- results.push_back (future.get ());
101+ results.push_back (future.get ()); // Wait for each future and collect the result.
78102 }
79103
80104 return results;
81105}
82106
83- // / Waits for all futures with void return type to complete.
107+ // / Waits for all futures with `void` return type to complete.
108+ // /
109+ // / This function waits for each provided `std::future<void>` to complete.
110+ // / It ensures that all asynchronous operations have finished execution.
111+ // /
112+ // / @param futures A container of `std::future<void>` (e.g., `std::vector<std::future<void>>`).
113+ // /
114+ // / @note Use this function when dealing with futures that do not return a value.
84115inline void Wait (std::vector<std::future<void >>& futures) {
85116 for (auto & future : futures) {
86117 if (future.valid ()) {
0 commit comments