std_interoperability.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Support for interoperability between Boost.System and <system_error>
  2. //
  3. // Copyright 2018 Peter Dimov
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See library home page at http://www.boost.org/libs/system
  9. #include <system_error>
  10. #include <map>
  11. #include <memory>
  12. #include <mutex>
  13. //
  14. namespace boost
  15. {
  16. namespace system
  17. {
  18. namespace detail
  19. {
  20. class BOOST_SYMBOL_VISIBLE std_category: public std::error_category
  21. {
  22. private:
  23. boost::system::error_category const * pc_;
  24. public:
  25. explicit std_category( boost::system::error_category const * pc, unsigned id ): pc_( pc )
  26. {
  27. if( id != 0 )
  28. {
  29. #if defined(_MSC_VER) && defined(_CPPLIB_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
  30. // Poking into the protected _Addr member of std::error_category
  31. // is not a particularly good programming practice, but what can
  32. // you do
  33. _Addr = id;
  34. #endif
  35. }
  36. }
  37. virtual const char * name() const BOOST_NOEXCEPT
  38. {
  39. return pc_->name();
  40. }
  41. virtual std::string message( int ev ) const
  42. {
  43. return pc_->message( ev );
  44. }
  45. virtual std::error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT
  46. {
  47. return pc_->default_error_condition( ev );
  48. }
  49. virtual bool equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT;
  50. virtual bool equivalent( const std::error_code & code, int condition ) const BOOST_NOEXCEPT;
  51. };
  52. inline std::error_category const & to_std_category( boost::system::error_category const & cat ) BOOST_SYMBOL_VISIBLE;
  53. struct cat_ptr_less
  54. {
  55. bool operator()( boost::system::error_category const * p1, boost::system::error_category const * p2 ) const BOOST_NOEXCEPT
  56. {
  57. return *p1 < *p2;
  58. }
  59. };
  60. inline std::error_category const & to_std_category( boost::system::error_category const & cat )
  61. {
  62. if( cat == boost::system::system_category() )
  63. {
  64. static const std_category system_instance( &cat, 0x1F4D7 );
  65. return system_instance;
  66. }
  67. else if( cat == boost::system::generic_category() )
  68. {
  69. static const std_category generic_instance( &cat, 0x1F4D3 );
  70. return generic_instance;
  71. }
  72. else
  73. {
  74. typedef std::map< boost::system::error_category const *, std::unique_ptr<std_category>, cat_ptr_less > map_type;
  75. static map_type map_;
  76. static std::mutex map_mx_;
  77. std::lock_guard<std::mutex> guard( map_mx_ );
  78. map_type::iterator i = map_.find( &cat );
  79. if( i == map_.end() )
  80. {
  81. std::unique_ptr<std_category> p( new std_category( &cat, 0 ) );
  82. std::pair<map_type::iterator, bool> r = map_.insert( map_type::value_type( &cat, std::move( p ) ) );
  83. i = r.first;
  84. }
  85. return *i->second;
  86. }
  87. }
  88. inline bool std_category::equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT
  89. {
  90. if( condition.category() == *this )
  91. {
  92. boost::system::error_condition bn( condition.value(), *pc_ );
  93. return pc_->equivalent( code, bn );
  94. }
  95. else if( condition.category() == std::generic_category() || condition.category() == boost::system::generic_category() )
  96. {
  97. boost::system::error_condition bn( condition.value(), boost::system::generic_category() );
  98. return pc_->equivalent( code, bn );
  99. }
  100. #ifndef BOOST_NO_RTTI
  101. else if( std_category const* pc2 = dynamic_cast< std_category const* >( &condition.category() ) )
  102. {
  103. boost::system::error_condition bn( condition.value(), *pc2->pc_ );
  104. return pc_->equivalent( code, bn );
  105. }
  106. #endif
  107. else
  108. {
  109. return default_error_condition( code ) == condition;
  110. }
  111. }
  112. inline bool std_category::equivalent( const std::error_code & code, int condition ) const BOOST_NOEXCEPT
  113. {
  114. if( code.category() == *this )
  115. {
  116. boost::system::error_code bc( code.value(), *pc_ );
  117. return pc_->equivalent( bc, condition );
  118. }
  119. else if( code.category() == std::generic_category() || code.category() == boost::system::generic_category() )
  120. {
  121. boost::system::error_code bc( code.value(), boost::system::generic_category() );
  122. return pc_->equivalent( bc, condition );
  123. }
  124. #ifndef BOOST_NO_RTTI
  125. else if( std_category const* pc2 = dynamic_cast< std_category const* >( &code.category() ) )
  126. {
  127. boost::system::error_code bc( code.value(), *pc2->pc_ );
  128. return pc_->equivalent( bc, condition );
  129. }
  130. #endif
  131. else if( *pc_ == boost::system::generic_category() )
  132. {
  133. return std::generic_category().equivalent( code, condition );
  134. }
  135. else
  136. {
  137. return false;
  138. }
  139. }
  140. } // namespace detail
  141. } // namespace system
  142. } // namespace boost