base.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Policies for result and outcome
  2. (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (6 commits) and Andrzej Krzemieński <akrzemi1@gmail.com> (1 commit)
  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_POLICY_BASE_HPP
  26. #define BOOST_OUTCOME_POLICY_BASE_HPP
  27. #include "../config.hpp"
  28. #include <cassert>
  29. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  30. namespace policy
  31. {
  32. /*! AWAITING HUGO JSON CONVERSION TOOL
  33. SIGNATURE NOT RECOGNISED
  34. */
  35. struct base
  36. {
  37. protected:
  38. template <class Impl>
  39. static constexpr
  40. #ifdef _MSC_VER
  41. __declspec(noreturn)
  42. #elif defined(__GNUC__) || defined(__clang__)
  43. __attribute__((noreturn))
  44. #endif
  45. void _ub(Impl && /*unused*/)
  46. {
  47. assert(false); // NOLINT
  48. #if defined(__GNUC__) || defined(__clang__)
  49. __builtin_unreachable();
  50. #elif defined(_MSC_VER)
  51. __assume(0);
  52. #endif
  53. }
  54. template <class Impl> static constexpr bool _has_value(Impl &&self) noexcept { return (self._state._status & BOOST_OUTCOME_V2_NAMESPACE::detail::status_have_value) != 0; }
  55. template <class Impl> static constexpr bool _has_error(Impl &&self) noexcept { return (self._state._status & BOOST_OUTCOME_V2_NAMESPACE::detail::status_have_error) != 0; }
  56. template <class Impl> static constexpr bool _has_exception(Impl &&self) noexcept { return (self._state._status & BOOST_OUTCOME_V2_NAMESPACE::detail::status_have_exception) != 0; }
  57. template <class Impl> static constexpr bool _has_error_is_errno(Impl &&self) noexcept { return (self._state._status & BOOST_OUTCOME_V2_NAMESPACE::detail::status_error_is_errno) != 0; }
  58. template <class Impl> static constexpr void _set_has_value(Impl &&self, bool v) noexcept { v ? self._state._status |= BOOST_OUTCOME_V2_NAMESPACE::detail::status_have_value : self._state._status &= ~BOOST_OUTCOME_V2_NAMESPACE::detail::status_have_value; }
  59. template <class Impl> static constexpr void _set_has_error(Impl &&self, bool v) noexcept { v ? self._state._status |= BOOST_OUTCOME_V2_NAMESPACE::detail::status_have_error : self._state._status &= ~BOOST_OUTCOME_V2_NAMESPACE::detail::status_have_error; }
  60. template <class Impl> static constexpr void _set_has_exception(Impl &&self, bool v) noexcept { v ? self._state._status |= BOOST_OUTCOME_V2_NAMESPACE::detail::status_have_exception : self._state._status &= ~BOOST_OUTCOME_V2_NAMESPACE::detail::status_have_exception; }
  61. template <class Impl> static constexpr void _set_has_error_is_errno(Impl &&self, bool v) noexcept { v ? self._state._status |= BOOST_OUTCOME_V2_NAMESPACE::detail::status_error_is_errno : self._state._status &= ~BOOST_OUTCOME_V2_NAMESPACE::detail::status_error_is_errno; }
  62. template <class Impl> static constexpr auto &&_value(Impl &&self) noexcept { return static_cast<Impl &&>(self)._state._value; }
  63. template <class Impl> static constexpr auto &&_error(Impl &&self) noexcept { return static_cast<Impl &&>(self)._error; }
  64. public:
  65. template <class R, class S, class P, class NoValuePolicy, class Impl> static inline constexpr auto &&_exception(Impl &&self) noexcept;
  66. template <class Impl> static constexpr void narrow_value_check(Impl &&self) noexcept
  67. {
  68. if(!_has_value(self))
  69. {
  70. _ub(self);
  71. }
  72. }
  73. template <class Impl> static constexpr void narrow_error_check(Impl &&self) noexcept
  74. {
  75. if(!_has_error(self))
  76. {
  77. _ub(self);
  78. }
  79. }
  80. template <class Impl> static constexpr void narrow_exception_check(Impl &&self) noexcept
  81. {
  82. if(!_has_exception(self))
  83. {
  84. _ub(self);
  85. }
  86. }
  87. };
  88. } // namespace policy
  89. BOOST_OUTCOME_V2_NAMESPACE_END
  90. #endif