error_code.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. #ifndef BOOST_SYSTEM_DETAIL_ERROR_CODE_HPP_INCLUDED
  2. #define BOOST_SYSTEM_DETAIL_ERROR_CODE_HPP_INCLUDED
  3. // Copyright Beman Dawes 2006, 2007
  4. // Copyright Christoper Kohlhoff 2007
  5. // Copyright Peter Dimov 2017-2021
  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/is_error_code_enum.hpp>
  12. #include <boost/system/detail/error_category.hpp>
  13. #include <boost/system/detail/error_condition.hpp>
  14. #include <boost/system/detail/system_category.hpp>
  15. #include <boost/system/detail/system_category_impl.hpp>
  16. #include <boost/system/detail/interop_category.hpp>
  17. #include <boost/system/detail/enable_if.hpp>
  18. #include <boost/system/detail/is_same.hpp>
  19. #include <boost/system/detail/append_int.hpp>
  20. #include <boost/system/detail/snprintf.hpp>
  21. #include <boost/system/detail/config.hpp>
  22. #include <boost/system/detail/std_category.hpp>
  23. #include <boost/assert/source_location.hpp>
  24. #include <boost/cstdint.hpp>
  25. #include <boost/config.hpp>
  26. #include <boost/config/workaround.hpp>
  27. #include <ostream>
  28. #include <new>
  29. #include <cstdio>
  30. #include <system_error>
  31. #if defined(BOOST_GCC) && BOOST_GCC >= 40600 && BOOST_GCC < 70000
  32. # pragma GCC diagnostic push
  33. # pragma GCC diagnostic ignored "-Wstrict-aliasing"
  34. #endif
  35. namespace boost
  36. {
  37. namespace system
  38. {
  39. // class error_code
  40. // We want error_code to be a value type that can be copied without slicing
  41. // and without requiring heap allocation, but we also want it to have
  42. // polymorphic behavior based on the error category. This is achieved by
  43. // abstract base class error_category supplying the polymorphic behavior,
  44. // and error_code containing a pointer to an object of a type derived
  45. // from error_category.
  46. bool operator==( const error_code & code, const error_condition & condition ) noexcept;
  47. std::size_t hash_value( error_code const & ec );
  48. class error_code
  49. {
  50. private:
  51. friend bool operator==( const error_code & code, const error_condition & condition ) noexcept;
  52. friend std::size_t hash_value( error_code const & ec );
  53. private:
  54. struct data
  55. {
  56. int val_;
  57. const error_category * cat_;
  58. };
  59. union
  60. {
  61. data d1_;
  62. unsigned char d2_[ sizeof(std::error_code) ];
  63. };
  64. // 0: default constructed, d1_ value initialized
  65. // 1: holds std::error_code in d2_
  66. // 2: holds error code in d1_, failed == false
  67. // 3: holds error code in d1_, failed == true
  68. // >3: pointer to source_location, failed_ in lsb
  69. boost::uintptr_t lc_flags_;
  70. private:
  71. char const* category_name() const noexcept
  72. {
  73. // return category().name();
  74. if( lc_flags_ == 0 )
  75. {
  76. // must match detail::system_error_category::name()
  77. return "system";
  78. }
  79. else if( lc_flags_ == 1 )
  80. {
  81. // must match detail::interop_error_category::name()
  82. return "std:unknown";
  83. }
  84. else
  85. {
  86. return d1_.cat_->name();
  87. }
  88. }
  89. public:
  90. // constructors:
  91. constexpr error_code() noexcept:
  92. d1_(), lc_flags_( 0 )
  93. {
  94. }
  95. BOOST_SYSTEM_CONSTEXPR error_code( int val, const error_category & cat ) noexcept:
  96. d1_(), lc_flags_( 2 + detail::failed_impl( val, cat ) )
  97. {
  98. d1_.val_ = val;
  99. d1_.cat_ = &cat;
  100. }
  101. error_code( int val, const error_category & cat, source_location const * loc ) noexcept:
  102. d1_(), lc_flags_( ( loc? reinterpret_cast<boost::uintptr_t>( loc ): 2 ) | +detail::failed_impl( val, cat ) )
  103. {
  104. d1_.val_ = val;
  105. d1_.cat_ = &cat;
  106. }
  107. template<class ErrorCodeEnum> BOOST_SYSTEM_CONSTEXPR error_code( ErrorCodeEnum e,
  108. typename detail::enable_if<
  109. is_error_code_enum<ErrorCodeEnum>::value
  110. || std::is_error_code_enum<ErrorCodeEnum>::value
  111. >::type* = 0 ) noexcept: d1_(), lc_flags_( 0 )
  112. {
  113. *this = make_error_code( e );
  114. }
  115. error_code( error_code const& ec, source_location const * loc ) noexcept:
  116. d1_(), lc_flags_( 0 )
  117. {
  118. *this = ec;
  119. if( ec.lc_flags_ != 0 && ec.lc_flags_ != 1 )
  120. {
  121. lc_flags_ = ( loc? reinterpret_cast<boost::uintptr_t>( loc ): 2 ) | ( ec.lc_flags_ & 1 );
  122. }
  123. }
  124. error_code( std::error_code const& ec ) noexcept:
  125. d1_(), lc_flags_( 0 )
  126. {
  127. #ifndef BOOST_NO_RTTI
  128. if( detail::std_category const* pc2 = dynamic_cast< detail::std_category const* >( &ec.category() ) )
  129. {
  130. *this = boost::system::error_code( ec.value(), pc2->original_category() );
  131. }
  132. else
  133. #endif
  134. {
  135. ::new( d2_ ) std::error_code( ec );
  136. lc_flags_ = 1;
  137. }
  138. }
  139. // modifiers:
  140. BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) noexcept
  141. {
  142. *this = error_code( val, cat );
  143. }
  144. void assign( int val, const error_category & cat, source_location const * loc ) noexcept
  145. {
  146. *this = error_code( val, cat, loc );
  147. }
  148. void assign( error_code const& ec, source_location const * loc ) noexcept
  149. {
  150. *this = error_code( ec, loc );
  151. }
  152. template<typename ErrorCodeEnum>
  153. BOOST_SYSTEM_CONSTEXPR typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value, error_code>::type &
  154. operator=( ErrorCodeEnum val ) noexcept
  155. {
  156. *this = make_error_code( val );
  157. return *this;
  158. }
  159. BOOST_SYSTEM_CONSTEXPR void clear() noexcept
  160. {
  161. *this = error_code();
  162. }
  163. // observers:
  164. BOOST_SYSTEM_CONSTEXPR int value() const noexcept
  165. {
  166. if( lc_flags_ != 1 )
  167. {
  168. return d1_.val_;
  169. }
  170. else
  171. {
  172. std::error_code const& ec = *reinterpret_cast<std::error_code const*>( d2_ );
  173. unsigned cv = static_cast<unsigned>( ec.value() );
  174. unsigned ch = static_cast<unsigned>( reinterpret_cast<boost::uintptr_t>( &ec.category() ) % 2097143 ); // 2^21-9, prime
  175. return static_cast<int>( cv + 1000 * ch );
  176. }
  177. }
  178. BOOST_SYSTEM_CONSTEXPR const error_category & category() const noexcept
  179. {
  180. if( lc_flags_ == 0 )
  181. {
  182. return system_category();
  183. }
  184. else if( lc_flags_ == 1 )
  185. {
  186. return detail::interop_category();
  187. }
  188. else
  189. {
  190. return *d1_.cat_;
  191. }
  192. }
  193. // deprecated?
  194. error_condition default_error_condition() const noexcept
  195. {
  196. return category().default_error_condition( value() );
  197. }
  198. std::string message() const
  199. {
  200. if( lc_flags_ == 1 )
  201. {
  202. std::error_code const& ec = *reinterpret_cast<std::error_code const*>( d2_ );
  203. return ec.message();
  204. }
  205. else if( lc_flags_ == 0 )
  206. {
  207. return detail::system_error_category_message( value() );
  208. }
  209. else
  210. {
  211. return category().message( value() );
  212. }
  213. }
  214. char const * message( char * buffer, std::size_t len ) const noexcept
  215. {
  216. if( lc_flags_ == 1 )
  217. {
  218. std::error_code const& ec = *reinterpret_cast<std::error_code const*>( d2_ );
  219. #if !defined(BOOST_NO_EXCEPTIONS)
  220. try
  221. #endif
  222. {
  223. detail::snprintf( buffer, len, "%s", ec.message().c_str() );
  224. return buffer;
  225. }
  226. #if !defined(BOOST_NO_EXCEPTIONS)
  227. catch( ... )
  228. {
  229. detail::snprintf( buffer, len, "No message text available for error std:%s:%d", ec.category().name(), ec.value() );
  230. return buffer;
  231. }
  232. #endif
  233. }
  234. else if( lc_flags_ == 0 )
  235. {
  236. return detail::system_error_category_message( value(), buffer, len );
  237. }
  238. else
  239. {
  240. return category().message( value(), buffer, len );
  241. }
  242. }
  243. BOOST_SYSTEM_CONSTEXPR bool failed() const noexcept
  244. {
  245. if( lc_flags_ & 1 )
  246. {
  247. if( lc_flags_ == 1 )
  248. {
  249. std::error_code const& ec = *reinterpret_cast<std::error_code const*>( d2_ );
  250. return ec.value() != 0;
  251. }
  252. return true;
  253. }
  254. else
  255. {
  256. return false;
  257. }
  258. }
  259. BOOST_SYSTEM_CONSTEXPR explicit operator bool() const noexcept // true if error
  260. {
  261. return failed();
  262. }
  263. bool has_location() const noexcept
  264. {
  265. return lc_flags_ >= 4;
  266. }
  267. source_location const & location() const noexcept
  268. {
  269. BOOST_STATIC_CONSTEXPR source_location loc;
  270. return lc_flags_ >= 4? *reinterpret_cast<source_location const*>( lc_flags_ &~ static_cast<boost::uintptr_t>( 1 ) ): loc;
  271. }
  272. // relationals:
  273. private:
  274. // private equality for use in error_category::equivalent
  275. friend class error_category;
  276. BOOST_SYSTEM_CONSTEXPR bool equals( int val, error_category const& cat ) const noexcept
  277. {
  278. if( lc_flags_ == 0 )
  279. {
  280. return val == 0 && cat.id_ == detail::system_category_id;
  281. }
  282. else if( lc_flags_ == 1 )
  283. {
  284. return cat.id_ == detail::interop_category_id && val == value();
  285. }
  286. else
  287. {
  288. return val == d1_.val_ && cat == *d1_.cat_;
  289. }
  290. }
  291. public:
  292. // the more symmetrical non-member syntax allows enum
  293. // conversions work for both rhs and lhs.
  294. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_code & lhs, const error_code & rhs ) noexcept
  295. {
  296. bool s1 = lhs.lc_flags_ == 1;
  297. bool s2 = rhs.lc_flags_ == 1;
  298. if( s1 != s2 ) return false;
  299. if( s1 && s2 )
  300. {
  301. std::error_code const& e1 = *reinterpret_cast<std::error_code const*>( lhs.d2_ );
  302. std::error_code const& e2 = *reinterpret_cast<std::error_code const*>( rhs.d2_ );
  303. return e1 == e2;
  304. }
  305. else
  306. {
  307. return lhs.value() == rhs.value() && lhs.category() == rhs.category();
  308. }
  309. }
  310. BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_code & lhs, const error_code & rhs ) noexcept
  311. {
  312. bool s1 = lhs.lc_flags_ == 1;
  313. bool s2 = rhs.lc_flags_ == 1;
  314. if( s1 < s2 ) return true;
  315. if( s2 < s1 ) return false;
  316. if( s1 && s2 )
  317. {
  318. std::error_code const& e1 = *reinterpret_cast<std::error_code const*>( lhs.d2_ );
  319. std::error_code const& e2 = *reinterpret_cast<std::error_code const*>( rhs.d2_ );
  320. return e1 < e2;
  321. }
  322. else
  323. {
  324. return lhs.category() < rhs.category() || (lhs.category() == rhs.category() && lhs.value() < rhs.value());
  325. }
  326. }
  327. BOOST_SYSTEM_CONSTEXPR inline friend bool operator!=( const error_code & lhs, const error_code & rhs ) noexcept
  328. {
  329. return !( lhs == rhs );
  330. }
  331. inline friend bool operator==( std::error_code const & lhs, error_code const & rhs ) noexcept
  332. {
  333. return lhs == static_cast< std::error_code >( rhs );
  334. }
  335. inline friend bool operator==( error_code const & lhs, std::error_code const & rhs ) noexcept
  336. {
  337. return static_cast< std::error_code >( lhs ) == rhs;
  338. }
  339. inline friend bool operator!=( std::error_code const & lhs, error_code const & rhs ) noexcept
  340. {
  341. return !( lhs == rhs );
  342. }
  343. inline friend bool operator!=( error_code const & lhs, std::error_code const & rhs ) noexcept
  344. {
  345. return !( lhs == rhs );
  346. }
  347. //
  348. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  349. inline friend bool operator==( error_code const & lhs, E rhs ) noexcept
  350. {
  351. return lhs == make_error_condition( rhs );
  352. }
  353. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  354. inline friend bool operator==( E lhs, error_code const & rhs ) noexcept
  355. {
  356. return make_error_condition( lhs ) == rhs;
  357. }
  358. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  359. inline friend bool operator!=( error_code const & lhs, E rhs ) noexcept
  360. {
  361. return !( lhs == rhs );
  362. }
  363. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  364. inline friend bool operator!=( E lhs, error_code const & rhs ) noexcept
  365. {
  366. return !( lhs == rhs );
  367. }
  368. //
  369. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  370. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( error_code const & lhs, E rhs ) noexcept
  371. {
  372. return lhs == make_error_code( rhs );
  373. }
  374. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  375. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( E lhs, error_code const & rhs ) noexcept
  376. {
  377. return make_error_code( lhs ) == rhs;
  378. }
  379. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  380. BOOST_SYSTEM_CONSTEXPR inline friend bool operator!=( error_code const & lhs, E rhs ) noexcept
  381. {
  382. return !( lhs == rhs );
  383. }
  384. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  385. BOOST_SYSTEM_CONSTEXPR inline friend bool operator!=( E lhs, error_code const & rhs ) noexcept
  386. {
  387. return !( lhs == rhs );
  388. }
  389. #if defined(BOOST_SYSTEM_CLANG_6)
  390. inline friend bool operator==( error_code const & lhs, std::error_condition const & rhs ) noexcept
  391. {
  392. return static_cast< std::error_code >( lhs ) == rhs;
  393. }
  394. inline friend bool operator==( std::error_condition const & lhs, error_code const & rhs ) noexcept
  395. {
  396. return lhs == static_cast< std::error_code >( rhs );
  397. }
  398. inline friend bool operator!=( error_code const & lhs, std::error_condition const & rhs ) noexcept
  399. {
  400. return !( lhs == rhs );
  401. }
  402. inline friend bool operator!=( std::error_condition const & lhs, error_code const & rhs ) noexcept
  403. {
  404. return !( lhs == rhs );
  405. }
  406. #endif
  407. // conversions
  408. operator std::error_code () const
  409. {
  410. if( lc_flags_ == 1 )
  411. {
  412. return *reinterpret_cast<std::error_code const*>( d2_ );
  413. }
  414. else if( lc_flags_ == 0 )
  415. {
  416. // This condition must be the same as the one in error_category_impl.hpp
  417. #if defined(BOOST_SYSTEM_AVOID_STD_SYSTEM_CATEGORY)
  418. return std::error_code( 0, boost::system::system_category() );
  419. #else
  420. return std::error_code();
  421. #endif
  422. }
  423. else
  424. {
  425. return std::error_code( d1_.val_, *d1_.cat_ );
  426. }
  427. }
  428. operator std::error_code ()
  429. {
  430. return const_cast<error_code const&>( *this );
  431. }
  432. template<class T,
  433. class E = typename detail::enable_if<detail::is_same<T, std::error_code>::value>::type>
  434. operator T& ()
  435. {
  436. if( lc_flags_ != 1 )
  437. {
  438. std::error_code e2( *this );
  439. ::new( d2_ ) std::error_code( e2 );
  440. lc_flags_ = 1;
  441. }
  442. return *reinterpret_cast<std::error_code*>( d2_ );
  443. }
  444. #if defined(BOOST_SYSTEM_CLANG_6)
  445. template<class T,
  446. class E = typename std::enable_if<std::is_same<T, std::error_code>::value>::type>
  447. operator T const& () = delete;
  448. #endif
  449. std::string to_string() const
  450. {
  451. if( lc_flags_ == 1 )
  452. {
  453. std::error_code const& e2 = *reinterpret_cast<std::error_code const*>( d2_ );
  454. std::string r( "std:" );
  455. r += e2.category().name();
  456. detail::append_int( r, e2.value() );
  457. return r;
  458. }
  459. else
  460. {
  461. std::string r = category_name();
  462. detail::append_int( r, value() );
  463. return r;
  464. }
  465. }
  466. template<class Ch, class Tr>
  467. inline friend std::basic_ostream<Ch, Tr>&
  468. operator<< (std::basic_ostream<Ch, Tr>& os, error_code const & ec)
  469. {
  470. return os << ec.to_string().c_str();
  471. }
  472. std::string what() const
  473. {
  474. std::string r = message();
  475. r += " [";
  476. r += to_string();
  477. if( has_location() )
  478. {
  479. r += " at ";
  480. r += location().to_string();
  481. }
  482. r += "]";
  483. return r;
  484. }
  485. };
  486. inline bool operator==( const error_code & code, const error_condition & condition ) noexcept
  487. {
  488. if( code.lc_flags_ == 1 )
  489. {
  490. return static_cast<std::error_code>( code ) == static_cast<std::error_condition>( condition );
  491. }
  492. else
  493. {
  494. return code.category().equivalent( code.value(), condition ) || condition.category().equivalent( code, condition.value() );
  495. }
  496. }
  497. inline bool operator==( const error_condition & condition, const error_code & code ) noexcept
  498. {
  499. return code == condition;
  500. }
  501. inline bool operator!=( const error_code & lhs, const error_condition & rhs ) noexcept
  502. {
  503. return !( lhs == rhs );
  504. }
  505. inline bool operator!=( const error_condition & lhs, const error_code & rhs ) noexcept
  506. {
  507. return !( lhs == rhs );
  508. }
  509. inline std::size_t hash_value( error_code const & ec )
  510. {
  511. if( ec.lc_flags_ == 1 )
  512. {
  513. std::error_code const& e2 = *reinterpret_cast<std::error_code const*>( ec.d2_ );
  514. return std::hash<std::error_code>()( e2 );
  515. }
  516. error_category const & cat = ec.category();
  517. boost::ulong_long_type id_ = cat.id_;
  518. if( id_ == 0 )
  519. {
  520. id_ = reinterpret_cast<boost::uintptr_t>( &cat );
  521. }
  522. boost::ulong_long_type hv = ( boost::ulong_long_type( 0xCBF29CE4 ) << 32 ) + 0x84222325;
  523. boost::ulong_long_type const prime = ( boost::ulong_long_type( 0x00000100 ) << 32 ) + 0x000001B3;
  524. // id
  525. hv ^= id_;
  526. hv *= prime;
  527. // value
  528. hv ^= static_cast<unsigned>( ec.value() );
  529. hv *= prime;
  530. return static_cast<std::size_t>( hv );
  531. }
  532. } // namespace system
  533. } // namespace boost
  534. #if defined(BOOST_GCC) && BOOST_GCC >= 40600 && BOOST_GCC < 70000
  535. # pragma GCC diagnostic pop
  536. #endif
  537. #endif // #ifndef BOOST_SYSTEM_DETAIL_ERROR_CODE_HPP_INCLUDED