]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/outcome/experimental/status-code/status_code_ptr.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / outcome / experimental / status-code / status_code_ptr.hpp
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
5
6 Boost Software License - Version 1.0 - August 17th, 2003
7
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:
14
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.
21
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.
29 */
30
31 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_CODE_PTR_HPP
32 #define BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_CODE_PTR_HPP
33
34 #include "status_code.hpp"
35
36 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
37
38 namespace detail
39 {
40 template <class StatusCode> class indirecting_domain : public status_code_domain
41 {
42 template <class DomainType> friend class status_code;
43 using _base = status_code_domain;
44
45 public:
46 using value_type = StatusCode *;
47 using _base::string_ref;
48
49 constexpr indirecting_domain() noexcept
50 : _base(0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id() /* unique-ish based on domain's unique id */)
51 {
52 }
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;
58
59 #if __cplusplus < 201402L && !defined(_MSC_VER)
60 static inline const indirecting_domain &get()
61 {
62 static indirecting_domain v;
63 return v;
64 }
65 #else
66 static inline constexpr const indirecting_domain &get();
67 #endif
68
69 virtual string_ref name() const noexcept override { return typename StatusCode::domain_type().name(); } // NOLINT
70 protected:
71 using _mycode = status_code<indirecting_domain>;
72 virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
73 {
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());
77 }
78 virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
79 {
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);
83 }
84 virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
85 {
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());
89 }
90 virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
91 {
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());
95 }
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
98 {
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());
102 }
103 #endif
104 virtual void _do_erased_copy(status_code<void> &dst, const status_code<void> &src, size_t /*unused*/) const override // NOLINT
105 {
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));
112 }
113 virtual void _do_erased_destroy(status_code<void> &code, size_t /*unused*/) const noexcept override // NOLINT
114 {
115 assert(code.domain() == *this);
116 auto &c = static_cast<_mycode &>(code); // NOLINT
117 delete c.value(); // NOLINT
118 }
119 };
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>; }
123 #endif
124 } // namespace detail
125
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`.
130 */
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)
133 {
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)));
136 }
137
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.
140 */
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
142 {
143 if((0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id()) != v->domain().id())
144 {
145 return nullptr;
146 }
147 union {
148 U value;
149 StatusCode *ret;
150 };
151 value = v->value();
152 return ret;
153 }
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
156 {
157 if((0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id()) != v->domain().id())
158 {
159 return nullptr;
160 }
161 union {
162 U value;
163 const StatusCode *ret;
164 };
165 value = v->value();
166 return ret;
167 }
168
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.
171 */
172 template <class U> inline typename status_code_domain::unique_id_type get_id(const status_code<erased<U>> &v) noexcept
173 {
174 return 0xc44f7bdeb2cc50e9 ^ v.domain().id();
175 }
176
177 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
178
179 #endif