From 5210a3ae01dbbbb308de866516b2788680c371f0 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Wed, 28 Dec 2022 08:51:06 -0500 Subject: [PATCH] use a macro to determine whether except::Throwable derives from std::exception --- modules/c++/except/include/except/Exception.h | 10 ++++---- modules/c++/except/include/except/Throwable.h | 24 +++++++++++++++++++ modules/c++/sys/tests/MemoryTest1.cpp | 10 ++++---- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/modules/c++/except/include/except/Exception.h b/modules/c++/except/include/except/Exception.h index f7d95c924..782494a0f 100644 --- a/modules/c++/except/include/except/Exception.h +++ b/modules/c++/except/include/except/Exception.h @@ -51,7 +51,7 @@ #endif #endif -#define DECLARE_EXTENDED_EXCEPTION_(_Name, Exception_, _Base, getType_specifiers) \ +#define DECLARE_EXTENDED_EXCEPTION_(_Name, Exception_, _Base) \ struct _Name##Exception_ : public _Base \ { \ _Name##Exception_() = default; virtual ~_Name##Exception_() = default; \ @@ -62,12 +62,10 @@ _Name##Exception_(const except::Throwable& t, const except::Context& c) : _Base(t, c){} \ _Name##Exception_(const except::Throwable11& t, const except::Context& c) : _Base(t, c){} \ CODA_OSS_except_Exception_suppress_26447_BEGIN_ \ - std::string getType() getType_specifiers { return #_Name #Exception_; } \ + std::string getType() const noexcept override { return #_Name #Exception_; } \ CODA_OSS_except_Exception_suppress_26447_END_ }; -#define DECLARE_EXTENDED_EXCEPTION(_Name, _Base) \ - DECLARE_EXTENDED_EXCEPTION_(_Name, Exception, _Base, const override) -#define DECLARE_EXTENDED_EXCEPTION11(_Name, _Base) \ - DECLARE_EXTENDED_EXCEPTION_(_Name, Exception11, _Base, const noexcept override) +#define DECLARE_EXTENDED_EXCEPTION(_Name, _Base) DECLARE_EXTENDED_EXCEPTION_(_Name, Exception, _Base) +#define DECLARE_EXTENDED_EXCEPTION11(_Name, _Base) DECLARE_EXTENDED_EXCEPTION_(_Name, Exception11, _Base) #define DECLARE_EXCEPTION(_Name) \ DECLARE_EXTENDED_EXCEPTION(_Name, except::Exception) \ diff --git a/modules/c++/except/include/except/Throwable.h b/modules/c++/except/include/except/Throwable.h index ac322a974..88b9a7158 100644 --- a/modules/c++/except/include/except/Throwable.h +++ b/modules/c++/except/include/except/Throwable.h @@ -36,6 +36,23 @@ #include "config/compiler_extensions.h" #include "except/Trace.h" +/* Determine whether except::Throwable derives from std::exception. + * + * It can be quite convenient to derive from std::exception as often one less + * "catch" will be needed and we'll have standard what(). But doing so could + * break existing code as "catch (const std::exception&)" will catch + * except::Throwable when it didn't before. + */ +#ifdef CODA_OSS_THROWABLE_IS_NOT_AN_EXCEPTION // -DCODA_OSS_THROWABLE_IS_NOT_AN_EXCEPTION +#ifdef CODA_OSS_except_Throwable_ISA_std_exception +#error "CODA_OSS_except_Throwable_ISA_std_exception already #define'd." +#endif +#define CODA_OSS_except_Throwable_ISA_std_exception 0 +#endif +#ifndef CODA_OSS_except_Throwable_ISA_std_exception // or, -DCODA_OSS_except_Throwable_ISA_std_exception=0 +#define CODA_OSS_except_Throwable_ISA_std_exception 0 +#endif + /*! * \file Throwable.h * \brief Contains the classes to do with error handling @@ -55,6 +72,9 @@ namespace except class Throwable11; class CODA_OSS_API Throwable +#if CODA_OSS_except_Throwable_ISA_std_exception + : public std::exception +#endif { void doGetBacktrace(); template @@ -169,6 +189,10 @@ class CODA_OSS_API Throwable } const char* what() const noexcept + #if CODA_OSS_except_Throwable_ISA_std_exception + // can't use "final" unless what() is virtual + final // derived classes override toString() + #endif { // adding this to toString() output could (significantly) alter existing display mWhat = toString(true /*includeBacktrace*/); // call any derived toString() diff --git a/modules/c++/sys/tests/MemoryTest1.cpp b/modules/c++/sys/tests/MemoryTest1.cpp index 76d9500c3..534c0586b 100644 --- a/modules/c++/sys/tests/MemoryTest1.cpp +++ b/modules/c++/sys/tests/MemoryTest1.cpp @@ -35,17 +35,17 @@ int main(int, char**) std::cout << "Total system memory: " << totalPhysMem << " MB" << std::endl; std::cout << "Free system memory: " << freePhysMem << " MB" << std::endl; } - catch (const std::exception& ex) - { - std::cerr << "Caught std::exception: " << ex.what() << std::endl; - return 1; - } catch (const except::Exception& ex) { std::cerr << "Caught except::exception: " << ex.getMessage() << std::endl; return 1; } + catch (const std::exception& ex) + { + std::cerr << "Caught std::exception: " << ex.what() << std::endl; + return 1; + } catch(...) { std::cerr << "Caught unknown exception\n";