status_error.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Proposed SG14 status_code
  2. (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Feb 2018
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_ERROR_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_ERROR_HPP
  27. #include "status_code.hpp"
  28. #include <exception> // for std::exception
  29. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  30. /*! Exception type representing a thrown status_code
  31. */
  32. template <class DomainType> class status_error;
  33. /*! The erased type edition of status_error.
  34. */
  35. template <> class status_error<void> : public std::exception
  36. {
  37. protected:
  38. //! Constructs an instance. Not publicly available.
  39. status_error() = default;
  40. //! Copy constructor. Not publicly available
  41. status_error(const status_error &) = default;
  42. //! Move constructor. Not publicly available
  43. status_error(status_error &&) = default;
  44. //! Copy assignment. Not publicly available
  45. status_error &operator=(const status_error &) = default;
  46. //! Move assignment. Not publicly available
  47. status_error &operator=(status_error &&) = default;
  48. //! Destructor. Not publicly available.
  49. ~status_error() override = default;
  50. public:
  51. //! The type of the status domain
  52. using domain_type = void;
  53. //! The type of the status code
  54. using status_code_type = status_code<void>;
  55. };
  56. /*! Exception type representing a thrown status_code
  57. */
  58. template <class DomainType> class status_error : public status_error<void>
  59. {
  60. status_code<DomainType> _code;
  61. typename DomainType::string_ref _msgref;
  62. public:
  63. //! The type of the status domain
  64. using domain_type = DomainType;
  65. //! The type of the status code
  66. using status_code_type = status_code<DomainType>;
  67. //! Constructs an instance
  68. explicit status_error(status_code<DomainType> code)
  69. : _code(static_cast<status_code<DomainType> &&>(code))
  70. , _msgref(_code.message())
  71. {
  72. }
  73. //! Return an explanatory string
  74. virtual const char *what() const noexcept override { return _msgref.c_str(); } // NOLINT
  75. //! Returns a reference to the code
  76. const status_code_type &code() const & { return _code; }
  77. //! Returns a reference to the code
  78. status_code_type &code() & { return _code; }
  79. //! Returns a reference to the code
  80. const status_code_type &&code() const && { return _code; }
  81. //! Returns a reference to the code
  82. status_code_type &&code() && { return _code; }
  83. };
  84. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  85. #endif