1 #ifndef BOOST_SYSTEM_ERROR_CODE_HPP_INCLUDED
2 #define BOOST_SYSTEM_ERROR_CODE_HPP_INCLUDED
3
4 // Copyright Beman Dawes 2006, 2007
5 // Copyright Christoper Kohlhoff 2007
6 // Copyright Peter Dimov 2017, 2018
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // See library home page at http://www.boost.org/libs/system
12
13 #include <boost/system/api_config.hpp>
14 #include <boost/system/detail/config.hpp>
15 #include <boost/cstdint.hpp>
16 #include <boost/config.hpp>
17 #include <ostream>
18 #include <string>
19 #include <functional>
20 #include <cstring>
21
22 // TODO: undef these macros if not already defined
23 #include <boost/cerrno.hpp>
24
25 #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
26 # include <system_error>
27 #endif
28
29 #if !defined(BOOST_POSIX_API) && !defined(BOOST_WINDOWS_API)
30 # error BOOST_POSIX_API or BOOST_WINDOWS_API must be defined
31 #endif
32
33 namespace boost
34 {
35
36 namespace system
37 {
38
39 class error_code; // values defined by the operating system
40 class error_condition; // portable generic values defined below, but ultimately
41 // based on the POSIX standard
42
43 // "Concept" helpers
44
45 template<class T> struct is_error_code_enum
46 {
47 static const bool value = false;
48 };
49
50 template<class T> struct is_error_condition_enum
51 {
52 static const bool value = false;
53 };
54
55 // Generic error_conditions
56
57 namespace errc
58 {
59
60 enum errc_t
61 {
62 success = 0,
63 address_family_not_supported = EAFNOSUPPORT,
64 address_in_use = EADDRINUSE,
65 address_not_available = EADDRNOTAVAIL,
66 already_connected = EISCONN,
67 argument_list_too_long = E2BIG,
68 argument_out_of_domain = EDOM,
69 bad_address = EFAULT,
70 bad_file_descriptor = EBADF,
71 bad_message = EBADMSG,
72 broken_pipe = EPIPE,
73 connection_aborted = ECONNABORTED,
74 connection_already_in_progress = EALREADY,
75 connection_refused = ECONNREFUSED,
76 connection_reset = ECONNRESET,
77 cross_device_link = EXDEV,
78 destination_address_required = EDESTADDRREQ,
79 device_or_resource_busy = EBUSY,
80 directory_not_empty = ENOTEMPTY,
81 executable_format_error = ENOEXEC,
82 file_exists = EEXIST,
83 file_too_large = EFBIG,
84 filename_too_long = ENAMETOOLONG,
85 function_not_supported = ENOSYS,
86 host_unreachable = EHOSTUNREACH,
87 identifier_removed = EIDRM,
88 illegal_byte_sequence = EILSEQ,
89 inappropriate_io_control_operation = ENOTTY,
90 interrupted = EINTR,
91 invalid_argument = EINVAL,
92 invalid_seek = ESPIPE,
93 io_error = EIO,
94 is_a_directory = EISDIR,
95 message_size = EMSGSIZE,
96 network_down = ENETDOWN,
97 network_reset = ENETRESET,
98 network_unreachable = ENETUNREACH,
99 no_buffer_space = ENOBUFS,
100 no_child_process = ECHILD,
101 no_link = ENOLINK,
102 no_lock_available = ENOLCK,
103 no_message_available = ENODATA,
104 no_message = ENOMSG,
105 no_protocol_option = ENOPROTOOPT,
106 no_space_on_device = ENOSPC,
107 no_stream_resources = ENOSR,
108 no_such_device_or_address = ENXIO,
109 no_such_device = ENODEV,
110 no_such_file_or_directory = ENOENT,
111 no_such_process = ESRCH,
112 not_a_directory = ENOTDIR,
113 not_a_socket = ENOTSOCK,
114 not_a_stream = ENOSTR,
115 not_connected = ENOTCONN,
116 not_enough_memory = ENOMEM,
117 not_supported = ENOTSUP,
118 operation_canceled = ECANCELED,
119 operation_in_progress = EINPROGRESS,
120 operation_not_permitted = EPERM,
121 operation_not_supported = EOPNOTSUPP,
122 operation_would_block = EWOULDBLOCK,
123 owner_dead = EOWNERDEAD,
124 permission_denied = EACCES,
125 protocol_error = EPROTO,
126 protocol_not_supported = EPROTONOSUPPORT,
127 read_only_file_system = EROFS,
128 resource_deadlock_would_occur = EDEADLK,
129 resource_unavailable_try_again = EAGAIN,
130 result_out_of_range = ERANGE,
131 state_not_recoverable = ENOTRECOVERABLE,
132 stream_timeout = ETIME,
133 text_file_busy = ETXTBSY,
134 timed_out = ETIMEDOUT,
135 too_many_files_open_in_system = ENFILE,
136 too_many_files_open = EMFILE,
137 too_many_links = EMLINK,
138 too_many_symbolic_link_levels = ELOOP,
139 value_too_large = EOVERFLOW,
140 wrong_protocol_type = EPROTOTYPE
141 };
142
143 } // namespace errc
144
145 #ifdef BOOST_SYSTEM_ENABLE_DEPRECATED
146
147 namespace posix = errc;
148 namespace posix_error = errc;
149
150 #endif
151
152 template<> struct is_error_condition_enum<errc::errc_t>
153 {
154 static const bool value = true;
155 };
156
157 // class error_category
158 #if ( defined( BOOST_GCC ) && BOOST_GCC >= 40600 ) || defined( BOOST_CLANG )
159 #pragma GCC diagnostic push
160 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
161 #endif
162
163 #ifdef BOOST_MSVC
164 #pragma warning( push )
165 // 'this' : used in base member initializer list
166 #pragma warning( disable: 4355 )
167 #endif
168
169 std::size_t hash_value( error_code const & ec );
170
171 class BOOST_SYMBOL_VISIBLE error_category
172 {
173 private:
174
175 friend std::size_t hash_value( error_code const & ec );
176
177 #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
178 public:
179
180 error_category( error_category const & ) = delete;
181 error_category& operator=( error_category const & ) = delete;
182
183 #else
184 private:
185
186 error_category( error_category const & );
187 error_category& operator=( error_category const & );
188
189 #endif
190
191 private:
192
193 boost::ulong_long_type id_;
194
195 protected:
196
197 #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && !defined(BOOST_NO_CXX11_NON_PUBLIC_DEFAULTED_FUNCTIONS)
198
199 ~error_category() = default;
200
201 #else
202
203 // We'd like to make the destructor protected, to make code that deletes
204 // an error_category* not compile; unfortunately, doing the below makes
205 // the destructor user-provided and hence breaks use after main, as the
206 // categories may get destroyed before code that uses them
207
208 // ~error_category() {}
209
210 #endif
211
error_category()212 BOOST_SYSTEM_CONSTEXPR error_category() BOOST_NOEXCEPT: id_( 0 )
213 {
214 }
215
error_category(boost::ulong_long_type id)216 explicit BOOST_SYSTEM_CONSTEXPR error_category( boost::ulong_long_type id ) BOOST_NOEXCEPT: id_( id )
217 {
218 }
219
220 public:
221
222 virtual const char * name() const BOOST_NOEXCEPT = 0;
223
224 virtual error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT;
225 virtual bool equivalent( int code, const error_condition & condition ) const BOOST_NOEXCEPT;
226 virtual bool equivalent( const error_code & code, int condition ) const BOOST_NOEXCEPT;
227
228 virtual std::string message( int ev ) const = 0;
229 virtual char const * message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT;
230
231 virtual bool failed( int ev ) const BOOST_NOEXCEPT;
232
operator ==(const error_category & rhs) const233 BOOST_SYSTEM_CONSTEXPR bool operator==( const error_category & rhs ) const BOOST_NOEXCEPT
234 {
235 return rhs.id_ == 0? this == &rhs: id_ == rhs.id_;
236 }
237
operator !=(const error_category & rhs) const238 BOOST_SYSTEM_CONSTEXPR bool operator!=( const error_category & rhs ) const BOOST_NOEXCEPT
239 {
240 return !( *this == rhs );
241 }
242
operator <(const error_category & rhs) const243 BOOST_SYSTEM_CONSTEXPR bool operator<( const error_category & rhs ) const BOOST_NOEXCEPT
244 {
245 if( id_ < rhs.id_ )
246 {
247 return true;
248 }
249
250 if( id_ > rhs.id_ )
251 {
252 return false;
253 }
254
255 if( rhs.id_ != 0 )
256 {
257 return false; // equal
258 }
259
260 return std::less<error_category const *>()( this, &rhs );
261 }
262
263 #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
264
265 operator std::error_category const & () const;
266
267 #endif
268 };
269
270 #ifdef BOOST_MSVC
271 #pragma warning( pop )
272 #endif
273
274 // predefined error categories
275
276 namespace detail
277 {
278
279 class BOOST_SYMBOL_VISIBLE generic_error_category: public error_category
280 {
281 public:
282
283 // clang++ 3.8 and below: initialization of const object
284 // requires a user-provided default constructor
generic_error_category()285 BOOST_SYSTEM_CONSTEXPR generic_error_category() BOOST_NOEXCEPT:
286 error_category( ( boost::ulong_long_type( 0xB2AB117A ) << 32 ) + 0x257EDF0D )
287 {
288 }
289
name() const290 const char * name() const BOOST_NOEXCEPT
291 {
292 return "generic";
293 }
294
295 std::string message( int ev ) const;
296 char const * message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT;
297 };
298
299 class BOOST_SYMBOL_VISIBLE system_error_category: public error_category
300 {
301 public:
302
system_error_category()303 BOOST_SYSTEM_CONSTEXPR system_error_category() BOOST_NOEXCEPT:
304 error_category( ( boost::ulong_long_type( 0x8FAFD21E ) << 32 ) + 0x25C5E09B )
305 {
306 }
307
name() const308 const char * name() const BOOST_NOEXCEPT
309 {
310 return "system";
311 }
312
313 error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT;
314
315 std::string message( int ev ) const;
316 char const * message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT;
317 };
318
319 } // namespace detail
320
321 #if ( defined( BOOST_GCC ) && BOOST_GCC >= 40600 ) || defined( BOOST_CLANG )
322 #pragma GCC diagnostic pop
323 #endif
324
325 // generic_category(), system_category()
326
327 #if defined(BOOST_SYSTEM_HAS_CONSTEXPR)
328
329 namespace detail
330 {
331
332 template<class T> struct cat_holder
333 {
334 BOOST_SYSTEM_REQUIRE_CONST_INIT static constexpr system_error_category system_category_instance{};
335 BOOST_SYSTEM_REQUIRE_CONST_INIT static constexpr generic_error_category generic_category_instance{};
336 };
337
338 template<class T> BOOST_SYSTEM_REQUIRE_CONST_INIT constexpr system_error_category cat_holder<T>::system_category_instance;
339 template<class T> BOOST_SYSTEM_REQUIRE_CONST_INIT constexpr generic_error_category cat_holder<T>::generic_category_instance;
340
341 } // namespace detail
342
system_category()343 constexpr error_category const & system_category() BOOST_NOEXCEPT
344 {
345 return detail::cat_holder<void>::system_category_instance;
346 }
347
generic_category()348 constexpr error_category const & generic_category() BOOST_NOEXCEPT
349 {
350 return detail::cat_holder<void>::generic_category_instance;
351 }
352
353 #else // #if defined(BOOST_SYSTEM_HAS_CONSTEXPR)
354
system_category()355 inline error_category const & system_category() BOOST_NOEXCEPT
356 {
357 static const detail::system_error_category system_category_instance;
358 return system_category_instance;
359 }
360
generic_category()361 inline error_category const & generic_category() BOOST_NOEXCEPT
362 {
363 static const detail::generic_error_category generic_category_instance;
364 return generic_category_instance;
365 }
366
367 #endif // #if defined(BOOST_SYSTEM_HAS_CONSTEXPR)
368
369 // deprecated synonyms
370
371 #ifdef BOOST_SYSTEM_ENABLE_DEPRECATED
372
get_system_category()373 inline const error_category & get_system_category() { return system_category(); }
get_generic_category()374 inline const error_category & get_generic_category() { return generic_category(); }
get_posix_category()375 inline const error_category & get_posix_category() { return generic_category(); }
376 static const error_category & posix_category BOOST_ATTRIBUTE_UNUSED = generic_category();
377 static const error_category & errno_ecat BOOST_ATTRIBUTE_UNUSED = generic_category();
378 static const error_category & native_ecat BOOST_ATTRIBUTE_UNUSED = system_category();
379
380 #endif
381
382 // enable_if
383
384 namespace detail
385 {
386
387 template<bool C, class T = void> struct enable_if
388 {
389 typedef T type;
390 };
391
392 template<class T> struct enable_if<false, T>
393 {
394 };
395
396 // failed_impl
397
398 #if !defined(BOOST_SYSTEM_HAS_CONSTEXPR)
399
failed_impl(int ev,error_category const & cat)400 inline bool failed_impl( int ev, error_category const & cat )
401 {
402 return cat.failed( ev );
403 }
404
405 #else
406
failed_impl(int ev,error_category const & cat)407 BOOST_SYSTEM_CONSTEXPR inline bool failed_impl( int ev, error_category const & cat )
408 {
409 if( cat == system_category() || cat == generic_category() )
410 {
411 return ev != 0;
412 }
413 else
414 {
415 return cat.failed( ev );
416 }
417 }
418
419 #endif
420
421 } // namespace detail
422
423 // class error_condition
424
425 // error_conditions are portable, error_codes are system or library specific
426
427 class error_condition
428 {
429 private:
430
431 int val_;
432 bool failed_;
433 error_category const * cat_;
434
435 public:
436
437 // constructors:
438
error_condition()439 BOOST_SYSTEM_CONSTEXPR error_condition() BOOST_NOEXCEPT:
440 val_( 0 ), failed_( false ), cat_( &generic_category() )
441 {
442 }
443
error_condition(int val,const error_category & cat)444 BOOST_SYSTEM_CONSTEXPR error_condition( int val, const error_category & cat ) BOOST_NOEXCEPT:
445 val_( val ), failed_( detail::failed_impl( val, cat ) ), cat_( &cat )
446 {
447 }
448
error_condition(ErrorConditionEnum e,typename detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value>::type * =0)449 template<class ErrorConditionEnum> BOOST_SYSTEM_CONSTEXPR error_condition( ErrorConditionEnum e,
450 typename detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value>::type* = 0) BOOST_NOEXCEPT
451 {
452 *this = make_error_condition( e );
453 }
454
455 // modifiers:
456
assign(int val,const error_category & cat)457 BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) BOOST_NOEXCEPT
458 {
459 val_ = val;
460 failed_ = detail::failed_impl( val, cat );
461 cat_ = &cat;
462 }
463
464 template<typename ErrorConditionEnum>
465 BOOST_SYSTEM_CONSTEXPR typename detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value, error_condition>::type &
operator =(ErrorConditionEnum val)466 operator=( ErrorConditionEnum val ) BOOST_NOEXCEPT
467 {
468 *this = make_error_condition( val );
469 return *this;
470 }
471
clear()472 BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT
473 {
474 val_ = 0;
475 failed_ = false;
476 cat_ = &generic_category();
477 }
478
479 // observers:
480
value() const481 BOOST_SYSTEM_CONSTEXPR int value() const BOOST_NOEXCEPT
482 {
483 return val_;
484 }
485
category() const486 BOOST_SYSTEM_CONSTEXPR const error_category & category() const BOOST_NOEXCEPT
487 {
488 return *cat_;
489 }
490
message() const491 std::string message() const
492 {
493 return cat_->message( value() );
494 }
495
message(char * buffer,std::size_t len) const496 char const * message( char * buffer, std::size_t len ) const BOOST_NOEXCEPT
497 {
498 return cat_->message( value(), buffer, len );
499 }
500
failed() const501 BOOST_SYSTEM_CONSTEXPR bool failed() const BOOST_NOEXCEPT
502 {
503 return failed_;
504 }
505
506 #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
507
operator bool() const508 BOOST_SYSTEM_CONSTEXPR explicit operator bool() const BOOST_NOEXCEPT // true if error
509 {
510 return val_ != 0;
511 }
512
513 #else
514
515 typedef void (*unspecified_bool_type)();
unspecified_bool_true()516 static void unspecified_bool_true() {}
517
operator unspecified_bool_type() const518 BOOST_SYSTEM_CONSTEXPR operator unspecified_bool_type() const BOOST_NOEXCEPT // true if error
519 {
520 return val_ != 0? unspecified_bool_true: 0;
521 }
522
operator !() const523 BOOST_SYSTEM_CONSTEXPR bool operator!() const BOOST_NOEXCEPT // true if no error
524 {
525 return val_ == 0;
526 }
527
528 #endif
529
530 // relationals:
531 // the more symmetrical non-member syntax allows enum
532 // conversions work for both rhs and lhs.
533
operator ==(const error_condition & lhs,const error_condition & rhs)534 BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
535 {
536 return lhs.val_ == rhs.val_ && *lhs.cat_ == *rhs.cat_;
537 }
538
operator <(const error_condition & lhs,const error_condition & rhs)539 BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
540 {
541 return *lhs.cat_ < *rhs.cat_ || ( *lhs.cat_ == *rhs.cat_ && lhs.val_ < rhs.val_ );
542 }
543
544 #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
545
operator std::error_condition() const546 operator std::error_condition () const
547 {
548 return std::error_condition( value(), category() );
549 }
550
551 #endif
552 };
553
554 // class error_code
555
556 // We want error_code to be a value type that can be copied without slicing
557 // and without requiring heap allocation, but we also want it to have
558 // polymorphic behavior based on the error category. This is achieved by
559 // abstract base class error_category supplying the polymorphic behavior,
560 // and error_code containing a pointer to an object of a type derived
561 // from error_category.
562
563 class error_code
564 {
565 private:
566
567 int val_;
568 bool failed_;
569 const error_category * cat_;
570
571 public:
572
573 // constructors:
574
error_code()575 BOOST_SYSTEM_CONSTEXPR error_code() BOOST_NOEXCEPT:
576 val_( 0 ), failed_( false ), cat_( &system_category() )
577 {
578 }
579
error_code(int val,const error_category & cat)580 BOOST_SYSTEM_CONSTEXPR error_code( int val, const error_category & cat ) BOOST_NOEXCEPT:
581 val_( val ), failed_( detail::failed_impl( val, cat ) ), cat_( &cat )
582 {
583 }
584
error_code(ErrorCodeEnum e,typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value>::type * =0)585 template<class ErrorCodeEnum> BOOST_SYSTEM_CONSTEXPR error_code( ErrorCodeEnum e,
586 typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value>::type* = 0 ) BOOST_NOEXCEPT
587 {
588 *this = make_error_code( e );
589 }
590
591 // modifiers:
592
assign(int val,const error_category & cat)593 BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) BOOST_NOEXCEPT
594 {
595 val_ = val;
596 failed_ = detail::failed_impl( val, cat );
597 cat_ = &cat;
598 }
599
600 template<typename ErrorCodeEnum>
601 BOOST_SYSTEM_CONSTEXPR typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value, error_code>::type &
operator =(ErrorCodeEnum val)602 operator=( ErrorCodeEnum val ) BOOST_NOEXCEPT
603 {
604 *this = make_error_code( val );
605 return *this;
606 }
607
clear()608 BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT
609 {
610 val_ = 0;
611 failed_ = false;
612 cat_ = &system_category();
613 }
614
615 // observers:
616
value() const617 BOOST_SYSTEM_CONSTEXPR int value() const BOOST_NOEXCEPT
618 {
619 return val_;
620 }
621
category() const622 BOOST_SYSTEM_CONSTEXPR const error_category & category() const BOOST_NOEXCEPT
623 {
624 return *cat_;
625 }
626
default_error_condition() const627 error_condition default_error_condition() const BOOST_NOEXCEPT
628 {
629 return cat_->default_error_condition( value() );
630 }
631
message() const632 std::string message() const
633 {
634 return cat_->message( value() );
635 }
636
message(char * buffer,std::size_t len) const637 char const * message( char * buffer, std::size_t len ) const BOOST_NOEXCEPT
638 {
639 return cat_->message( value(), buffer, len );
640 }
641
failed() const642 BOOST_SYSTEM_CONSTEXPR bool failed() const BOOST_NOEXCEPT
643 {
644 return failed_;
645 }
646
647 #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
648
operator bool() const649 BOOST_SYSTEM_CONSTEXPR explicit operator bool() const BOOST_NOEXCEPT // true if error
650 {
651 return val_ != 0;
652 }
653
654 #else
655
656 typedef void (*unspecified_bool_type)();
unspecified_bool_true()657 static void unspecified_bool_true() {}
658
operator unspecified_bool_type() const659 BOOST_SYSTEM_CONSTEXPR operator unspecified_bool_type() const BOOST_NOEXCEPT // true if error
660 {
661 return val_ != 0? unspecified_bool_true: 0;
662 }
663
operator !() const664 BOOST_SYSTEM_CONSTEXPR bool operator!() const BOOST_NOEXCEPT // true if no error
665 {
666 return val_ == 0;
667 }
668
669 #endif
670
671 // relationals:
672
673 // the more symmetrical non-member syntax allows enum
674 // conversions work for both rhs and lhs.
675
operator ==(const error_code & lhs,const error_code & rhs)676 BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
677 {
678 return lhs.val_ == rhs.val_ && *lhs.cat_ == *rhs.cat_;
679 }
680
operator <(const error_code & lhs,const error_code & rhs)681 BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
682 {
683 return *lhs.cat_ < *rhs.cat_ || ( *lhs.cat_ == *rhs.cat_ && lhs.val_ < rhs.val_ );
684 }
685
686 #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
687
operator std::error_code() const688 operator std::error_code () const
689 {
690 return std::error_code( value(), category() );
691 }
692
693 #endif
694 };
695
696 } // namespace system
697
698 // boost::throws()
699
700 namespace detail
701 {
702
703 // Misuse of the error_code object is turned into a noisy failure by
704 // poisoning the reference. This particular implementation doesn't
705 // produce warnings or errors from popular compilers, is very efficient
706 // (as determined by inspecting generated code), and does not suffer
707 // from order of initialization problems. In practice, it also seems
708 // cause user function error handling implementation errors to be detected
709 // very early in the development cycle.
710
throws()711 inline system::error_code* throws()
712 {
713 // See github.com/boostorg/system/pull/12 by visigoth for why the return
714 // is poisoned with nonzero rather than (0). A test, test_throws_usage(),
715 // has been added to error_code_test.cpp, and as visigoth mentioned it
716 // fails on clang for release builds with a return of 0 but works fine
717 // with (1).
718 // Since the undefined behavior sanitizer (-fsanitize=undefined) does not
719 // allow a reference to be formed to the unaligned address of (1), we use
720 // (8) instead.
721
722 return reinterpret_cast<system::error_code*>(8);
723 }
724
725 } // namespace detail
726
throws()727 inline system::error_code& throws()
728 {
729 return *detail::throws();
730 }
731
732 // non-member functions of error_code and error_condition
733
734 namespace system
735 {
736
operator !=(const error_code & lhs,const error_code & rhs)737 BOOST_SYSTEM_CONSTEXPR inline bool operator!=( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
738 {
739 return !( lhs == rhs );
740 }
741
operator !=(const error_condition & lhs,const error_condition & rhs)742 BOOST_SYSTEM_CONSTEXPR inline bool operator!=( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
743 {
744 return !( lhs == rhs );
745 }
746
operator ==(const error_code & code,const error_condition & condition)747 inline bool operator==( const error_code & code, const error_condition & condition ) BOOST_NOEXCEPT
748 {
749 return code.category().equivalent( code.value(), condition ) || condition.category().equivalent( code, condition.value() );
750 }
751
operator !=(const error_code & lhs,const error_condition & rhs)752 inline bool operator!=( const error_code & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
753 {
754 return !( lhs == rhs );
755 }
756
operator ==(const error_condition & condition,const error_code & code)757 inline bool operator==( const error_condition & condition, const error_code & code ) BOOST_NOEXCEPT
758 {
759 return code.category().equivalent( code.value(), condition ) || condition.category().equivalent( code, condition.value() );
760 }
761
operator !=(const error_condition & lhs,const error_code & rhs)762 inline bool operator!=( const error_condition & lhs, const error_code & rhs ) BOOST_NOEXCEPT
763 {
764 return !( lhs == rhs );
765 }
766
767 template <class charT, class traits>
768 inline std::basic_ostream<charT,traits>&
operator <<(std::basic_ostream<charT,traits> & os,error_code ec)769 operator<< (std::basic_ostream<charT,traits>& os, error_code ec)
770 {
771 os << ec.category().name() << ':' << ec.value();
772 return os;
773 }
774
hash_value(error_code const & ec)775 inline std::size_t hash_value( error_code const & ec )
776 {
777 error_category const & cat = ec.category();
778
779 boost::ulong_long_type id = cat.id_;
780
781 if( id == 0 )
782 {
783 id = reinterpret_cast<boost::ulong_long_type>( &cat );
784 }
785
786 boost::ulong_long_type hv = ( boost::ulong_long_type( 0xCBF29CE4 ) << 32 ) + 0x84222325;
787 boost::ulong_long_type const prime = ( boost::ulong_long_type( 0x00000100 ) << 32 ) + 0x000001B3;
788
789 // id
790
791 hv ^= id;
792 hv *= prime;
793
794 // value
795
796 hv ^= static_cast<unsigned>( ec.value() );
797 hv *= prime;
798
799 return static_cast<std::size_t>( hv );
800 }
801
802 // make_* functions for errc::errc_t
803
804 namespace errc
805 {
806
807 // explicit conversion:
make_error_code(errc_t e)808 BOOST_SYSTEM_CONSTEXPR inline error_code make_error_code( errc_t e ) BOOST_NOEXCEPT
809 {
810 return error_code( e, generic_category() );
811 }
812
813 // implicit conversion:
make_error_condition(errc_t e)814 BOOST_SYSTEM_CONSTEXPR inline error_condition make_error_condition( errc_t e ) BOOST_NOEXCEPT
815 {
816 return error_condition( e, generic_category() );
817 }
818
819 } // namespace errc
820
821 // error_category default implementation
822
default_error_condition(int ev) const823 inline error_condition error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
824 {
825 return error_condition( ev, *this );
826 }
827
equivalent(int code,const error_condition & condition) const828 inline bool error_category::equivalent( int code, const error_condition & condition ) const BOOST_NOEXCEPT
829 {
830 return default_error_condition( code ) == condition;
831 }
832
equivalent(const error_code & code,int condition) const833 inline bool error_category::equivalent( const error_code & code, int condition ) const BOOST_NOEXCEPT
834 {
835 return *this == code.category() && code.value() == condition;
836 }
837
message(int ev,char * buffer,std::size_t len) const838 inline char const * error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
839 {
840 if( len == 0 )
841 {
842 return buffer;
843 }
844
845 if( len == 1 )
846 {
847 buffer[0] = 0;
848 return buffer;
849 }
850
851 #if !defined(BOOST_NO_EXCEPTIONS)
852 try
853 #endif
854 {
855 std::string m = this->message( ev );
856
857 # if defined( BOOST_MSVC )
858 # pragma warning( push )
859 # pragma warning( disable: 4996 )
860 # elif defined(__clang__) && defined(__has_warning)
861 # pragma clang diagnostic push
862 # if __has_warning("-Wdeprecated-declarations")
863 # pragma clang diagnostic ignored "-Wdeprecated-declarations"
864 # endif
865 # endif
866
867 std::strncpy( buffer, m.c_str(), len - 1 );
868 buffer[ len-1 ] = 0;
869
870 # if defined( BOOST_MSVC )
871 # pragma warning( pop )
872 # elif defined(__clang__) && defined(__has_warning)
873 # pragma clang diagnostic pop
874 # endif
875
876 return buffer;
877 }
878 #if !defined(BOOST_NO_EXCEPTIONS)
879 catch( ... )
880 {
881 return "Message text unavailable";
882 }
883 #endif
884 }
885
failed(int ev) const886 inline bool error_category::failed( int ev ) const BOOST_NOEXCEPT
887 {
888 return ev != 0;
889 }
890
891 } // namespace system
892
893 } // namespace boost
894
895 // generic_error_category implementation
896
897 #include <boost/system/detail/generic_category.hpp>
898
message(int ev) const899 inline std::string boost::system::detail::generic_error_category::message( int ev ) const
900 {
901 return generic_error_category_message( ev );
902 }
903
message(int ev,char * buffer,std::size_t len) const904 inline char const * boost::system::detail::generic_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
905 {
906 return generic_error_category_message( ev, buffer, len );
907 }
908
909 // system_error_category implementation
910
911 #if defined(BOOST_WINDOWS_API)
912
913 #include <boost/system/detail/system_category_win32.hpp>
914
default_error_condition(int ev) const915 inline boost::system::error_condition boost::system::detail::system_error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
916 {
917 return system_category_default_error_condition_win32( ev );
918 }
919
message(int ev) const920 inline std::string boost::system::detail::system_error_category::message( int ev ) const
921 {
922 return system_category_message_win32( ev );
923 }
924
message(int ev,char * buffer,std::size_t len) const925 inline char const * boost::system::detail::system_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
926 {
927 return system_category_message_win32( ev, buffer, len );
928 }
929
930 #else // #if defined(BOOST_WINDOWS_API)
931
932 #include <boost/system/detail/system_category_posix.hpp>
933
default_error_condition(int ev) const934 inline boost::system::error_condition boost::system::detail::system_error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
935 {
936 return system_category_default_error_condition_posix( ev );
937 }
938
message(int ev) const939 inline std::string boost::system::detail::system_error_category::message( int ev ) const
940 {
941 return generic_error_category_message( ev );
942 }
943
message(int ev,char * buffer,std::size_t len) const944 inline char const * boost::system::detail::system_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
945 {
946 return generic_error_category_message( ev, buffer, len );
947 }
948
949 #endif // #if defined(BOOST_WINDOWS_API)
950
951 // interoperability with std::error_code, std::error_condition
952
953 #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
954
955 #include <boost/system/detail/std_interoperability.hpp>
956
operator std::error_category const&() const957 inline boost::system::error_category::operator std::error_category const & () const
958 {
959 return boost::system::detail::to_std_category( *this );
960 }
961
962 #endif // #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
963
964 #endif // BOOST_SYSTEM_ERROR_CODE_HPP_INCLUDED
965