basic_outcome_exception_observers_impl.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Exception observers for outcome type
  2. (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (6 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_EXCEPTION_OBSERVERS_IMPL_HPP
  26. #define BOOST_OUTCOME_BASIC_OUTCOME_EXCEPTION_OBSERVERS_IMPL_HPP
  27. #include "basic_outcome_exception_observers.hpp"
  28. #include "../policy/base.hpp"
  29. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  30. namespace policy
  31. {
  32. template <class R, class S, class P, class NoValuePolicy, class Impl> inline constexpr auto &&base::_exception(Impl &&self) noexcept
  33. {
  34. // Impl will be some internal implementation class which has no knowledge of the _ptr stored
  35. // beneath it. So statically cast, preserving rvalue and constness, to the derived class.
  36. using Outcome = BOOST_OUTCOME_V2_NAMESPACE::detail::rebind_type<basic_outcome<R, S, P, NoValuePolicy>, decltype(self)>;
  37. #if defined(_MSC_VER) && _MSC_VER < 1920
  38. // VS2017 tries a copy construction in the correct implementation despite that Outcome is always a rvalue or lvalue ref! :(
  39. basic_outcome<R, S, P, NoValuePolicy> &_self = (basic_outcome<R, S, P, NoValuePolicy> &) (self); // NOLINT
  40. #else
  41. Outcome _self = static_cast<Outcome>(self); // NOLINT
  42. #endif
  43. return static_cast<Outcome>(_self)._ptr;
  44. }
  45. } // namespace policy
  46. namespace detail
  47. {
  48. template <class Base, class R, class S, class P, class NoValuePolicy> inline constexpr typename basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception_type &basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::assume_exception() & noexcept
  49. {
  50. NoValuePolicy::narrow_exception_check(*this);
  51. return NoValuePolicy::template _exception<R, S, P, NoValuePolicy>(*this);
  52. }
  53. template <class Base, class R, class S, class P, class NoValuePolicy> inline constexpr const typename basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception_type &basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::assume_exception() const &noexcept
  54. {
  55. NoValuePolicy::narrow_exception_check(*this);
  56. return NoValuePolicy::template _exception<R, S, P, NoValuePolicy>(*this);
  57. }
  58. template <class Base, class R, class S, class P, class NoValuePolicy> inline constexpr typename basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception_type &&basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::assume_exception() && noexcept
  59. {
  60. NoValuePolicy::narrow_exception_check(std::move(*this));
  61. return NoValuePolicy::template _exception<R, S, P, NoValuePolicy>(std::move(*this));
  62. }
  63. template <class Base, class R, class S, class P, class NoValuePolicy> inline constexpr const typename basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception_type &&basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::assume_exception() const &&noexcept
  64. {
  65. NoValuePolicy::narrow_exception_check(std::move(*this));
  66. return NoValuePolicy::template _exception<R, S, P, NoValuePolicy>(std::move(*this));
  67. }
  68. template <class Base, class R, class S, class P, class NoValuePolicy> inline constexpr typename basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception_type &basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception() &
  69. {
  70. NoValuePolicy::wide_exception_check(*this);
  71. return NoValuePolicy::template _exception<R, S, P, NoValuePolicy>(*this);
  72. }
  73. template <class Base, class R, class S, class P, class NoValuePolicy> inline constexpr const typename basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception_type &basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception() const &
  74. {
  75. NoValuePolicy::wide_exception_check(*this);
  76. return NoValuePolicy::template _exception<R, S, P, NoValuePolicy>(*this);
  77. }
  78. template <class Base, class R, class S, class P, class NoValuePolicy> inline constexpr typename basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception_type &&basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception() &&
  79. {
  80. NoValuePolicy::wide_exception_check(std::move(*this));
  81. return NoValuePolicy::template _exception<R, S, P, NoValuePolicy>(std::move(*this));
  82. }
  83. template <class Base, class R, class S, class P, class NoValuePolicy> inline constexpr const typename basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception_type &&basic_outcome_exception_observers<Base, R, S, P, NoValuePolicy>::exception() const &&
  84. {
  85. NoValuePolicy::wide_exception_check(std::move(*this));
  86. return NoValuePolicy::template _exception<R, S, P, NoValuePolicy>(std::move(*this));
  87. }
  88. } // namespace detail
  89. BOOST_OUTCOME_V2_NAMESPACE_END
  90. #endif