Line data Source code
1 : // 2 : // Copyright 2024 OpenModelViewer Authors 3 : // 4 : // Licensed under the Apache License, Version 2.0 (the "License"); 5 : // you may not use this file except in compliance with the License. 6 : // You may obtain a copy of the License at 7 : // 8 : // http://www.apache.org/licenses/LICENSE-2.0 9 : // 10 : // Unless required by applicable law or agreed to in writing, software 11 : // distributed under the License is distributed on an "AS IS" BASIS, 12 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 : // See the License for the specific language governing permissions and 14 : // limitations under the License. 15 : // 16 : 17 : #pragma once 18 : 19 : #include "openmodelviewer/core/async/task_type.hpp" 20 : #include "openmodelviewer/core/id/unique_id.hpp" 21 : 22 : #include <functional> 23 : 24 : namespace openmodelviewer::core::async 25 : { 26 : /** 27 : * @brief Tag type used to distinguish task IDs from other unique ID types. 28 : */ 29 : struct TaskTag {}; 30 : 31 : /** 32 : * @brief Type alias for unique task identifiers. 33 : * 34 : * This ID is unique per task and is used for identification and comparison. 35 : */ 36 : using TaskId = openmodelviewer::core::id::UniqueId<TaskTag>; 37 : 38 : /** 39 : * @brief Lightweight handle used to identify a task submitted to the TaskScheduler. 40 : * 41 : * Contains both the unique task ID and the type of execution context 42 : * (e.g., IO thread, main thread, worker thread). 43 : */ 44 : struct TaskHandle 45 : { 46 : TaskId id; ///< Unique identifier of the task. 47 : TaskType type; ///< Type of thread or queue where the task is scheduled. 48 : }; 49 : 50 : /** 51 : * @brief Equality comparison operator. 52 : * 53 : * Compares two TaskHandles based on their TaskId. 54 : * 55 : * @param lhs Left-hand side TaskHandle. 56 : * @param rhs Right-hand side TaskHandle. 57 : * @return True if both handles refer to the same task. 58 : */ 59 168 : inline bool operator==(const TaskHandle& lhs, const TaskHandle& rhs) noexcept 60 : { 61 168 : return lhs.id == rhs.id; 62 : } 63 : 64 : /** 65 : * @brief Inequality comparison operator. 66 : * 67 : * Logical negation of operator==. Returns true if the handles refer to different tasks. 68 : * 69 : * @param lhs Left-hand side TaskHandle. 70 : * @param rhs Right-hand side TaskHandle. 71 : * @return True if the handles refer to different tasks. 72 : */ 73 2 : inline bool operator!=(const TaskHandle& lhs, const TaskHandle& rhs) noexcept 74 : { 75 2 : return !(lhs == rhs); 76 : } 77 : } 78 : 79 : 80 : namespace std 81 : { 82 : /** 83 : * @brief Hash specialization for TaskHandle to enable use in unordered containers. 84 : * 85 : * This hash implementation delegates to the hash of the underlying TaskId. 86 : * It ensures TaskHandle can be used as a key in std::unordered_map or std::unordered_set. 87 : */ 88 : template<> 89 : struct hash<openmodelviewer::core::async::TaskHandle> 90 : { 91 353 : inline std::size_t operator()(const openmodelviewer::core::async::TaskHandle& handle) const noexcept 92 : { 93 353 : return std::hash<openmodelviewer::core::async::TaskId>{}(handle.id); 94 : } 95 : }; 96 : } // namespace openmodelviewer::core::async