Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
common.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/core_pch.hpp>
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/**
18 * @brief Types of tasks in the async system.
19 */
20enum class TaskType : uint8_t {
22 Static, ///< Static task with fixed callable
23 SubTask, ///< Dynamic task that can spawn subtasks
24 Async ///< Asynchronous task executed independently
25};
26
27/**
28 * @brief Error codes for async operations.
29 */
30enum class AsyncError : uint8_t {
31 InvalidTask, ///< Task handle is invalid or empty
32 ExecutorShutdown, ///< Executor has been shut down
33 TaskNotFound, ///< Specified task could not be found
34 InvalidDependency, ///< Dependency relationship is invalid
35 CircularDependency, ///< Circular dependency detected in task graph
36 SchedulingFailed, ///< Task could not be scheduled for execution
37 ThreadNotAvailable ///< No worker thread available for execution
38};
39
40/**
41 * @brief Converts AsyncError to string representation.
42 */
43[[nodiscard]] constexpr std::string_view ToString(AsyncError error) noexcept {
44 switch (error) {
46 return "Invalid task";
48 return "Executor is shutdown";
50 return "Task not found";
52 return "Invalid dependency";
54 return "Circular dependency detected";
56 return "Task scheduling failed";
58 return "Thread not available";
59 default:
60 return "Unknown async error";
61 }
62}
63
64/**
65 * @brief Result type for async operations.
66 */
67template <typename T = void>
68using AsyncResult = std::expected<T, AsyncError>;
69
70/**
71 * @brief Concept for static task callables.
72 */
73template <typename C>
74concept StaticTask = std::invocable<C> && std::same_as<std::invoke_result_t<C>, void>;
75
76/**
77 * @brief Concept for sub-task callables that receive a SubTaskGraph reference.
78 */
79template <typename C>
80concept SubTask = std::invocable<C, SubTaskGraph&>;
81
82/**
83 * @brief Concept for any valid task callable.
84 */
85template <typename C>
87
88namespace details {
89
90/**
91 * @brief Converts Taskflow task type to Helios task type.
92 * @param type Taskflow task type
93 * @return Corresponding Helios task type
94 */
95[[nodiscard]] static constexpr TaskType ConvertTaskType(tf::TaskType type) noexcept {
96 switch (type) {
97 case tf::TaskType::STATIC:
98 return TaskType::Static;
99 case tf::TaskType::SUBFLOW:
100 return TaskType::SubTask;
101 case tf::TaskType::ASYNC:
102 return TaskType::Async;
103 default:
104 return TaskType::Static; // Fallback to Static if unknown
105 }
106}
107
108} // namespace details
109
110} // namespace helios::async
Concept for any valid task callable.
Definition common.hpp:86
Concept for static task callables.
Definition common.hpp:74
Concept for sub-task callables that receive a SubTaskGraph reference.
Definition common.hpp:80
static constexpr TaskType ConvertTaskType(tf::TaskType type) noexcept
Converts Taskflow task type to Helios task type.
Definition common.hpp:95
AsyncError
Error codes for async operations.
Definition common.hpp:30
@ ThreadNotAvailable
No worker thread available for execution.
@ SchedulingFailed
Task could not be scheduled for execution.
@ CircularDependency
Circular dependency detected in task graph.
@ TaskNotFound
Specified task could not be found.
@ ExecutorShutdown
Executor has been shut down.
@ InvalidDependency
Dependency relationship is invalid.
@ InvalidTask
Task handle is invalid or empty.
TaskType
Types of tasks in the async system.
Definition common.hpp:20
@ Async
Asynchronous task executed independently.
@ Static
Static task with fixed callable.
@ SubTask
Dynamic task that can spawn subtasks.
constexpr std::string_view ToString(AsyncError error) noexcept
Converts AsyncError to string representation.
Definition common.hpp:43
std::expected< T, AsyncError > AsyncResult
Result type for async operations.
Definition common.hpp:68