Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
future.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/async/details/profile.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
16 * operations. Move-only type that represents the eventual result of an async
17 * computation.
18 * @note All member functions are thread-safe.
19 * @tparam T The type of value the future will hold
20 */
21template <typename T>
22class Future {
23public:
24 Future() = default;
25 Future(const Future&) = delete;
26 Future(Future&& other) noexcept = default;
27 ~Future() = default;
28
29 Future& operator=(const Future&) = delete;
30 Future& operator=(Future&& other) noexcept = default;
31
32 /**
33 * @brief Blocks until the result is available and returns it.
34 * @details This method blocks the calling thread. Can only be called once.
35 * @return The result of the asynchronous operation
36 */
37 T Get();
38
39 /**
40 * @brief Attempts to cancel the associated task.
41 * @details May not succeed if the task has already started or completed.
42 * @return True if cancellation was successful, false otherwise
43 */
44 bool Cancel();
45
46 /**
47 * @brief Blocks until the result is available.
48 * @details Unlike `Get()`, this doesn't retrieve the result and can be called
49 * multiple times.
50 */
51 void Wait();
52
53 /**
54 * @brief Waits for the result to become available for the specified duration.
55 * @tparam Rep Duration representation type
56 * @tparam Period Duration period type
57 * @param rel_time Maximum time to wait
58 * @return Status indicating whether the result is ready, timeout occurred, or
59 * task was deferred
60 */
61 template <typename Rep, typename Period>
62 requires requires { typename std::chrono::duration<Rep, Period>; }
63 std::future_status WaitFor(
64 const std::chrono::duration<Rep, Period>& rel_time) const;
65
66 /**
67 * @brief Waits for the result to become available until the specified time
68 * point.
69 * @tparam Clock Clock type
70 * @tparam Duration Duration type
71 * @param abs_time Absolute time point to wait until
72 * @return Status indicating whether the result is ready, timeout occurred, or
73 * task was deferred
74 */
75 template <typename Clock, typename Duration>
76 requires requires { typename std::chrono::time_point<Clock, Duration>; }
77 std::future_status WaitUntil(
78 const std::chrono::time_point<Clock, Duration>& abs_time) const;
79
80 /**
81 * @brief Checks if the future has a shared state.
82 * @details A future becomes invalid after Get() is called or if it was
83 * default-constructed.
84 * @return True if the future is valid (has an associated task), false
85 * otherwise
86 */
87 [[nodiscard]] bool Valid() const { return future_.valid(); }
88
89private:
90 explicit Future(tf::Future<T> future) : future_(std::move(future)) {}
91
92 mutable tf::Future<T> future_;
93
94 friend class Executor;
95 friend class SubTaskGraph;
96};
97
98template <typename T>
99inline T Future<T>::Get() {
100 HELIOS_ASYNC_PROFILE_SCOPE_N("helios::async::Future::Get");
101 return future_.get();
102}
103
104template <typename T>
105inline bool Future<T>::Cancel() {
106 HELIOS_ASYNC_PROFILE_SCOPE_N("helios::async::Future::Cancel");
107 return future_.cancel();
108}
109
110template <typename T>
111inline void Future<T>::Wait() {
112 HELIOS_ASYNC_PROFILE_SCOPE_N("helios::async::Future::Wait");
113 future_.wait();
114}
115
116template <typename T>
117template <typename Rep, typename Period>
118 requires requires { typename std::chrono::duration<Rep, Period>; }
119inline std::future_status Future<T>::WaitFor(
120 const std::chrono::duration<Rep, Period>& rel_time) const {
121 HELIOS_ASYNC_PROFILE_SCOPE_N("helios::async::Future::WaitFor");
122 return future_.wait_for(rel_time);
123}
124
125template <typename T>
126template <typename Clock, typename Duration>
127 requires requires { typename std::chrono::time_point<Clock, Duration>; }
128inline std::future_status Future<T>::WaitUntil(
129 const std::chrono::time_point<Clock, Duration>& abs_time) const {
130 HELIOS_ASYNC_PROFILE_SCOPE_N("helios::async::Future::WaitUntil");
131 return future_.wait_until(abs_time);
132}
133
134} // namespace helios::async
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:128
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:119
friend class Executor
Definition future.hpp:94
Future(Future &&other) noexcept=default
bool Cancel()
Attempts to cancel the associated task.
Definition future.hpp:105
T Get()
Blocks until the result is available and returns it.
Definition future.hpp:99
Future & operator=(const Future &)=delete
bool Valid() const
Checks if the future has a shared state.
Definition future.hpp:87
void Wait()
Blocks until the result is available.
Definition future.hpp:111
friend class SubTaskGraph
Definition future.hpp:95
STL namespace.