1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 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/boostorg/url
7  
// Official repository: https://github.com/boostorg/url
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_URL_GRAMMAR_IMPL_ERROR_HPP
10  
#ifndef BOOST_URL_GRAMMAR_IMPL_ERROR_HPP
11  
#define BOOST_URL_GRAMMAR_IMPL_ERROR_HPP
11  
#define BOOST_URL_GRAMMAR_IMPL_ERROR_HPP
12  

12  

13  
#include <type_traits>
13  
#include <type_traits>
14  

14  

15  
namespace boost {
15  
namespace boost {
16  
namespace system {
16  
namespace system {
17  
template<>
17  
template<>
18  
struct is_error_code_enum<
18  
struct is_error_code_enum<
19  
    ::boost::urls::grammar::error>
19  
    ::boost::urls::grammar::error>
20  
{
20  
{
21  
    static bool const value = true;
21  
    static bool const value = true;
22  
};
22  
};
23  
template<>
23  
template<>
24  
struct is_error_condition_enum<
24  
struct is_error_condition_enum<
25  
    ::boost::urls::grammar::condition>
25  
    ::boost::urls::grammar::condition>
26  
{
26  
{
27  
    static bool const value = true;
27  
    static bool const value = true;
28  
};
28  
};
29  
} // system
29  
} // system
30  
} // boost
30  
} // boost
31  

31  

32  
namespace boost {
32  
namespace boost {
33  
namespace urls {
33  
namespace urls {
34  
namespace grammar {
34  
namespace grammar {
35  

35  

36  
namespace detail {
36  
namespace detail {
37  

37  

38  
struct BOOST_SYMBOL_VISIBLE
38  
struct BOOST_SYMBOL_VISIBLE
39  
    error_cat_type
39  
    error_cat_type
40  
    : system::error_category
40  
    : system::error_category
41  
{
41  
{
42  
    BOOST_URL_CXX20_CONSTEXPR
42  
    BOOST_URL_CXX20_CONSTEXPR
43  
    const char* name(
43  
    const char* name(
44  
        ) const noexcept override
44  
        ) const noexcept override
45  
    {
45  
    {
46  
        return "boost.url.grammar";
46  
        return "boost.url.grammar";
47  
    }
47  
    }
48  

48  

49  
    std::string message(
49  
    std::string message(
50  
        int code) const override
50  
        int code) const override
51  
    {
51  
    {
52  
        return message(code, nullptr, 0);
52  
        return message(code, nullptr, 0);
53  
    }
53  
    }
54  

54  

55  
    BOOST_URL_CXX20_CONSTEXPR
55  
    BOOST_URL_CXX20_CONSTEXPR
56  
    char const* message(
56  
    char const* message(
57  
        int code,
57  
        int code,
58  
        char*,
58  
        char*,
59  
        std::size_t) const noexcept override
59  
        std::size_t) const noexcept override
60  
    {
60  
    {
61  
        switch(static_cast<error>(code))
61  
        switch(static_cast<error>(code))
62  
        {
62  
        {
63  
        default:
63  
        default:
64  
        case error::need_more: return "need more";
64  
        case error::need_more: return "need more";
65  
        case error::mismatch: return "mismatch";
65  
        case error::mismatch: return "mismatch";
66  
        case error::invalid: return "invalid";
66  
        case error::invalid: return "invalid";
67  
        case error::end_of_range: return "end of range";
67  
        case error::end_of_range: return "end of range";
68  
        case error::leftover: return "leftover";
68  
        case error::leftover: return "leftover";
69  
        case error::out_of_range: return "out of range";
69  
        case error::out_of_range: return "out of range";
70  
        }
70  
        }
71  
    }
71  
    }
72  

72  

73  
    BOOST_URL_CXX20_CONSTEXPR
73  
    BOOST_URL_CXX20_CONSTEXPR
74  
    system::error_condition
74  
    system::error_condition
75  
        default_error_condition(
75  
        default_error_condition(
76  
            int ev) const noexcept override;
76  
            int ev) const noexcept override;
77  

77  

78  
    BOOST_SYSTEM_CONSTEXPR error_cat_type() noexcept
78  
    BOOST_SYSTEM_CONSTEXPR error_cat_type() noexcept
79  
        : error_category(0x0536e50a30f9e9f2)
79  
        : error_category(0x0536e50a30f9e9f2)
80  
    {
80  
    {
81  
    }
81  
    }
82  
};
82  
};
83  

83  

84  
struct BOOST_SYMBOL_VISIBLE
84  
struct BOOST_SYMBOL_VISIBLE
85  
    condition_cat_type
85  
    condition_cat_type
86  
    : system::error_category
86  
    : system::error_category
87  
{
87  
{
88  
    BOOST_URL_CXX20_CONSTEXPR
88  
    BOOST_URL_CXX20_CONSTEXPR
89  
    const char* name(
89  
    const char* name(
90  
        ) const noexcept override
90  
        ) const noexcept override
91  
    {
91  
    {
92  
        return "boost.url.grammar";
92  
        return "boost.url.grammar";
93  
    }
93  
    }
94  

94  

95  
    std::string message(
95  
    std::string message(
96  
        int code) const override
96  
        int code) const override
97  
    {
97  
    {
98  
        return message(code, nullptr, 0);
98  
        return message(code, nullptr, 0);
99  
    }
99  
    }
100  

100  

101  
    BOOST_URL_CXX20_CONSTEXPR
101  
    BOOST_URL_CXX20_CONSTEXPR
102  
    char const* message(
102  
    char const* message(
103  
        int code,
103  
        int code,
104  
        char*,
104  
        char*,
105  
        std::size_t) const noexcept override
105  
        std::size_t) const noexcept override
106  
    {
106  
    {
107  
        switch(static_cast<condition>(code))
107  
        switch(static_cast<condition>(code))
108  
        {
108  
        {
109  
        default:
109  
        default:
110  
        case condition::fatal:
110  
        case condition::fatal:
111  
            return "fatal condition";
111  
            return "fatal condition";
112  
        }
112  
        }
113  
    }
113  
    }
114  

114  

115  
    BOOST_SYSTEM_CONSTEXPR condition_cat_type()
115  
    BOOST_SYSTEM_CONSTEXPR condition_cat_type()
116  
        : error_category(0x0536e50a30f9e9f2)
116  
        : error_category(0x0536e50a30f9e9f2)
117  
    {
117  
    {
118  
    }
118  
    }
119  
};
119  
};
120  

120  

121  
#if defined(BOOST_URL_HAS_CXX20_CONSTEXPR)
121  
#if defined(BOOST_URL_HAS_CXX20_CONSTEXPR)
122  
inline constexpr error_cat_type error_cat{};
122  
inline constexpr error_cat_type error_cat{};
123  
inline constexpr condition_cat_type condition_cat{};
123  
inline constexpr condition_cat_type condition_cat{};
124  
#else
124  
#else
125  
BOOST_URL_DECL extern error_cat_type error_cat;
125  
BOOST_URL_DECL extern error_cat_type error_cat;
126  
BOOST_URL_DECL extern condition_cat_type condition_cat;
126  
BOOST_URL_DECL extern condition_cat_type condition_cat;
127  
#endif
127  
#endif
128  

128  

129  
} // detail
129  
} // detail
130  

130  

131  
inline
131  
inline
132  
BOOST_SYSTEM_CONSTEXPR
132  
BOOST_SYSTEM_CONSTEXPR
133  
system::error_code
133  
system::error_code
134  
make_error_code(
134  
make_error_code(
135  
    error ev) noexcept
135  
    error ev) noexcept
136  
{
136  
{
137  
    return system::error_code{
137  
    return system::error_code{
138  
        static_cast<std::underlying_type<
138  
        static_cast<std::underlying_type<
139  
            error>::type>(ev),
139  
            error>::type>(ev),
140  
                detail::error_cat};
140  
                detail::error_cat};
141  
}
141  
}
142  

142  

143  
inline
143  
inline
144  
BOOST_SYSTEM_CONSTEXPR
144  
BOOST_SYSTEM_CONSTEXPR
145  
system::error_condition
145  
system::error_condition
146  
make_error_condition(
146  
make_error_condition(
147  
    condition c) noexcept
147  
    condition c) noexcept
148  
{
148  
{
149  
    return system::error_condition{
149  
    return system::error_condition{
150  
        static_cast<std::underlying_type<
150  
        static_cast<std::underlying_type<
151  
            condition>::type>(c),
151  
            condition>::type>(c),
152  
                detail::condition_cat};
152  
                detail::condition_cat};
153  
}
153  
}
154  

154  

155  
BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
155  
BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
156  
system::error_condition
156  
system::error_condition
157  
detail::error_cat_type::
157  
detail::error_cat_type::
158  
    default_error_condition(
158  
    default_error_condition(
159  
        int ev) const noexcept
159  
        int ev) const noexcept
160  
{
160  
{
161  
    switch(static_cast<error>(ev))
161  
    switch(static_cast<error>(ev))
162  
    {
162  
    {
163  
    case error::invalid:
163  
    case error::invalid:
164  
    case error::out_of_range:
164  
    case error::out_of_range:
165  
        return condition::fatal;
165  
        return condition::fatal;
166  
    default:
166  
    default:
167  
        return {ev, *this};
167  
        return {ev, *this};
168  
    }
168  
    }
169  
}
169  
}
170  

170  

171  
} // grammar
171  
} // grammar
172  
} // urls
172  
} // urls
173  
} // boost
173  
} // boost
174  

174  

175  
#endif
175  
#endif