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 <optional> 20 : #include <exception> 21 : 22 : namespace openmodelviewer::core::async 23 : { 24 : /** 25 : * @brief Represents the outcome of an asynchronous task, either a success with data 26 : * or a failure with an exception. 27 : * 28 : * @tparam RType The type of the result data. 29 : */ 30 : template <typename RType> 31 : struct TaskResult 32 : { 33 : std::optional<RType> data; 34 : std::exception_ptr error = nullptr; 35 : 36 : /** 37 : * @brief Indicates whether the task completed successfully. 38 : */ 39 50 : inline bool success() const noexcept 40 : { 41 50 : return data.has_value(); 42 : } 43 : 44 : /** 45 : * @brief Indicates whether the task failed with an exception. 46 : */ 47 27 : inline bool errored() const noexcept 48 : { 49 27 : return error != nullptr; 50 : } 51 : 52 : /** 53 : * @brief Throws the stored exception if one exists. 54 : * @throws Any exception that was captured during the task. 55 : */ 56 3 : void rethrow() const 57 : { 58 3 : if (error) 59 : { 60 2 : std::rethrow_exception(error); 61 : } 62 1 : } 63 : }; 64 : } // namespace openmodelviewer::core::async