basic_outcome_failure_observers.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Failure observers for outcome type
  2. (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (7 commits)
  3. File Created: Oct 2017
  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_BASIC_OUTCOME_FAILURE_OBSERVERS_HPP
  26. #define BOOST_OUTCOME_BASIC_OUTCOME_FAILURE_OBSERVERS_HPP
  27. #include "basic_result_storage.hpp"
  28. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  29. namespace detail
  30. {
  31. namespace adl
  32. {
  33. struct search_detail_adl
  34. {
  35. };
  36. BOOST_OUTCOME_TEMPLATE(class S) //
  37. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(basic_outcome_failure_exception_from_error(std::declval<S>()))) //
  38. inline auto _delayed_lookup_basic_outcome_failure_exception_from_error(const S &ec, search_detail_adl /*unused*/)
  39. {
  40. // ADL discovered
  41. return basic_outcome_failure_exception_from_error(ec);
  42. }
  43. } // namespace adl
  44. #if defined(_MSC_VER) && _MSC_VER <= 1920 // VS2019
  45. // VS2017 and VS2019 with /permissive- chokes on the correct form due to over eager early instantiation.
  46. 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!"); }
  47. #else
  48. 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!
  49. #endif
  50. template <class exception_type> inline exception_type current_exception_or_fatal(std::exception_ptr e) { std::rethrow_exception(e); }
  51. template <> inline std::exception_ptr current_exception_or_fatal<std::exception_ptr>(std::exception_ptr e) { return e; }
  52. template <class Base, class R, class S, class P, class NoValuePolicy> class basic_outcome_failure_observers : public Base
  53. {
  54. public:
  55. using exception_type = P;
  56. using Base::Base;
  57. exception_type failure() const noexcept
  58. {
  59. #ifndef BOOST_NO_EXCEPTIONS
  60. try
  61. #endif
  62. {
  63. if((this->_state._status & detail::status_have_exception) != 0)
  64. {
  65. return this->assume_exception();
  66. }
  67. if((this->_state._status & detail::status_have_error) != 0)
  68. {
  69. return _delayed_lookup_basic_outcome_failure_exception_from_error(this->assume_error(), adl::search_detail_adl());
  70. }
  71. return exception_type();
  72. }
  73. #ifndef BOOST_NO_EXCEPTIONS
  74. catch(...)
  75. {
  76. // Return the failure if exception_type is std::exception_ptr,
  77. // otherwise terminate same as throwing an exception inside noexcept
  78. return current_exception_or_fatal<exception_type>(std::current_exception());
  79. }
  80. #endif
  81. }
  82. };
  83. } // namespace detail
  84. BOOST_OUTCOME_V2_NAMESPACE_END
  85. #endif