-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror.cpp
More file actions
658 lines (600 loc) · 17.9 KB
/
Copy patherror.cpp
File metadata and controls
658 lines (600 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#include "include/error.hpp"
#if __cplusplus >= 201703L
#include <any>
#include <variant>
#include <optional>
#endif
#include <functional>
namespace stdx {
namespace {
inline const char* dynamic_exception_errc_str(unsigned ev) noexcept
{
constexpr const char* msg[] =
{
"Success",
"std::runtime_error",
"std::domain_error",
"std::invalid_argument",
"std::length_error",
"std::out_of_range",
"std::logic_error",
"std::range_error",
"std::overflow_error",
"std::underflow_error",
"std::bad_alloc",
"std::bad_array_new_length",
"std::bad_optional_access",
"std::bad_typeid",
"std::bad_any_cast",
"std::bad_cast",
"std::bad_weak_ptr",
"std::bad_function_call",
"std::bad_exception",
"std::bad_variant_access",
"unspecified dynamic exception"
};
assert(ev < (sizeof(msg) / sizeof(const char*)));
return msg[ev];
}
class dynamic_exception_error_category : public std::error_category
{
public:
const char* name() const noexcept override
{
return "dynamic_exception";
}
std::string message(int code) const override
{
return dynamic_exception_errc_str(code);
}
bool equivalent(int code, const std::error_condition& cond) const noexcept override
{
switch (static_cast<dynamic_exception_errc>(code))
{
case dynamic_exception_errc::domain_error:
return (cond == std::errc::argument_out_of_domain);
case dynamic_exception_errc::invalid_argument:
return (cond == std::errc::invalid_argument);
case dynamic_exception_errc::length_error:
return (cond == std::errc::value_too_large);
case dynamic_exception_errc::out_of_range:
case dynamic_exception_errc::range_error:
case dynamic_exception_errc::underflow_error:
return (cond == std::errc::result_out_of_range);
case dynamic_exception_errc::overflow_error:
return (cond == std::errc::value_too_large);
case dynamic_exception_errc::bad_alloc:
case dynamic_exception_errc::bad_array_new_length:
return (cond == std::errc::not_enough_memory);
default:;
}
return false;
}
};
const dynamic_exception_error_category dynamic_exception_error_category_instance;
} // end anonymous namespace
const std::error_category& dynamic_exception_category() noexcept
{
return dynamic_exception_error_category_instance;
}
std::error_code error_code_from_exception(
std::exception_ptr eptr,
std::error_code not_matched
) noexcept
{
if (!eptr) return make_error_code(dynamic_exception_errc::bad_exception);
try { std::rethrow_exception(eptr); }
catch (const std::domain_error&)
{
return make_error_code(dynamic_exception_errc::domain_error);
}
catch (const std::invalid_argument&)
{
return make_error_code(dynamic_exception_errc::invalid_argument);
}
catch (const std::length_error&)
{
return make_error_code(dynamic_exception_errc::length_error);
}
catch (const std::out_of_range&)
{
return make_error_code(dynamic_exception_errc::out_of_range);
}
catch (const std::logic_error&)
{
return make_error_code(dynamic_exception_errc::logic_error);
}
catch (const std::range_error&)
{
return make_error_code(dynamic_exception_errc::range_error);
}
catch (const std::overflow_error&) {
return make_error_code(dynamic_exception_errc::overflow_error);
}
catch (const std::underflow_error&) {
return make_error_code(dynamic_exception_errc::underflow_error);
}
catch (const std::system_error& e)
{
return e.code();
}
catch (const std::runtime_error&)
{
return make_error_code(dynamic_exception_errc::runtime_error);
}
catch (const std::bad_array_new_length&)
{
return make_error_code(dynamic_exception_errc::bad_array_new_length);
}
catch (const std::bad_alloc&)
{
return make_error_code(dynamic_exception_errc::bad_alloc);
}
catch (const std::bad_typeid&)
{
return make_error_code(dynamic_exception_errc::bad_typeid);
}
#if __cplusplus >= 201703L
catch (const std::bad_optional_access&)
{
return make_error_code(dynamic_exception_errc::bad_optional_access);
}
catch (const std::bad_any_cast&)
{
return make_error_code(dynamic_exception_errc::bad_any_cast);
}
catch (const std::bad_variant_access&)
{
return make_error_code(dynamic_exception_errc::bad_variant_access);
}
#endif
catch (const std::bad_cast&)
{
return make_error_code(dynamic_exception_errc::bad_cast);
}
catch (const std::bad_weak_ptr&)
{
return make_error_code(dynamic_exception_errc::bad_weak_ptr);
}
catch (const std::bad_function_call&)
{
return make_error_code(dynamic_exception_errc::bad_function_call);
}
catch (const std::bad_exception&)
{
return make_error_code(dynamic_exception_errc::bad_exception);
}
catch (...)
{ }
return not_matched;
}
error error_from_exception(std::exception_ptr eptr) noexcept
{
if (!eptr) return make_error(dynamic_exception_errc::bad_exception);
try
{
std::rethrow_exception(eptr);
}
catch (const std::domain_error&)
{
return make_error(dynamic_exception_errc::domain_error);
}
catch (const std::invalid_argument&)
{
return make_error(dynamic_exception_errc::invalid_argument);
}
catch (const std::length_error&)
{
return make_error(dynamic_exception_errc::length_error);
}
catch (const std::out_of_range&)
{
return make_error(dynamic_exception_errc::out_of_range);
}
catch (const std::logic_error&)
{
return make_error(dynamic_exception_errc::logic_error);
}
catch (const std::range_error&)
{
return make_error(dynamic_exception_errc::range_error);
}
catch (const std::overflow_error&)
{
return make_error(dynamic_exception_errc::overflow_error);
}
catch (const std::underflow_error&)
{
return make_error(dynamic_exception_errc::underflow_error);
}
catch (const std::system_error& e)
{
return error{e.code()};
}
catch (const std::runtime_error&)
{
return make_error(dynamic_exception_errc::runtime_error);
}
catch (const std::bad_array_new_length&)
{
return make_error(dynamic_exception_errc::bad_array_new_length);
}
catch (const std::bad_alloc&)
{
return make_error(dynamic_exception_errc::bad_alloc);
}
catch (const std::bad_typeid&)
{
return make_error(dynamic_exception_errc::bad_typeid);
}
#if __cplusplus >= 201703L
catch (const std::bad_optional_access&)
{
return make_error(dynamic_exception_errc::bad_optional_access);
}
catch (const std::bad_any_cast&)
{
return make_error(dynamic_exception_errc::bad_any_cast);
}
catch (const std::bad_variant_access&)
{
return make_error(dynamic_exception_errc::bad_variant_access);
}
#endif
catch (const std::bad_cast&)
{
return make_error(dynamic_exception_errc::bad_cast);
}
catch (const std::bad_weak_ptr&)
{
return make_error(dynamic_exception_errc::bad_weak_ptr);
}
catch (const std::bad_function_call&)
{
return make_error(dynamic_exception_errc::bad_function_call);
}
catch (const std::bad_exception&)
{
return make_error(dynamic_exception_errc::bad_exception);
}
catch (...)
{ }
return make_error(dynamic_exception_errc::unspecified_exception);
}
// ---------- ErrorDomain (abstract base class)
//
void error_domain::throw_exception(const error& e) const
{
throw thrown_dynamic_exception{e};
}
// ---------- GenericErrorDomain
//
bool generic_error_domain::equivalent(const error& lhs, const error& rhs) const noexcept
{
assert(lhs.domain() == *this);
if (lhs.domain() == rhs.domain())
{
return error_cast<std::errc>(lhs) == error_cast<std::errc>(rhs);
}
return false;
}
namespace {
string_ref generic_error_code_message(std::errc code) noexcept
{
switch (code)
{
case std::errc::address_family_not_supported:
return "Address family not supported by protocol";
case std::errc::address_in_use:
return "Address already in use";
case std::errc::address_not_available:
return "Cannot assign requested address";
case std::errc::already_connected:
return "Transport endpoint is already connected";
case std::errc::argument_list_too_long:
return "Argument list too long";
case std::errc::argument_out_of_domain:
return "Numerical argument out of domain";
case std::errc::bad_address:
return "Bad address";
case std::errc::bad_file_descriptor:
return "Bad file descriptor";
case std::errc::bad_message:
return "Bad message";
case std::errc::broken_pipe:
return "Broken pipe";
case std::errc::connection_aborted:
return "Software caused connection abort";
case std::errc::connection_already_in_progress:
return "Operation already in progress";
case std::errc::connection_refused:
return "Connection refused";
case std::errc::connection_reset:
return "Connection reset by peer";
case std::errc::cross_device_link:
return "Invalid cross-device link";
case std::errc::destination_address_required:
return "Destination address required";
case std::errc::device_or_resource_busy:
return "Device or resource busy";
case std::errc::directory_not_empty:
return "Directory not empty";
case std::errc::executable_format_error:
return "Exec format error";
case std::errc::file_exists:
return "File exists";
case std::errc::file_too_large:
return "File too large";
case std::errc::filename_too_long:
return "File name too long";
case std::errc::function_not_supported:
return "Function not implemented";
case std::errc::host_unreachable:
return "No route to host";
case std::errc::identifier_removed:
return "Identifier removed";
case std::errc::illegal_byte_sequence:
return "Invalid or incomplete multibyte or wide character";
case std::errc::inappropriate_io_control_operation:
return "Inappropriate ioctl for device";
case std::errc::interrupted:
return "Interrupted system call";
case std::errc::invalid_argument:
return "Invalid argument";
case std::errc::invalid_seek:
return "Illegal seek";
case std::errc::io_error:
return "Input/output error";
case std::errc::is_a_directory:
return "Is a directory";
case std::errc::message_size:
return "Message too long";
case std::errc::network_down:
return "Network is down";
case std::errc::network_reset:
return "Network dropped connection on reset";
case std::errc::network_unreachable:
return "Network is unreachable";
case std::errc::no_buffer_space:
return "No buffer space available";
case std::errc::no_child_process:
return "No child processes";
case std::errc::no_link:
return "Link has been severed";
case std::errc::no_lock_available:
return "No locks available";
case std::errc::no_message:
return "No message of desired type";
case std::errc::no_protocol_option:
return "Protocol not available";
case std::errc::no_space_on_device:
return "No space left on device";
case std::errc::no_stream_resources:
return "Out of streams resources";
case std::errc::no_such_device_or_address:
return "No such device or address";
case std::errc::no_such_device:
return "No such device";
case std::errc::no_such_file_or_directory:
return "No such file or directory";
case std::errc::no_such_process:
return "No such process";
case std::errc::not_a_directory:
return "Not a directory";
case std::errc::not_a_socket:
return "Socket operation on non-socket";
case std::errc::not_a_stream:
return "Device not a stream";
case std::errc::not_connected:
return "Transport endpoint is not connected";
case std::errc::not_enough_memory:
return "Cannot allocate memory";
#if ENOTSUP != EOPNOTSUPP
case std::errc::not_supported:
return "Operation not supported";
#endif
case std::errc::operation_canceled:
return "Operation canceled";
case std::errc::operation_in_progress:
return "Operation now in progress";
case std::errc::operation_not_permitted:
return "Operation not permitted";
case std::errc::operation_not_supported:
return "Operation not supported";
#if EAGAIN != EWOULDBLOCK
case std::errc::operation_would_block:
return "Resource temporarily unavailable";
#endif
case std::errc::owner_dead:
return "Owner died";
case std::errc::permission_denied:
return "Permission denied";
case std::errc::protocol_error:
return "Protocol error";
case std::errc::protocol_not_supported:
return "Protocol not supported";
case std::errc::read_only_file_system:
return "Read-only file system";
case std::errc::resource_deadlock_would_occur:
return "Resource deadlock avoided";
case std::errc::resource_unavailable_try_again:
return "Resource temporarily unavailable";
case std::errc::result_out_of_range:
return "Numerical result out of range";
case std::errc::state_not_recoverable:
return "State not recoverable";
case std::errc::stream_timeout:
return "Timer expired";
case std::errc::text_file_busy:
return "Text file busy";
case std::errc::timed_out:
return "Connection timed out";
case std::errc::too_many_files_open_in_system:
return "Too many open files in system";
case std::errc::too_many_files_open:
return "Too many open files";
case std::errc::too_many_links:
return "Too many links";
case std::errc::too_many_symbolic_link_levels:
return "Too many levels of symbolic links";
case std::errc::value_too_large:
return "Value too large for defined data type";
case std::errc::wrong_protocol_type:
return "Protocol wrong type for socket";
default:
return "Unspecified error";
}
}
} // end anonymous namespace
string_ref generic_error_domain::message(const error& e) const noexcept
{
assert(e.domain() == *this);
return generic_error_code_message(error_cast<std::errc>(e));
}
// ---------- ErrorCodeErrorDomain
//
string_ref error_code_error_domain::message(const error& e) const noexcept
{
assert(e.domain() == *this);
auto ptr = error_cast<internal_value_type>(e);
if (ptr)
{
std::string msg = ptr->code.message();
return shared_string_ref{msg.c_str(), msg.c_str() + msg.size()};
}
return string_ref{"Bad error code"};
}
void error_code_error_domain::throw_exception(const error& e) const
{
assert(e.domain() == *this);
std::error_code code;
auto ptr = error_cast<internal_value_type>(e);
if (ptr) code = ptr->code;
throw std::system_error{code};
}
bool error_code_error_domain::equivalent(const error& lhs, const error& rhs) const noexcept
{
assert(lhs.domain() == *this);
if (lhs.domain() == rhs.domain())
{
auto ptr1 = error_cast<internal_value_type>(lhs);
auto ptr2 = error_cast<internal_value_type>(rhs);
if (ptr1 && ptr2) return ptr1->code == ptr2->code.default_error_condition();
return false;
}
if (rhs.domain() == generic_domain)
{
auto ptr1 = error_cast<internal_value_type>(lhs);
if (ptr1) return ptr1->code == error_cast<std::errc>(rhs);
}
return false;
}
stdx::error error_traits<std::error_code>::to_error(std::error_code ec) noexcept
{
using internal_value_type = error_code_error_domain::internal_value_type;
if (ec.category() == std::generic_category())
{
return error{
error_value<std::errc>{static_cast<std::errc>(ec.default_error_condition().value())},
generic_domain
};
}
return error{
error_value<internal_value_type>{internal_value_type{new detail::error_code_wrapper{ec}}},
error_code_domain
};
}
// ---------- DynamicExceptionErrorDomain
//
string_ref dynamic_exception_error_domain::message(const error& e) const noexcept
{
assert(e.domain() == *this);
std::exception_ptr eptr = error_cast<detail::exception_ptr_wrapper>(e).get();
try
{
std::rethrow_exception(eptr);
}
catch (const std::exception& ex)
{
return shared_string_ref{ex.what()};
}
catch (...) {}
return string_ref{"Unknown dynamic exception"};
}
namespace {
std::errc dynamic_exception_code_to_generic_code(dynamic_exception_errc code) noexcept
{
switch (code)
{
case dynamic_exception_errc::domain_error:
return std::errc::argument_out_of_domain;
case dynamic_exception_errc::invalid_argument:
return std::errc::invalid_argument;
case dynamic_exception_errc::length_error:
return std::errc::value_too_large;
case dynamic_exception_errc::out_of_range:
case dynamic_exception_errc::range_error:
case dynamic_exception_errc::underflow_error:
return std::errc::result_out_of_range;
case dynamic_exception_errc::overflow_error:
return std::errc::value_too_large;
case dynamic_exception_errc::bad_alloc:
case dynamic_exception_errc::bad_array_new_length:
return std::errc::not_enough_memory;
default:;
}
return std::errc{};
}
} // end anonymous namespace
bool dynamic_exception_error_domain::equivalent(const error& lhs, const error& rhs) const noexcept
{
assert(lhs.domain() == *this);
std::exception_ptr eptr = error_cast<detail::exception_ptr_wrapper>(lhs).get();
if (rhs.domain() == *this)
{
std::exception_ptr eptr2 = error_cast<detail::exception_ptr_wrapper>(rhs).get();
if (eptr == eptr2) return true;
error e1 = error_from_exception(eptr);
error e2 = error_from_exception(eptr2);
return e1.domain().equivalent(e1, e2);
}
else if (rhs.domain() == error_code_domain)
{
std::error_code ec = error_code_from_exception(eptr);
return error_code_domain.equivalent(rhs, error{ec});
}
error e = error_from_exception(eptr);
return e.domain().equivalent(e, rhs);
}
// ---------- DynamicExceptionCodeErrorDomain
//
bool dynamic_exception_code_error_domain::equivalent(
const error& lhs,
const error& rhs
) const noexcept
{
assert(lhs.domain() == *this);
const dynamic_exception_errc code = error_cast<dynamic_exception_errc>(lhs);
if (rhs.domain() == *this)
{
return code == error_cast<dynamic_exception_errc>(rhs);
}
else if (rhs.domain() == error_code_domain)
{
return error_code_domain.equivalent(rhs, make_error_code(code));
}
else if (rhs.domain() == generic_domain)
{
std::errc generic_code = dynamic_exception_code_to_generic_code(code);
return generic_domain.equivalent(rhs, generic_code);
}
return false;
}
string_ref dynamic_exception_code_error_domain::message(const error& e) const noexcept
{
assert(e.domain() == *this);
return string_ref{
dynamic_exception_errc_str(static_cast<unsigned>(error_cast<dynamic_exception_errc>(e)))
};
}
} // end namespace stdx