mirror of
https://github.com/isar/libmdbx.git
synced 2024-12-30 02:04:12 +08:00
mdbx++: refine exceptions.
Change-Id: I56d3fd4716320af7a5aec1824fbded5f9efc416c
This commit is contained in:
parent
9fdaa1c7e5
commit
e6696178db
19
mdbx.h++
19
mdbx.h++
@ -304,35 +304,34 @@ public:
|
||||
// Base for libmdbx exceptions
|
||||
class LIBMDBX_API_TYPE exception : public ::std::runtime_error {
|
||||
using base = ::std::runtime_error;
|
||||
error error_;
|
||||
::mdbx::error error_;
|
||||
|
||||
public:
|
||||
exception(const error &) noexcept;
|
||||
exception(const ::mdbx::error &) noexcept;
|
||||
exception(const exception &) = default;
|
||||
exception(exception &&) = default;
|
||||
exception &operator=(const exception &) = default;
|
||||
exception &operator=(exception &&) = default;
|
||||
virtual ~exception() noexcept;
|
||||
const mdbx::error error() const noexcept { return error_; }
|
||||
};
|
||||
|
||||
/** Fatal exception that lead termination anyway */
|
||||
class LIBMDBX_API_TYPE fatal : public ::std::exception {
|
||||
using base = ::std::exception;
|
||||
error error_;
|
||||
class LIBMDBX_API_TYPE fatal : public exception {
|
||||
using base = exception;
|
||||
|
||||
public:
|
||||
fatal(const error &) noexcept;
|
||||
fatal(const fatal &) noexcept;
|
||||
fatal(fatal &&) noexcept;
|
||||
fatal(const ::mdbx::error &) noexcept;
|
||||
fatal(const exception &src) noexcept : fatal(src.error()) {}
|
||||
fatal(exception &&src) noexcept : fatal(src.error()) {}
|
||||
fatal &operator=(fatal &&) = default;
|
||||
fatal &operator=(const fatal &) = default;
|
||||
virtual const char *what() const noexcept;
|
||||
virtual ~fatal() noexcept;
|
||||
};
|
||||
|
||||
#define MDBX_DECLARE_EXCEPTION(NAME) \
|
||||
struct LIBMDBX_API_TYPE NAME : public exception { \
|
||||
NAME(const error &); \
|
||||
NAME(const ::mdbx::error &); \
|
||||
virtual ~NAME() noexcept; \
|
||||
}
|
||||
MDBX_DECLARE_EXCEPTION(bad_map_id);
|
||||
|
19
src/mdbx.c++
19
src/mdbx.c++
@ -288,22 +288,14 @@ namespace mdbx {
|
||||
"an argument that exceeds the length");
|
||||
}
|
||||
|
||||
__cold exception::exception(const error &error) noexcept
|
||||
__cold exception::exception(const ::mdbx::error &error) noexcept
|
||||
: base(error.what()), error_(error) {}
|
||||
|
||||
__cold exception::~exception() noexcept {}
|
||||
|
||||
static std::atomic_int fatal_countdown;
|
||||
|
||||
__cold fatal::fatal(const error &error_) noexcept : error_(error_) {
|
||||
++fatal_countdown;
|
||||
}
|
||||
|
||||
__cold fatal::fatal(const fatal &src) noexcept : error_(src.error_) {
|
||||
++fatal_countdown;
|
||||
}
|
||||
|
||||
__cold fatal::fatal(fatal &&src) noexcept : error_(src.error_) {
|
||||
__cold fatal::fatal(const ::mdbx::error &error) noexcept : base(error) {
|
||||
++fatal_countdown;
|
||||
}
|
||||
|
||||
@ -312,10 +304,8 @@ __cold fatal::~fatal() noexcept {
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
__cold const char *fatal::what() const noexcept { return error_.what(); }
|
||||
|
||||
#define DEFINE_EXCEPTION(NAME) \
|
||||
__cold NAME::NAME(const error &rc) : exception(rc) {} \
|
||||
__cold NAME::NAME(const ::mdbx::error &rc) : exception(rc) {} \
|
||||
__cold NAME::~NAME() noexcept {}
|
||||
|
||||
DEFINE_EXCEPTION(bad_map_id)
|
||||
@ -383,8 +373,7 @@ __cold std::string error::message() const {
|
||||
[[noreturn]] __cold void error::panic(const char *context,
|
||||
const char *func) const noexcept {
|
||||
assert(code() != MDBX_SUCCESS);
|
||||
::mdbx_panic("mdbx::%s.%s() failed: \"%s\" (%d)", context, func, what(),
|
||||
code());
|
||||
::mdbx_panic("mdbx::%s.%s(): \"%s\" (%d)", context, func, what(), code());
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user