error_condition.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef BOOST_SYSTEM_DETAIL_ERROR_CONDITION_HPP_INCLUDED
  2. #define BOOST_SYSTEM_DETAIL_ERROR_CONDITION_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/generic_category.hpp>
  13. #include <boost/system/detail/enable_if.hpp>
  14. #include <boost/system/is_error_condition_enum.hpp>
  15. #include <boost/system/detail/config.hpp>
  16. #include <boost/config.hpp>
  17. namespace boost
  18. {
  19. namespace system
  20. {
  21. // class error_condition
  22. // error_conditions are portable, error_codes are system or library specific
  23. class error_condition
  24. {
  25. private:
  26. int val_;
  27. error_category const * cat_;
  28. public:
  29. // constructors:
  30. BOOST_SYSTEM_CONSTEXPR error_condition() BOOST_NOEXCEPT:
  31. val_( 0 ), cat_( &generic_category() )
  32. {
  33. }
  34. BOOST_SYSTEM_CONSTEXPR error_condition( int val, const error_category & cat ) BOOST_NOEXCEPT:
  35. val_( val ), cat_( &cat )
  36. {
  37. }
  38. template<class ErrorConditionEnum> BOOST_SYSTEM_CONSTEXPR error_condition( ErrorConditionEnum e,
  39. typename detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value>::type* = 0) BOOST_NOEXCEPT
  40. {
  41. *this = make_error_condition( e );
  42. }
  43. // modifiers:
  44. BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) BOOST_NOEXCEPT
  45. {
  46. val_ = val;
  47. cat_ = &cat;
  48. }
  49. template<typename ErrorConditionEnum>
  50. BOOST_SYSTEM_CONSTEXPR typename detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value, error_condition>::type &
  51. operator=( ErrorConditionEnum val ) BOOST_NOEXCEPT
  52. {
  53. *this = make_error_condition( val );
  54. return *this;
  55. }
  56. BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT
  57. {
  58. val_ = 0;
  59. cat_ = &generic_category();
  60. }
  61. // observers:
  62. BOOST_SYSTEM_CONSTEXPR int value() const BOOST_NOEXCEPT
  63. {
  64. return val_;
  65. }
  66. BOOST_SYSTEM_CONSTEXPR const error_category & category() const BOOST_NOEXCEPT
  67. {
  68. return *cat_;
  69. }
  70. std::string message() const
  71. {
  72. return cat_->message( value() );
  73. }
  74. BOOST_SYSTEM_DEPRECATED("this function is slated for removal") char const * message( char * buffer, std::size_t len ) const BOOST_NOEXCEPT
  75. {
  76. return cat_->message( value(), buffer, len );
  77. }
  78. BOOST_SYSTEM_DEPRECATED("this function is slated for removal") BOOST_SYSTEM_CONSTEXPR bool failed() const BOOST_NOEXCEPT
  79. {
  80. return detail::failed_impl( val_, *cat_ );
  81. }
  82. #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  83. BOOST_SYSTEM_CONSTEXPR explicit operator bool() const BOOST_NOEXCEPT // true if error
  84. {
  85. return val_ != 0;
  86. }
  87. #else
  88. typedef void (*unspecified_bool_type)();
  89. static void unspecified_bool_true() {}
  90. BOOST_SYSTEM_CONSTEXPR operator unspecified_bool_type() const BOOST_NOEXCEPT // true if error
  91. {
  92. return val_ != 0? unspecified_bool_true: 0;
  93. }
  94. BOOST_SYSTEM_CONSTEXPR bool operator!() const BOOST_NOEXCEPT // true if no error
  95. {
  96. return val_ == 0;
  97. }
  98. #endif
  99. // relationals:
  100. // the more symmetrical non-member syntax allows enum
  101. // conversions work for both rhs and lhs.
  102. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  103. {
  104. return lhs.val_ == rhs.val_ && *lhs.cat_ == *rhs.cat_;
  105. }
  106. BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  107. {
  108. return *lhs.cat_ < *rhs.cat_ || ( *lhs.cat_ == *rhs.cat_ && lhs.val_ < rhs.val_ );
  109. }
  110. #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
  111. operator std::error_condition () const
  112. {
  113. return std::error_condition( value(), category() );
  114. }
  115. #endif
  116. };
  117. BOOST_SYSTEM_CONSTEXPR inline bool operator!=( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  118. {
  119. return !( lhs == rhs );
  120. }
  121. } // namespace system
  122. } // namespace boost
  123. #endif // #ifndef BOOST_SYSTEM_DETAIL_ERROR_CONDITION_HPP_INCLUDED