1 /* Proposed SG14 status_code
2 (C) 2018 - 2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License in the accompanying file
11 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
20 Distributed under the Boost Software License, Version 1.0.
21 (See accompanying file Licence.txt or copy at
22 http://www.boost.org/LICENSE_1_0.txt)
25 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_CONFIG_HPP
26 #define BOOST_OUTCOME_SYSTEM_ERROR2_CONFIG_HPP
30 #include <cstddef> // for size_t
31 #include <cstdlib> // for free
34 #include <type_traits>
39 // 0.28 (0.15 of which is exception_ptr)
40 #include <exception> // for std::exception
41 // <new> includes <exception>, <exception> includes <new>
45 #include <initializer_list>
47 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14
48 #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
49 //! Defined to be `constexpr` when on C++ 14 or better compilers. Usually automatic, can be overriden.
50 #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 constexpr
52 #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14
56 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
57 #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (_HAS_CXX17 && _MSC_VER >= 1911 /* VS2017.3 */)
58 #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN [[noreturn]]
61 #if !defined(BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN)
62 #ifdef __has_cpp_attribute
63 #if __has_cpp_attribute(noreturn)
64 #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN [[noreturn]]
68 #if !defined(BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN)
70 #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN __declspec(noreturn)
71 #elif defined(__GNUC__)
72 #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN __attribute__((__noreturn__))
74 #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
77 // GCCs before 7 don't grok [[noreturn]] virtual functions, and warn annoyingly
78 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 7
79 #undef BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
80 #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
83 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
84 #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (_HAS_CXX17 && _MSC_VER >= 1911 /* VS2017.3 */)
85 #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD [[nodiscard]]
88 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
89 #ifdef __has_cpp_attribute
90 #if __has_cpp_attribute(nodiscard)
91 #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD [[nodiscard]]
93 #elif defined(__clang__)
94 #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD __attribute__((warn_unused_result))
95 #elif defined(_MSC_VER)
96 // _Must_inspect_result_ expands into this
97 #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD \
98 __declspec("SAL_name" \
100 "\"_Must_inspect_result_\"" \
105 ")") __declspec("SAL_begin") __declspec("SAL_post") __declspec("SAL_mustInspect") __declspec("SAL_post") __declspec("SAL_checkReturn") __declspec("SAL_end")
108 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
109 #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
112 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI
113 #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (__clang_major__ >= 7 && !defined(__APPLE__))
114 //! Defined to be `[[clang::trivial_abi]]` when on a new enough clang compiler. Usually automatic, can be overriden.
115 #define BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI [[clang::trivial_abi]]
117 #define BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI
121 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE
122 //! The system_error2 namespace name.
123 #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE system_error2
124 //! Begins the system_error2 namespace.
125 #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN \
126 namespace system_error2 \
128 //! Ends the system_error2 namespace.
129 #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END }
132 //! Namespace for the library
133 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
135 //! Namespace for user specialised traits
138 /*! Specialise to true if you guarantee that a type is move bitcopying (i.e.
139 its move constructor equals copying bits from old to new, old is left in a
140 default constructed state, and calling the destructor on a default constructed
141 instance is trivial). All trivially copyable types are move bitcopying by
142 definition, and that is the unspecialised implementation.
144 template <class T> struct is_move_bitcopying
146 static constexpr bool value = std::is_trivially_copyable<T>::value;
148 } // namespace traits
152 inline BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 size_t cstrlen(const char *str)
154 const char *end = nullptr;
155 for(end = str; *end != 0; ++end) // NOLINT
160 /* A partially compliant implementation of C++20's std::bit_cast function contributed
161 by Jesse Towner. TODO FIXME Replace with C++ 20 bit_cast when available.
163 Our bit_cast is only guaranteed to be constexpr when both the input and output
164 arguments are either integrals or enums. However, this covers most use cases
165 since the vast majority of status_codes have an underlying type that is either
166 an integral or enum. We still attempt a constexpr union-based type pun for non-array
167 input types, which some compilers accept. For array inputs, we fall back to
168 non-constexpr memmove.
171 template <class T> using is_integral_or_enum = std::integral_constant<bool, std::is_integral<T>::value || std::is_enum<T>::value>;
173 template <class To, class From> using is_static_castable = std::integral_constant<bool, is_integral_or_enum<To>::value && is_integral_or_enum<From>::value>;
175 template <class To, class From> using is_union_castable = std::integral_constant<bool, !is_static_castable<To, From>::value && !std::is_array<To>::value && !std::is_array<From>::value>;
177 template <class To, class From> using is_bit_castable = std::integral_constant<bool, sizeof(To) == sizeof(From) && traits::is_move_bitcopying<To>::value && traits::is_move_bitcopying<From>::value>;
179 template <class To, class From> union bit_cast_union {
184 template <class To, class From,
185 typename std::enable_if< //
186 is_bit_castable<To, From>::value //
187 && is_static_castable<To, From>::value //
188 && !is_union_castable<To, From>::value, //
189 bool>::type = true> //
190 constexpr To bit_cast(const From &from) noexcept
192 return static_cast<To>(from);
195 #if defined(__GNUC__) && !defined(__clang__)
196 #pragma GCC diagnostic push
197 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
199 template <class To, class From,
200 typename std::enable_if< //
201 is_bit_castable<To, From>::value //
202 && !is_static_castable<To, From>::value //
203 && is_union_castable<To, From>::value, //
204 bool>::type = true> //
205 constexpr To bit_cast(const From &from) noexcept
207 return bit_cast_union<To, From>{from}.target;
209 #if defined(__GNUC__) && !defined(__clang__)
210 #pragma GCC diagnostic pop
213 template <class To, class From,
214 typename std::enable_if< //
215 is_bit_castable<To, From>::value //
216 && !is_static_castable<To, From>::value //
217 && !is_union_castable<To, From>::value, //
218 bool>::type = true> //
219 To bit_cast(const From &from) noexcept
221 bit_cast_union<To, From> ret;
222 memmove(&ret.source, &from, sizeof(ret.source));
226 /* erasure_cast performs a bit_cast with additional rules to handle types
227 of differing sizes. For integral & enum types, it may perform a narrowing
228 or widing conversion with static_cast if necessary, before doing the final
229 conversion with bit_cast. When casting to or from non-integral, non-enum
230 types it may insert the value into another object with extra padding bytes
231 to satisfy bit_cast's preconditions that both types have the same size. */
233 template <class To, class From> using is_erasure_castable = std::integral_constant<bool, traits::is_move_bitcopying<To>::value && traits::is_move_bitcopying<From>::value>;
235 template <class T, bool = std::is_enum<T>::value> struct identity_or_underlying_type
239 template <class T> struct identity_or_underlying_type<T, true>
241 using type = typename std::underlying_type<T>::type;
244 template <class OfSize, class OfSign>
245 using erasure_integer_type = typename std::conditional<std::is_signed<typename identity_or_underlying_type<OfSign>::type>::value, typename std::make_signed<typename identity_or_underlying_type<OfSize>::type>::type, typename std::make_unsigned<typename identity_or_underlying_type<OfSize>::type>::type>::type;
247 template <class ErasedType, std::size_t N> struct padded_erasure_object
249 static_assert(traits::is_move_bitcopying<ErasedType>::value, "ErasedType must be TriviallyCopyable or MoveBitcopying");
250 static_assert(alignof(ErasedType) <= sizeof(ErasedType), "ErasedType must not be over-aligned");
253 constexpr explicit padded_erasure_object(const ErasedType &v) noexcept
260 template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && (sizeof(To) == sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept { return bit_cast<To>(from); }
262 template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && is_static_castable<To, From>::value && (sizeof(To) < sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept { return static_cast<To>(bit_cast<erasure_integer_type<From, To>>(from)); }
264 template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && is_static_castable<To, From>::value && (sizeof(To) > sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept { return bit_cast<To>(static_cast<erasure_integer_type<To, From>>(from)); }
266 template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && !is_static_castable<To, From>::value && (sizeof(To) < sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept
268 return bit_cast<padded_erasure_object<To, sizeof(From) - sizeof(To)>>(from).value;
271 template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && !is_static_castable<To, From>::value && (sizeof(To) > sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept
273 return bit_cast<To>(padded_erasure_object<From, sizeof(To) - sizeof(From)>{from});
275 } // namespace detail
276 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
278 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_FATAL
279 #ifdef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX
280 #error If BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX is defined, you must define your own BOOST_OUTCOME_SYSTEM_ERROR2_FATAL implementation!
282 #include <cstdlib> // for abort
284 #include <unistd.h> // for write
287 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
290 namespace avoid_stdio_include
292 #if !defined(__APPLE__) && !defined(_MSC_VER)
293 extern "C" ptrdiff_t write(int, const void *, size_t);
294 #elif defined(_MSC_VER)
295 extern ptrdiff_t write(int, const void *, size_t);
297 #pragma comment(linker, "/alternatename:?write@avoid_stdio_include@detail@system_error2@@YA_JHPEBX_K@Z=write")
299 #pragma comment(linker, "/alternatename:?write@avoid_stdio_include@detail@system_error2@@YAHHPBXI@Z=_write")
302 } // namespace avoid_stdio_include
303 inline void do_fatal_exit(const char *msg)
305 using namespace avoid_stdio_include;
306 write(2 /*stderr*/, msg, cstrlen(msg));
307 write(2 /*stderr*/, "\n", 1);
310 } // namespace detail
311 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
312 //! Prints msg to stderr, and calls `std::terminate()`. Can be overriden via predefinition.
313 #define BOOST_OUTCOME_SYSTEM_ERROR2_FATAL(msg) ::BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::detail::do_fatal_exit(msg)