1 #ifndef BOOST_SYSTEM_DETAIL_TO_STD_CATEGORY_HPP_INCLUDED
2 #define BOOST_SYSTEM_DETAIL_TO_STD_CATEGORY_HPP_INCLUDED
4 // Support for interoperability between Boost.System and <system_error>
6 // Copyright 2018 Peter Dimov
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 // See library home page at http://www.boost.org/libs/system
13 #include <boost/system/detail/error_category.hpp>
14 #include <boost/system/detail/error_condition.hpp>
15 #include <boost/system/detail/error_code.hpp>
16 #include <boost/system/detail/generic_category.hpp>
17 #include <system_error>
33 class BOOST_SYMBOL_VISIBLE std_category: public std::error_category
37 boost::system::error_category const * pc_;
41 explicit std_category( boost::system::error_category const * pc, unsigned id ): pc_( pc )
45 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
47 // Poking into the protected _Addr member of std::error_category
48 // is not a particularly good programming practice, but what can
57 const char * name() const BOOST_NOEXCEPT BOOST_OVERRIDE
62 std::string message( int ev ) const BOOST_OVERRIDE
64 return pc_->message( ev );
67 std::error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT BOOST_OVERRIDE
69 return pc_->default_error_condition( ev );
72 bool equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT BOOST_OVERRIDE;
73 bool equivalent( const std::error_code & code, int condition ) const BOOST_NOEXCEPT BOOST_OVERRIDE;
76 #if !defined(__SUNPRO_CC) // trailing __global is not supported
77 inline std::error_category const & to_std_category( boost::system::error_category const & cat ) BOOST_SYMBOL_VISIBLE;
82 bool operator()( boost::system::error_category const * p1, boost::system::error_category const * p2 ) const BOOST_NOEXCEPT
88 inline std::error_category const & to_std_category( boost::system::error_category const & cat )
90 if( cat.id_ == boost::system::detail::system_category_id )
92 static const std_category system_instance( &cat, 0x1F4D7 );
93 return system_instance;
95 else if( cat.id_ == boost::system::detail::generic_category_id )
97 static const std_category generic_instance( &cat, 0x1F4D3 );
98 return generic_instance;
102 typedef std::map< boost::system::error_category const *, std::unique_ptr<std_category>, cat_ptr_less > map_type;
104 static map_type map_;
105 static std::mutex map_mx_;
107 std::lock_guard<std::mutex> guard( map_mx_ );
109 map_type::iterator i = map_.find( &cat );
111 if( i == map_.end() )
113 std::unique_ptr<std_category> p( new std_category( &cat, 0 ) );
115 std::pair<map_type::iterator, bool> r = map_.insert( map_type::value_type( &cat, std::move( p ) ) );
124 inline bool std_category::equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT
126 if( condition.category() == *this )
128 boost::system::error_condition bn( condition.value(), *pc_ );
129 return pc_->equivalent( code, bn );
131 else if( condition.category() == std::generic_category() || condition.category() == boost::system::generic_category() )
133 boost::system::error_condition bn( condition.value(), boost::system::generic_category() );
134 return pc_->equivalent( code, bn );
137 #ifndef BOOST_NO_RTTI
139 else if( std_category const* pc2 = dynamic_cast< std_category const* >( &condition.category() ) )
141 boost::system::error_condition bn( condition.value(), *pc2->pc_ );
142 return pc_->equivalent( code, bn );
149 return default_error_condition( code ) == condition;
153 inline bool std_category::equivalent( const std::error_code & code, int condition ) const BOOST_NOEXCEPT
155 if( code.category() == *this )
157 boost::system::error_code bc( code.value(), *pc_ );
158 return pc_->equivalent( bc, condition );
160 else if( code.category() == std::generic_category() || code.category() == boost::system::generic_category() )
162 boost::system::error_code bc( code.value(), boost::system::generic_category() );
163 return pc_->equivalent( bc, condition );
166 #ifndef BOOST_NO_RTTI
168 else if( std_category const* pc2 = dynamic_cast< std_category const* >( &code.category() ) )
170 boost::system::error_code bc( code.value(), *pc2->pc_ );
171 return pc_->equivalent( bc, condition );
175 else if( *pc_ == boost::system::generic_category() )
177 return std::generic_category().equivalent( code, condition );
185 } // namespace detail
187 } // namespace system
191 #endif // #ifndef BOOST_SYSTEM_DETAIL_TO_STD_CATEGORY_HPP_INCLUDED