| 1 | /* Proposed SG14 status_code |
| 2 | (C) 2018 - 2022 Niall Douglas <http://www.nedproductions.biz/> (5 commits) |
| 3 | File Created: Feb 2018 |
| 4 | |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License in the accompanying file |
| 9 | Licence.txt or at |
| 10 | |
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | Unless required by applicable law or agreed to in writing, software |
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | See the License for the specific language governing permissions and |
| 17 | limitations under the License. |
| 18 | |
| 19 | |
| 20 | Distributed under the Boost Software License, Version 1.0. |
| 21 | (See accompanying file Licence.txt or copy at |
| 22 | http://www.boost.org/LICENSE_1_0.txt) |
| 23 | */ |
| 24 | |
| 25 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_ERROR_HPP |
| 26 | #define BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_ERROR_HPP |
| 27 | |
| 28 | #include "status_code.hpp" |
| 29 | |
| 30 | #include <exception> // for std::exception |
| 31 | |
| 32 | BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN |
| 33 | |
| 34 | /*! Exception type representing a thrown status_code |
| 35 | */ |
| 36 | template <class DomainType> class status_error; |
| 37 | |
| 38 | /*! The erased type edition of status_error. |
| 39 | */ |
| 40 | template <> class status_error<void> : public std::exception |
| 41 | { |
| 42 | protected: |
| 43 | //! Constructs an instance. Not publicly available. |
| 44 | status_error() = default; |
| 45 | //! Copy constructor. Not publicly available |
| 46 | status_error(const status_error &) = default; |
| 47 | //! Move constructor. Not publicly available |
| 48 | status_error(status_error &&) = default; |
| 49 | //! Copy assignment. Not publicly available |
| 50 | status_error &operator=(const status_error &) = default; |
| 51 | //! Move assignment. Not publicly available |
| 52 | status_error &operator=(status_error &&) = default; |
| 53 | //! Destructor. Not publicly available. |
| 54 | ~status_error() override = default; |
| 55 | |
| 56 | virtual const status_code<void> &_do_code() const noexcept = 0; |
| 57 | |
| 58 | public: |
| 59 | //! The type of the status domain |
| 60 | using domain_type = void; |
| 61 | //! The type of the status code |
| 62 | using status_code_type = status_code<void>; |
| 63 | |
| 64 | public: |
| 65 | //! The erased status code which generated this exception instance. |
| 66 | const status_code<void> &code() const noexcept { return _do_code(); } |
| 67 | }; |
| 68 | |
| 69 | /*! Exception type representing a thrown status_code |
| 70 | */ |
| 71 | template <class DomainType> class status_error : public status_error<void> |
| 72 | { |
| 73 | status_code<DomainType> _code; |
| 74 | typename DomainType::string_ref _msgref; |
| 75 | |
| 76 | virtual const status_code<void> &_do_code() const noexcept override final { return _code; } |
| 77 | |
| 78 | public: |
| 79 | //! The type of the status domain |
| 80 | using domain_type = DomainType; |
| 81 | //! The type of the status code |
| 82 | using status_code_type = status_code<DomainType>; |
| 83 | |
| 84 | //! Constructs an instance |
| 85 | explicit status_error(status_code<DomainType> code) |
| 86 | : _code(static_cast<status_code<DomainType> &&>(code)) |
| 87 | , _msgref(_code.message()) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | //! Return an explanatory string |
| 92 | virtual const char *what() const noexcept override { return _msgref.c_str(); } // NOLINT |
| 93 | |
| 94 | //! Returns a reference to the code |
| 95 | const status_code_type &code() const & { return _code; } |
| 96 | //! Returns a reference to the code |
| 97 | status_code_type &code() & { return _code; } |
| 98 | //! Returns a reference to the code |
| 99 | const status_code_type &&code() const && { return _code; } |
| 100 | //! Returns a reference to the code |
| 101 | status_code_type &&code() && { return _code; } |
| 102 | }; |
| 103 | |
| 104 | /*! Exception type representing a thrown erased status_code |
| 105 | */ |
| 106 | template <class ErasedType> class status_error<detail::erased<ErasedType>> : public status_error<void> |
| 107 | { |
| 108 | status_code<detail::erased<ErasedType>> _code; |
| 109 | typename status_code_domain::string_ref _msgref; |
| 110 | |
| 111 | virtual const status_code<detail::erased<ErasedType>> &_do_code() const noexcept override final { return _code; } |
| 112 | |
| 113 | public: |
| 114 | //! The type of the status domain |
| 115 | using domain_type = void; |
| 116 | //! The type of the status code |
| 117 | using status_code_type = status_code<detail::erased<ErasedType>>; |
| 118 | |
| 119 | //! Constructs an instance |
| 120 | explicit status_error(status_code<detail::erased<ErasedType>> code) |
| 121 | : _code(static_cast<status_code<detail::erased<ErasedType>> &&>(code)) |
| 122 | , _msgref(_code.message()) |
| 123 | { |
| 124 | } |
| 125 | |
| 126 | //! Return an explanatory string |
| 127 | virtual const char *what() const noexcept override { return _msgref.c_str(); } // NOLINT |
| 128 | |
| 129 | //! Returns a reference to the code |
| 130 | const status_code_type &code() const & { return _code; } |
| 131 | //! Returns a reference to the code |
| 132 | status_code_type &code() & { return _code; } |
| 133 | //! Returns a reference to the code |
| 134 | const status_code_type &&code() const && { return _code; } |
| 135 | //! Returns a reference to the code |
| 136 | status_code_type &&code() && { return _code; } |
| 137 | }; |
| 138 | |
| 139 | BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END |
| 140 | |
| 141 | #endif |
| 142 | |