| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #ifndef LIBCPP_BITS_SYSTEM_ERROR
|
|---|
| 8 | #define LIBCPP_BITS_SYSTEM_ERROR
|
|---|
| 9 |
|
|---|
| 10 | #include <__bits/aux.hpp>
|
|---|
| 11 | #include <__bits/string/stringfwd.hpp>
|
|---|
| 12 | #include <stdexcept>
|
|---|
| 13 | #include <type_traits>
|
|---|
| 14 |
|
|---|
| 15 | namespace std
|
|---|
| 16 | {
|
|---|
| 17 | class error_condition;
|
|---|
| 18 | class error_code;
|
|---|
| 19 |
|
|---|
| 20 | enum class errc
|
|---|
| 21 | { // TODO: add matching values
|
|---|
| 22 | address_family_not_supported,
|
|---|
| 23 | address_in_use,
|
|---|
| 24 | address_not_available,
|
|---|
| 25 | already_connected,
|
|---|
| 26 | argument_list_too_long,
|
|---|
| 27 | argument_out_of_domain,
|
|---|
| 28 | bad_address,
|
|---|
| 29 | bad_file_descriptor,
|
|---|
| 30 | bad_message,
|
|---|
| 31 | broken_pipe,
|
|---|
| 32 | connection_aborted,
|
|---|
| 33 | connection_already_in_progress,
|
|---|
| 34 | connection_refused,
|
|---|
| 35 | connection_reset,
|
|---|
| 36 | cross_device_link,
|
|---|
| 37 | destination_address_required,
|
|---|
| 38 | device_or_resource_busy,
|
|---|
| 39 | directory_not_empty,
|
|---|
| 40 | executable_format_error,
|
|---|
| 41 | file_exists,
|
|---|
| 42 | file_too_large,
|
|---|
| 43 | filename_too_long,
|
|---|
| 44 | function_not_supported,
|
|---|
| 45 | host_unreachable,
|
|---|
| 46 | identifier_removed,
|
|---|
| 47 | illegal_byte_sequence,
|
|---|
| 48 | inappropriate_io_control_operation,
|
|---|
| 49 | interrupted,
|
|---|
| 50 | invalid_argument,
|
|---|
| 51 | invalid_seek,
|
|---|
| 52 | io_error,
|
|---|
| 53 | is_a_directory,
|
|---|
| 54 | message_size,
|
|---|
| 55 | network_down,
|
|---|
| 56 | network_reset,
|
|---|
| 57 | network_unreachable,
|
|---|
| 58 | no_buffer_space,
|
|---|
| 59 | no_child_process,
|
|---|
| 60 | no_link,
|
|---|
| 61 | no_lock_available,
|
|---|
| 62 | no_message_available,
|
|---|
| 63 | no_message,
|
|---|
| 64 | no_protocol_option,
|
|---|
| 65 | no_space_on_device,
|
|---|
| 66 | no_stream_resources,
|
|---|
| 67 | no_such_device_or_address,
|
|---|
| 68 | no_such_device,
|
|---|
| 69 | no_such_file_or_directory,
|
|---|
| 70 | no_such_process,
|
|---|
| 71 | not_a_directory,
|
|---|
| 72 | not_a_socket,
|
|---|
| 73 | not_a_stream,
|
|---|
| 74 | not_connected,
|
|---|
| 75 | not_enough_memory,
|
|---|
| 76 | not_supported,
|
|---|
| 77 | operation_canceled,
|
|---|
| 78 | operation_in_progress,
|
|---|
| 79 | operation_not_permitted,
|
|---|
| 80 | operation_not_supported,
|
|---|
| 81 | operation_would_block,
|
|---|
| 82 | owner_dead,
|
|---|
| 83 | permission_denied,
|
|---|
| 84 | protocol_error,
|
|---|
| 85 | protocol_not_supported,
|
|---|
| 86 | read_only_file_system,
|
|---|
| 87 | resource_deadlock_would_occur,
|
|---|
| 88 | resource_unavailable_try_again,
|
|---|
| 89 | result_out_of_range,
|
|---|
| 90 | state_not_recoverable,
|
|---|
| 91 | stream_timeout,
|
|---|
| 92 | text_file_busy,
|
|---|
| 93 | timed_out,
|
|---|
| 94 | too_many_files_open_in_system,
|
|---|
| 95 | too_many_files_open,
|
|---|
| 96 | too_many_links,
|
|---|
| 97 | too_many_symbolic_link_levels,
|
|---|
| 98 | value_too_large,
|
|---|
| 99 | wrong_protocol_type
|
|---|
| 100 | };
|
|---|
| 101 |
|
|---|
| 102 | template<class>
|
|---|
| 103 | struct is_error_code_enum: false_type
|
|---|
| 104 | { /* DUMMY BODY */ };
|
|---|
| 105 |
|
|---|
| 106 | template<>
|
|---|
| 107 | struct is_error_code_enum<errc>: true_type
|
|---|
| 108 | { /* DUMMY BODY */ };
|
|---|
| 109 |
|
|---|
| 110 | template<class T>
|
|---|
| 111 | inline constexpr bool is_error_code_enum_v = is_error_code_enum<T>::value;
|
|---|
| 112 |
|
|---|
| 113 | template<class>
|
|---|
| 114 | struct is_error_condition_enum: false_type
|
|---|
| 115 | { /* DUMMY BODY */ };
|
|---|
| 116 |
|
|---|
| 117 | template<>
|
|---|
| 118 | struct is_error_condition_enum<errc>: true_type
|
|---|
| 119 | { /* DUMMY BODY */ };
|
|---|
| 120 |
|
|---|
| 121 | template<class T>
|
|---|
| 122 | inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<T>::value;
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * 19.5.1, class error_category:
|
|---|
| 126 | */
|
|---|
| 127 |
|
|---|
| 128 | class error_category
|
|---|
| 129 | {
|
|---|
| 130 | public:
|
|---|
| 131 | constexpr error_category() noexcept = default;
|
|---|
| 132 | virtual ~error_category();
|
|---|
| 133 |
|
|---|
| 134 | error_category(const error_category&) = delete;
|
|---|
| 135 | error_category& operator=(const error_category&) = delete;
|
|---|
| 136 |
|
|---|
| 137 | virtual const char* name() const noexcept = 0;
|
|---|
| 138 | virtual error_condition default_error_condition(int) const noexcept;
|
|---|
| 139 | virtual bool equivalent(int, const error_condition&) const noexcept;
|
|---|
| 140 | virtual bool equivalent(const error_code&, int) const noexcept;
|
|---|
| 141 | virtual string message(int) const = 0;
|
|---|
| 142 |
|
|---|
| 143 | bool operator==(const error_category&) const noexcept;
|
|---|
| 144 | bool operator!=(const error_category&) const noexcept;
|
|---|
| 145 | bool operator<(const error_category&) const noexcept;
|
|---|
| 146 | };
|
|---|
| 147 |
|
|---|
| 148 | const error_category& generic_category() noexcept;
|
|---|
| 149 | const error_category& system_category() noexcept;
|
|---|
| 150 |
|
|---|
| 151 | /**
|
|---|
| 152 | * 19.5.2, class error_code:
|
|---|
| 153 | */
|
|---|
| 154 |
|
|---|
| 155 | class error_code
|
|---|
| 156 | {
|
|---|
| 157 | public:
|
|---|
| 158 | /**
|
|---|
| 159 | * 19.5.2.2, constructors:
|
|---|
| 160 | */
|
|---|
| 161 |
|
|---|
| 162 | error_code() noexcept;
|
|---|
| 163 | error_code(int, const error_category&) noexcept;
|
|---|
| 164 |
|
|---|
| 165 | template<class ErrorCodeEnum>
|
|---|
| 166 | error_code(
|
|---|
| 167 | enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
|
|---|
| 168 | ) noexcept
|
|---|
| 169 | {
|
|---|
| 170 | val_ = static_cast<int>(e);
|
|---|
| 171 | cat_ = &generic_category();
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /**
|
|---|
| 175 | * 19.5.2.3, modifiers:
|
|---|
| 176 | */
|
|---|
| 177 |
|
|---|
| 178 | void assign(int, const error_category&) noexcept;
|
|---|
| 179 |
|
|---|
| 180 | template<class ErrorCodeEnum>
|
|---|
| 181 | error_code& operator=(
|
|---|
| 182 | enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
|
|---|
| 183 | ) noexcept
|
|---|
| 184 | {
|
|---|
| 185 | val_ = static_cast<int>(e);
|
|---|
| 186 | cat_ = &generic_category();
|
|---|
| 187 |
|
|---|
| 188 | return *this;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | void clear() noexcept;
|
|---|
| 192 |
|
|---|
| 193 | /**
|
|---|
| 194 | * 19.5.2.4, observers:
|
|---|
| 195 | */
|
|---|
| 196 |
|
|---|
| 197 | int value() const noexcept;
|
|---|
| 198 | const error_category& category() const noexcept;
|
|---|
| 199 | error_condition default_error_condition() const noexcept;
|
|---|
| 200 | string message() const;
|
|---|
| 201 |
|
|---|
| 202 | explicit operator bool() const noexcept
|
|---|
| 203 | {
|
|---|
| 204 | return val_ != 0;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | private:
|
|---|
| 208 | int val_;
|
|---|
| 209 | const error_category* cat_;
|
|---|
| 210 | };
|
|---|
| 211 |
|
|---|
| 212 | /**
|
|---|
| 213 | * 19.5.2.5, non-member functions:
|
|---|
| 214 | */
|
|---|
| 215 |
|
|---|
| 216 | error_code make_error_code(errc e) noexcept;
|
|---|
| 217 | bool operator<(const error_code&, const error_code&) noexcept;
|
|---|
| 218 |
|
|---|
| 219 | template<class Char, class Traits>
|
|---|
| 220 | basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os,
|
|---|
| 221 | const error_code& ec)
|
|---|
| 222 | {
|
|---|
| 223 | return os << ec.category().name() << ": " << ec.value();
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | * 19.5.3, class error_condition:
|
|---|
| 228 | */
|
|---|
| 229 |
|
|---|
| 230 | class error_condition
|
|---|
| 231 | {
|
|---|
| 232 | public:
|
|---|
| 233 | /**
|
|---|
| 234 | * 19.5.3.2, constructors:
|
|---|
| 235 | */
|
|---|
| 236 |
|
|---|
| 237 | error_condition() noexcept;
|
|---|
| 238 | error_condition(int, const error_category&) noexcept;
|
|---|
| 239 |
|
|---|
| 240 | template<class ErrorCodeEnum>
|
|---|
| 241 | error_condition(
|
|---|
| 242 | enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
|
|---|
| 243 | ) noexcept
|
|---|
| 244 | {
|
|---|
| 245 | val_ = static_cast<int>(e);
|
|---|
| 246 | cat_ = &generic_category();
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /**
|
|---|
| 250 | * 19.5.3.3, modifiers:
|
|---|
| 251 | */
|
|---|
| 252 |
|
|---|
| 253 | void assign(int, const error_category&) noexcept;
|
|---|
| 254 |
|
|---|
| 255 | template<class ErrorCodeEnum>
|
|---|
| 256 | error_condition& operator=(
|
|---|
| 257 | enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
|
|---|
| 258 | ) noexcept
|
|---|
| 259 | {
|
|---|
| 260 | val_ = static_cast<int>(e);
|
|---|
| 261 | cat_ = &generic_category();
|
|---|
| 262 |
|
|---|
| 263 | return *this;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | void clear() noexcept;
|
|---|
| 267 |
|
|---|
| 268 | /**
|
|---|
| 269 | * 19.5.3.4, observers:
|
|---|
| 270 | */
|
|---|
| 271 |
|
|---|
| 272 | int value() const noexcept;
|
|---|
| 273 | const error_category& category() const noexcept;
|
|---|
| 274 | string message() const;
|
|---|
| 275 |
|
|---|
| 276 | explicit operator bool() const noexcept
|
|---|
| 277 | {
|
|---|
| 278 | return val_ != 0;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | private:
|
|---|
| 282 | int val_;
|
|---|
| 283 | const error_category* cat_;
|
|---|
| 284 | };
|
|---|
| 285 |
|
|---|
| 286 | /**
|
|---|
| 287 | * 19.5.3.4, non-member functions:
|
|---|
| 288 | */
|
|---|
| 289 |
|
|---|
| 290 | error_condition make_error_condition(errc e) noexcept;
|
|---|
| 291 | bool operator<(const error_condition&, const error_condition&) noexcept;
|
|---|
| 292 |
|
|---|
| 293 | /**
|
|---|
| 294 | * 19.5.4, comparison operators:
|
|---|
| 295 | */
|
|---|
| 296 |
|
|---|
| 297 | bool operator==(const error_code&, const error_code&) noexcept;
|
|---|
| 298 | bool operator==(const error_code&, const error_condition&) noexcept;
|
|---|
| 299 | bool operator==(const error_condition&, const error_code&) noexcept;
|
|---|
| 300 | bool operator==(const error_condition&, const error_condition&) noexcept;
|
|---|
| 301 | bool operator!=(const error_code&, const error_code&) noexcept;
|
|---|
| 302 | bool operator!=(const error_code&, const error_condition&) noexcept;
|
|---|
| 303 | bool operator!=(const error_condition&, const error_code&) noexcept;
|
|---|
| 304 | bool operator!=(const error_condition&, const error_condition&) noexcept;
|
|---|
| 305 |
|
|---|
| 306 | /**
|
|---|
| 307 | * 19.5.6, class system_error:
|
|---|
| 308 | */
|
|---|
| 309 |
|
|---|
| 310 | class system_error: public runtime_error
|
|---|
| 311 | {
|
|---|
| 312 | public:
|
|---|
| 313 | system_error(error_code, const string&);
|
|---|
| 314 | system_error(error_code, const char*);
|
|---|
| 315 | system_error(error_code);
|
|---|
| 316 | system_error(int, const error_category&, const string&);
|
|---|
| 317 | system_error(int, const error_category&, const char*);
|
|---|
| 318 | system_error(int, const error_category&);
|
|---|
| 319 |
|
|---|
| 320 | const error_code& code() const noexcept;
|
|---|
| 321 |
|
|---|
| 322 | private:
|
|---|
| 323 | error_code code_;
|
|---|
| 324 | };
|
|---|
| 325 |
|
|---|
| 326 | /**
|
|---|
| 327 | * 19.5.5, hash support:
|
|---|
| 328 | */
|
|---|
| 329 |
|
|---|
| 330 | template<class>
|
|---|
| 331 | struct hash;
|
|---|
| 332 |
|
|---|
| 333 | template<>
|
|---|
| 334 | struct hash<error_code>
|
|---|
| 335 | {
|
|---|
| 336 | size_t operator()(const error_code& ec) const noexcept
|
|---|
| 337 | {
|
|---|
| 338 | return static_cast<size_t>(ec.value());
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | using result_type = size_t;
|
|---|
| 342 | using argument_type = error_code;
|
|---|
| 343 | };
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | #endif
|
|---|