mirror of
https://github.com/isar/libmdbx.git
synced 2025-01-02 00:54:14 +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
|
// Base for libmdbx exceptions
|
||||||
class LIBMDBX_API_TYPE exception : public ::std::runtime_error {
|
class LIBMDBX_API_TYPE exception : public ::std::runtime_error {
|
||||||
using base = ::std::runtime_error;
|
using base = ::std::runtime_error;
|
||||||
error error_;
|
::mdbx::error error_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
exception(const error &) noexcept;
|
exception(const ::mdbx::error &) noexcept;
|
||||||
exception(const exception &) = default;
|
exception(const exception &) = default;
|
||||||
exception(exception &&) = default;
|
exception(exception &&) = default;
|
||||||
exception &operator=(const exception &) = default;
|
exception &operator=(const exception &) = default;
|
||||||
exception &operator=(exception &&) = default;
|
exception &operator=(exception &&) = default;
|
||||||
virtual ~exception() noexcept;
|
virtual ~exception() noexcept;
|
||||||
|
const mdbx::error error() const noexcept { return error_; }
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Fatal exception that lead termination anyway */
|
/** Fatal exception that lead termination anyway */
|
||||||
class LIBMDBX_API_TYPE fatal : public ::std::exception {
|
class LIBMDBX_API_TYPE fatal : public exception {
|
||||||
using base = ::std::exception;
|
using base = exception;
|
||||||
error error_;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
fatal(const error &) noexcept;
|
fatal(const ::mdbx::error &) noexcept;
|
||||||
fatal(const fatal &) noexcept;
|
fatal(const exception &src) noexcept : fatal(src.error()) {}
|
||||||
fatal(fatal &&) noexcept;
|
fatal(exception &&src) noexcept : fatal(src.error()) {}
|
||||||
fatal &operator=(fatal &&) = default;
|
fatal &operator=(fatal &&) = default;
|
||||||
fatal &operator=(const fatal &) = default;
|
fatal &operator=(const fatal &) = default;
|
||||||
virtual const char *what() const noexcept;
|
|
||||||
virtual ~fatal() noexcept;
|
virtual ~fatal() noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define MDBX_DECLARE_EXCEPTION(NAME) \
|
#define MDBX_DECLARE_EXCEPTION(NAME) \
|
||||||
struct LIBMDBX_API_TYPE NAME : public exception { \
|
struct LIBMDBX_API_TYPE NAME : public exception { \
|
||||||
NAME(const error &); \
|
NAME(const ::mdbx::error &); \
|
||||||
virtual ~NAME() noexcept; \
|
virtual ~NAME() noexcept; \
|
||||||
}
|
}
|
||||||
MDBX_DECLARE_EXCEPTION(bad_map_id);
|
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");
|
"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) {}
|
: base(error.what()), error_(error) {}
|
||||||
|
|
||||||
__cold exception::~exception() noexcept {}
|
__cold exception::~exception() noexcept {}
|
||||||
|
|
||||||
static std::atomic_int fatal_countdown;
|
static std::atomic_int fatal_countdown;
|
||||||
|
|
||||||
__cold fatal::fatal(const error &error_) noexcept : error_(error_) {
|
__cold fatal::fatal(const ::mdbx::error &error) noexcept : base(error) {
|
||||||
++fatal_countdown;
|
|
||||||
}
|
|
||||||
|
|
||||||
__cold fatal::fatal(const fatal &src) noexcept : error_(src.error_) {
|
|
||||||
++fatal_countdown;
|
|
||||||
}
|
|
||||||
|
|
||||||
__cold fatal::fatal(fatal &&src) noexcept : error_(src.error_) {
|
|
||||||
++fatal_countdown;
|
++fatal_countdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,10 +304,8 @@ __cold fatal::~fatal() noexcept {
|
|||||||
std::terminate();
|
std::terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
__cold const char *fatal::what() const noexcept { return error_.what(); }
|
|
||||||
|
|
||||||
#define DEFINE_EXCEPTION(NAME) \
|
#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 {}
|
__cold NAME::~NAME() noexcept {}
|
||||||
|
|
||||||
DEFINE_EXCEPTION(bad_map_id)
|
DEFINE_EXCEPTION(bad_map_id)
|
||||||
@ -383,8 +373,7 @@ __cold std::string error::message() const {
|
|||||||
[[noreturn]] __cold void error::panic(const char *context,
|
[[noreturn]] __cold void error::panic(const char *context,
|
||||||
const char *func) const noexcept {
|
const char *func) const noexcept {
|
||||||
assert(code() != MDBX_SUCCESS);
|
assert(code() != MDBX_SUCCESS);
|
||||||
::mdbx_panic("mdbx::%s.%s() failed: \"%s\" (%d)", context, func, what(),
|
::mdbx_panic("mdbx::%s.%s(): \"%s\" (%d)", context, func, what(), code());
|
||||||
code());
|
|
||||||
std::terminate();
|
std::terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user