error_condition.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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-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/detail/error_category.hpp>
  12. #include <boost/system/detail/generic_category.hpp>
  13. #include <boost/system/detail/enable_if.hpp>
  14. #include <boost/system/detail/is_same.hpp>
  15. #include <boost/system/detail/errc.hpp>
  16. #include <boost/system/detail/append_int.hpp>
  17. #include <boost/system/is_error_condition_enum.hpp>
  18. #include <boost/system/detail/config.hpp>
  19. #include <boost/config.hpp>
  20. namespace boost
  21. {
  22. namespace system
  23. {
  24. // class error_condition
  25. // error_conditions are portable, error_codes are system or library specific
  26. namespace detail
  27. {
  28. struct generic_value_tag
  29. {
  30. int value;
  31. BOOST_SYSTEM_CONSTEXPR explicit generic_value_tag( int v ): value( v ) {}
  32. };
  33. } // namespace detail
  34. class error_condition
  35. {
  36. private:
  37. int val_;
  38. error_category const * cat_;
  39. private:
  40. boost::ulong_long_type cat_id() const noexcept
  41. {
  42. return cat_? cat_->id_: detail::generic_category_id;
  43. }
  44. public:
  45. // constructors:
  46. BOOST_SYSTEM_CONSTEXPR error_condition() noexcept:
  47. val_( 0 ), cat_( 0 )
  48. {
  49. }
  50. BOOST_SYSTEM_CONSTEXPR error_condition( int val, const error_category & cat ) noexcept:
  51. val_( val ), cat_( &cat )
  52. {
  53. }
  54. BOOST_SYSTEM_CONSTEXPR explicit error_condition( boost::system::detail::generic_value_tag vt ) noexcept:
  55. val_( vt.value ), cat_( 0 )
  56. {
  57. }
  58. template<class ErrorConditionEnum> BOOST_SYSTEM_CONSTEXPR error_condition( ErrorConditionEnum e,
  59. typename detail::enable_if<
  60. is_error_condition_enum<ErrorConditionEnum>::value && !boost::system::detail::is_same<ErrorConditionEnum, errc::errc_t>::value
  61. >::type* = 0) noexcept
  62. {
  63. *this = make_error_condition( e );
  64. }
  65. template<class ErrorConditionEnum> BOOST_SYSTEM_CONSTEXPR error_condition( ErrorConditionEnum e,
  66. typename detail::enable_if<boost::system::detail::is_same<ErrorConditionEnum, errc::errc_t>::value>::type* = 0) noexcept:
  67. val_( e ), cat_( 0 )
  68. {
  69. }
  70. // modifiers:
  71. BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) noexcept
  72. {
  73. val_ = val;
  74. cat_ = &cat;
  75. }
  76. template<typename ErrorConditionEnum>
  77. BOOST_SYSTEM_CONSTEXPR typename detail::enable_if<is_error_condition_enum<ErrorConditionEnum>::value, error_condition>::type &
  78. operator=( ErrorConditionEnum val ) noexcept
  79. {
  80. *this = error_condition( val );
  81. return *this;
  82. }
  83. BOOST_SYSTEM_CONSTEXPR void clear() noexcept
  84. {
  85. val_ = 0;
  86. cat_ = 0;
  87. }
  88. // observers:
  89. BOOST_SYSTEM_CONSTEXPR int value() const noexcept
  90. {
  91. return val_;
  92. }
  93. BOOST_SYSTEM_CONSTEXPR const error_category & category() const noexcept
  94. {
  95. return cat_? *cat_: generic_category();
  96. }
  97. std::string message() const
  98. {
  99. if( cat_ )
  100. {
  101. return cat_->message( value() );
  102. }
  103. else
  104. {
  105. return detail::generic_error_category_message( value() );
  106. }
  107. }
  108. char const * message( char * buffer, std::size_t len ) const noexcept
  109. {
  110. if( cat_ )
  111. {
  112. return cat_->message( value(), buffer, len );
  113. }
  114. else
  115. {
  116. return detail::generic_error_category_message( value(), buffer, len );
  117. }
  118. }
  119. BOOST_SYSTEM_CONSTEXPR bool failed() const noexcept
  120. {
  121. if( cat_ )
  122. {
  123. return detail::failed_impl( val_, *cat_ );
  124. }
  125. else
  126. {
  127. return val_ != 0;
  128. }
  129. }
  130. BOOST_SYSTEM_CONSTEXPR explicit operator bool() const noexcept // true if error
  131. {
  132. return failed();
  133. }
  134. // relationals:
  135. // the more symmetrical non-member syntax allows enum
  136. // conversions work for both rhs and lhs.
  137. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_condition & lhs, const error_condition & rhs ) noexcept
  138. {
  139. if( lhs.val_ != rhs.val_ )
  140. {
  141. return false;
  142. }
  143. else if( lhs.cat_ == 0 )
  144. {
  145. return rhs.cat_id() == detail::generic_category_id;
  146. }
  147. else if( rhs.cat_ == 0 )
  148. {
  149. return lhs.cat_id() == detail::generic_category_id;
  150. }
  151. else
  152. {
  153. return *lhs.cat_ == *rhs.cat_;
  154. }
  155. }
  156. BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_condition & lhs, const error_condition & rhs ) noexcept
  157. {
  158. error_category const& lcat = lhs.category();
  159. error_category const& rcat = rhs.category();
  160. return lcat < rcat || ( lcat == rcat && lhs.val_ < rhs.val_ );
  161. }
  162. BOOST_SYSTEM_CONSTEXPR inline friend bool operator!=( const error_condition & lhs, const error_condition & rhs ) noexcept
  163. {
  164. return !( lhs == rhs );
  165. }
  166. operator std::error_condition () const
  167. {
  168. // This condition must be the same as the one in error_category_impl.hpp
  169. #if defined(BOOST_SYSTEM_AVOID_STD_GENERIC_CATEGORY)
  170. return std::error_condition( value(), category() );
  171. #else
  172. if( cat_ )
  173. {
  174. return std::error_condition( val_, *cat_ );
  175. }
  176. else
  177. {
  178. return std::error_condition( val_, std::generic_category() );
  179. }
  180. #endif
  181. }
  182. inline friend bool operator==( std::error_code const & lhs, error_condition const & rhs ) noexcept
  183. {
  184. return lhs == static_cast< std::error_condition >( rhs );
  185. }
  186. inline friend bool operator==( error_condition const & lhs, std::error_code const & rhs ) noexcept
  187. {
  188. return static_cast< std::error_condition >( lhs ) == rhs;
  189. }
  190. inline friend bool operator!=( std::error_code const & lhs, error_condition const & rhs ) noexcept
  191. {
  192. return !( lhs == rhs );
  193. }
  194. inline friend bool operator!=( error_condition const & lhs, std::error_code const & rhs ) noexcept
  195. {
  196. return !( lhs == rhs );
  197. }
  198. //
  199. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  200. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( error_condition const & lhs, E rhs ) noexcept
  201. {
  202. return lhs == make_error_condition( rhs );
  203. }
  204. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  205. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( E lhs, error_condition const & rhs ) noexcept
  206. {
  207. return make_error_condition( lhs ) == rhs;
  208. }
  209. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  210. BOOST_SYSTEM_CONSTEXPR inline friend bool operator!=( error_condition const & lhs, E rhs ) noexcept
  211. {
  212. return !( lhs == rhs );
  213. }
  214. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  215. BOOST_SYSTEM_CONSTEXPR inline friend bool operator!=( E lhs, error_condition const & rhs ) noexcept
  216. {
  217. return !( lhs == rhs );
  218. }
  219. //
  220. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  221. inline friend bool operator==( error_condition const & lhs, E rhs ) noexcept
  222. {
  223. return lhs == make_error_code( rhs );
  224. }
  225. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  226. inline friend bool operator==( E lhs, error_condition const & rhs ) noexcept
  227. {
  228. return make_error_code( lhs ) == rhs;
  229. }
  230. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  231. inline friend bool operator!=( error_condition const & lhs, E rhs ) noexcept
  232. {
  233. return !( lhs == rhs );
  234. }
  235. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  236. inline friend bool operator!=( E lhs, error_condition const & rhs ) noexcept
  237. {
  238. return !( lhs == rhs );
  239. }
  240. std::string to_string() const
  241. {
  242. std::string r( "cond:" );
  243. if( cat_ )
  244. {
  245. r += cat_->name();
  246. }
  247. else
  248. {
  249. r += "generic";
  250. }
  251. detail::append_int( r, value() );
  252. return r;
  253. }
  254. template<class Ch, class Tr>
  255. inline friend std::basic_ostream<Ch, Tr>&
  256. operator<< (std::basic_ostream<Ch, Tr>& os, error_condition const & en)
  257. {
  258. os << en.to_string();
  259. return os;
  260. }
  261. };
  262. } // namespace system
  263. } // namespace boost
  264. #endif // #ifndef BOOST_SYSTEM_DETAIL_ERROR_CONDITION_HPP_INCLUDED