The Battle for Wesnoth  1.15.1+dev
libc_error.hpp
Go to the documentation of this file.
1 /*
2  By Iris Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  The contents of this file are placed in the public domain.
6  */
7 
8 #pragma once
9 
10 #include <exception>
11 #include <cerrno>
12 #include <cstring>
13 #include <string>
14 
15 /**
16  * Exception type used to propagate C runtime errors across functions.
17  */
18 class libc_error : public std::exception
19 {
20 public:
22  : e_(errno)
23  , desc_(strerror(e_))
24  , msg_("C library error: " + desc_)
25  {
26  }
27 
28  virtual ~libc_error() noexcept
29  {
30  }
31 
32  /** Returns the value of @a errno at the time the exception was thrown. */
33  int num() const
34  {
35  return e_;
36  }
37 
38  /** Returns an explanatory string describing the runtime error alone. */
39  const std::string& desc() const
40  {
41  return desc_;
42  }
43 
44  /** Returns an explanatory string describing the exception. */
45  const char* what() const noexcept
46  {
47  return msg_.c_str();
48  }
49 
50 private:
51  int e_;
52  std::string desc_;
53  std::string msg_;
54 };
std::string msg_
Definition: libc_error.hpp:53
Exception type used to propagate C runtime errors across functions.
Definition: libc_error.hpp:18
const std::string & desc() const
Returns an explanatory string describing the runtime error alone.
Definition: libc_error.hpp:39
const char * what() const noexcept
Returns an explanatory string describing the exception.
Definition: libc_error.hpp:45
std::string desc_
Definition: libc_error.hpp:52
int num() const
Returns the value of errno at the time the exception was thrown.
Definition: libc_error.hpp:33
virtual ~libc_error() noexcept
Definition: libc_error.hpp:28