error_code.hpp 30 KB

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