]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
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 | ||
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 : _base(0xc44f7bdeb2cc50e9 ^ typename StatusCode::domain_type().id() /* unique-ish based on domain's unique id */) {} | |
50 | indirecting_domain(const indirecting_domain &) = default; | |
51 | indirecting_domain(indirecting_domain &&) = default; // NOLINT | |
52 | indirecting_domain &operator=(const indirecting_domain &) = default; | |
53 | indirecting_domain &operator=(indirecting_domain &&) = default; // NOLINT | |
54 | ~indirecting_domain() = default; | |
55 | ||
56 | #if __cplusplus < 201402L && !defined(_MSC_VER) | |
57 | static inline const indirecting_domain &get() | |
58 | { | |
59 | static indirecting_domain v; | |
60 | return v; | |
61 | } | |
62 | #else | |
63 | static inline constexpr const indirecting_domain &get(); | |
64 | #endif | |
65 | ||
66 | virtual string_ref name() const noexcept override { return typename StatusCode::domain_type().name(); } // NOLINT | |
67 | protected: | |
68 | using _mycode = status_code<indirecting_domain>; | |
69 | virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT | |
70 | { | |
71 | assert(code.domain() == *this); | |
72 | const auto &c = static_cast<const _mycode &>(code); // NOLINT | |
73 | return typename StatusCode::domain_type()._do_failure(*c.value()); | |
74 | } | |
75 | virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT | |
76 | { | |
77 | assert(code1.domain() == *this); | |
78 | const auto &c1 = static_cast<const _mycode &>(code1); // NOLINT | |
79 | return typename StatusCode::domain_type()._do_equivalent(*c1.value(), code2); | |
80 | } | |
81 | virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT | |
82 | { | |
83 | assert(code.domain() == *this); | |
84 | const auto &c = static_cast<const _mycode &>(code); // NOLINT | |
85 | return typename StatusCode::domain_type()._generic_code(*c.value()); | |
86 | } | |
87 | virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT | |
88 | { | |
89 | assert(code.domain() == *this); | |
90 | const auto &c = static_cast<const _mycode &>(code); // NOLINT | |
91 | return typename StatusCode::domain_type()._do_message(*c.value()); | |
92 | } | |
93 | #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) | |
94 | BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT | |
95 | { | |
96 | assert(code.domain() == *this); | |
97 | const auto &c = static_cast<const _mycode &>(code); // NOLINT | |
98 | typename StatusCode::domain_type()._do_throw_exception(*c.value()); | |
99 | } | |
100 | #endif | |
101 | virtual void _do_erased_copy(status_code<void> &dst, const status_code<void> &src, size_t /*unused*/) const override // NOLINT | |
102 | { | |
103 | assert(dst.domain() == *this); | |
104 | assert(src.domain() == *this); | |
105 | auto &d = static_cast<_mycode &>(dst); // NOLINT | |
106 | const auto &_s = static_cast<const _mycode &>(src); // NOLINT | |
107 | const StatusCode &s = *_s.value(); | |
108 | new(&d) _mycode(in_place, new StatusCode(s)); | |
109 | } | |
110 | virtual void _do_erased_destroy(status_code<void> &code, size_t /*unused*/) const noexcept override // NOLINT | |
111 | { | |
112 | assert(code.domain() == *this); | |
113 | auto &c = static_cast<_mycode &>(code); // NOLINT | |
114 | delete c.value(); // NOLINT | |
115 | } | |
116 | }; | |
117 | #if __cplusplus >= 201402L || defined(_MSC_VER) | |
118 | template <class StatusCode> constexpr indirecting_domain<StatusCode> _indirecting_domain{}; | |
119 | template <class StatusCode> inline constexpr const indirecting_domain<StatusCode> &indirecting_domain<StatusCode>::get() { return _indirecting_domain<StatusCode>; } | |
120 | #endif | |
121 | } // namespace detail | |
122 | ||
123 | /*! Make an erased status code which indirects to a dynamically allocated status code. | |
124 | This is useful for shoehorning a rich status code with large value type into a small | |
125 | erased status code like `system_code`, with which the status code generated by this | |
126 | function is compatible. Note that this function can throw due to `bad_alloc`. | |
127 | */ | |
128 | template <class T, typename std::enable_if<is_status_code<T>::value, bool>::type = true> // | |
129 | inline status_code<erased<typename std::add_pointer<typename std::decay<T>::type>::type>> make_status_code_ptr(T &&v) | |
130 | { | |
131 | using status_code_type = typename std::decay<T>::type; | |
132 | return status_code<detail::indirecting_domain<status_code_type>>(in_place, new status_code_type(static_cast<T &&>(v))); | |
133 | } | |
134 | ||
135 | BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END | |
136 | ||
137 | #endif |