1#ifndef BOOST_SYSTEM_DETAIL_ERROR_CATEGORY_IMPL_HPP_INCLUDED
2#define BOOST_SYSTEM_DETAIL_ERROR_CATEGORY_IMPL_HPP_INCLUDED
3
4// Copyright Beman Dawes 2006, 2007
5// Copyright Christoper Kohlhoff 2007
6// Copyright Peter Dimov 2017, 2018
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/snprintf.hpp>
17#include <boost/system/detail/config.hpp>
18#include <boost/config.hpp>
19#include <string>
20#include <cstring>
21
22namespace boost
23{
24namespace system
25{
26
27// error_category default implementation
28
29inline error_condition error_category::default_error_condition( int ev ) const noexcept
30{
31 return error_condition( ev, *this );
32}
33
34inline bool error_category::equivalent( int code, const error_condition & condition ) const noexcept
35{
36 return default_error_condition( ev: code ) == condition;
37}
38
39inline bool error_category::equivalent( const error_code & code, int condition ) const noexcept
40{
41 return code.equals( val: condition, cat: *this );
42}
43
44inline char const * error_category::message( int ev, char * buffer, std::size_t len ) const noexcept
45{
46 if( len == 0 )
47 {
48 return buffer;
49 }
50
51 if( len == 1 )
52 {
53 buffer[0] = 0;
54 return buffer;
55 }
56
57#if !defined(BOOST_NO_EXCEPTIONS)
58 try
59#endif
60 {
61 detail::snprintf( s: buffer, maxlen: len, format: "%s", this->message( ev ).c_str() );
62 return buffer;
63 }
64#if !defined(BOOST_NO_EXCEPTIONS)
65 catch( ... )
66 {
67 detail::snprintf( s: buffer, maxlen: len, format: "No message text available for error %d", ev );
68 return buffer;
69 }
70#endif
71}
72
73} // namespace system
74} // namespace boost
75
76// interoperability with std::error_code, std::error_condition
77
78#include <boost/system/detail/std_category_impl.hpp>
79#include <boost/system/detail/mutex.hpp>
80#include <new>
81
82namespace boost
83{
84namespace system
85{
86
87inline void error_category::init_stdcat() const
88{
89 static_assert( sizeof( stdcat_ ) >= sizeof( boost::system::detail::std_category ), "sizeof(stdcat_) is not enough for std_category" );
90
91#if defined(BOOST_MSVC) && BOOST_MSVC < 1900
92 // no alignof
93#else
94
95 static_assert( alignof( decltype(stdcat_align_) ) >= alignof( boost::system::detail::std_category ), "alignof(stdcat_) is not enough for std_category" );
96
97#endif
98
99 // detail::mutex has a constexpr default constructor,
100 // and therefore guarantees static initialization, on
101 // everything except VS 2013 (msvc-12.0)
102
103 static system::detail::mutex mx_;
104
105 system::detail::lock_guard<system::detail::mutex> lk( mx_ );
106
107 if( sc_init_.load( m: std::memory_order_acquire ) == 0 )
108 {
109 ::new( static_cast<void*>( stdcat_ ) ) boost::system::detail::std_category( this, system::detail::id_wrapper<0>() );
110 sc_init_.store( i: 1, m: std::memory_order_release );
111 }
112}
113
114#if defined( BOOST_GCC ) && BOOST_GCC >= 40800 && BOOST_GCC < 70000
115#pragma GCC diagnostic push
116#pragma GCC diagnostic ignored "-Wstrict-aliasing"
117#endif
118
119inline BOOST_NOINLINE error_category::operator std::error_category const & () const
120{
121 if( id_ == detail::generic_category_id )
122 {
123// This condition must be the same as the one in error_condition.hpp
124#if defined(BOOST_SYSTEM_AVOID_STD_GENERIC_CATEGORY)
125
126 static const boost::system::detail::std_category generic_instance( this, system::detail::id_wrapper<0x1F4D3>() );
127 return generic_instance;
128
129#else
130
131 return std::generic_category();
132
133#endif
134 }
135
136 if( id_ == detail::system_category_id )
137 {
138// This condition must be the same as the one in error_code.hpp
139#if defined(BOOST_SYSTEM_AVOID_STD_SYSTEM_CATEGORY)
140
141 static const boost::system::detail::std_category system_instance( this, system::detail::id_wrapper<0x1F4D7>() );
142 return system_instance;
143
144#else
145
146 return std::system_category();
147
148#endif
149 }
150
151 if( sc_init_.load( m: std::memory_order_acquire ) == 0 )
152 {
153 init_stdcat();
154 }
155
156 return *static_cast<boost::system::detail::std_category const*>( static_cast<void const*>( stdcat_ ) );
157}
158
159#if defined( BOOST_GCC ) && BOOST_GCC >= 40800 && BOOST_GCC < 70000
160#pragma GCC diagnostic pop
161#endif
162
163} // namespace system
164} // namespace boost
165
166#endif // #ifndef BOOST_SYSTEM_DETAIL_ERROR_CATEGORY_IMPL_HPP_INCLUDED
167

source code of boost/libs/system/include/boost/system/detail/error_category_impl.hpp