1 // Support for interoperability between Boost.System and <system_error>
3 // Copyright 2018 Peter Dimov
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)
8 // See library home page at http://www.boost.org/libs/system
10 #include <system_error>
26 class BOOST_SYMBOL_VISIBLE std_category: public std::error_category
30 boost::system::error_category const * pc_;
34 explicit std_category( boost::system::error_category const * pc, unsigned id ): pc_( pc )
38 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
40 // Poking into the protected _Addr member of std::error_category
41 // is not a particularly good programming practice, but what can
50 virtual const char * name() const BOOST_NOEXCEPT
55 virtual std::string message( int ev ) const
57 return pc_->message( ev );
60 virtual std::error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT
62 return pc_->default_error_condition( ev );
65 virtual bool equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT;
66 virtual bool equivalent( const std::error_code & code, int condition ) const BOOST_NOEXCEPT;
69 #if !defined(__SUNPRO_CC) // trailing __global is not supported
70 inline std::error_category const & to_std_category( boost::system::error_category const & cat ) BOOST_SYMBOL_VISIBLE;
75 bool operator()( boost::system::error_category const * p1, boost::system::error_category const * p2 ) const BOOST_NOEXCEPT
81 inline std::error_category const & to_std_category( boost::system::error_category const & cat )
83 if( cat == boost::system::system_category() )
85 static const std_category system_instance( &cat, 0x1F4D7 );
86 return system_instance;
88 else if( cat == boost::system::generic_category() )
90 static const std_category generic_instance( &cat, 0x1F4D3 );
91 return generic_instance;
95 typedef std::map< boost::system::error_category const *, std::unique_ptr<std_category>, cat_ptr_less > map_type;
98 static std::mutex map_mx_;
100 std::lock_guard<std::mutex> guard( map_mx_ );
102 map_type::iterator i = map_.find( &cat );
104 if( i == map_.end() )
106 std::unique_ptr<std_category> p( new std_category( &cat, 0 ) );
108 std::pair<map_type::iterator, bool> r = map_.insert( map_type::value_type( &cat, std::move( p ) ) );
117 inline bool std_category::equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT
119 if( condition.category() == *this )
121 boost::system::error_condition bn( condition.value(), *pc_ );
122 return pc_->equivalent( code, bn );
124 else if( condition.category() == std::generic_category() || condition.category() == boost::system::generic_category() )
126 boost::system::error_condition bn( condition.value(), boost::system::generic_category() );
127 return pc_->equivalent( code, bn );
130 #ifndef BOOST_NO_RTTI
132 else if( std_category const* pc2 = dynamic_cast< std_category const* >( &condition.category() ) )
134 boost::system::error_condition bn( condition.value(), *pc2->pc_ );
135 return pc_->equivalent( code, bn );
142 return default_error_condition( code ) == condition;
146 inline bool std_category::equivalent( const std::error_code & code, int condition ) const BOOST_NOEXCEPT
148 if( code.category() == *this )
150 boost::system::error_code bc( code.value(), *pc_ );
151 return pc_->equivalent( bc, condition );
153 else if( code.category() == std::generic_category() || code.category() == boost::system::generic_category() )
155 boost::system::error_code bc( code.value(), boost::system::generic_category() );
156 return pc_->equivalent( bc, condition );
159 #ifndef BOOST_NO_RTTI
161 else if( std_category const* pc2 = dynamic_cast< std_category const* >( &code.category() ) )
163 boost::system::error_code bc( code.value(), *pc2->pc_ );
164 return pc_->equivalent( bc, condition );
168 else if( *pc_ == boost::system::generic_category() )
170 return std::generic_category().equivalent( code, condition );
178 } // namespace detail
180 } // namespace system