status_code_ptr.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Pointer to a SG14 status_code
  2. (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Sep 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_CODE_PTR_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_CODE_PTR_HPP
  27. #include "status_code.hpp"
  28. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  29. namespace detail
  30. {
  31. template <class StatusCode> class indirecting_domain : public status_code_domain
  32. {
  33. template <class DomainType> friend class status_code;
  34. using _base = status_code_domain;
  35. public:
  36. using value_type = StatusCode *;
  37. using _base::string_ref;
  38. constexpr indirecting_domain() noexcept : _base(0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id() /* unique-ish based on domain's unique id */) {}
  39. indirecting_domain(const indirecting_domain &) = default;
  40. indirecting_domain(indirecting_domain &&) = default; // NOLINT
  41. indirecting_domain &operator=(const indirecting_domain &) = default;
  42. indirecting_domain &operator=(indirecting_domain &&) = default; // NOLINT
  43. ~indirecting_domain() = default;
  44. #if __cplusplus < 201402L && !defined(_MSC_VER)
  45. static inline const indirecting_domain &get()
  46. {
  47. static indirecting_domain v;
  48. return v;
  49. }
  50. #else
  51. static inline constexpr const indirecting_domain &get();
  52. #endif
  53. virtual string_ref name() const noexcept override { return typename StatusCode::domain_type().name(); } // NOLINT
  54. protected:
  55. using _mycode = status_code<indirecting_domain>;
  56. virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
  57. {
  58. assert(code.domain() == *this);
  59. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  60. return typename StatusCode::domain_type()._do_failure(*c.value());
  61. }
  62. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
  63. {
  64. assert(code1.domain() == *this);
  65. const auto &c1 = static_cast<const _mycode &>(code1); // NOLINT
  66. return typename StatusCode::domain_type()._do_equivalent(*c1.value(), code2);
  67. }
  68. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
  69. {
  70. assert(code.domain() == *this);
  71. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  72. return typename StatusCode::domain_type()._generic_code(*c.value());
  73. }
  74. virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  75. {
  76. assert(code.domain() == *this);
  77. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  78. return typename StatusCode::domain_type()._do_message(*c.value());
  79. }
  80. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  81. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  82. {
  83. assert(code.domain() == *this);
  84. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  85. typename StatusCode::domain_type()._do_throw_exception(*c.value());
  86. }
  87. #endif
  88. virtual void _do_erased_copy(status_code<void> &dst, const status_code<void> &src, size_t /*unused*/) const override // NOLINT
  89. {
  90. assert(dst.domain() == *this);
  91. assert(src.domain() == *this);
  92. auto &d = static_cast<_mycode &>(dst); // NOLINT
  93. const auto &_s = static_cast<const _mycode &>(src); // NOLINT
  94. const StatusCode &s = *_s.value();
  95. new(&d) _mycode(in_place, new StatusCode(s));
  96. }
  97. virtual void _do_erased_destroy(status_code<void> &code, size_t /*unused*/) const noexcept override // NOLINT
  98. {
  99. assert(code.domain() == *this);
  100. auto &c = static_cast<_mycode &>(code); // NOLINT
  101. delete c.value(); // NOLINT
  102. }
  103. };
  104. #if __cplusplus >= 201402L || defined(_MSC_VER)
  105. template <class StatusCode> constexpr indirecting_domain<StatusCode> _indirecting_domain{};
  106. template <class StatusCode> inline constexpr const indirecting_domain<StatusCode> &indirecting_domain<StatusCode>::get() { return _indirecting_domain<StatusCode>; }
  107. #endif
  108. } // namespace detail
  109. /*! Make an erased status code which indirects to a dynamically allocated status code.
  110. This is useful for shoehorning a rich status code with large value type into a small
  111. erased status code like `system_code`, with which the status code generated by this
  112. function is compatible. Note that this function can throw due to `bad_alloc`.
  113. */
  114. template <class T, typename std::enable_if<is_status_code<T>::value, bool>::type = true> //
  115. inline status_code<erased<typename std::add_pointer<typename std::decay<T>::type>::type>> make_status_code_ptr(T &&v)
  116. {
  117. using status_code_type = typename std::decay<T>::type;
  118. return status_code<detail::indirecting_domain<status_code_type>>(in_place, new status_code_type(static_cast<T &&>(v)));
  119. }
  120. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  121. #endif