| 1 | /*
|
|---|
| 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #ifndef LIBCPP_BITS_SYSTEM_ERROR
|
|---|
| 30 | #define LIBCPP_BITS_SYSTEM_ERROR
|
|---|
| 31 |
|
|---|
| 32 | #include <__bits/aux.hpp>
|
|---|
| 33 | #include <__bits/string/stringfwd.hpp>
|
|---|
| 34 | #include <stdexcept>
|
|---|
| 35 | #include <type_traits>
|
|---|
| 36 |
|
|---|
| 37 | namespace std
|
|---|
| 38 | {
|
|---|
| 39 | class error_condition;
|
|---|
| 40 | class error_code;
|
|---|
| 41 |
|
|---|
| 42 | enum class errc
|
|---|
| 43 | { // TODO: add matching values
|
|---|
| 44 | address_family_not_supported,
|
|---|
| 45 | address_in_use,
|
|---|
| 46 | address_not_available,
|
|---|
| 47 | already_connected,
|
|---|
| 48 | argument_list_too_long,
|
|---|
| 49 | argument_out_of_domain,
|
|---|
| 50 | bad_address,
|
|---|
| 51 | bad_file_descriptor,
|
|---|
| 52 | bad_message,
|
|---|
| 53 | broken_pipe,
|
|---|
| 54 | connection_aborted,
|
|---|
| 55 | connection_already_in_progress,
|
|---|
| 56 | connection_refused,
|
|---|
| 57 | connection_reset,
|
|---|
| 58 | cross_device_link,
|
|---|
| 59 | destination_address_required,
|
|---|
| 60 | device_or_resource_busy,
|
|---|
| 61 | directory_not_empty,
|
|---|
| 62 | executable_format_error,
|
|---|
| 63 | file_exists,
|
|---|
| 64 | file_too_large,
|
|---|
| 65 | filename_too_long,
|
|---|
| 66 | function_not_supported,
|
|---|
| 67 | host_unreachable,
|
|---|
| 68 | identifier_removed,
|
|---|
| 69 | illegal_byte_sequence,
|
|---|
| 70 | inappropriate_io_control_operation,
|
|---|
| 71 | interrupted,
|
|---|
| 72 | invalid_argument,
|
|---|
| 73 | invalid_seek,
|
|---|
| 74 | io_error,
|
|---|
| 75 | is_a_directory,
|
|---|
| 76 | message_size,
|
|---|
| 77 | network_down,
|
|---|
| 78 | network_reset,
|
|---|
| 79 | network_unreachable,
|
|---|
| 80 | no_buffer_space,
|
|---|
| 81 | no_child_process,
|
|---|
| 82 | no_link,
|
|---|
| 83 | no_lock_available,
|
|---|
| 84 | no_message_available,
|
|---|
| 85 | no_message,
|
|---|
| 86 | no_protocol_option,
|
|---|
| 87 | no_space_on_device,
|
|---|
| 88 | no_stream_resources,
|
|---|
| 89 | no_such_device_or_address,
|
|---|
| 90 | no_such_device,
|
|---|
| 91 | no_such_file_or_directory,
|
|---|
| 92 | no_such_process,
|
|---|
| 93 | not_a_directory,
|
|---|
| 94 | not_a_socket,
|
|---|
| 95 | not_a_stream,
|
|---|
| 96 | not_connected,
|
|---|
| 97 | not_enough_memory,
|
|---|
| 98 | not_supported,
|
|---|
| 99 | operation_canceled,
|
|---|
| 100 | operation_in_progress,
|
|---|
| 101 | operation_not_permitted,
|
|---|
| 102 | operation_not_supported,
|
|---|
| 103 | operation_would_block,
|
|---|
| 104 | owner_dead,
|
|---|
| 105 | permission_denied,
|
|---|
| 106 | protocol_error,
|
|---|
| 107 | protocol_not_supported,
|
|---|
| 108 | read_only_file_system,
|
|---|
| 109 | resource_deadlock_would_occur,
|
|---|
| 110 | resource_unavailable_try_again,
|
|---|
| 111 | result_out_of_range,
|
|---|
| 112 | state_not_recoverable,
|
|---|
| 113 | stream_timeout,
|
|---|
| 114 | text_file_busy,
|
|---|
| 115 | timed_out,
|
|---|
| 116 | too_many_files_open_in_system,
|
|---|
| 117 | too_many_files_open,
|
|---|
| 118 | too_many_links,
|
|---|
| 119 | too_many_symbolic_link_levels,
|
|---|
| 120 | value_too_large,
|
|---|
| 121 | wrong_protocol_type
|
|---|
| 122 | };
|
|---|
| 123 |
|
|---|
| 124 | template<class>
|
|---|
| 125 | struct is_error_code_enum: false_type
|
|---|
| 126 | { /* DUMMY BODY */ };
|
|---|
| 127 |
|
|---|
| 128 | template<>
|
|---|
| 129 | struct is_error_code_enum<errc>: true_type
|
|---|
| 130 | { /* DUMMY BODY */ };
|
|---|
| 131 |
|
|---|
| 132 | template<class T>
|
|---|
| 133 | inline constexpr bool is_error_code_enum_v = is_error_code_enum<T>::value;
|
|---|
| 134 |
|
|---|
| 135 | template<class>
|
|---|
| 136 | struct is_error_condition_enum: false_type
|
|---|
| 137 | { /* DUMMY BODY */ };
|
|---|
| 138 |
|
|---|
| 139 | template<>
|
|---|
| 140 | struct is_error_condition_enum<errc>: true_type
|
|---|
| 141 | { /* DUMMY BODY */ };
|
|---|
| 142 |
|
|---|
| 143 | template<class T>
|
|---|
| 144 | inline constexpr bool is_error_condition_enum_v = is_error_condition_enum<T>::value;
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * 19.5.1, class error_category:
|
|---|
| 148 | */
|
|---|
| 149 |
|
|---|
| 150 | class error_category
|
|---|
| 151 | {
|
|---|
| 152 | public:
|
|---|
| 153 | constexpr error_category() noexcept = default;
|
|---|
| 154 | virtual ~error_category();
|
|---|
| 155 |
|
|---|
| 156 | error_category(const error_category&) = delete;
|
|---|
| 157 | error_category& operator=(const error_category&) = delete;
|
|---|
| 158 |
|
|---|
| 159 | virtual const char* name() const noexcept = 0;
|
|---|
| 160 | virtual error_condition default_error_condition(int) const noexcept;
|
|---|
| 161 | virtual bool equivalent(int, const error_condition&) const noexcept;
|
|---|
| 162 | virtual bool equivalent(const error_code&, int) const noexcept;
|
|---|
| 163 | virtual string message(int) const = 0;
|
|---|
| 164 |
|
|---|
| 165 | bool operator==(const error_category&) const noexcept;
|
|---|
| 166 | bool operator!=(const error_category&) const noexcept;
|
|---|
| 167 | bool operator<(const error_category&) const noexcept;
|
|---|
| 168 | };
|
|---|
| 169 |
|
|---|
| 170 | const error_category& generic_category() noexcept;
|
|---|
| 171 | const error_category& system_category() noexcept;
|
|---|
| 172 |
|
|---|
| 173 | /**
|
|---|
| 174 | * 19.5.2, class error_code:
|
|---|
| 175 | */
|
|---|
| 176 |
|
|---|
| 177 | class error_code
|
|---|
| 178 | {
|
|---|
| 179 | public:
|
|---|
| 180 | /**
|
|---|
| 181 | * 19.5.2.2, constructors:
|
|---|
| 182 | */
|
|---|
| 183 |
|
|---|
| 184 | error_code() noexcept;
|
|---|
| 185 | error_code(int, const error_category&) noexcept;
|
|---|
| 186 |
|
|---|
| 187 | template<class ErrorCodeEnum>
|
|---|
| 188 | error_code(
|
|---|
| 189 | enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
|
|---|
| 190 | ) noexcept
|
|---|
| 191 | {
|
|---|
| 192 | val_ = static_cast<int>(e);
|
|---|
| 193 | cat_ = &generic_category();
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * 19.5.2.3, modifiers:
|
|---|
| 198 | */
|
|---|
| 199 |
|
|---|
| 200 | void assign(int, const error_category&) noexcept;
|
|---|
| 201 |
|
|---|
| 202 | template<class ErrorCodeEnum>
|
|---|
| 203 | error_code& operator=(
|
|---|
| 204 | enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
|
|---|
| 205 | ) noexcept
|
|---|
| 206 | {
|
|---|
| 207 | val_ = static_cast<int>(e);
|
|---|
| 208 | cat_ = &generic_category();
|
|---|
| 209 |
|
|---|
| 210 | return *this;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | void clear() noexcept;
|
|---|
| 214 |
|
|---|
| 215 | /**
|
|---|
| 216 | * 19.5.2.4, observers:
|
|---|
| 217 | */
|
|---|
| 218 |
|
|---|
| 219 | int value() const noexcept;
|
|---|
| 220 | const error_category& category() const noexcept;
|
|---|
| 221 | error_condition default_error_condition() const noexcept;
|
|---|
| 222 | string message() const;
|
|---|
| 223 |
|
|---|
| 224 | explicit operator bool() const noexcept
|
|---|
| 225 | {
|
|---|
| 226 | return val_ != 0;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | private:
|
|---|
| 230 | int val_;
|
|---|
| 231 | const error_category* cat_;
|
|---|
| 232 | };
|
|---|
| 233 |
|
|---|
| 234 | /**
|
|---|
| 235 | * 19.5.2.5, non-member functions:
|
|---|
| 236 | */
|
|---|
| 237 |
|
|---|
| 238 | error_code make_error_code(errc e) noexcept;
|
|---|
| 239 | bool operator<(const error_code&, const error_code&) noexcept;
|
|---|
| 240 |
|
|---|
| 241 | template<class Char, class Traits>
|
|---|
| 242 | basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os,
|
|---|
| 243 | const error_code& ec)
|
|---|
| 244 | {
|
|---|
| 245 | return os << ec.category().name() << ": " << ec.value();
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /**
|
|---|
| 249 | * 19.5.3, class error_condition:
|
|---|
| 250 | */
|
|---|
| 251 |
|
|---|
| 252 | class error_condition
|
|---|
| 253 | {
|
|---|
| 254 | public:
|
|---|
| 255 | /**
|
|---|
| 256 | * 19.5.3.2, constructors:
|
|---|
| 257 | */
|
|---|
| 258 |
|
|---|
| 259 | error_condition() noexcept;
|
|---|
| 260 | error_condition(int, const error_category&) noexcept;
|
|---|
| 261 |
|
|---|
| 262 | template<class ErrorCodeEnum>
|
|---|
| 263 | error_condition(
|
|---|
| 264 | enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
|
|---|
| 265 | ) noexcept
|
|---|
| 266 | {
|
|---|
| 267 | val_ = static_cast<int>(e);
|
|---|
| 268 | cat_ = &generic_category();
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /**
|
|---|
| 272 | * 19.5.3.3, modifiers:
|
|---|
| 273 | */
|
|---|
| 274 |
|
|---|
| 275 | void assign(int, const error_category&) noexcept;
|
|---|
| 276 |
|
|---|
| 277 | template<class ErrorCodeEnum>
|
|---|
| 278 | error_condition& operator=(
|
|---|
| 279 | enable_if_t<is_error_code_enum_v<ErrorCodeEnum>, ErrorCodeEnum> e
|
|---|
| 280 | ) noexcept
|
|---|
| 281 | {
|
|---|
| 282 | val_ = static_cast<int>(e);
|
|---|
| 283 | cat_ = &generic_category();
|
|---|
| 284 |
|
|---|
| 285 | return *this;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | void clear() noexcept;
|
|---|
| 289 |
|
|---|
| 290 | /**
|
|---|
| 291 | * 19.5.3.4, observers:
|
|---|
| 292 | */
|
|---|
| 293 |
|
|---|
| 294 | int value() const noexcept;
|
|---|
| 295 | const error_category& category() const noexcept;
|
|---|
| 296 | string message() const;
|
|---|
| 297 |
|
|---|
| 298 | explicit operator bool() const noexcept
|
|---|
| 299 | {
|
|---|
| 300 | return val_ != 0;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | private:
|
|---|
| 304 | int val_;
|
|---|
| 305 | const error_category* cat_;
|
|---|
| 306 | };
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | * 19.5.3.4, non-member functions:
|
|---|
| 310 | */
|
|---|
| 311 |
|
|---|
| 312 | error_condition make_error_condition(errc e) noexcept;
|
|---|
| 313 | bool operator<(const error_condition&, const error_condition&) noexcept;
|
|---|
| 314 |
|
|---|
| 315 | /**
|
|---|
| 316 | * 19.5.4, comparison operators:
|
|---|
| 317 | */
|
|---|
| 318 |
|
|---|
| 319 | bool operator==(const error_code&, const error_code&) noexcept;
|
|---|
| 320 | bool operator==(const error_code&, const error_condition&) noexcept;
|
|---|
| 321 | bool operator==(const error_condition&, const error_code&) noexcept;
|
|---|
| 322 | bool operator==(const error_condition&, const error_condition&) noexcept;
|
|---|
| 323 | bool operator!=(const error_code&, const error_code&) noexcept;
|
|---|
| 324 | bool operator!=(const error_code&, const error_condition&) noexcept;
|
|---|
| 325 | bool operator!=(const error_condition&, const error_code&) noexcept;
|
|---|
| 326 | bool operator!=(const error_condition&, const error_condition&) noexcept;
|
|---|
| 327 |
|
|---|
| 328 | /**
|
|---|
| 329 | * 19.5.6, class system_error:
|
|---|
| 330 | */
|
|---|
| 331 |
|
|---|
| 332 | class system_error: public runtime_error
|
|---|
| 333 | {
|
|---|
| 334 | public:
|
|---|
| 335 | system_error(error_code, const string&);
|
|---|
| 336 | system_error(error_code, const char*);
|
|---|
| 337 | system_error(error_code);
|
|---|
| 338 | system_error(int, const error_category&, const string&);
|
|---|
| 339 | system_error(int, const error_category&, const char*);
|
|---|
| 340 | system_error(int, const error_category&);
|
|---|
| 341 |
|
|---|
| 342 | const error_code& code() const noexcept;
|
|---|
| 343 |
|
|---|
| 344 | private:
|
|---|
| 345 | error_code code_;
|
|---|
| 346 | };
|
|---|
| 347 |
|
|---|
| 348 | /**
|
|---|
| 349 | * 19.5.5, hash support:
|
|---|
| 350 | */
|
|---|
| 351 |
|
|---|
| 352 | template<class>
|
|---|
| 353 | struct hash;
|
|---|
| 354 |
|
|---|
| 355 | template<>
|
|---|
| 356 | struct hash<error_code>
|
|---|
| 357 | {
|
|---|
| 358 | size_t operator()(const error_code& ec) const noexcept
|
|---|
| 359 | {
|
|---|
| 360 | return static_cast<size_t>(ec.value());
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | using result_type = size_t;
|
|---|
| 364 | using argument_type = error_code;
|
|---|
| 365 | };
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | #endif
|
|---|