Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
future.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/core_pch.hpp>
4
5#include <taskflow/taskflow.hpp>
6
7#include <chrono>
8#include <future>
9
10namespace helios::async {
11
12/**
13 * @class Future
14 * @brief Wrapper around tf::Future for handling asynchronous task results.
15 * @details Provides methods to wait for and retrieve results from asynchronous operations.
16 * Move-only type that represents the eventual result of an async computation.
17 * @note All member functions are thread-safe.
18 * @tparam T The type of value the future will hold
19 */
20template <typename T>
21class Future {
22public:
23 Future() = default;
24 Future(const Future&) = delete;
25 Future(Future&& other) noexcept = default;
26 ~Future() = default;
27
28 Future& operator=(const Future&) = delete;
29 Future& operator=(Future&& other) noexcept = default;
30
31 /**
32 * @brief Blocks until the result is available and returns it.
33 * @details This method blocks the calling thread. Can only be called once.
34 * @return The result of the asynchronous operation
35 */
36 T Get() { return future_.get(); }
37
38 /**
39 * @brief Attempts to cancel the associated task.
40 * @details May not succeed if the task has already started or completed.
41 * @return True if cancellation was successful, false otherwise
42 */
43 bool Cancel() { return future_.cancel(); }
44
45 /**
46 * @brief Blocks until the result is available.
47 * @details Unlike Get(), this doesn't retrieve the result and can be called multiple times.
48 */
49 void Wait() { future_.wait(); }
50
51 /**
52 * @brief Waits for the result to become available for the specified duration.
53 * @tparam Rep Duration representation type
54 * @tparam Period Duration period type
55 * @param rel_time Maximum time to wait
56 * @return Status indicating whether the result is ready, timeout occurred, or task was deferred
57 */
58 template <typename Rep, typename Period>
59 requires requires { typename std::chrono::duration<Rep, Period>; }
60 std::future_status WaitFor(const std::chrono::duration<Rep, Period>& rel_time) const {
61 return future_.wait_for(rel_time);
62 }
63
64 /**
65 * @brief Waits for the result to become available until the specified time point.
66 * @tparam Clock Clock type
67 * @tparam Duration Duration type
68 * @param abs_time Absolute time point to wait until
69 * @return Status indicating whether the result is ready, timeout occurred, or task was deferred
70 */
71 template <typename Clock, typename Duration>
72 requires requires { typename std::chrono::time_point<Clock, Duration>; }
73 std::future_status WaitUntil(const std::chrono::time_point<Clock, Duration>& abs_time) const {
74 return future_.wait_until(abs_time);
75 }
76
77 /**
78 * @brief Checks if the future has a shared state.
79 * @details A future becomes invalid after Get() is called or if it was default-constructed.
80 * @return True if the future is valid (has an associated task), false otherwise
81 */
82 [[nodiscard]] bool Valid() const { return future_.valid(); }
83
84private:
85 explicit Future(tf::Future<T> future) : future_(std::move(future)) {}
86
87 mutable tf::Future<T> future_;
88
89 friend class Executor;
90 friend class SubTaskGraph;
91};
92
93} // namespace helios::async
Manages worker threads and executes task graphs using work-stealing scheduling.
Definition executor.hpp:33
Wrapper around tf::Future for handling asynchronous task results.
Definition future.hpp:21
Future & operator=(Future &&other) noexcept=default
Future(const Future &)=delete
std::future_status WaitUntil(const std::chrono::time_point< Clock, Duration > &abs_time) const
Waits for the result to become available until the specified time point.
Definition future.hpp:73
std::future_status WaitFor(const std::chrono::duration< Rep, Period > &rel_time) const
Waits for the result to become available for the specified duration.
Definition future.hpp:60
Future(Future &&other) noexcept=default
bool Cancel()
Attempts to cancel the associated task.
Definition future.hpp:43
T Get()
Blocks until the result is available and returns it.
Definition future.hpp:36
Future & operator=(const Future &)=delete
bool Valid() const
Checks if the future has a shared state.
Definition future.hpp:82
void Wait()
Blocks until the result is available.
Definition future.hpp:49
Dynamic task graph that can be created within the execution of a task.
STL namespace.