Exception std based option (#1232)

* 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 <bela.ivanyi@idata.hu>
This commit is contained in:
Iványi Béla 2025-12-13 09:28:29 +01:00 committed by GitHub
parent 558c04b0a0
commit 6a1ae01b65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 4 deletions

View File

@ -33,6 +33,14 @@ SOFTWARE.
#include "platform.h"
#if ETL_USING_STD_EXCEPTION
#include <exception>
#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;
}

View File

@ -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);

View File

@ -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

View File

@ -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
};
}