error_code.hpp 25 KB

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