Clementine
system_error.hpp
1 //
2 // system_error.hpp
3 // ~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_SYSTEM_ERROR_HPP
12 #define ASIO_SYSTEM_ERROR_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 
20 #if defined(ASIO_HAS_STD_SYSTEM_ERROR)
21 # include <system_error>
22 #else // defined(ASIO_HAS_STD_SYSTEM_ERROR)
23 # include <cerrno>
24 # include <exception>
25 # include <string>
26 # include "asio/error_code.hpp"
27 # include "asio/detail/scoped_ptr.hpp"
28 #endif // defined(ASIO_HAS_STD_SYSTEM_ERROR)
29 
30 #include "asio/detail/push_options.hpp"
31 
32 namespace asio {
33 
34 #if defined(ASIO_HAS_STD_SYSTEM_ERROR)
35 
36 typedef std::system_error system_error;
37 
38 #else // defined(ASIO_HAS_STD_SYSTEM_ERROR)
39 
42 class system_error
43  : public std::exception
44 {
45 public:
48  : code_(ec),
49  context_()
50  {
51  }
52 
54  system_error(const error_code& ec, const std::string& context)
55  : code_(ec),
56  context_(context)
57  {
58  }
59 
61  system_error(const system_error& other)
62  : std::exception(other),
63  code_(other.code_),
64  context_(other.context_),
65  what_()
66  {
67  }
68 
70  virtual ~system_error() throw ()
71  {
72  }
73 
75  system_error& operator=(const system_error& e)
76  {
77  context_ = e.context_;
78  code_ = e.code_;
79  what_.reset();
80  return *this;
81  }
82 
84  virtual const char* what() const throw ()
85  {
86 #if !defined(ASIO_NO_EXCEPTIONS)
87  try
88 #endif // !defined(ASIO_NO_EXCEPTIONS)
89  {
90  if (!what_.get())
91  {
92  std::string tmp(context_);
93  if (tmp.length())
94  tmp += ": ";
95  tmp += code_.message();
96  what_.reset(new std::string(tmp));
97  }
98  return what_->c_str();
99  }
100 #if !defined(ASIO_NO_EXCEPTIONS)
101  catch (std::exception&)
102  {
103  return "system_error";
104  }
105 #endif // !defined(ASIO_NO_EXCEPTIONS)
106  }
107 
109  error_code code() const
110  {
111  return code_;
112  }
113 
114 private:
115  // The code associated with the error.
116  error_code code_;
117 
118  // The context associated with the error.
119  std::string context_;
120 
121  // The string representation of the error.
123 };
124 
125 #endif // defined(ASIO_HAS_STD_SYSTEM_ERROR)
126 
127 } // namespace asio
128 
129 #include "asio/detail/pop_options.hpp"
130 
131 #endif // ASIO_SYSTEM_ERROR_HPP
error_code code() const
Get the error code associated with the exception.
Definition: system_error.hpp:109
system_error(const error_code &ec, const std::string &context)
Construct with an error code and context.
Definition: system_error.hpp:54
Definition: awaitable.hpp:421
An error returned by an operating system or a language runtime, for example a file opening error...
Definition: format.h:3267
system_error(const system_error &other)
Copy constructor.
Definition: system_error.hpp:61
virtual ~system_error()
Destructor.
Definition: system_error.hpp:70
std::string message() const
Get the message associated with the error.
Definition: error_code.hpp:131
Class to represent an error code value.
Definition: error_code.hpp:80
system_error(const error_code &ec)
Construct with an error code.
Definition: system_error.hpp:47
virtual const char * what() const
Get a string representation of the exception.
Definition: system_error.hpp:84
system_error & operator=(const system_error &e)
Assignment operator.
Definition: system_error.hpp:75
Definition: any_io_executor.hpp:28