std_interoperability.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #if !defined(__SUNPRO_CC) // trailing __global is not supported
  53. inline std::error_category const & to_std_category( boost::system::error_category const & cat ) BOOST_SYMBOL_VISIBLE;
  54. #endif
  55. struct cat_ptr_less
  56. {
  57. bool operator()( boost::system::error_category const * p1, boost::system::error_category const * p2 ) const BOOST_NOEXCEPT
  58. {
  59. return *p1 < *p2;
  60. }
  61. };
  62. inline std::error_category const & to_std_category( boost::system::error_category const & cat )
  63. {
  64. if( cat == boost::system::system_category() )
  65. {
  66. static const std_category system_instance( &cat, 0x1F4D7 );
  67. return system_instance;
  68. }
  69. else if( cat == boost::system::generic_category() )
  70. {
  71. static const std_category generic_instance( &cat, 0x1F4D3 );
  72. return generic_instance;
  73. }
  74. else
  75. {
  76. typedef std::map< boost::system::error_category const *, std::unique_ptr<std_category>, cat_ptr_less > map_type;
  77. static map_type map_;
  78. static std::mutex map_mx_;
  79. std::lock_guard<std::mutex> guard( map_mx_ );
  80. map_type::iterator i = map_.find( &cat );
  81. if( i == map_.end() )
  82. {
  83. std::unique_ptr<std_category> p( new std_category( &cat, 0 ) );
  84. std::pair<map_type::iterator, bool> r = map_.insert( map_type::value_type( &cat, std::move( p ) ) );
  85. i = r.first;
  86. }
  87. return *i->second;
  88. }
  89. }
  90. inline bool std_category::equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT
  91. {
  92. if( condition.category() == *this )
  93. {
  94. boost::system::error_condition bn( condition.value(), *pc_ );
  95. return pc_->equivalent( code, bn );
  96. }
  97. else if( condition.category() == std::generic_category() || condition.category() == boost::system::generic_category() )
  98. {
  99. boost::system::error_condition bn( condition.value(), boost::system::generic_category() );
  100. return pc_->equivalent( code, bn );
  101. }
  102. #ifndef BOOST_NO_RTTI
  103. else if( std_category const* pc2 = dynamic_cast< std_category const* >( &condition.category() ) )
  104. {
  105. boost::system::error_condition bn( condition.value(), *pc2->pc_ );
  106. return pc_->equivalent( code, bn );
  107. }
  108. #endif
  109. else
  110. {
  111. return default_error_condition( code ) == condition;
  112. }
  113. }
  114. inline bool std_category::equivalent( const std::error_code & code, int condition ) const BOOST_NOEXCEPT
  115. {
  116. if( code.category() == *this )
  117. {
  118. boost::system::error_code bc( code.value(), *pc_ );
  119. return pc_->equivalent( bc, condition );
  120. }
  121. else if( code.category() == std::generic_category() || code.category() == boost::system::generic_category() )
  122. {
  123. boost::system::error_code bc( code.value(), boost::system::generic_category() );
  124. return pc_->equivalent( bc, condition );
  125. }
  126. #ifndef BOOST_NO_RTTI
  127. else if( std_category const* pc2 = dynamic_cast< std_category const* >( &code.category() ) )
  128. {
  129. boost::system::error_code bc( code.value(), *pc2->pc_ );
  130. return pc_->equivalent( bc, condition );
  131. }
  132. #endif
  133. else if( *pc_ == boost::system::generic_category() )
  134. {
  135. return std::generic_category().equivalent( code, condition );
  136. }
  137. else
  138. {
  139. return false;
  140. }
  141. }
  142. } // namespace detail
  143. } // namespace system
  144. } // namespace boost