error_code.hpp 25 KB

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