nested_status_code.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Pointer to a SG14 status_code
  2. (C) 2018 - 2023 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Sep 2018
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License in the accompanying file
  7. Licence.txt or at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. Distributed under the Boost Software License, Version 1.0.
  15. (See accompanying file Licence.txt or copy at
  16. http://www.boost.org/LICENSE_1_0.txt)
  17. */
  18. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NESTED_STATUS_CODE_HPP
  19. #define BOOST_OUTCOME_SYSTEM_ERROR2_NESTED_STATUS_CODE_HPP
  20. #include "quick_status_code_from_enum.hpp"
  21. #include <memory> // for allocator
  22. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  23. namespace detail
  24. {
  25. template <class StatusCode, class Allocator> class indirecting_domain : public status_code_domain
  26. {
  27. template <class DomainType> friend class status_code;
  28. using _base = status_code_domain;
  29. public:
  30. struct payload_type
  31. {
  32. using allocator_traits = std::allocator_traits<Allocator>;
  33. union
  34. {
  35. char _uninit[sizeof(StatusCode)];
  36. StatusCode sc;
  37. };
  38. Allocator alloc;
  39. payload_type(StatusCode _sc, Allocator _alloc)
  40. : alloc(static_cast<Allocator &&>(_alloc))
  41. {
  42. allocator_traits::construct(alloc, &sc, static_cast<StatusCode &&>(_sc));
  43. }
  44. payload_type(const payload_type &) = delete;
  45. payload_type(payload_type &&) = delete;
  46. ~payload_type() { allocator_traits::destroy(alloc, &sc); }
  47. };
  48. using value_type = payload_type *;
  49. using payload_allocator_traits = typename payload_type::allocator_traits::template rebind_traits<payload_type>;
  50. using _base::string_ref;
  51. constexpr indirecting_domain() noexcept
  52. : _base(0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id() /* unique-ish based on domain's unique id */)
  53. {
  54. }
  55. indirecting_domain(const indirecting_domain &) = default;
  56. indirecting_domain(indirecting_domain &&) = default; // NOLINT
  57. indirecting_domain &operator=(const indirecting_domain &) = default;
  58. indirecting_domain &operator=(indirecting_domain &&) = default; // NOLINT
  59. ~indirecting_domain() = default;
  60. #if __cplusplus < 201402L && !defined(_MSC_VER)
  61. static inline const indirecting_domain &get()
  62. {
  63. static indirecting_domain v;
  64. return v;
  65. }
  66. #else
  67. static inline constexpr const indirecting_domain &get();
  68. #endif
  69. virtual string_ref name() const noexcept override { return typename StatusCode::domain_type().name(); } // NOLINT
  70. virtual payload_info_t payload_info() const noexcept override
  71. {
  72. return {sizeof(value_type), sizeof(status_code_domain *) + sizeof(value_type),
  73. (alignof(value_type) > alignof(status_code_domain *)) ? alignof(value_type) : alignof(status_code_domain *)};
  74. }
  75. protected:
  76. using _mycode = status_code<indirecting_domain>;
  77. virtual bool _do_failure(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 static_cast<status_code_domain &&>(typename StatusCode::domain_type())._do_failure(c.value()->sc);
  82. }
  83. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
  84. {
  85. assert(code1.domain() == *this);
  86. const auto &c1 = static_cast<const _mycode &>(code1); // NOLINT
  87. return static_cast<status_code_domain &&>(typename StatusCode::domain_type())._do_equivalent(c1.value()->sc, code2);
  88. }
  89. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
  90. {
  91. assert(code.domain() == *this);
  92. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  93. return static_cast<status_code_domain &&>(typename StatusCode::domain_type())._generic_code(c.value()->sc);
  94. }
  95. virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  96. {
  97. assert(code.domain() == *this);
  98. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  99. return static_cast<status_code_domain &&>(typename StatusCode::domain_type())._do_message(c.value()->sc);
  100. }
  101. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  102. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  103. {
  104. assert(code.domain() == *this);
  105. const auto &c = static_cast<const _mycode &>(code); // NOLINT
  106. static_cast<status_code_domain &&>(typename StatusCode::domain_type())._do_throw_exception(c.value()->sc);
  107. abort(); // suppress buggy GCC warning
  108. }
  109. #endif
  110. virtual bool _do_erased_copy(status_code<void> &dst, const status_code<void> &src, payload_info_t dstinfo) const override // NOLINT
  111. {
  112. // Note that dst may not have its domain set
  113. const auto srcinfo = payload_info();
  114. assert(src.domain() == *this);
  115. if(dstinfo.total_size < srcinfo.total_size)
  116. {
  117. return false;
  118. }
  119. auto &d = static_cast<_mycode &>(dst); // NOLINT
  120. const auto &_s = static_cast<const _mycode &>(src); // NOLINT
  121. const payload_type &sp = *_s.value();
  122. typename payload_allocator_traits::template rebind_alloc<payload_type> payload_alloc(sp.alloc);
  123. auto *dp = payload_allocator_traits::allocate(payload_alloc, 1);
  124. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  125. try
  126. #endif
  127. {
  128. payload_allocator_traits::construct(payload_alloc, dp, sp.sc, sp.alloc);
  129. new(BOOST_OUTCOME_SYSTEM_ERROR2_ADDRESS_OF(d)) _mycode(in_place, dp);
  130. }
  131. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  132. catch(...)
  133. {
  134. payload_allocator_traits::deallocate(payload_alloc, dp, 1);
  135. throw;
  136. }
  137. #endif
  138. return true;
  139. }
  140. virtual void _do_erased_destroy(status_code<void> &code, payload_info_t /*unused*/) const noexcept override // NOLINT
  141. {
  142. assert(code.domain() == *this);
  143. auto &c = static_cast<_mycode &>(code); // NOLINT
  144. payload_type *p = c.value();
  145. typename payload_allocator_traits::template rebind_alloc<payload_type> payload_alloc(p->alloc);
  146. payload_allocator_traits::destroy(payload_alloc, p);
  147. payload_allocator_traits::deallocate(payload_alloc, p, 1);
  148. }
  149. };
  150. #if __cplusplus >= 201402L || defined(_MSC_VER)
  151. template <class StatusCode, class Allocator> constexpr indirecting_domain<StatusCode, Allocator> _indirecting_domain{};
  152. template <class StatusCode, class Allocator>
  153. inline constexpr const indirecting_domain<StatusCode, Allocator> &indirecting_domain<StatusCode, Allocator>::get()
  154. {
  155. return _indirecting_domain<StatusCode, Allocator>;
  156. }
  157. #endif
  158. } // namespace detail
  159. /*! Make an erased status code which indirects to a dynamically allocated status code,
  160. using the allocator `alloc`.
  161. This is useful for shoehorning a rich status code with large value type into a small
  162. erased status code like `system_code`, with which the status code generated by this
  163. function is compatible. Note that this function can throw if the allocator throws.
  164. */
  165. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class T, class Alloc = std::allocator<typename std::decay<T>::type>)
  166. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_status_code<T>::value)) //
  167. inline status_code<detail::erased<typename std::add_pointer<typename std::decay<T>::type>::type>> make_nested_status_code(T &&v, Alloc alloc = {})
  168. {
  169. using status_code_type = typename std::decay<T>::type;
  170. using domain_type = detail::indirecting_domain<status_code_type, typename std::decay<Alloc>::type>;
  171. using payload_allocator_traits = typename domain_type::payload_allocator_traits;
  172. typename payload_allocator_traits::template rebind_alloc<typename domain_type::payload_type> payload_alloc(alloc);
  173. auto *p = payload_allocator_traits::allocate(payload_alloc, 1);
  174. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  175. try
  176. #endif
  177. {
  178. payload_allocator_traits::construct(payload_alloc, p, static_cast<T &&>(v), static_cast<Alloc &&>(alloc));
  179. return status_code<domain_type>(in_place, p);
  180. }
  181. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  182. catch(...)
  183. {
  184. payload_allocator_traits::deallocate(payload_alloc, p, 1);
  185. throw;
  186. }
  187. #endif
  188. }
  189. /*! If a status code refers to a `nested_status_code` which indirects to a status
  190. code of type `StatusCode`, return a pointer to that `StatusCode`. Otherwise return null.
  191. */
  192. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class StatusCode, class U)
  193. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_status_code<StatusCode>::value)) inline StatusCode *get_if(status_code<detail::erased<U>> *v) noexcept
  194. {
  195. if((0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id()) != v->domain().id())
  196. {
  197. return nullptr;
  198. }
  199. union
  200. {
  201. U value;
  202. StatusCode *ret;
  203. };
  204. value = v->value();
  205. return ret;
  206. }
  207. //! \overload Const overload
  208. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class StatusCode, class U)
  209. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_status_code<StatusCode>::value))
  210. inline const StatusCode *get_if(const status_code<detail::erased<U>> *v) noexcept
  211. {
  212. if((0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id()) != v->domain().id())
  213. {
  214. return nullptr;
  215. }
  216. union
  217. {
  218. U value;
  219. const StatusCode *ret;
  220. };
  221. value = v->value();
  222. return ret;
  223. }
  224. /*! If a status code refers to a `nested_status_code`, return the id of the erased
  225. status code's domain. Otherwise return a meaningless number.
  226. */
  227. template <class U> inline typename status_code_domain::unique_id_type get_id(const status_code<detail::erased<U>> &v) noexcept
  228. {
  229. return 0xc44f7bdeb2cc50e9 ^ v.domain().id();
  230. }
  231. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  232. #endif