error_code.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 BOOST_SYMBOL_VISIBLE cat_holder
  261. {
  262. static constexpr system_error_category system_category_instance{};
  263. static constexpr generic_error_category generic_category_instance{};
  264. };
  265. // Before C++17 it was mandatory to redeclare all static constexpr
  266. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  267. template<class T> constexpr system_error_category cat_holder<T>::system_category_instance;
  268. template<class T> constexpr generic_error_category cat_holder<T>::generic_category_instance;
  269. #endif
  270. } // namespace detail
  271. constexpr error_category const & system_category() BOOST_NOEXCEPT
  272. {
  273. return detail::cat_holder<void>::system_category_instance;
  274. }
  275. constexpr error_category const & generic_category() BOOST_NOEXCEPT
  276. {
  277. return detail::cat_holder<void>::generic_category_instance;
  278. }
  279. #else // #if defined(BOOST_SYSTEM_HAS_CONSTEXPR)
  280. inline error_category const & system_category() BOOST_NOEXCEPT BOOST_SYMBOL_VISIBLE;
  281. inline error_category const & system_category() BOOST_NOEXCEPT
  282. {
  283. static const detail::system_error_category system_category_instance;
  284. return system_category_instance;
  285. }
  286. inline error_category const & generic_category() BOOST_NOEXCEPT BOOST_SYMBOL_VISIBLE;
  287. inline error_category const & generic_category() BOOST_NOEXCEPT
  288. {
  289. static const detail::generic_error_category generic_category_instance;
  290. return generic_category_instance;
  291. }
  292. #endif // #if defined(BOOST_SYSTEM_HAS_CONSTEXPR)
  293. // deprecated synonyms
  294. #ifdef BOOST_SYSTEM_ENABLE_DEPRECATED
  295. inline const error_category & get_system_category() { return system_category(); }
  296. inline const error_category & get_generic_category() { return generic_category(); }
  297. inline const error_category & get_posix_category() { return generic_category(); }
  298. static const error_category & posix_category BOOST_ATTRIBUTE_UNUSED = generic_category();
  299. static const error_category & errno_ecat BOOST_ATTRIBUTE_UNUSED = generic_category();
  300. static const error_category & native_ecat BOOST_ATTRIBUTE_UNUSED = system_category();
  301. #endif
  302. // enable_if
  303. namespace detail
  304. {
  305. template<bool C, class T = void> struct enable_if
  306. {
  307. typedef T type;
  308. };
  309. template<class T> struct enable_if<false, T>
  310. {
  311. };
  312. // failed_impl
  313. #if !defined(BOOST_SYSTEM_HAS_CONSTEXPR)
  314. inline bool failed_impl( int ev, error_category const & cat )
  315. {
  316. return cat.failed( ev );
  317. }
  318. #else
  319. BOOST_SYSTEM_CONSTEXPR inline bool failed_impl( int ev, error_category const & cat )
  320. {
  321. if( cat == system_category() || cat == generic_category() )
  322. {
  323. return ev != 0;
  324. }
  325. else
  326. {
  327. return cat.failed( ev );
  328. }
  329. }
  330. #endif
  331. } // namespace detail
  332. // class error_condition
  333. // error_conditions are portable, error_codes are system or library specific
  334. class error_condition
  335. {
  336. private:
  337. int val_;
  338. bool failed_;
  339. error_category const * cat_;
  340. public:
  341. // constructors:
  342. BOOST_SYSTEM_CONSTEXPR error_condition() BOOST_NOEXCEPT:
  343. val_( 0 ), failed_( false ), cat_( &generic_category() )
  344. {
  345. }
  346. BOOST_SYSTEM_CONSTEXPR error_condition( int val, const error_category & cat ) BOOST_NOEXCEPT:
  347. val_( val ), failed_( detail::failed_impl( val, cat ) ), cat_( &cat )
  348. {
  349. }
  350. template<class ErrorConditionEnum> BOOST_SYSTEM_CONSTEXPR error_condition( ErrorConditionEnum e,
  351. typename detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value>::type* = 0) BOOST_NOEXCEPT
  352. {
  353. *this = make_error_condition( e );
  354. }
  355. // modifiers:
  356. BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) BOOST_NOEXCEPT
  357. {
  358. val_ = val;
  359. failed_ = detail::failed_impl( val, cat );
  360. cat_ = &cat;
  361. }
  362. template<typename ErrorConditionEnum>
  363. BOOST_SYSTEM_CONSTEXPR typename detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value, error_condition>::type &
  364. operator=( ErrorConditionEnum val ) BOOST_NOEXCEPT
  365. {
  366. *this = make_error_condition( val );
  367. return *this;
  368. }
  369. BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT
  370. {
  371. val_ = 0;
  372. failed_ = false;
  373. cat_ = &generic_category();
  374. }
  375. // observers:
  376. BOOST_SYSTEM_CONSTEXPR int value() const BOOST_NOEXCEPT
  377. {
  378. return val_;
  379. }
  380. BOOST_SYSTEM_CONSTEXPR const error_category & category() const BOOST_NOEXCEPT
  381. {
  382. return *cat_;
  383. }
  384. std::string message() const
  385. {
  386. return cat_->message( value() );
  387. }
  388. char const * message( char * buffer, std::size_t len ) const BOOST_NOEXCEPT
  389. {
  390. return cat_->message( value(), buffer, len );
  391. }
  392. BOOST_SYSTEM_CONSTEXPR bool failed() const BOOST_NOEXCEPT
  393. {
  394. return failed_;
  395. }
  396. #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  397. BOOST_SYSTEM_CONSTEXPR explicit operator bool() const BOOST_NOEXCEPT // true if error
  398. {
  399. return val_ != 0;
  400. }
  401. #else
  402. typedef void (*unspecified_bool_type)();
  403. static void unspecified_bool_true() {}
  404. BOOST_SYSTEM_CONSTEXPR operator unspecified_bool_type() const BOOST_NOEXCEPT // true if error
  405. {
  406. return val_ != 0? unspecified_bool_true: 0;
  407. }
  408. BOOST_SYSTEM_CONSTEXPR bool operator!() const BOOST_NOEXCEPT // true if no error
  409. {
  410. return val_ == 0;
  411. }
  412. #endif
  413. // relationals:
  414. // the more symmetrical non-member syntax allows enum
  415. // conversions work for both rhs and lhs.
  416. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  417. {
  418. return lhs.val_ == rhs.val_ && *lhs.cat_ == *rhs.cat_;
  419. }
  420. BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  421. {
  422. return *lhs.cat_ < *rhs.cat_ || ( *lhs.cat_ == *rhs.cat_ && lhs.val_ < rhs.val_ );
  423. }
  424. #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
  425. operator std::error_condition () const
  426. {
  427. return std::error_condition( value(), category() );
  428. }
  429. #endif
  430. };
  431. // class error_code
  432. // We want error_code to be a value type that can be copied without slicing
  433. // and without requiring heap allocation, but we also want it to have
  434. // polymorphic behavior based on the error category. This is achieved by
  435. // abstract base class error_category supplying the polymorphic behavior,
  436. // and error_code containing a pointer to an object of a type derived
  437. // from error_category.
  438. class error_code
  439. {
  440. private:
  441. int val_;
  442. bool failed_;
  443. const error_category * cat_;
  444. public:
  445. // constructors:
  446. BOOST_SYSTEM_CONSTEXPR error_code() BOOST_NOEXCEPT:
  447. val_( 0 ), failed_( false ), cat_( &system_category() )
  448. {
  449. }
  450. BOOST_SYSTEM_CONSTEXPR error_code( int val, const error_category & cat ) BOOST_NOEXCEPT:
  451. val_( val ), failed_( detail::failed_impl( val, cat ) ), cat_( &cat )
  452. {
  453. }
  454. template<class ErrorCodeEnum> BOOST_SYSTEM_CONSTEXPR error_code( ErrorCodeEnum e,
  455. typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value>::type* = 0 ) BOOST_NOEXCEPT
  456. {
  457. *this = make_error_code( e );
  458. }
  459. // modifiers:
  460. BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) BOOST_NOEXCEPT
  461. {
  462. val_ = val;
  463. failed_ = detail::failed_impl( val, cat );
  464. cat_ = &cat;
  465. }
  466. template<typename ErrorCodeEnum>
  467. BOOST_SYSTEM_CONSTEXPR typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value, error_code>::type &
  468. operator=( ErrorCodeEnum val ) BOOST_NOEXCEPT
  469. {
  470. *this = make_error_code( val );
  471. return *this;
  472. }
  473. BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT
  474. {
  475. val_ = 0;
  476. failed_ = false;
  477. cat_ = &system_category();
  478. }
  479. // observers:
  480. BOOST_SYSTEM_CONSTEXPR int value() const BOOST_NOEXCEPT
  481. {
  482. return val_;
  483. }
  484. BOOST_SYSTEM_CONSTEXPR const error_category & category() const BOOST_NOEXCEPT
  485. {
  486. return *cat_;
  487. }
  488. error_condition default_error_condition() const BOOST_NOEXCEPT
  489. {
  490. return cat_->default_error_condition( value() );
  491. }
  492. std::string message() const
  493. {
  494. return cat_->message( value() );
  495. }
  496. char const * message( char * buffer, std::size_t len ) const BOOST_NOEXCEPT
  497. {
  498. return cat_->message( value(), buffer, len );
  499. }
  500. BOOST_SYSTEM_CONSTEXPR bool failed() const BOOST_NOEXCEPT
  501. {
  502. return failed_;
  503. }
  504. #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  505. BOOST_SYSTEM_CONSTEXPR explicit operator bool() const BOOST_NOEXCEPT // true if error
  506. {
  507. return val_ != 0;
  508. }
  509. #else
  510. typedef void (*unspecified_bool_type)();
  511. static void unspecified_bool_true() {}
  512. BOOST_SYSTEM_CONSTEXPR operator unspecified_bool_type() const BOOST_NOEXCEPT // true if error
  513. {
  514. return val_ != 0? unspecified_bool_true: 0;
  515. }
  516. BOOST_SYSTEM_CONSTEXPR bool operator!() const BOOST_NOEXCEPT // true if no error
  517. {
  518. return val_ == 0;
  519. }
  520. #endif
  521. // relationals:
  522. // the more symmetrical non-member syntax allows enum
  523. // conversions work for both rhs and lhs.
  524. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
  525. {
  526. return lhs.val_ == rhs.val_ && *lhs.cat_ == *rhs.cat_;
  527. }
  528. BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
  529. {
  530. return *lhs.cat_ < *rhs.cat_ || ( *lhs.cat_ == *rhs.cat_ && lhs.val_ < rhs.val_ );
  531. }
  532. #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
  533. operator std::error_code () const
  534. {
  535. return std::error_code( value(), category() );
  536. }
  537. #endif
  538. };
  539. } // namespace system
  540. // boost::throws()
  541. namespace detail
  542. {
  543. // Misuse of the error_code object is turned into a noisy failure by
  544. // poisoning the reference. This particular implementation doesn't
  545. // produce warnings or errors from popular compilers, is very efficient
  546. // (as determined by inspecting generated code), and does not suffer
  547. // from order of initialization problems. In practice, it also seems
  548. // cause user function error handling implementation errors to be detected
  549. // very early in the development cycle.
  550. inline system::error_code* throws()
  551. {
  552. // See github.com/boostorg/system/pull/12 by visigoth for why the return
  553. // is poisoned with nonzero rather than (0). A test, test_throws_usage(),
  554. // has been added to error_code_test.cpp, and as visigoth mentioned it
  555. // fails on clang for release builds with a return of 0 but works fine
  556. // with (1).
  557. // Since the undefined behavior sanitizer (-fsanitize=undefined) does not
  558. // allow a reference to be formed to the unaligned address of (1), we use
  559. // (8) instead.
  560. return reinterpret_cast<system::error_code*>(8);
  561. }
  562. } // namespace detail
  563. inline system::error_code& throws()
  564. {
  565. return *detail::throws();
  566. }
  567. // non-member functions of error_code and error_condition
  568. namespace system
  569. {
  570. BOOST_SYSTEM_CONSTEXPR inline bool operator!=( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
  571. {
  572. return !( lhs == rhs );
  573. }
  574. BOOST_SYSTEM_CONSTEXPR inline bool operator!=( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  575. {
  576. return !( lhs == rhs );
  577. }
  578. inline bool operator==( const error_code & code, const error_condition & condition ) BOOST_NOEXCEPT
  579. {
  580. return code.category().equivalent( code.value(), condition ) || condition.category().equivalent( code, condition.value() );
  581. }
  582. inline bool operator!=( const error_code & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  583. {
  584. return !( lhs == rhs );
  585. }
  586. inline bool operator==( const error_condition & condition, const error_code & code ) BOOST_NOEXCEPT
  587. {
  588. return code.category().equivalent( code.value(), condition ) || condition.category().equivalent( code, condition.value() );
  589. }
  590. inline bool operator!=( const error_condition & lhs, const error_code & rhs ) BOOST_NOEXCEPT
  591. {
  592. return !( lhs == rhs );
  593. }
  594. template <class charT, class traits>
  595. inline std::basic_ostream<charT,traits>&
  596. operator<< (std::basic_ostream<charT,traits>& os, error_code ec)
  597. {
  598. os << ec.category().name() << ':' << ec.value();
  599. return os;
  600. }
  601. inline std::size_t hash_value( error_code const & ec )
  602. {
  603. error_category const & cat = ec.category();
  604. boost::ulong_long_type id = cat.id_;
  605. if( id == 0 )
  606. {
  607. id = reinterpret_cast<boost::uintptr_t>( &cat );
  608. }
  609. boost::ulong_long_type hv = ( boost::ulong_long_type( 0xCBF29CE4 ) << 32 ) + 0x84222325;
  610. boost::ulong_long_type const prime = ( boost::ulong_long_type( 0x00000100 ) << 32 ) + 0x000001B3;
  611. // id
  612. hv ^= id;
  613. hv *= prime;
  614. // value
  615. hv ^= static_cast<unsigned>( ec.value() );
  616. hv *= prime;
  617. return static_cast<std::size_t>( hv );
  618. }
  619. // make_* functions for errc::errc_t
  620. namespace errc
  621. {
  622. // explicit conversion:
  623. BOOST_SYSTEM_CONSTEXPR inline error_code make_error_code( errc_t e ) BOOST_NOEXCEPT
  624. {
  625. return error_code( e, generic_category() );
  626. }
  627. // implicit conversion:
  628. BOOST_SYSTEM_CONSTEXPR inline error_condition make_error_condition( errc_t e ) BOOST_NOEXCEPT
  629. {
  630. return error_condition( e, generic_category() );
  631. }
  632. } // namespace errc
  633. // error_category default implementation
  634. inline error_condition error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
  635. {
  636. return error_condition( ev, *this );
  637. }
  638. inline bool error_category::equivalent( int code, const error_condition & condition ) const BOOST_NOEXCEPT
  639. {
  640. return default_error_condition( code ) == condition;
  641. }
  642. inline bool error_category::equivalent( const error_code & code, int condition ) const BOOST_NOEXCEPT
  643. {
  644. return *this == code.category() && code.value() == condition;
  645. }
  646. inline char const * error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
  647. {
  648. if( len == 0 )
  649. {
  650. return buffer;
  651. }
  652. if( len == 1 )
  653. {
  654. buffer[0] = 0;
  655. return buffer;
  656. }
  657. #if !defined(BOOST_NO_EXCEPTIONS)
  658. try
  659. #endif
  660. {
  661. std::string m = this->message( ev );
  662. # if defined( BOOST_MSVC )
  663. # pragma warning( push )
  664. # pragma warning( disable: 4996 )
  665. # elif defined(__clang__) && defined(__has_warning)
  666. # pragma clang diagnostic push
  667. # if __has_warning("-Wdeprecated-declarations")
  668. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  669. # endif
  670. # endif
  671. std::strncpy( buffer, m.c_str(), len - 1 );
  672. buffer[ len-1 ] = 0;
  673. # if defined( BOOST_MSVC )
  674. # pragma warning( pop )
  675. # elif defined(__clang__) && defined(__has_warning)
  676. # pragma clang diagnostic pop
  677. # endif
  678. return buffer;
  679. }
  680. #if !defined(BOOST_NO_EXCEPTIONS)
  681. catch( ... )
  682. {
  683. return "Message text unavailable";
  684. }
  685. #endif
  686. }
  687. inline bool error_category::failed( int ev ) const BOOST_NOEXCEPT
  688. {
  689. return ev != 0;
  690. }
  691. } // namespace system
  692. } // namespace boost
  693. // generic_error_category implementation
  694. #include <boost/system/detail/generic_category.hpp>
  695. inline std::string boost::system::detail::generic_error_category::message( int ev ) const
  696. {
  697. return generic_error_category_message( ev );
  698. }
  699. inline char const * boost::system::detail::generic_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
  700. {
  701. return generic_error_category_message( ev, buffer, len );
  702. }
  703. // system_error_category implementation
  704. #if defined(BOOST_WINDOWS_API)
  705. #include <boost/system/detail/system_category_win32.hpp>
  706. inline boost::system::error_condition boost::system::detail::system_error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
  707. {
  708. return system_category_default_error_condition_win32( ev );
  709. }
  710. inline std::string boost::system::detail::system_error_category::message( int ev ) const
  711. {
  712. return system_category_message_win32( ev );
  713. }
  714. inline char const * boost::system::detail::system_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
  715. {
  716. return system_category_message_win32( ev, buffer, len );
  717. }
  718. #else // #if defined(BOOST_WINDOWS_API)
  719. #include <boost/system/detail/system_category_posix.hpp>
  720. inline boost::system::error_condition boost::system::detail::system_error_category::default_error_condition( int ev ) const BOOST_NOEXCEPT
  721. {
  722. return system_category_default_error_condition_posix( ev );
  723. }
  724. inline std::string boost::system::detail::system_error_category::message( int ev ) const
  725. {
  726. return generic_error_category_message( ev );
  727. }
  728. inline char const * boost::system::detail::system_error_category::message( int ev, char * buffer, std::size_t len ) const BOOST_NOEXCEPT
  729. {
  730. return generic_error_category_message( ev, buffer, len );
  731. }
  732. #endif // #if defined(BOOST_WINDOWS_API)
  733. // interoperability with std::error_code, std::error_condition
  734. #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
  735. #include <boost/system/detail/std_interoperability.hpp>
  736. inline boost::system::error_category::operator std::error_category const & () const
  737. {
  738. return boost::system::detail::to_std_category( *this );
  739. }
  740. #endif // #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
  741. #endif // BOOST_SYSTEM_ERROR_CODE_HPP_INCLUDED