error_condition.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 BOOST_NOEXCEPT
  41. {
  42. return cat_? cat_->id_: detail::generic_category_id;
  43. }
  44. public:
  45. // constructors:
  46. BOOST_SYSTEM_CONSTEXPR error_condition() BOOST_NOEXCEPT:
  47. val_( 0 ), cat_( 0 )
  48. {
  49. }
  50. BOOST_SYSTEM_CONSTEXPR error_condition( int val, const error_category & cat ) BOOST_NOEXCEPT:
  51. val_( val ), cat_( &cat )
  52. {
  53. }
  54. BOOST_SYSTEM_CONSTEXPR explicit error_condition( boost::system::detail::generic_value_tag vt ) BOOST_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) BOOST_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) BOOST_NOEXCEPT:
  67. val_( e ), cat_( 0 )
  68. {
  69. }
  70. // modifiers:
  71. BOOST_SYSTEM_CONSTEXPR void assign( int val, const error_category & cat ) BOOST_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 ) BOOST_NOEXCEPT
  79. {
  80. *this = error_condition( val );
  81. return *this;
  82. }
  83. BOOST_SYSTEM_CONSTEXPR void clear() BOOST_NOEXCEPT
  84. {
  85. val_ = 0;
  86. cat_ = 0;
  87. }
  88. // observers:
  89. BOOST_SYSTEM_CONSTEXPR int value() const BOOST_NOEXCEPT
  90. {
  91. return val_;
  92. }
  93. BOOST_SYSTEM_CONSTEXPR const error_category & category() const BOOST_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 BOOST_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 BOOST_NOEXCEPT
  120. {
  121. if( cat_ )
  122. {
  123. return detail::failed_impl( val_, *cat_ );
  124. }
  125. else
  126. {
  127. return val_ != 0;
  128. }
  129. }
  130. #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  131. BOOST_SYSTEM_CONSTEXPR explicit operator bool() const BOOST_NOEXCEPT // true if error
  132. {
  133. return failed();
  134. }
  135. #else
  136. typedef void (*unspecified_bool_type)();
  137. static void unspecified_bool_true() {}
  138. BOOST_SYSTEM_CONSTEXPR operator unspecified_bool_type() const BOOST_NOEXCEPT // true if error
  139. {
  140. return failed()? unspecified_bool_true: 0;
  141. }
  142. BOOST_SYSTEM_CONSTEXPR bool operator!() const BOOST_NOEXCEPT // true if no error
  143. {
  144. return !failed();
  145. }
  146. #endif
  147. // relationals:
  148. // the more symmetrical non-member syntax allows enum
  149. // conversions work for both rhs and lhs.
  150. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  151. {
  152. if( lhs.val_ != rhs.val_ )
  153. {
  154. return false;
  155. }
  156. else if( lhs.cat_ == 0 )
  157. {
  158. return rhs.cat_id() == detail::generic_category_id;
  159. }
  160. else if( rhs.cat_ == 0 )
  161. {
  162. return lhs.cat_id() == detail::generic_category_id;
  163. }
  164. else
  165. {
  166. return *lhs.cat_ == *rhs.cat_;
  167. }
  168. }
  169. BOOST_SYSTEM_CONSTEXPR inline friend bool operator<( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  170. {
  171. error_category const& lcat = lhs.category();
  172. error_category const& rcat = rhs.category();
  173. return lcat < rcat || ( lcat == rcat && lhs.val_ < rhs.val_ );
  174. }
  175. BOOST_SYSTEM_CONSTEXPR inline friend bool operator!=( const error_condition & lhs, const error_condition & rhs ) BOOST_NOEXCEPT
  176. {
  177. return !( lhs == rhs );
  178. }
  179. #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
  180. operator std::error_condition () const
  181. {
  182. // This condition must be the same as the one in error_category_impl.hpp
  183. #if defined(BOOST_SYSTEM_AVOID_STD_GENERIC_CATEGORY)
  184. return std::error_condition( value(), category() );
  185. #else
  186. if( cat_ )
  187. {
  188. return std::error_condition( val_, *cat_ );
  189. }
  190. else
  191. {
  192. return std::error_condition( val_, std::generic_category() );
  193. }
  194. #endif
  195. }
  196. inline friend bool operator==( std::error_code const & lhs, error_condition const & rhs ) BOOST_NOEXCEPT
  197. {
  198. return lhs == static_cast< std::error_condition >( rhs );
  199. }
  200. inline friend bool operator==( error_condition const & lhs, std::error_code const & rhs ) BOOST_NOEXCEPT
  201. {
  202. return static_cast< std::error_condition >( lhs ) == rhs;
  203. }
  204. inline friend bool operator!=( std::error_code const & lhs, error_condition const & rhs ) BOOST_NOEXCEPT
  205. {
  206. return !( lhs == rhs );
  207. }
  208. inline friend bool operator!=( error_condition const & lhs, std::error_code const & rhs ) BOOST_NOEXCEPT
  209. {
  210. return !( lhs == rhs );
  211. }
  212. //
  213. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  214. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( error_condition const & lhs, E rhs ) BOOST_NOEXCEPT
  215. {
  216. return lhs == make_error_condition( rhs );
  217. }
  218. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  219. BOOST_SYSTEM_CONSTEXPR inline friend bool operator==( E lhs, error_condition const & rhs ) BOOST_NOEXCEPT
  220. {
  221. return make_error_condition( lhs ) == rhs;
  222. }
  223. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  224. BOOST_SYSTEM_CONSTEXPR inline friend bool operator!=( error_condition const & lhs, E rhs ) BOOST_NOEXCEPT
  225. {
  226. return !( lhs == rhs );
  227. }
  228. template<class E, class N = typename detail::enable_if<std::is_error_condition_enum<E>::value>::type>
  229. BOOST_SYSTEM_CONSTEXPR inline friend bool operator!=( E lhs, error_condition const & rhs ) BOOST_NOEXCEPT
  230. {
  231. return !( lhs == rhs );
  232. }
  233. //
  234. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  235. inline friend bool operator==( error_condition const & lhs, E rhs ) BOOST_NOEXCEPT
  236. {
  237. return lhs == make_error_code( rhs );
  238. }
  239. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  240. inline friend bool operator==( E lhs, error_condition const & rhs ) BOOST_NOEXCEPT
  241. {
  242. return make_error_code( lhs ) == rhs;
  243. }
  244. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  245. inline friend bool operator!=( error_condition const & lhs, E rhs ) BOOST_NOEXCEPT
  246. {
  247. return !( lhs == rhs );
  248. }
  249. template<class E, class N1 = void, class N2 = typename detail::enable_if<std::is_error_code_enum<E>::value>::type>
  250. inline friend bool operator!=( E lhs, error_condition const & rhs ) BOOST_NOEXCEPT
  251. {
  252. return !( lhs == rhs );
  253. }
  254. #endif
  255. std::string to_string() const
  256. {
  257. std::string r( "cond:" );
  258. if( cat_ )
  259. {
  260. r += cat_->name();
  261. }
  262. else
  263. {
  264. r += "generic";
  265. }
  266. detail::append_int( r, value() );
  267. return r;
  268. }
  269. template<class Ch, class Tr>
  270. inline friend std::basic_ostream<Ch, Tr>&
  271. operator<< (std::basic_ostream<Ch, Tr>& os, error_condition const & en)
  272. {
  273. os << en.to_string();
  274. return os;
  275. }
  276. };
  277. } // namespace system
  278. } // namespace boost
  279. #endif // #ifndef BOOST_SYSTEM_DETAIL_ERROR_CONDITION_HPP_INCLUDED