include/boost/corosio/detail/make_err.hpp
75.0% Lines (6/8)
100.0% Functions (1/1)
70.0% Branches (7/10)
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2025 Vinnie Falco ([email protected]) | |||
| 3 | // Copyright (c) 2026 Steve Gerbino | |||
| 4 | // | |||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||
| 7 | // | |||
| 8 | // Official repository: https://github.com/cppalliance/corosio | |||
| 9 | // | |||
| 10 | ||||
| 11 | #ifndef BOOST_COROSIO_DETAIL_MAKE_ERR_HPP | |||
| 12 | #define BOOST_COROSIO_DETAIL_MAKE_ERR_HPP | |||
| 13 | ||||
| 14 | #include <boost/corosio/detail/config.hpp> | |||
| 15 | #include <boost/corosio/detail/platform.hpp> | |||
| 16 | #include <boost/capy/error.hpp> | |||
| 17 | #include <system_error> | |||
| 18 | ||||
| 19 | #if BOOST_COROSIO_POSIX | |||
| 20 | #include <errno.h> | |||
| 21 | #else | |||
| 22 | #ifndef WIN32_LEAN_AND_MEAN | |||
| 23 | #define WIN32_LEAN_AND_MEAN | |||
| 24 | #endif | |||
| 25 | #include <Windows.h> | |||
| 26 | #endif | |||
| 27 | ||||
| 28 | namespace boost::corosio::detail { | |||
| 29 | ||||
| 30 | #if BOOST_COROSIO_POSIX | |||
| 31 | ||||
| 32 | /** Convert a POSIX errno value to std::error_code. | |||
| 33 | ||||
| 34 | Maps ECANCELED to capy::error::canceled. | |||
| 35 | ||||
| 36 | @param errn The errno value. | |||
| 37 | @return The corresponding std::error_code. | |||
| 38 | */ | |||
| 39 | inline std::error_code | |||
| 40 | make_err(int errn) noexcept | |||
| 41 | { | |||
| 42 | if (errn == 0) | |||
| 43 | return {}; | |||
| 44 | ||||
| 45 | if (errn == ECANCELED) | |||
| 46 | return capy::error::canceled; | |||
| 47 | ||||
| 48 | return std::error_code(errn, std::system_category()); | |||
| 49 | } | |||
| 50 | ||||
| 51 | #else | |||
| 52 | ||||
| 53 | /** Convert a Windows error code to std::error_code. | |||
| 54 | ||||
| 55 | Maps ERROR_OPERATION_ABORTED, ERROR_CANCELLED, and | |||
| 56 | ERROR_NETNAME_DELETED to capy::error::canceled. | |||
| 57 | Maps ERROR_HANDLE_EOF to capy::error::eof. | |||
| 58 | ||||
| 59 | ERROR_NETNAME_DELETED (64) is what IOCP actually delivers | |||
| 60 | when closesocket() cancels pending overlapped I/O, despite | |||
| 61 | MSDN documenting ERROR_OPERATION_ABORTED for that case. | |||
| 62 | ||||
| 63 | @param dwError The Windows error code (DWORD). | |||
| 64 | @return The corresponding std::error_code. | |||
| 65 | */ | |||
| 66 | inline std::error_code | |||
| 67 | 16 | make_err(unsigned long dwError) noexcept | ||
| 68 | { | |||
| 69 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 16 times.
|
16 | if (dwError == 0) | |
| 70 | ✗ | return {}; | ||
| 71 | ||||
| 72 |
5/6✓ Branch 4 → 5 taken 14 times.
✓ Branch 4 → 7 taken 2 times.
✓ Branch 5 → 6 taken 14 times.
✗ Branch 5 → 7 not taken.
✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 8 taken 12 times.
|
16 | if (dwError == ERROR_OPERATION_ABORTED || dwError == ERROR_CANCELLED || | |
| 73 | dwError == ERROR_NETNAME_DELETED) | |||
| 74 | 4 | return capy::error::canceled; | ||
| 75 | ||||
| 76 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 12 times.
|
12 | if (dwError == ERROR_HANDLE_EOF) | |
| 77 | ✗ | return capy::error::eof; | ||
| 78 | ||||
| 79 | 12 | return std::error_code(static_cast<int>(dwError), std::system_category()); | ||
| 80 | } | |||
| 81 | ||||
| 82 | #endif | |||
| 83 | ||||
| 84 | } // namespace boost::corosio::detail | |||
| 85 | ||||
| 86 | #endif | |||
| 87 |