RESTinio
asio_include.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Selective include of asio/boost::asio.
7 */
8 
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 
24  //! @name Adoptation functions to cover differences between snad-alone and beast asio.
25  ///@{
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  }
37  ///@}
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 */
44  //! \}
45 
46  //! An alias for base class of error category entity.
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 
77  //! @name Adoptation functions to cover differences between snad-alone and beast asio.
78  ///@{
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  }
88  ///@}
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 
98  //! An alias for base class of error category entity.
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 
117 //! Enum for restinio errors that must presented as asio_ns::error_code value.
118 /*
119  @since v.0.4.8
120 */
121 enum class asio_convertible_error_t : int
122 {
123  // Notificators error.
124 
125  //! After write notificator error: data was not sent,
126  //! connection closed (or aborted) before a given piece of data.
128 
129  //! After write notificator error: a notificator was set for a write_group_t
130  //! but no external invokation happened, so write_group_t destructor
131  //! calls it with error.
133 
134  //! A call to async_write failed.
135  //! The corresponding write operation wasn't done.
136  /*!
137  * @since v.0.6.0
138  */
140 
141  //! A call to async_read_some_at failed.
142  //! The corresponding sendfile operation wasn't done.
143  /*!
144  * @since v.0.6.0
145  */
147 };
148 
149 namespace impl
150 {
151 
152 //! Error category for asio compatible error codes.
153 /*
154  @since v.0.4.8
155 */
157 {
158  public:
159  virtual const char*
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  {
172  result.assign( "write operation was not" );
173  break;
175  result.assign(
176  "write group destroyed without external notificato invokation" );
177  break;
179  result.assign(
180  "a call to async_write() failed" );
181  break;
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 
194 //! Get restinio error category.
195 /*
196  @since v.0.4.8
197 */
198 inline const error_category_base_t &
200 {
201  static impl::restinio_err_category_t instance;
202  return instance;
203 }
204 
205 //! Make restinio error_code compatible with asio_ns::error_code.
206 /*
207  @since v.0.4.8
208 */
209 inline asio_ns::error_code
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
223 #else
225 #endif
226 
227 } /* namespace restinio */
bool error_is_eof(const asio_ns::error_code &ec) noexcept
#define RESTINIO_ASIO_VERSION
After write notificator error: a notificator was set for a write_group_t but no external invokation h...
After write notificator error: data was not sent, connection closed (or aborted) before a given piece...
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, std::optional< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
Definition: value_or.hpp:64
bool error_is_operation_aborted(const asio_ns::error_code &ec) noexcept
asio_convertible_error_t
Enum for restinio errors that must presented as asio_ns::error_code value.
virtual std::string message(int value) const override
const auto & system_category()
virtual const char * name() const RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT override
A call to async_write failed. The corresponding write operation wasn&#39;t done.
#define RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT
constexpr auto eof
asio_ns::error_code make_asio_compaible_error(asio_convertible_error_t err) noexcept
Make restinio error_code compatible with asio_ns::error_code.
const error_category_base_t & restinio_err_category()
Get restinio error category.
A call to async_read_some_at failed. The corresponding sendfile operation wasn&#39;t done.