From e6696178db1222037e2fe6f9572277a0658fbd5b Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Tue, 1 Sep 2020 16:19:14 +0300 Subject: [PATCH] mdbx++: refine exceptions. Change-Id: I56d3fd4716320af7a5aec1824fbded5f9efc416c --- mdbx.h++ | 19 +++++++++---------- src/mdbx.c++ | 19 ++++--------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/mdbx.h++ b/mdbx.h++ index 435c4a13..8dbd0436 100644 --- a/mdbx.h++ +++ b/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); diff --git a/src/mdbx.c++ b/src/mdbx.c++ index 04c4fade..8d6d0013 100644 --- a/src/mdbx.c++ +++ b/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(); }