1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/capy
7  
// Official repository: https://github.com/cppalliance/capy
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_CAPY_ERROR_HPP
10  
#ifndef BOOST_CAPY_ERROR_HPP
11  
#define BOOST_CAPY_ERROR_HPP
11  
#define BOOST_CAPY_ERROR_HPP
12  

12  

13  
#include <boost/capy/detail/config.hpp>
13  
#include <boost/capy/detail/config.hpp>
14  
#include <system_error>
14  
#include <system_error>
15  

15  

16  
namespace boost {
16  
namespace boost {
17  
namespace capy {
17  
namespace capy {
18  

18  

19  
/** Error codes for capy I/O operations.
19  
/** Error codes for capy I/O operations.
20  

20  

21  
    These codes are produced by capy algorithms and I/O operations.
21  
    These codes are produced by capy algorithms and I/O operations.
22  

22  

23  
    @warning Callers must never compare received `error_code` values
23  
    @warning Callers must never compare received `error_code` values
24  
    directly against this enum. Always compare against the portable
24  
    directly against this enum. Always compare against the portable
25  
    @ref cond error conditions instead. These enum values are
25  
    @ref cond error conditions instead. These enum values are
26  
    implementation details subject to change.
26  
    implementation details subject to change.
27  

27  

28  
    @see cond
28  
    @see cond
29  
*/
29  
*/
30  
enum class error
30  
enum class error
31  
{
31  
{
32  
    /// End-of-stream reached. Compare with `cond::eof`.
32  
    /// End-of-stream reached. Compare with `cond::eof`.
33  
    eof = 1,
33  
    eof = 1,
34  

34  

35  
    /// Operation was cancelled. Compare with `cond::canceled`.
35  
    /// Operation was cancelled. Compare with `cond::canceled`.
36  
    canceled,
36  
    canceled,
37  

37  

38  
    /// Internal test assertion failed.
38  
    /// Internal test assertion failed.
39  
    test_failure,
39  
    test_failure,
40  

40  

41  
    /// Peer closed connection without proper TLS shutdown.
41  
    /// Peer closed connection without proper TLS shutdown.
42  
    /// Compare with `cond::stream_truncated`.
42  
    /// Compare with `cond::stream_truncated`.
43  
    stream_truncated,
43  
    stream_truncated,
44  

44  

45  
    /// Requested item was not found. Compare with `cond::not_found`.
45  
    /// Requested item was not found. Compare with `cond::not_found`.
46 -
    not_found,
46 +
    not_found
47 -

 
48 -
    /// Operation timed out. Compare with `cond::timeout`.
 
49 -
    timeout
 
50  
};
47  
};
51  

48  

52  
} // capy
49  
} // capy
53  
} // boost
50  
} // boost
54  

51  

55  
namespace std {
52  
namespace std {
56  
template<>
53  
template<>
57  
struct is_error_code_enum<
54  
struct is_error_code_enum<
58  
    ::boost::capy::error>
55  
    ::boost::capy::error>
59  
    : std::true_type {};
56  
    : std::true_type {};
60  
} // std
57  
} // std
61  

58  

62  
namespace boost {
59  
namespace boost {
63  
namespace capy {
60  
namespace capy {
64  

61  

65  
namespace detail {
62  
namespace detail {
66  
 
63  
 
67  
struct BOOST_CAPY_SYMBOL_VISIBLE
64  
struct BOOST_CAPY_SYMBOL_VISIBLE
68  
    error_cat_type
65  
    error_cat_type
69  
    : std::error_category
66  
    : std::error_category
70  
{
67  
{
71  
    BOOST_CAPY_DECL const char* name(
68  
    BOOST_CAPY_DECL const char* name(
72  
        ) const noexcept override;
69  
        ) const noexcept override;
73  
    BOOST_CAPY_DECL std::string message(
70  
    BOOST_CAPY_DECL std::string message(
74  
        int) const override;
71  
        int) const override;
75  
    constexpr error_cat_type() noexcept = default;
72  
    constexpr error_cat_type() noexcept = default;
76  
};
73  
};
77  

74  

78  
BOOST_CAPY_DECL extern error_cat_type error_cat;
75  
BOOST_CAPY_DECL extern error_cat_type error_cat;
79  

76  

80  
} // detail
77  
} // detail
81  

78  

82  
/// Create an error_code from an error value.
79  
/// Create an error_code from an error value.
83  
inline
80  
inline
84  
std::error_code
81  
std::error_code
85  
make_error_code(
82  
make_error_code(
86  
    error ev) noexcept
83  
    error ev) noexcept
87  
{
84  
{
88  
    return std::error_code{
85  
    return std::error_code{
89  
        static_cast<std::underlying_type<
86  
        static_cast<std::underlying_type<
90  
            error>::type>(ev),
87  
            error>::type>(ev),
91  
        detail::error_cat};
88  
        detail::error_cat};
92  
}
89  
}
93  

90  

94  
} // capy
91  
} // capy
95  
} // boost
92  
} // boost
96  

93  

97  
#endif
94  
#endif