1// Copyright 2021, 2022 Peter Dimov.
2// Distributed under the Boost Software License, Version 1.0.
3// http://www.boost.org/LICENSE_1_0.txt
4
5#include <boost/system/error_code.hpp>
6#include <boost/system/error_category.hpp>
7#include <boost/system/errc.hpp>
8#include <boost/core/lightweight_test.hpp>
9#include <boost/config.hpp>
10#include <cerrno>
11#include <system_error>
12
13enum my_errc
14{
15 my_enoent = ENOENT
16};
17
18class my_category: public boost::system::error_category
19{
20public:
21
22 char const* name() const noexcept
23 {
24 return "mycat";
25 }
26
27 boost::system::error_condition default_error_condition( int ev ) const noexcept
28 {
29 switch( ev )
30 {
31 case my_enoent:
32
33 return boost::system::error_condition( ENOENT, boost::system::generic_category() );
34
35 default:
36
37 return boost::system::error_condition( ev, *this );
38 }
39 }
40
41 std::string message( int ev ) const
42 {
43 switch( ev )
44 {
45 case my_enoent:
46
47 return "No such entity";
48
49 default:
50
51 return "Unknown error";
52 }
53 }
54};
55
56#if defined(BOOST_GCC) && BOOST_GCC < 70000
57
58// g++ 6 and earlier do not allow specializations outside the namespace
59
60namespace boost
61{
62namespace system
63{
64
65template<> struct is_error_code_enum<my_errc>: std::true_type {};
66
67} // namespace system
68} // namespace boost
69
70namespace std
71{
72
73template<> struct is_error_code_enum<my_errc>: std::true_type {};
74
75} // namespace std
76
77#else
78
79template<> struct boost::system::is_error_code_enum<my_errc>: std::true_type {};
80template<> struct std::is_error_code_enum<my_errc>: std::true_type {};
81
82#endif
83
84boost::system::error_code make_error_code( my_errc e )
85{
86 // If `cat` is declared constexpr or const, msvc-14.1 and
87 // msvc-14.2 before 19.29 put it in read-only memory,
88 // despite the `ps_` member being mutable. So it crashes.
89
90 static /*BOOST_SYSTEM_CONSTEXPR*/ my_category cat;
91 return boost::system::error_code( e, cat );
92}
93
94int main()
95{
96 {
97 boost::system::error_code e1 = my_enoent;
98
99 BOOST_TEST( e1 == my_enoent );
100 BOOST_TEST_NOT( e1 != my_enoent );
101
102 BOOST_TEST( e1 == boost::system::errc::no_such_file_or_directory );
103 BOOST_TEST( e1 == std::errc::no_such_file_or_directory );
104 }
105
106 {
107 std::error_code e1 = my_enoent;
108
109 BOOST_TEST( e1 == my_enoent );
110 BOOST_TEST_NOT( e1 != my_enoent );
111
112 BOOST_TEST( e1 == std::errc::no_such_file_or_directory );
113 }
114
115 return boost::report_errors();
116}
117

source code of boost/libs/system/test/std_interop_test12.cpp