status_code_ptr.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Pointer to a SG14 status_code
  2. (C) 2018-2020 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
  39. : _base(0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id() /* unique-ish based on domain's unique id */)
  40. {
  41. }
  42. indirecting_domain(const indirecting_domain &) = default;
  43. indirecting_domain(indirecting_domain &&) = default; // NOLINT
  44. indirecting_domain &operator=(const indirecting_domain &) = default;
  45. indirecting_domain &operator=(indirecting_domain &&) = default; // NOLINT
  46. ~indirecting_domain() = default;
  47. #if __cplusplus < 201402L && !defined(_MSC_VER)
  48. static inline const indirecting_domain &get()
  49. {
  50. static indirecting_domain v;
  51. return v;
  52. }
  53. #else
  54. static inline constexpr const indirecting_domain &get();
  55. #endif
  56. virtual string_ref name() const noexcept override { return typename StatusCode::domain_type().name(); } // NOLINT
  57. protected:
  58. using _mycode = status_code<indirecting_domain>;
  59. virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
  60. {
  61. assert(code.domain() == *this);
  62. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  63. return typename StatusCode::domain_type()._do_failure(*c.value());
  64. }
  65. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
  66. {
  67. assert(code1.domain() == *this);
  68. const auto &c1 = static_cast<const _mycode &>(code1); // NOLINT
  69. return typename StatusCode::domain_type()._do_equivalent(*c1.value(), code2);
  70. }
  71. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
  72. {
  73. assert(code.domain() == *this);
  74. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  75. return typename StatusCode::domain_type()._generic_code(*c.value());
  76. }
  77. virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  78. {
  79. assert(code.domain() == *this);
  80. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  81. return typename StatusCode::domain_type()._do_message(*c.value());
  82. }
  83. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  84. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  85. {
  86. assert(code.domain() == *this);
  87. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  88. typename StatusCode::domain_type()._do_throw_exception(*c.value());
  89. }
  90. #endif
  91. virtual void _do_erased_copy(status_code<void> &dst, const status_code<void> &src, size_t /*unused*/) const override // NOLINT
  92. {
  93. assert(dst.domain() == *this);
  94. assert(src.domain() == *this);
  95. auto &d = static_cast<_mycode &>(dst); // NOLINT
  96. const auto &_s = static_cast<const _mycode &>(src); // NOLINT
  97. const StatusCode &s = *_s.value();
  98. new(&d) _mycode(in_place, new StatusCode(s));
  99. }
  100. virtual void _do_erased_destroy(status_code<void> &code, size_t /*unused*/) const noexcept override // NOLINT
  101. {
  102. assert(code.domain() == *this);
  103. auto &c = static_cast<_mycode &>(code); // NOLINT
  104. delete c.value(); // NOLINT
  105. }
  106. };
  107. #if __cplusplus >= 201402L || defined(_MSC_VER)
  108. template <class StatusCode> constexpr indirecting_domain<StatusCode> _indirecting_domain{};
  109. template <class StatusCode> inline constexpr const indirecting_domain<StatusCode> &indirecting_domain<StatusCode>::get() { return _indirecting_domain<StatusCode>; }
  110. #endif
  111. } // namespace detail
  112. /*! Make an erased status code which indirects to a dynamically allocated status code.
  113. This is useful for shoehorning a rich status code with large value type into a small
  114. erased status code like `system_code`, with which the status code generated by this
  115. function is compatible. Note that this function can throw due to `bad_alloc`.
  116. */
  117. template <class T, typename std::enable_if<is_status_code<T>::value, bool>::type = true> //
  118. inline status_code<erased<typename std::add_pointer<typename std::decay<T>::type>::type>> make_status_code_ptr(T &&v)
  119. {
  120. using status_code_type = typename std::decay<T>::type;
  121. return status_code<detail::indirecting_domain<status_code_type>>(in_place, new status_code_type(static_cast<T &&>(v)));
  122. }
  123. /*! If a status code refers to a `status_code_ptr` which indirects to a status
  124. code of type `StatusCode`, return a pointer to that `StatusCode`. Otherwise return null.
  125. */
  126. template <class StatusCode, class U, typename std::enable_if<is_status_code<StatusCode>::value, bool>::type = true> inline StatusCode *get_if(status_code<erased<U>> *v) noexcept
  127. {
  128. if((0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id()) != v->domain().id())
  129. {
  130. return nullptr;
  131. }
  132. union {
  133. U value;
  134. StatusCode *ret;
  135. };
  136. value = v->value();
  137. return ret;
  138. }
  139. //! \overload Const overload
  140. template <class StatusCode, class U, typename std::enable_if<is_status_code<StatusCode>::value, bool>::type = true> inline const StatusCode *get_if(const status_code<erased<U>> *v) noexcept
  141. {
  142. if((0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id()) != v->domain().id())
  143. {
  144. return nullptr;
  145. }
  146. union {
  147. U value;
  148. const StatusCode *ret;
  149. };
  150. value = v->value();
  151. return ret;
  152. }
  153. /*! If a status code refers to a `status_code_ptr`, return the id of the erased
  154. status code's domain. Otherwise return a meaningless number.
  155. */
  156. template <class U> inline typename status_code_domain::unique_id_type get_id(const status_code<erased<U>> &v) noexcept
  157. {
  158. return 0xc44f7bdeb2cc50e9 ^ v.domain().id();
  159. }
  160. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  161. #endif