src/error.cpp
100.0% Lines (20/20)
100.0% List of functions (3/3)
Functions (3)
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 3 | // Copyright (c) 2026 Michael Vandeberg | ||
| 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/capy | ||
| 9 | // | ||
| 10 | |||
| 11 | #include <boost/capy/error.hpp> | ||
| 12 | #include <boost/capy/cond.hpp> | ||
| 13 | |||
| 14 | namespace boost { | ||
| 15 | namespace capy { | ||
| 16 | |||
| 17 | namespace detail { | ||
| 18 | |||
| 19 | const char* | ||
| 20 | 2x | error_cat_type:: | |
| 21 | name() const noexcept | ||
| 22 | { | ||
| 23 | 2x | return "boost.capy"; | |
| 24 | } | ||
| 25 | |||
| 26 | std::string | ||
| 27 | 1037x | error_cat_type:: | |
| 28 | message(int code) const | ||
| 29 | { | ||
| 30 | 1037x | switch(static_cast<error>(code)) | |
| 31 | { | ||
| 32 | 6x | case error::eof: return "eof"; | |
| 33 | 3x | case error::canceled: return "operation canceled"; | |
| 34 | 3090x | case error::test_failure: return "test failure"; | |
| 35 | 3x | case error::stream_truncated: return "stream truncated"; | |
| 36 | 3x | case error::not_found: return "not found"; | |
| 37 | 3x | case error::timeout: return "timeout"; | |
| 38 | 1x | default: | |
| 39 | 2x | return "unknown"; | |
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | // Map each capy error code to its canonical portable condition. | ||
| 44 | // canceled and timeout have standard equivalents, so they map to the | ||
| 45 | // generic conditions rather than capy's own cond enumerators; this is | ||
| 46 | // what lets, e.g., error::canceled compare equal to | ||
| 47 | // std::errc::operation_canceled. | ||
| 48 | std::error_condition | ||
| 49 | 546x | error_cat_type:: | |
| 50 | default_error_condition(int code) const noexcept | ||
| 51 | { | ||
| 52 | 546x | switch(static_cast<error>(code)) | |
| 53 | { | ||
| 54 | 301x | case error::eof: return make_error_condition(cond::eof); | |
| 55 | 4x | case error::canceled: return std::make_error_condition(std::errc::operation_canceled); | |
| 56 | 1x | case error::stream_truncated: return make_error_condition(cond::stream_truncated); | |
| 57 | 12x | case error::not_found: return make_error_condition(cond::not_found); | |
| 58 | 7x | case error::timeout: return std::make_error_condition(std::errc::timed_out); | |
| 59 | 221x | default: return std::error_condition(code, *this); | |
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | //----------------------------------------------- | ||
| 64 | |||
| 65 | // msvc 14.0 has a bug that warns about inability | ||
| 66 | // to use constexpr construction here, even though | ||
| 67 | // there's no constexpr construction | ||
| 68 | #if BOOST_CAPY_WORKAROUND(_MSC_VER, <= 1900) | ||
| 69 | BOOST_CAPY_MSVC_WARNING_PUSH | ||
| 70 | BOOST_CAPY_MSVC_WARNING_DISABLE(4592) | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #if defined(__cpp_constinit) && __cpp_constinit >= 201907L | ||
| 74 | constinit error_cat_type error_cat; | ||
| 75 | #else | ||
| 76 | error_cat_type error_cat; | ||
| 77 | #endif | ||
| 78 | |||
| 79 | #if BOOST_CAPY_WORKAROUND(_MSC_VER, <= 1900) | ||
| 80 | BOOST_CAPY_MSVC_WARNING_POP | ||
| 81 | #endif | ||
| 82 | |||
| 83 | } // detail | ||
| 84 | |||
| 85 | } // capy | ||
| 86 | } // boost | ||
| 87 |