From 6a1ae01b65168079ae5b84c17fca04dcd3a8249f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1nyi=20B=C3=A9la?= <91312503+ivanyib@users.noreply.github.com> Date: Sat, 13 Dec 2025 09:28:29 +0100 Subject: [PATCH] Exception std based option (#1232) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add ability to derive etl::exception from std::exception * Only add test for exception std base when using STL * Use ETL_NOEXCEPT macro to define a function as noexcept * Rename macro to keep in line with common style * Add using_std_exception to etl::traits --------- Co-authored-by: Béla Iványi --- include/etl/exception.h | 22 ++++++++++++++++++---- include/etl/platform.h | 12 ++++++++++++ test/etl_profile.h | 3 +++ test/test_exception.cpp | 16 ++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/include/etl/exception.h b/include/etl/exception.h index 49e6adca..4323df78 100644 --- a/include/etl/exception.h +++ b/include/etl/exception.h @@ -33,6 +33,14 @@ SOFTWARE. #include "platform.h" +#if ETL_USING_STD_EXCEPTION +#include + + #define ETL_EXCEPTION_CONSTEXPR +#else + #define ETL_EXCEPTION_CONSTEXPR ETL_CONSTEXPR +#endif + ///\defgroup exception exception /// The base class for all ETL exceptions. ///\ingroup utilities @@ -44,6 +52,9 @@ namespace etl /// A low overhead exception base class. //*************************************************************************** class exception +#if ETL_USING_STD_EXCEPTION + : public std::exception +#endif { public: @@ -54,7 +65,7 @@ namespace etl //************************************************************************* /// Constructor. //************************************************************************* - ETL_CONSTEXPR + ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type file_, numeric_type line_) : reason_text(reason_), file_text(file_), @@ -65,7 +76,7 @@ namespace etl //************************************************************************* /// Constructor. //************************************************************************* - ETL_CONSTEXPR + ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type /*file_*/, numeric_type line_) : reason_text(reason_), line(line_) @@ -77,8 +88,11 @@ namespace etl /// Gets the reason for the exception. /// \return const char* to the reason. //*************************************************************************** - ETL_CONSTEXPR - string_type what() const + ETL_EXCEPTION_CONSTEXPR + string_type what() const ETL_NOEXCEPT +#if ETL_USING_STD_EXCEPTION + override +#endif { return reason_text; } diff --git a/include/etl/platform.h b/include/etl/platform.h index 89fafeef..62a5da60 100644 --- a/include/etl/platform.h +++ b/include/etl/platform.h @@ -293,6 +293,17 @@ SOFTWARE. #define ETL_HAS_VIRTUAL_MESSAGES 1 #endif +//************************************* +// Indicate if etl::exception is to be derived from std::exception. +#if defined(ETL_USE_STD_EXCEPTION) +#if ETL_NOT_USING_STL + #error "Requested std base for etl::exception, but STL is not used" +#endif + #define ETL_USING_STD_EXCEPTION 1 +#else + #define ETL_USING_STD_EXCEPTION 0 +#endif + //************************************* // Indicate if etl::literals::chrono_literals uses ETL verbose style. #if defined(ETL_USE_VERBOSE_CHRONO_LITERALS) && ETL_USING_CPP11 @@ -631,6 +642,7 @@ namespace etl static ETL_CONSTANT bool using_legacy_bitset = (ETL_USING_LEGACY_BITSET == 1); static ETL_CONSTANT bool using_exceptions = (ETL_USING_EXCEPTIONS == 1); static ETL_CONSTANT bool using_libc_wchar_h = (ETL_USING_LIBC_WCHAR_H == 1); + static ETL_CONSTANT bool using_std_exception = (ETL_USING_STD_EXCEPTION == 1); // Has... static ETL_CONSTANT bool has_initializer_list = (ETL_HAS_INITIALIZER_LIST == 1); diff --git a/test/etl_profile.h b/test/etl_profile.h index a7c680ff..b4868515 100644 --- a/test/etl_profile.h +++ b/test/etl_profile.h @@ -44,6 +44,9 @@ SOFTWARE. #define ETL_IN_UNIT_TEST #define ETL_DEBUG_COUNT #define ETL_ARRAY_VIEW_IS_MUTABLE +#if !defined(ETL_NO_STL) +#define ETL_USE_STD_EXCEPTION +#endif #define ETL_MESSAGE_TIMER_USE_ATOMIC_LOCK #define ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK diff --git a/test/test_exception.cpp b/test/test_exception.cpp index fde141cc..474eee96 100644 --- a/test/test_exception.cpp +++ b/test/test_exception.cpp @@ -61,5 +61,21 @@ namespace CHECK_EQUAL(123, c.line_number()); } } + +#if ETL_USING_STD_EXCEPTION + TEST(test_exception_std_base) + { + etl::exception e("An exception", "Some file", 123); + + try + { + throw e; + } + catch (std::exception& c) + { + CHECK_EQUAL(std::string("An exception"), std::string(c.what())); + } + } +#endif }; }