]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/system/detail/to_std_category.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / system / detail / to_std_category.hpp
1 #ifndef BOOST_SYSTEM_DETAIL_TO_STD_CATEGORY_HPP_INCLUDED
2 #define BOOST_SYSTEM_DETAIL_TO_STD_CATEGORY_HPP_INCLUDED
3
4 // Support for interoperability between Boost.System and <system_error>
5 //
6 // Copyright 2018 Peter Dimov
7 //
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)
10 //
11 // See library home page at http://www.boost.org/libs/system
12
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>
18 #include <map>
19 #include <memory>
20 #include <mutex>
21
22 //
23
24 namespace boost
25 {
26
27 namespace system
28 {
29
30 namespace detail
31 {
32
33 class BOOST_SYMBOL_VISIBLE std_category: public std::error_category
34 {
35 private:
36
37 boost::system::error_category const * pc_;
38
39 public:
40
41 explicit std_category( boost::system::error_category const * pc, unsigned id ): pc_( pc )
42 {
43 if( id != 0 )
44 {
45 #if defined(_MSC_VER) && defined(_CPPLIB_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
46
47 // Poking into the protected _Addr member of std::error_category
48 // is not a particularly good programming practice, but what can
49 // you do
50
51 _Addr = id;
52
53 #endif
54 }
55 }
56
57 const char * name() const BOOST_NOEXCEPT BOOST_OVERRIDE
58 {
59 return pc_->name();
60 }
61
62 std::string message( int ev ) const BOOST_OVERRIDE
63 {
64 return pc_->message( ev );
65 }
66
67 std::error_condition default_error_condition( int ev ) const BOOST_NOEXCEPT BOOST_OVERRIDE
68 {
69 return pc_->default_error_condition( ev );
70 }
71
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;
74 };
75
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;
78 #endif
79
80 struct cat_ptr_less
81 {
82 bool operator()( boost::system::error_category const * p1, boost::system::error_category const * p2 ) const BOOST_NOEXCEPT
83 {
84 return *p1 < *p2;
85 }
86 };
87
88 inline std::error_category const & to_std_category( boost::system::error_category const & cat )
89 {
90 if( cat.id_ == boost::system::detail::system_category_id )
91 {
92 static const std_category system_instance( &cat, 0x1F4D7 );
93 return system_instance;
94 }
95 else if( cat.id_ == boost::system::detail::generic_category_id )
96 {
97 static const std_category generic_instance( &cat, 0x1F4D3 );
98 return generic_instance;
99 }
100 else
101 {
102 typedef std::map< boost::system::error_category const *, std::unique_ptr<std_category>, cat_ptr_less > map_type;
103
104 static map_type map_;
105 static std::mutex map_mx_;
106
107 std::lock_guard<std::mutex> guard( map_mx_ );
108
109 map_type::iterator i = map_.find( &cat );
110
111 if( i == map_.end() )
112 {
113 std::unique_ptr<std_category> p( new std_category( &cat, 0 ) );
114
115 std::pair<map_type::iterator, bool> r = map_.insert( map_type::value_type( &cat, std::move( p ) ) );
116
117 i = r.first;
118 }
119
120 return *i->second;
121 }
122 }
123
124 inline bool std_category::equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT
125 {
126 if( condition.category() == *this )
127 {
128 boost::system::error_condition bn( condition.value(), *pc_ );
129 return pc_->equivalent( code, bn );
130 }
131 else if( condition.category() == std::generic_category() || condition.category() == boost::system::generic_category() )
132 {
133 boost::system::error_condition bn( condition.value(), boost::system::generic_category() );
134 return pc_->equivalent( code, bn );
135 }
136
137 #ifndef BOOST_NO_RTTI
138
139 else if( std_category const* pc2 = dynamic_cast< std_category const* >( &condition.category() ) )
140 {
141 boost::system::error_condition bn( condition.value(), *pc2->pc_ );
142 return pc_->equivalent( code, bn );
143 }
144
145 #endif
146
147 else
148 {
149 return default_error_condition( code ) == condition;
150 }
151 }
152
153 inline bool std_category::equivalent( const std::error_code & code, int condition ) const BOOST_NOEXCEPT
154 {
155 if( code.category() == *this )
156 {
157 boost::system::error_code bc( code.value(), *pc_ );
158 return pc_->equivalent( bc, condition );
159 }
160 else if( code.category() == std::generic_category() || code.category() == boost::system::generic_category() )
161 {
162 boost::system::error_code bc( code.value(), boost::system::generic_category() );
163 return pc_->equivalent( bc, condition );
164 }
165
166 #ifndef BOOST_NO_RTTI
167
168 else if( std_category const* pc2 = dynamic_cast< std_category const* >( &code.category() ) )
169 {
170 boost::system::error_code bc( code.value(), *pc2->pc_ );
171 return pc_->equivalent( bc, condition );
172 }
173 #endif
174
175 else if( *pc_ == boost::system::generic_category() )
176 {
177 return std::generic_category().equivalent( code, condition );
178 }
179 else
180 {
181 return false;
182 }
183 }
184
185 } // namespace detail
186
187 } // namespace system
188
189 } // namespace boost
190
191 #endif // #ifndef BOOST_SYSTEM_DETAIL_TO_STD_CATEGORY_HPP_INCLUDED