From 07abd19c7d6b643f33ab93c03e3c20057c204c3b Mon Sep 17 00:00:00 2001 From: yamacir-kit Date: Sat, 1 Jun 2024 21:39:02 +0900 Subject: [PATCH] Update the struct `error` to inherit from `std::exception` Signed-off-by: yamacir-kit --- README.md | 6 ++-- VERSION | 2 +- include/meevax/kernel/dynamic_environment.hpp | 8 ++--- include/meevax/kernel/error.hpp | 8 +++-- src/kernel/error.cpp | 36 +++++++++++-------- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 2b90aa2cb..aa563887b 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Procedures for each standard are provided by the following R7RS-style libraries: cmake -B build -DCMAKE_BUILD_TYPE=Release cd build make package -sudo apt install build/meevax_0.5.188_amd64.deb +sudo apt install build/meevax_0.5.189_amd64.deb ``` or @@ -122,9 +122,9 @@ sudo rm -rf /usr/local/share/meevax | Target Name | Description |-------------|------------- -| `all` | Build shared-library `libmeevax.0.5.188.so` and executable `meevax` +| `all` | Build shared-library `libmeevax.0.5.189.so` and executable `meevax` | `test` | Test executable `meevax` -| `package` | Generate debian package `meevax_0.5.188_amd64.deb` +| `package` | Generate debian package `meevax_0.5.189_amd64.deb` | `install` | Copy files into `/usr/local` directly ## Usage diff --git a/VERSION b/VERSION index 0de0d7ec3..a8816aa2e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.5.188 +0.5.189 diff --git a/include/meevax/kernel/dynamic_environment.hpp b/include/meevax/kernel/dynamic_environment.hpp index b51f26c9f..8dcf56e03 100644 --- a/include/meevax/kernel/dynamic_environment.hpp +++ b/include/meevax/kernel/dynamic_environment.hpp @@ -525,14 +525,14 @@ inline namespace kernel }(); } } - catch (std::exception const& exception) - { - return reraise(make(make(exception.what()))); - } catch (error const& error) { return reraise(make(error)); } + catch (std::exception const& exception) + { + return reraise(make(make(exception.what()))); + } } }; } // namespace kernel diff --git a/include/meevax/kernel/error.hpp b/include/meevax/kernel/error.hpp index b8e6af5e3..297dcc249 100644 --- a/include/meevax/kernel/error.hpp +++ b/include/meevax/kernel/error.hpp @@ -25,6 +25,7 @@ namespace meevax inline namespace kernel { struct error : public virtual pair // ( . ) + , public std::exception { struct detail { @@ -37,8 +38,12 @@ inline namespace kernel std::vector details {}; + mutable std::string brief {}; + using pair::pair; + ~error() override = default; + template auto append(Ts&&... xs) -> auto & { @@ -59,8 +64,7 @@ inline namespace kernel auto report(std::ostream &) const -> std::ostream &; - [[deprecated]] - auto what() const -> std::string; + auto what() const noexcept -> char const* override; }; auto operator <<(std::ostream &, error const&) -> std::ostream &; diff --git a/src/kernel/error.cpp b/src/kernel/error.cpp index e94e8fc91..b37d9db96 100644 --- a/src/kernel/error.cpp +++ b/src/kernel/error.cpp @@ -39,14 +39,7 @@ inline namespace kernel auto error::report(std::ostream & output) const -> std::ostream & { - output << red("; error! ", static_cast(message().as())); - - if (irritants()) - { - output << red(": ", irritants()); - } - - output << std::endl; + output << red("; error! ", what()) << std::endl; for (auto iter = details.rbegin(); iter != details.rend(); ++iter) { @@ -56,18 +49,31 @@ inline namespace kernel return output; } - auto error::what() const -> std::string + auto error::what() const noexcept -> char const* { - std::stringstream ss {}; + try + { + if (brief.empty()) + { + auto output = std::stringstream(); - ss << "error: " << static_cast(message().as()); + output << static_cast(message().as()); - if (irritants()) + if (irritants()) + { + output << ": " << irritants(); + } + + brief = output.str(); + } + + return brief.c_str(); + } + catch (...) { - ss << ": " << irritants(); + std::cerr << "meevax::error::what failed to create an explanatory string for std::exception::what" << std::endl; + std::exit(EXIT_FAILURE); } - - return ss.str(); } auto operator <<(std::ostream & os, error const& datum) -> std::ostream &