error_code.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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, 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/detail/error_category.hpp>
  12. #include <boost/system/detail/error_condition.hpp>
  13. #include <boost/system/detail/system_category.hpp>
  14. #include <boost/system/detail/enable_if.hpp>
  15. #include <boost/system/is_error_code_enum.hpp>
  16. #include <boost/system/detail/config.hpp>
  17. #include <boost/cstdint.hpp>
  18. #include <boost/config.hpp>
  19. #include <ostream>
  20. namespace boost
  21. {
  22. namespace system
  23. {
  24. // class error_code
  25. // We want error_code to be a value type that can be copied without slicing
  26. // and without requiring heap allocation, but we also want it to have
  27. // polymorphic behavior based on the error category. This is achieved by
  28. // abstract base class error_category supplying the polymorphic behavior,
  29. // and error_code containing a pointer to an object of a type derived
  30. // from error_category.
  31. class error_code
  32. {
  33. private:
  34. int val_;
  35. bool failed_;
  36. const error_category * cat_;
  37. public:
  38. // constructors:
  39. BOOST_SYSTEM_CONSTEXPR error_code() BOOST_NOEXCEPT:
  40. val_( 0 ), failed_( false ), cat_( &system_category() )
  41. {
  42. }
  43. BOOST_SYSTEM_CONSTEXPR error_code( int val, const error_category & cat ) BOOST_NOEXCEPT:
  44. val_( val ), failed_( detail::failed_impl( val, cat ) ), cat_( &cat )
  45. {
  46. }
  47. template<class ErrorCodeEnum> BOOST_SYSTEM_CONSTEXPR error_code( ErrorCodeEnum e,
  48. typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value>::type* = 0 ) BOOST_NOEXCEPT
  49. {
  50. *this = make_error_code( e );
  51. }
  52. // modifiers:
  53. BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) BOOST_NOEXCEPT
  54. {
  55. val_ = val;
  56. failed_ = detail::failed_impl( val, cat );
  57. cat_ = &cat;
  58. }
  59. template<typename ErrorCodeEnum>
  60. BOOST_SYSTEM_CONSTEXPR typename detail::enable_if<is_error_code_enum<ErrorCodeEnum>::value, error_code>::type &
  61. operator=( ErrorCodeEnum val ) BOOST_NOEXCEPT
  62. {
  63. *this = make_error_code( val );
  64. return *this;
  65. }
  66. BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT
  67. {
  68. val_ = 0;
  69. failed_ = false;
  70. cat_ = &system_category();
  71. }
  72. // observers:
  73. BOOST_SYSTEM_CONSTEXPR int value() const BOOST_NOEXCEPT
  74. {
  75. return val_;
  76. }
  77. BOOST_SYSTEM_CONSTEXPR const error_category & category() const BOOST_NOEXCEPT
  78. {
  79. return *cat_;
  80. }
  81. error_condition default_error_condition() const BOOST_NOEXCEPT
  82. {
  83. return cat_->default_error_condition( value() );
  84. }
  85. std::string message() const
  86. {
  87. return cat_->message( value() );
  88. }
  89. char const * message( char * buffer, std::size_t len ) const BOOST_NOEXCEPT
  90. {
  91. return cat_->message( value(), buffer, len );
  92. }
  93. BOOST_SYSTEM_CONSTEXPR bool failed() const BOOST_NOEXCEPT
  94. {
  95. return failed_;
  96. }
  97. #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  98. BOOST_SYSTEM_CONSTEXPR explicit operator bool() const BOOST_NOEXCEPT // true if error
  99. {
  100. return failed_;
  101. }
  102. #else
  103. typedef void (*unspecified_bool_type)();
  104. static void unspecified_bool_true() {}
  105. BOOST_SYSTEM_CONSTEXPR operator unspecified_bool_type() const BOOST_NOEXCEPT // true if error
  106. {
  107. return failed_? unspecified_bool_true: 0;
  108. }
  109. BOOST_SYSTEM_CONSTEXPR bool operator!() const BOOST_NOEXCEPT // true if no error
  110. {
  111. return !failed_;
  112. }
  113. #endif
  114. // relationals:
  115. // the more symmetrical non-member syntax allows enum
  116. // conversions work for both rhs and lhs.
  117. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
  118. {
  119. return lhs.val_ == rhs.val_ && *lhs.cat_ == *rhs.cat_;
  120. }
  121. BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
  122. {
  123. return *lhs.cat_ < *rhs.cat_ || ( *lhs.cat_ == *rhs.cat_ && lhs.val_ < rhs.val_ );
  124. }
  125. #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
  126. operator std::error_code () const
  127. {
  128. return std::error_code( value(), category() );
  129. }
  130. #endif
  131. };
  132. BOOST_SYSTEM_CONSTEXPR inline bool operator!=( const error_code & lhs, const error_code & rhs ) BOOST_NOEXCEPT
  133. {
  134. return !( lhs == rhs );
  135. }
  136. template<class Ch, class Tr>
  137. inline std::basic_ostream<Ch, Tr>&
  138. operator<< (std::basic_ostream<Ch, Tr>& os, error_code const & ec)
  139. {
  140. os << ec.category().name() << ':' << ec.value();
  141. return os;
  142. }
  143. inline std::size_t hash_value( error_code const & ec )
  144. {
  145. error_category const & cat = ec.category();
  146. boost::ulong_long_type id_ = cat.id_;
  147. if( id_ == 0 )
  148. {
  149. id_ = reinterpret_cast<boost::uintptr_t>( &cat );
  150. }
  151. boost::ulong_long_type hv = ( boost::ulong_long_type( 0xCBF29CE4 ) << 32 ) + 0x84222325;
  152. boost::ulong_long_type const prime = ( boost::ulong_long_type( 0x00000100 ) << 32 ) + 0x000001B3;
  153. // id
  154. hv ^= id_;
  155. hv *= prime;
  156. // value
  157. hv ^= static_cast<unsigned>( ec.value() );
  158. hv *= prime;
  159. return static_cast<std::size_t>( hv );
  160. }
  161. } // namespace system
  162. } // namespace boost
  163. #endif // #ifndef BOOST_SYSTEM_DETAIL_ERROR_CODE_HPP_INCLUDED