| 1 | /* Failure observers for outcome type |
| 2 | (C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (7 commits) |
| 3 | File Created: Oct 2017 |
| 4 | |
| 5 | |
| 6 | Boost Software License - Version 1.0 - August 17th, 2003 |
| 7 | |
| 8 | Permission is hereby granted, free of charge, to any person or organization |
| 9 | obtaining a copy of the software and accompanying documentation covered by |
| 10 | this license (the "Software") to use, reproduce, display, distribute, |
| 11 | execute, and transmit the Software, and to prepare derivative works of the |
| 12 | Software, and to permit third-parties to whom the Software is furnished to |
| 13 | do so, all subject to the following: |
| 14 | |
| 15 | The copyright notices in the Software and this entire statement, including |
| 16 | the above license grant, this restriction and the following disclaimer, |
| 17 | must be included in all copies of the Software, in whole or in part, and |
| 18 | all derivative works of the Software, unless such copies or derivative |
| 19 | works are solely in the form of machine-executable object code generated by |
| 20 | a source language processor. |
| 21 | |
| 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 25 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 26 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 27 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 28 | DEALINGS IN THE SOFTWARE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef BOOST_OUTCOME_BASIC_OUTCOME_FAILURE_OBSERVERS_HPP |
| 32 | #define BOOST_OUTCOME_BASIC_OUTCOME_FAILURE_OBSERVERS_HPP |
| 33 | |
| 34 | #include "basic_result_storage.hpp" |
| 35 | |
| 36 | #include <exception> |
| 37 | |
| 38 | BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN |
| 39 | |
| 40 | namespace detail |
| 41 | { |
| 42 | namespace adl |
| 43 | { |
| 44 | struct search_detail_adl |
| 45 | { |
| 46 | }; |
| 47 | // Do NOT use template requirements here! |
| 48 | template <class S, typename = decltype(basic_outcome_failure_exception_from_error(std::declval<S>()))> |
| 49 | inline auto _delayed_lookup_basic_outcome_failure_exception_from_error(const S &ec, search_detail_adl /*unused*/) |
| 50 | { |
| 51 | // ADL discovered |
| 52 | return basic_outcome_failure_exception_from_error(ec); |
| 53 | } |
| 54 | } // namespace adl |
| 55 | #if defined(_MSC_VER) && _MSC_VER <= 1923 // VS2019 |
| 56 | // VS2017 and VS2019 with /permissive- chokes on the correct form due to over eager early instantiation. |
| 57 | template <class S, class P> inline void _delayed_lookup_basic_outcome_failure_exception_from_error(...) { static_assert(sizeof(S) == 0, "No specialisation for these error and exception types available!" ); } |
| 58 | #else |
| 59 | template <class S, class P> inline void _delayed_lookup_basic_outcome_failure_exception_from_error(...) = delete; // NOLINT No specialisation for these error and exception types available! |
| 60 | #endif |
| 61 | |
| 62 | template <class exception_type> inline exception_type current_exception_or_fatal(std::exception_ptr e) { std::rethrow_exception(e); } |
| 63 | template <> inline std::exception_ptr current_exception_or_fatal<std::exception_ptr>(std::exception_ptr e) { return e; } |
| 64 | |
| 65 | template <class Base, class R, class S, class P, class NoValuePolicy> class basic_outcome_failure_observers : public Base |
| 66 | { |
| 67 | public: |
| 68 | using exception_type = P; |
| 69 | using Base::Base; |
| 70 | |
| 71 | exception_type failure() const noexcept |
| 72 | { |
| 73 | #ifndef BOOST_NO_EXCEPTIONS |
| 74 | try |
| 75 | #endif |
| 76 | { |
| 77 | if(this->_state._status.have_exception()) |
| 78 | { |
| 79 | return this->assume_exception(); |
| 80 | } |
| 81 | if(this->_state._status.have_error()) |
| 82 | { |
| 83 | return _delayed_lookup_basic_outcome_failure_exception_from_error(this->assume_error(), adl::search_detail_adl()); |
| 84 | } |
| 85 | return exception_type(); |
| 86 | } |
| 87 | #ifndef BOOST_NO_EXCEPTIONS |
| 88 | catch(...) |
| 89 | { |
| 90 | // Return the failure if exception_type is std::exception_ptr, |
| 91 | // otherwise terminate same as throwing an exception inside noexcept |
| 92 | return current_exception_or_fatal<exception_type>(std::current_exception()); |
| 93 | } |
| 94 | #endif |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | } // namespace detail |
| 99 | |
| 100 | BOOST_OUTCOME_V2_NAMESPACE_END |
| 101 | |
| 102 | #endif |
| 103 | |