Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
async_task.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <taskflow/core/async_task.hpp>
6
7#include <cstddef>
8#include <utility>
9
10namespace helios::async {
11
12/**
13 * @brief Handle to an asynchronous task managed by the `Executor`.
14 * @details Wraps a `tf::AsyncTask`, providing methods to query task state and
15 * manage its lifecycle. Instances are typically returned by `Executor`'s
16 * asynchronous methods.
17 * @note Thread-safe.
18 */
19class AsyncTask {
20public:
21 AsyncTask() = default;
22 AsyncTask(const AsyncTask&) = default;
23 AsyncTask(AsyncTask&&) noexcept = default;
24 ~AsyncTask() = default;
25
26 AsyncTask& operator=(const AsyncTask&) = default;
27 AsyncTask& operator=(AsyncTask&&) noexcept = default;
28
29 /// @brief Resets the underlying asynchronous task handle.
30 void Reset() { task_.reset(); }
31
32 bool operator==(const AsyncTask& other) const noexcept {
33 return task_.hash_value() == other.task_.hash_value();
34 }
35
36 bool operator!=(const AsyncTask& other) const noexcept {
37 return !(*this == other);
38 }
39
40 /**
41 * @brief Checks if the asynchronous task has completed.
42 * @return True if the task is done, false otherwise.
43 * @details Returns false if task is empty.
44 */
45 [[nodiscard]] bool Done() const;
46
47 /**
48 * @brief Checks if the task handle is empty (not associated with any task).
49 * @return True if empty, false otherwise.
50 */
51 [[nodiscard]] bool Empty() const { return task_.empty(); }
52
53 /**
54 * @brief Returns a hash value for the task.
55 * @return Hash value.
56 * @details Returns '0' if task is empty.
57 */
58 [[nodiscard]] size_t Hash() const;
59
60 /**
61 * @brief Returns the number of references to the underlying task.
62 * @return Reference count.
63 * @details Returns '0' if task is empty.
64 */
65 [[nodiscard]] size_t UseCount() const { return task_.use_count(); }
66
67 /**
68 * @brief Gets the type of the task.
69 * @return TaskType::Async
70 */
71 [[nodiscard]] static constexpr TaskType GetTaskType() noexcept {
72 return TaskType::kAsync;
73 }
74
75private:
76 explicit AsyncTask(tf::AsyncTask task) : task_(std::move(task)) {}
77
78 [[nodiscard]] tf::AsyncTask& UnderlyingTask() noexcept { return task_; }
79 [[nodiscard]] const tf::AsyncTask& UnderlyingTask() const noexcept {
80 return task_;
81 }
82
83 tf::AsyncTask task_;
84
85 friend class Executor;
86 friend class SubTaskGraph;
87};
88
89inline bool AsyncTask::Done() const {
90 if (Empty()) [[unlikely]] {
91 return false;
92 }
93
94 return task_.is_done();
95}
96
97inline size_t AsyncTask::Hash() const {
98 if (Empty()) [[unlikely]] {
99 return 0;
100 }
101
102 return task_.hash_value();
103}
104
105} // namespace helios::async
AsyncTask(AsyncTask &&) noexcept=default
size_t UseCount() const
Returns the number of references to the underlying task.
void Reset()
Resets the underlying asynchronous task handle.
size_t Hash() const
Returns a hash value for the task.
bool Done() const
Checks if the asynchronous task has completed.
bool Empty() const
Checks if the task handle is empty (not associated with any task).
bool operator==(const AsyncTask &other) const noexcept
AsyncTask(const AsyncTask &)=default
bool operator!=(const AsyncTask &other) const noexcept
static constexpr TaskType GetTaskType() noexcept
Gets the type of the task.
TaskType
Types of tasks in the async system.
Definition common.hpp:18
@ kAsync
Asynchronous task executed independently.
Definition common.hpp:22
STL namespace.