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/id/unique_id.hpp" 20 : 21 : #include <functional> 22 : 23 : namespace openmodelviewer::core::resource 24 : { 25 : /** 26 : * @brief Tag type used to distinguish resource IDs from other unique ID types. 27 : */ 28 : struct ResourceTag {}; 29 : 30 : /** 31 : * @brief Type alias for unique resource identifiers. 32 : * 33 : * This ID is unique per resource and is used for identification and comparison. 34 : */ 35 : using ResourceId = openmodelviewer::core::id::UniqueId<ResourceTag>; 36 : 37 : /** 38 : * @brief Lightweight handle used to identify a resource. 39 : */ 40 : struct ResourceHandle 41 : { 42 : ResourceId id; ///< Unique identifier of the resource. 43 : }; 44 : 45 : /** 46 : * @brief Equality comparison operator. 47 : * 48 : * Compares two ResourceHandles based on their ResourceId. 49 : * 50 : * @param lhs Left-hand side ResourceHandle. 51 : * @param rhs Right-hand side ResourceHandle. 52 : * @return True if both handles refer to the same resource. 53 : */ 54 83 : inline bool operator==(const ResourceHandle& lhs, const ResourceHandle& rhs) noexcept 55 : { 56 83 : return lhs.id == rhs.id; 57 : } 58 : 59 : /** 60 : * @brief Inequality comparison operator. 61 : * 62 : * Logical negation of operator==. Returns true if the handles refer to different resources. 63 : * 64 : * @param lhs Left-hand side ResourceHandle. 65 : * @param rhs Right-hand side ResourceHandle. 66 : * @return True if the handles refer to different resources. 67 : */ 68 3 : inline bool operator!=(const ResourceHandle& lhs, const ResourceHandle& rhs) noexcept 69 : { 70 3 : return !(lhs == rhs); 71 : } 72 : } 73 : 74 : 75 : namespace std 76 : { 77 : /** 78 : * @brief Hash specialization for ResourceHandle to enable use in unordered containers. 79 : * 80 : * This hash implementation delegates to the hash of the underlying ResourceId. 81 : * It ensures ResourceHandle can be used as a key in std::unordered_map or std::unordered_set. 82 : */ 83 : template<> 84 : struct hash<openmodelviewer::core::resource::ResourceHandle> 85 : { 86 182 : inline std::size_t operator()(const openmodelviewer::core::resource::ResourceHandle& handle) const noexcept 87 : { 88 182 : return std::hash<openmodelviewer::core::resource::ResourceId>{}(handle.id); 89 : } 90 : }; 91 : } // namespace openmodelviewer::core::async