Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
common.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4
5#include <taskflow/core/task.hpp>
6
7#include <concepts>
8#include <cstdint>
9#include <expected>
10#include <string_view>
11#include <type_traits>
12
13namespace helios::async {
14
15class SubTaskGraph;
16
17/// @brief Types of tasks in the async system.
18enum class TaskType : uint8_t {
20 kStatic, ///< Static task with fixed callable
21 kSubTask, ///< Dynamic task that can spawn subtasks
22 kAsync ///< Asynchronous task executed independently
23};
24
25/// @brief Error codes for async operations.
26enum class AsyncError : uint8_t {
27 kInvalidTask, ///< Task handle is invalid or empty
28 kExecutorShutdown, ///< Executor has been shut down
29 kTaskNotFound, ///< Specified task could not be found
30 kInvalidDependency, ///< Dependency relationship is invalid
31 kCircularDependency, ///< Circular dependency detected in task graph
32 kSchedulingFailed, ///< Task could not be scheduled for execution
33 kThreadNotAvailable ///< No worker thread available for execution
34};
35
36/**
37 * @brief Converts `AsyncError` to string representation.
38 * @param error AsyncError value to convert
39 * @return String view describing the error
40 */
41[[nodiscard]] constexpr std::string_view ToString(AsyncError error) noexcept {
42 switch (error) {
44 return "Invalid task";
46 return "Executor is shutdown";
48 return "Task not found";
50 return "Invalid dependency";
52 return "Circular dependency detected";
54 return "Task scheduling failed";
56 return "Thread not available";
57 default:
58 return "Unknown async error";
59 }
60}
61
62/// @brief Result type for async operations.
63template <typename T = void>
64using AsyncResult = std::expected<T, AsyncError>;
65
66/// @brief Concept for static task callables.
67template <typename C>
68concept StaticTask =
69 std::invocable<C> && std::same_as<std::invoke_result_t<C>, void>;
70
71/// @brief Concept for sub-task callables that receive a SubTaskGraph reference.
72template <typename C>
73concept SubTask = std::invocable<C, SubTaskGraph&>;
74
75/// @brief Concept for any valid task callable.
76template <typename C>
78
79namespace details {
80
81/**
82 * @brief Converts Taskflow task type to Helios task type.
83 * @param type Taskflow task type
84 * @return Corresponding Helios task type
85 */
86[[nodiscard]] static constexpr TaskType ConvertTaskType(
87 tf::TaskType type) noexcept {
88 switch (type) {
89 case tf::TaskType::STATIC:
90 return TaskType::kStatic;
91 case tf::TaskType::SUBFLOW:
92 return TaskType::kSubTask;
93 case tf::TaskType::ASYNC:
94 return TaskType::kAsync;
95 default:
96 return TaskType::kStatic; // Fallback to Static if unknown
97 }
98}
99
100} // namespace details
101
102} // namespace helios::async
Dynamic task graph that can be created within the execution of a task.
Concept for any valid task callable.
Definition common.hpp:77
Concept for static task callables.
Definition common.hpp:68
Concept for sub-task callables that receive a SubTaskGraph reference.
Definition common.hpp:73
static constexpr TaskType ConvertTaskType(tf::TaskType type) noexcept
Converts Taskflow task type to Helios task type.
Definition common.hpp:86
AsyncError
Error codes for async operations.
Definition common.hpp:26
@ kTaskNotFound
Specified task could not be found.
Definition common.hpp:29
@ kThreadNotAvailable
No worker thread available for execution.
Definition common.hpp:33
@ kExecutorShutdown
Executor has been shut down.
Definition common.hpp:28
@ kInvalidDependency
Dependency relationship is invalid.
Definition common.hpp:30
@ kSchedulingFailed
Task could not be scheduled for execution.
Definition common.hpp:32
@ kInvalidTask
Task handle is invalid or empty.
Definition common.hpp:27
@ kCircularDependency
Circular dependency detected in task graph.
Definition common.hpp:31
TaskType
Types of tasks in the async system.
Definition common.hpp:18
@ kSubTask
Dynamic task that can spawn subtasks.
Definition common.hpp:21
@ kAsync
Asynchronous task executed independently.
Definition common.hpp:22
@ kStatic
Static task with fixed callable.
Definition common.hpp:20
constexpr std::string_view ToString(AsyncError error) noexcept
Converts AsyncError to string representation.
Definition common.hpp:41
std::expected< T, AsyncError > AsyncResult
Result type for async operations.
Definition common.hpp:64