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