status_result.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* A very simple result type
  2. (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (59 commits)
  3. File Created: Apr 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_EXPERIMENTAL_STATUS_RESULT_HPP
  26. #define BOOST_OUTCOME_EXPERIMENTAL_STATUS_RESULT_HPP
  27. #include "../basic_result.hpp"
  28. #include "../policy/fail_to_compile_observers.hpp"
  29. #include "status-code/system_error2.hpp"
  30. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  31. namespace detail
  32. {
  33. // Customise _set_error_is_errno
  34. template <class State> constexpr inline void _set_error_is_errno(State &state, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::generic_code & /*unused*/) { state._status |= status_error_is_errno; }
  35. template <class State> constexpr inline void _set_error_is_errno(State &state, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::posix_code & /*unused*/) { state._status |= status_error_is_errno; }
  36. template <class State> constexpr inline void _set_error_is_errno(State &state, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errc & /*unused*/) { state._status |= status_error_is_errno; }
  37. } // namespace detail
  38. namespace experimental
  39. {
  40. using namespace BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE;
  41. using BOOST_OUTCOME_V2_NAMESPACE::success;
  42. using BOOST_OUTCOME_V2_NAMESPACE::failure;
  43. namespace policy
  44. {
  45. using namespace BOOST_OUTCOME_V2_NAMESPACE::policy;
  46. template <class T, class EC, class E> struct status_code_throw
  47. {
  48. static_assert(!std::is_same<T, T>::value, "policy::status_code_throw not specialised for these types, did you use status_result<T, status_code<DomainType>, E>?");
  49. };
  50. template <class T, class DomainType> struct status_code_throw<T, status_code<DomainType>, void> : base
  51. {
  52. using _base = base;
  53. template <class Impl> static constexpr void wide_value_check(Impl &&self)
  54. {
  55. if(!base::_has_value(static_cast<Impl &&>(self)))
  56. {
  57. if(base::_has_error(static_cast<Impl &&>(self)))
  58. {
  59. #ifndef BOOST_NO_EXCEPTIONS
  60. base::_error(static_cast<Impl &&>(self)).throw_exception();
  61. #else
  62. BOOST_OUTCOME_THROW_EXCEPTION("wide value check failed");
  63. #endif
  64. }
  65. }
  66. }
  67. template <class Impl> static constexpr void wide_error_check(Impl &&self) { _base::narrow_error_check(static_cast<Impl &&>(self)); }
  68. };
  69. template <class T, class DomainType> struct status_code_throw<T, errored_status_code<DomainType>, void> : status_code_throw<T, status_code<DomainType>, void>
  70. {
  71. status_code_throw() = default;
  72. using status_code_throw<T, status_code<DomainType>, void>::status_code_throw;
  73. };
  74. template <class T, class EC>
  75. using default_status_result_policy = std::conditional_t< //
  76. std::is_void<EC>::value, //
  77. BOOST_OUTCOME_V2_NAMESPACE::policy::terminate, //
  78. std::conditional_t<is_status_code<EC>::value || is_errored_status_code<EC>::value, //
  79. status_code_throw<T, EC, void>, //
  80. BOOST_OUTCOME_V2_NAMESPACE::policy::fail_to_compile_observers //
  81. >>;
  82. } // namespace policy
  83. /*! AWAITING HUGO JSON CONVERSION TOOL
  84. SIGNATURE NOT RECOGNISED
  85. */
  86. template <class R, class S = system_code, class NoValuePolicy = policy::default_status_result_policy<R, S>> //
  87. using status_result = basic_result<R, S, NoValuePolicy>;
  88. } // namespace experimental
  89. BOOST_OUTCOME_V2_NAMESPACE_END
  90. #endif