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 namespace restinio
17 {
18  namespace asio_ns = ::asio;
19 
20  //! @name Adoptation functions to cover differences between snad-alone and beast asio.
21  ///@{
22  inline bool error_is_operation_aborted( const asio_ns::error_code & ec )
23  {
24  return ec == asio_ns::error::operation_aborted;
25  }
26 
27  inline bool error_is_eof( const asio_ns::error_code & ec )
28  {
29  return ec == asio_ns::error::eof;
30  }
31  ///@}
32 
33  namespace asio_ec
34  {
35  constexpr auto eof = asio_ns::error::eof;
36  inline const auto & system_category() { return asio_ns::system_category(); }
37  } /* namespace err */
38  //! \}
39 
40  //! An alias for base class of error category entity.
42 
43 // Define a proxy macro for having the same name for asio stand-alone and boost.
44 #define RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT ASIO_ERROR_CATEGORY_NOEXCEPT
45 
46 } /* namespace restinio */
47 
48  #if defined(ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
49  // Define feature macro with the same name for stand-alone and boost asio.
50  #define RESTINIO_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
51  #endif
52 
53 #else
54 
55 // RESTinio uses boost::asio.
56 #include <boost/asio.hpp>
57 
58 namespace restinio
59 {
60 
61  namespace asio_ns
62  {
63  using namespace ::boost::asio;
64  using error_code = ::boost::system::error_code;
65  } /* namespace asio_ns */
66 
67  //! @name Adoptation functions to cover differences between snad-alone and beast asio.
68  ///@{
69  inline bool error_is_operation_aborted( const asio_ns::error_code & ec )
70  {
71  return ec == asio_ns::error::basic_errors::operation_aborted;
72  }
73 
74  inline bool error_is_eof( const asio_ns::error_code & ec )
75  {
76  return ec == asio_ns::error::misc_errors::eof;
77  }
78  ///@}
79 
80  namespace asio_ec
81  {
82  constexpr auto eof = asio_ns::error::misc_errors::eof;
83 
84  inline const auto & system_category() { return ::boost::system::system_category(); }
85 
86  } /* namespace err */
87 
88  //! An alias for base class of error category entity.
89  using error_category_base_t = ::boost::system::error_category;
90 
91  // Define a proxy macro for having the same name for asio stand-alone and boost.
92  #define RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT BOOST_SYSTEM_NOEXCEPT
93 
94 } /* namespace restinio */
95 
96  #if defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
97  // Define feature macro with the same name for stand-alone and boost asio.
98  #define RESTINIO_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
99  #endif
100 
101 #endif
102 
103 namespace restinio
104 {
105 
106 
107 //! Enum for restinio errors that must presented as asio_ns::error_code value.
108 /*
109  @since v.0.4.8
110 */
111 enum class asio_convertible_error_t : int
112 {
113  // Notificators error.
114 
115  //! After write notificator error: data was not sent,
116  //! connection closed (or aborted) before a given piece of data.
118 
119  //! After write notificator error: a notificator was set for a write_group_t
120  //! but no external invokation happened, so write_group_t destructor
121  // calls it with error.
123 };
124 
125 namespace impl
126 {
127 
128 //! Error category for asio compatible error codes.
129 /*
130  @since v.0.4.8
131 */
133 {
134  public:
135  virtual const char*
137  {
138  return "restinio";
139  }
140 
141  virtual std::string
142  message( int value ) const override
143  {
144  std::string result{};
145  switch( static_cast< asio_convertible_error_t >( value ) )
146  {
148  result.assign( "write operation was not" );
149  break;
151  result.assign(
152  "write group destroyed without external notificato invokation" );
153  break;
154  }
155 
156  return result;
157  }
158 };
159 
160 } /* namespace impl */
161 
162 //! Get restinio error category.
163 /*
164  @since v.0.4.8
165 */
166 inline const error_category_base_t &
168 {
169  static impl::restinio_err_category_t instance;
170  return instance;
171 }
172 
173 //! Make restinio error_code compatible with asio_ns::error_code.
174 /*
175  @since v.0.4.8
176 */
177 inline asio_ns::error_code
179 {
180  return asio_ns::error_code{ static_cast< int >( err ), restinio_err_category() };
181 }
182 
183 } /* namespace restinio */
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...
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
#define RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT
bool error_is_operation_aborted(const asio_ns::error_code &ec)
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.
bool error_is_eof(const asio_ns::error_code &ec)
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64