1 /* Pointer to a SG14 status_code
2 (C) 2018-2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
6 Boost Software License - Version 1.0 - August 17th, 2003
8 Permission is hereby granted, free of charge, to any person or organization
9 obtaining a copy of the software and accompanying documentation covered by
10 this license (the "Software") to use, reproduce, display, distribute,
11 execute, and transmit the Software, and to prepare derivative works of the
12 Software, and to permit third-parties to whom the Software is furnished to
13 do so, all subject to the following:
15 The copyright notices in the Software and this entire statement, including
16 the above license grant, this restriction and the following disclaimer,
17 must be included in all copies of the Software, in whole or in part, and
18 all derivative works of the Software, unless such copies or derivative
19 works are solely in the form of machine-executable object code generated by
20 a source language processor.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
31 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_CODE_PTR_HPP
32 #define BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_CODE_PTR_HPP
34 #include "status_code.hpp"
36 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
40 template <class StatusCode> class indirecting_domain : public status_code_domain
42 template <class DomainType> friend class status_code;
43 using _base = status_code_domain;
46 using value_type = StatusCode *;
47 using _base::string_ref;
49 constexpr indirecting_domain() noexcept
50 : _base(0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id() /* unique-ish based on domain's unique id */)
53 indirecting_domain(const indirecting_domain &) = default;
54 indirecting_domain(indirecting_domain &&) = default; // NOLINT
55 indirecting_domain &operator=(const indirecting_domain &) = default;
56 indirecting_domain &operator=(indirecting_domain &&) = default; // NOLINT
57 ~indirecting_domain() = default;
59 #if __cplusplus < 201402L && !defined(_MSC_VER)
60 static inline const indirecting_domain &get()
62 static indirecting_domain v;
66 static inline constexpr const indirecting_domain &get();
69 virtual string_ref name() const noexcept override { return typename StatusCode::domain_type().name(); } // NOLINT
71 using _mycode = status_code<indirecting_domain>;
72 virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
74 assert(code.domain() == *this);
75 const auto &c = static_cast<const _mycode &>(code); // NOLINT
76 return typename StatusCode::domain_type()._do_failure(*c.value());
78 virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
80 assert(code1.domain() == *this);
81 const auto &c1 = static_cast<const _mycode &>(code1); // NOLINT
82 return typename StatusCode::domain_type()._do_equivalent(*c1.value(), code2);
84 virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
86 assert(code.domain() == *this);
87 const auto &c = static_cast<const _mycode &>(code); // NOLINT
88 return typename StatusCode::domain_type()._generic_code(*c.value());
90 virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
92 assert(code.domain() == *this);
93 const auto &c = static_cast<const _mycode &>(code); // NOLINT
94 return typename StatusCode::domain_type()._do_message(*c.value());
96 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
97 BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
99 assert(code.domain() == *this);
100 const auto &c = static_cast<const _mycode &>(code); // NOLINT
101 typename StatusCode::domain_type()._do_throw_exception(*c.value());
104 virtual void _do_erased_copy(status_code<void> &dst, const status_code<void> &src, size_t /*unused*/) const override // NOLINT
106 // Note that dst will not have its domain set
107 assert(src.domain() == *this);
108 auto &d = static_cast<_mycode &>(dst); // NOLINT
109 const auto &_s = static_cast<const _mycode &>(src); // NOLINT
110 const StatusCode &s = *_s.value();
111 new(&d) _mycode(in_place, new StatusCode(s));
113 virtual void _do_erased_destroy(status_code<void> &code, size_t /*unused*/) const noexcept override // NOLINT
115 assert(code.domain() == *this);
116 auto &c = static_cast<_mycode &>(code); // NOLINT
117 delete c.value(); // NOLINT
120 #if __cplusplus >= 201402L || defined(_MSC_VER)
121 template <class StatusCode> constexpr indirecting_domain<StatusCode> _indirecting_domain{};
122 template <class StatusCode> inline constexpr const indirecting_domain<StatusCode> &indirecting_domain<StatusCode>::get() { return _indirecting_domain<StatusCode>; }
124 } // namespace detail
126 /*! Make an erased status code which indirects to a dynamically allocated status code.
127 This is useful for shoehorning a rich status code with large value type into a small
128 erased status code like `system_code`, with which the status code generated by this
129 function is compatible. Note that this function can throw due to `bad_alloc`.
131 template <class T, typename std::enable_if<is_status_code<T>::value, bool>::type = true> //
132 inline status_code<erased<typename std::add_pointer<typename std::decay<T>::type>::type>> make_status_code_ptr(T &&v)
134 using status_code_type = typename std::decay<T>::type;
135 return status_code<detail::indirecting_domain<status_code_type>>(in_place, new status_code_type(static_cast<T &&>(v)));
138 /*! If a status code refers to a `status_code_ptr` which indirects to a status
139 code of type `StatusCode`, return a pointer to that `StatusCode`. Otherwise return null.
141 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
143 if((0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id()) != v->domain().id())
154 //! \overload Const overload
155 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
157 if((0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id()) != v->domain().id())
163 const StatusCode *ret;
169 /*! If a status code refers to a `status_code_ptr`, return the id of the erased
170 status code's domain. Otherwise return a meaningless number.
172 template <class U> inline typename status_code_domain::unique_id_type get_id(const status_code<erased<U>> &v) noexcept
174 return 0xc44f7bdeb2cc50e9 ^ v.domain().id();
177 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END