RESTinio

asio_include.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#if !defined(RESTINIO_USE_BOOST_ASIO)
12
13// RESTinio uses stand-alone version of asio.
14#include <asio.hpp>
15
16// Define added to not have to distinguish between boost and non-boost asio in
17// other code.
18#define RESTINIO_ASIO_VERSION ASIO_VERSION
19
20 namespace restinio
21{
22 namespace asio_ns = ::asio;
23
26 inline bool
27 error_is_operation_aborted( const asio_ns::error_code & ec ) noexcept
28 {
29 return ec == asio_ns::error::operation_aborted;
30 }
31
32 inline bool
33 error_is_eof( const asio_ns::error_code & ec ) noexcept
34 {
35 return ec == asio_ns::error::eof;
36 }
38
39 namespace asio_ec
40 {
41 constexpr auto eof = asio_ns::error::eof;
42 inline const auto & system_category() { return asio_ns::system_category(); }
43 } /* namespace err */
45
47 using error_category_base_t = asio_ns::error_category;
48
49// Define a proxy macro for having the same name for asio stand-alone and boost.
50#define RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT ASIO_ERROR_CATEGORY_NOEXCEPT
51
52} /* namespace restinio */
53
54 #if defined(ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
55 // Define feature macro with the same name for stand-alone and boost asio.
56 #define RESTINIO_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
57 #endif
58
59#else
60
61// RESTinio uses boost::asio.
62#include <boost/asio.hpp>
63
64// Define added to not have to distinguish between boost and non-boost asio in
65// other code.
66#define RESTINIO_ASIO_VERSION BOOST_ASIO_VERSION
67
68 namespace restinio
69{
70
71 namespace asio_ns
72 {
73 using namespace ::boost::asio;
74 using error_code = ::boost::system::error_code;
75 } /* namespace asio_ns */
76
79 inline bool error_is_operation_aborted( const asio_ns::error_code & ec )
80 {
81 return ec == asio_ns::error::basic_errors::operation_aborted;
82 }
83
84 inline bool error_is_eof( const asio_ns::error_code & ec )
85 {
86 return ec == asio_ns::error::misc_errors::eof;
87 }
89
90 namespace asio_ec
91 {
92 constexpr auto eof = asio_ns::error::misc_errors::eof;
93
94 inline const auto & system_category() { return ::boost::system::system_category(); }
95
96 } /* namespace err */
97
99 using error_category_base_t = ::boost::system::error_category;
100
101 // Define a proxy macro for having the same name for asio stand-alone and boost.
102 #define RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT BOOST_SYSTEM_NOEXCEPT
103
104} /* namespace restinio */
105
106 #if defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
107 // Define feature macro with the same name for stand-alone and boost asio.
108 #define RESTINIO_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
109 #endif
110
111#endif
112
113 namespace restinio
114{
115
116
118/*
119 @since v.0.4.8
120*/
121 enum class asio_convertible_error_t : int
122{
123 // Notificators error.
124
127 write_was_not_executed = 100,
128
132 write_group_destroyed_passively,
133
136
139 async_write_call_failed,
140
143
146 async_read_some_at_call_failed
147};
148
149 namespace impl
150{
151
153/*
154 @since v.0.4.8
155*/
156 class restinio_err_category_t : public error_category_base_t
157{
158 public:
159 virtual const char*
160 name() const RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT override
161 {
162 return "restinio";
163 }
164
165 virtual std::string
166 message( int value ) const override
167 {
168 std::string result{};
169 switch( static_cast< asio_convertible_error_t >( value ) )
170 {
171 case asio_convertible_error_t::write_was_not_executed:
172 result.assign( "write operation was not" );
173 break;
174 case asio_convertible_error_t::write_group_destroyed_passively:
175 result.assign(
176 "write group destroyed without external notificato invokation" );
177 break;
178 case asio_convertible_error_t::async_write_call_failed:
179 result.assign(
180 "a call to async_write() failed" );
181 break;
182 case asio_convertible_error_t::async_read_some_at_call_failed:
183 result.assign(
184 "a call to async_read_some_at_call_failed() failed" );
185 break;
186 }
187
188 return result;
189 }
190};
191
192} /* namespace impl */
193
195/*
196 @since v.0.4.8
197*/
198 inline const error_category_base_t &
199 restinio_err_category()
200{
201 static impl::restinio_err_category_t instance;
202 return instance;
203}
204
206/*
207 @since v.0.4.8
208*/
209 inline asio_ns::error_code
210 make_asio_compaible_error( asio_convertible_error_t err ) noexcept
211{
212 return asio_ns::error_code{ static_cast< int >( err ), restinio_err_category() };
213}
214
215// Since Asio 1.17 the usage of asio::executor requires
216// a special define ASIO_USE_TS_EXECUTOR_AS_DEFAULT (otherwise the
217// code won't compile).
218// A new name any_io_executor is introduced in Asio 1.17.
219// We'll use that name for Asio 1.17 or newer.
220// Old name asio::executor will be used for older versions of Asio.
221#if RESTINIO_ASIO_VERSION >= 101700
222 using default_asio_executor = asio_ns::any_io_executor;
223#else
224 using default_asio_executor = asio_ns::executor;
225#endif
226
227} /* namespace restinio */
228
RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT
#define RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT
Definition: asio_include.hpp:50
restinio::impl::restinio_err_category_t
Error category for asio compatible error codes.
Definition: asio_include.hpp:157
restinio::impl::restinio_err_category_t::message
virtual std::string message(int value) const override
Definition: asio_include.hpp:166
restinio::impl::restinio_err_category_t::name
virtual const char * name() const RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT override
Definition: asio_include.hpp:160
restinio::asio_ec::eof
constexpr auto eof
Definition: asio_include.hpp:41
restinio::asio_ec::system_category
const auto & system_category()
Definition: asio_include.hpp:42
restinio
Definition: asio_include.hpp:21
restinio::default_asio_executor
asio_ns::executor default_asio_executor
Definition: asio_include.hpp:224
restinio::error_is_operation_aborted
bool error_is_operation_aborted(const asio_ns::error_code &ec) noexcept
Definition: asio_include.hpp:27
restinio::asio_convertible_error_t
asio_convertible_error_t
Enum for restinio errors that must presented as asio_ns::error_code value.
Definition: asio_include.hpp:122
restinio::asio_convertible_error_t::write_was_not_executed
@ write_was_not_executed
After write notificator error: data was not sent, connection closed (or aborted) before a given piece...
restinio::asio_convertible_error_t::async_read_some_at_call_failed
@ async_read_some_at_call_failed
A call to async_read_some_at failed. The corresponding sendfile operation wasn't done.
restinio::asio_convertible_error_t::write_group_destroyed_passively
@ write_group_destroyed_passively
After write notificator error: a notificator was set for a write_group_t but no external invokation h...
restinio::asio_convertible_error_t::async_write_call_failed
@ async_write_call_failed
A call to async_write failed. The corresponding write operation wasn't done.
restinio::error_is_eof
bool error_is_eof(const asio_ns::error_code &ec) noexcept
Definition: asio_include.hpp:33
restinio::error_category_base_t
asio_ns::error_category error_category_base_t
An alias for base class of error category entity.
Definition: asio_include.hpp:47
restinio::make_asio_compaible_error
asio_ns::error_code make_asio_compaible_error(asio_convertible_error_t err) noexcept
Make restinio error_code compatible with asio_ns::error_code.
Definition: asio_include.hpp:210
restinio::restinio_err_category
const error_category_base_t & restinio_err_category()
Get restinio error category.
Definition: asio_include.hpp:199
const
#define const
Definition: zconf.h:230